Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 122 lines (95 sloc) 4.103 kb
f92114c @stuarthalloway clojure.contrib.jmx
stuarthalloway authored
1 ;; Copyright (c) Stuart Halloway, 2009. All rights reserved. The use
2 ;; and distribution terms for this software are covered by the Eclipse
3 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4 ;; which can be found in the file epl-v10.html at the root of this
5 ;; distribution. By using this software in any fashion, you are
6 ;; agreeing to be bound by the terms of this license. You must not
7 ;; remove this notice, or any other, from this software.
8
d596fee @stuarthalloway better doc metadata for jmx
stuarthalloway authored
9
b8d2743 use the 1.2 metadata reader macro ^ instead of #^
Aaron Bedra and Stuart Halloway authored
10 (ns ^{:author "Stuart Halloway"
d596fee @stuarthalloway better doc metadata for jmx
stuarthalloway authored
11 :doc "JMX support for Clojure
12
13 Requires post-Clojure 1.0 git edge for clojure.test, clojure.backtrace.
14 This is prerelease.
15 This API will change.
16 Send reports to stu@thinkrelevance.com.
17
18 Usage
19 (require '[clojure.contrib.jmx :as jmx])
20
21 What beans do I have?
22
23 (jmx/mbean-names \"*:*\")
24 -> #<HashSet [java.lang:type=MemoryPool,name=CMS Old Gen,
25 java.lang:type=Memory, ...]
26
27 What attributes does a bean have?
28
29 (jmx/attribute-names \"java.lang:type=Memory\")
30 -> (:Verbose :ObjectPendingFinalizationCount
31 :HeapMemoryUsage :NonHeapMemoryUsage)
32
33 What is the value of an attribute?
34
35 (jmx/read \"java.lang:type=Memory\" :ObjectPendingFinalizationCount)
36 -> 0
37
38 Can't I just have *all* the attributes in a Clojure map?
39
40 (jmx/mbean \"java.lang:type=Memory\")
41 -> {:NonHeapMemoryUsage
42 {:used 16674024, :max 138412032, :init 24317952, :committed 24317952},
43 :HeapMemoryUsage
44 {:used 18619064, :max 85393408, :init 0, :committed 83230720},
45 :ObjectPendingFinalizationCount 0,
46 :Verbose false}
47
48 Can I find and invoke an operation?
49
50 (jmx/operation-names \"java.lang:type=Memory\")
51 -> (:gc)
52 (jmx/invoke \"java.lang:type=Memory\" :gc)
53 -> nil
f92114c @stuarthalloway clojure.contrib.jmx
stuarthalloway authored
54
d596fee @stuarthalloway better doc metadata for jmx
stuarthalloway authored
55 What about some other process? Just run *any* of the above code
56 inside a with-connection:
57
58 (jmx/with-connection {:host \"localhost\", :port 3000}
59 (jmx/mbean \"java.lang:type=Memory\"))
60 -> {:ObjectPendingFinalizationCount 0,
61 :HeapMemoryUsage ... etc.}
62
63 Can I serve my own beans? Sure, just drop a Clojure ref
64 into an instance of clojure.contrib.jmx.Bean, and the bean
65 will expose read-only attributes for every key/value pair
66 in the ref:
67
68 (jmx/register-mbean
69 (Bean.
70 (ref {:string-attribute \"a-string\"}))
71 \"my.namespace:name=Value\")"}
72 clojure.contrib.jmx
f92114c @stuarthalloway clojure.contrib.jmx
stuarthalloway authored
73 (:refer-clojure :exclude [read])
74 (:use clojure.contrib.def
5ec001a @stuartsierra c.c.jmx: change from c.c.java to c.c.string
stuartsierra authored
75 [clojure.contrib.string :only [as-str]]
f92114c @stuarthalloway clojure.contrib.jmx
stuarthalloway authored
76 [clojure.stacktrace :only (root-cause)]
77 [clojure.walk :only [postwalk]])
78 (:import [clojure.lang Associative]
79 java.lang.management.ManagementFactory
80 [javax.management Attribute DynamicMBean MBeanInfo ObjectName RuntimeMBeanException MBeanAttributeInfo]
81 [javax.management.remote JMXConnectorFactory JMXServiceURL]))
82
83 (defvar *connection* (ManagementFactory/getPlatformMBeanServer)
84 "The connection to be used for JMX ops. Defaults to the local process.")
85
86 (load "jmx/data")
87 (load "jmx/client")
88 (load "jmx/server")
89
90 (defn mbean-names
91 "Finds all MBeans matching a name on the current *connection*."
92 [n]
93 (.queryNames *connection* (as-object-name n) nil))
94
95 (defn attribute-names
96 "All attribute names available on an MBean."
97 [n]
98 (doall (map #(-> % .getName keyword)
99 (.getAttributes (mbean-info n)))))
100
101 (defn operation-names
102 "All operation names available on an MBean."
103 [n]
104 (doall (map #(-> % .getName keyword) (operations n))))
105
106 (defn invoke [n op & args]
107 (if ( seq args)
108 (.invoke *connection* (as-object-name n) (as-str op)
109 (into-array args)
110 (into-array String (op-param-types n op)))
111 (.invoke *connection* (as-object-name n) (as-str op)
112 nil nil)))
113
114 (defn mbean
115 "Like clojure.core/bean, but for JMX beans. Returns a read-only map of
116 a JMX bean's attributes. If an attribute it not supported, value is
117 set to the exception thrown."
118 [n]
119 (into {} (map (fn [attr-name] [(keyword attr-name) (read-supported n attr-name)])
120 (attribute-names n))))
121
Something went wrong with that request. Please try again.