Skip to content

Commit

Permalink
Add --set-value option
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Lazurkin committed Mar 4, 2024
1 parent 13918c1 commit 08a02af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Usage: jmx-dump [options]
-p, --port PORT 3000 JMX Port
-u, --url URL JMX URL
-v, --value MBEAN ATTR1... Dump values of specific MBEAN attributes
-s, --set-value MBEAN ATTR VALUE Set value of specific MBEAN attribute
--help
```

Expand Down
21 changes: 21 additions & 0 deletions src/jmx_dump/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
["-p" "--port PORT" "JMX Port" :default 3000]
["-u" "--url URL" "JMX URL"]
["-v" "--value MBEAN ATTR1..." "Dump values of specific MBEAN attributes"]
["-s" "--set-value MBEAN ATTR VALUE" "Set value of specific MBEAN attribute"]
[nil "--help"]])

;; cli usage
Expand Down Expand Up @@ -83,6 +84,19 @@
(let [f (fn [[k v]] (if (map? v) [k v] [k (encode-jmx-data v)]))]
(w/postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) m)))

(defn decode-jmx-value [mbean attr s]
(let [attr-info (jmx/attribute-info mbean attr)
attr-type (.getType attr-info)]
(cond
(= attr-type "long") (Long/parseLong s)
(= attr-type "float") (Float/parseFloat s)
(= attr-type "double") (Double/parseDouble s)
(= attr-type "int") (Integer/parseInt s)
(= attr-type "boolean") (Boolean/parseBoolean s)
(= attr-type "short") (Short/parseShort s)
(= attr-type "byte") (Byte/parseByte s)
:else (throw (IllegalArgumentException. (format "Unsupported attr type: %s" attr-type))))))

(defn jmx-mbean-names []
(map #(.getCanonicalName %1) (jmx/mbean-names "*:*")))

Expand Down Expand Up @@ -180,6 +194,13 @@
(let [attrs (if (< (count arguments) 2) (keyword (first arguments)) (map keyword arguments))]
(println (cc/generate-string (jmx/read dump-mbean-attr attrs)))))

;; set mbean attr
(when-let [set-mbean-attr (options :set-value)]
(let [attr (keyword (first arguments))
value-str (second arguments)
value (decode-jmx-value set-mbean-attr attr value-str)]
(jmx/write! set-mbean-attr attr value)))

;; dump all mbeans
(when-let [_dump-all? (options :dump-all)]
(let [m (jmx-mbean-names)]
Expand Down

0 comments on commit 08a02af

Please sign in to comment.