From 08a02af17f82f96dcd0896d56e4d4d091b9773e5 Mon Sep 17 00:00:00 2001 From: Dmitry Lazurkin Date: Mon, 4 Mar 2024 16:24:56 +0300 Subject: [PATCH] Add --set-value option --- README.md | 1 + src/jmx_dump/core.clj | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/README.md b/README.md index dbfb282..11a123c 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/src/jmx_dump/core.clj b/src/jmx_dump/core.clj index dce8660..1eb4da0 100644 --- a/src/jmx_dump/core.clj +++ b/src/jmx_dump/core.clj @@ -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 @@ -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 "*:*"))) @@ -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)]