-
Notifications
You must be signed in to change notification settings - Fork 27
/
metrics_utils.clj
42 lines (38 loc) · 1.41 KB
/
metrics_utils.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(ns puppetlabs.trapperkeeper.services.metrics.metrics-utils
(:require [cheshire.custom :refer [JSONable]]
[clojure.java.jmx :as jmx]
[puppetlabs.kitchensink.core :as ks]))
(defn filter-mbean
"Converts an mbean to a map. For attributes that can't be converted to JSON,
return a string representation of the value."
[mbean]
{:post [(map? %)]}
(->> mbean
(ks/mapvals (fn [v]
(cond
;; Nested structures should themselves be filtered
(map? v) (filter-mbean v)
(instance? java.util.HashMap v) (->> v
(into {})
filter-mbean)
(satisfies? JSONable v) v
:else (str v))))))
(defn all-mbean-names
"Return a seq of all mbeans names"
[]
{:post [(coll? %)]}
(map str (jmx/mbean-names "*:*")))
(defn mbean-names
"Return a map of mbean name to a link that will retrieve the attributes"
[]
(->> (all-mbean-names)
(map (fn [mbean-name]
[mbean-name
(format "/mbeans/%s" (java.net.URLEncoder/encode mbean-name "UTF-8"))]))
(into (sorted-map))))
(defn get-mbean
"Returns the attributes of a given MBean"
[name]
(when (some #(= name %) (all-mbean-names))
(filter-mbean
(jmx/mbean name))))