Created
September 5, 2016 06:13
-
-
Save andy722/0b4baa5492e04209d82c445f7f68473b to your computer and use it in GitHub Desktop.
Simple monitoring & alert notification script: track JMX properties and send email if values increase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.mail.HtmlEmail | |
import javax.management.remote.JMXConnector | |
import javax.management.remote.JMXConnectorFactory | |
import javax.management.remote.JMXServiceURL | |
// @Grab(group = 'org.apache.commons', module = 'commons-email', version = '1.3.2', transitive = true) | |
// | |
// Notification target e-mails. | |
// | |
final targets = ['[email protected]', '[email protected]'] | |
// | |
// "Connection URL" -> { "Bean name" -> { "Incremental property" }* }* | |
// | |
final triggers = [ | |
// Core. | |
'service:jmx:rmi:///jndi/rmi://10.20.30.40:11997/jmxrmi' : [ | |
'com.example:name=Service1' : [ | |
'nErrors' | |
], | |
'com.example:name=DBService': [ | |
'dbErrors' | |
] | |
], | |
// Site. | |
'service:jmx:rmi:///jndi/rmi://10.20.30.40:11998/jmxrmi': [ | |
'com.example:name=Service1': [ | |
'nErrors' | |
], | |
'com.example:name=DbService': [ | |
'nErrors' | |
] | |
] | |
] | |
final prevValues = triggers.collectEntries { url, params -> | |
[ | |
(url): params.collectEntries { beanName, properties -> | |
[ | |
beanName, properties.collectEntries { p -> [(p) : 0]} | |
] | |
} | |
] | |
} | |
final send = { String message -> | |
try { | |
def msg = java.net.URLEncoder.encode(message, 'UTF-8') | |
final email = new HtmlEmail( | |
hostName: 'smtp.example.com', | |
smtpPort: 25, | |
startTLSRequired: true, | |
SSLCheckServerIdentity: false, | |
charset: 'UTF-8', | |
htmlMsg: message, | |
subject: "PROBLEM: Production service failure", | |
debug: true | |
) | |
.setFrom('[email protected]', '[email protected]') | |
.setAuthentication('login', 'password') | |
targets.each { email.addTo it } | |
email.send() | |
} catch (e) { e.printStackTrace() } | |
} | |
final loop = { -> | |
final check = { String serverUrl, GroovyMBean bean, String beanName, String name -> | |
final prev = prevValues[serverUrl][beanName][name] | |
final now = bean.getProperty name | |
if (prev != now) { | |
send """ | |
JMX error: host = [$serverUrl], bean = [$beanName], property = [$name]: | |
Value increased: $prev -> $now | |
""" | |
prevValues[serverUrl][beanName][name] = now | |
} | |
} | |
triggers.each { serverUrl, serverBeans -> | |
JMXConnectorFactory.connect(new JMXServiceURL(serverUrl)).withCloseable { connector -> | |
final server = (connector as JMXConnector).MBeanServerConnection | |
serverBeans.each { beanName, beanProps -> | |
final bean = new GroovyMBean(server, beanName) | |
beanProps.each { check serverUrl, bean, beanName, it } | |
} | |
} | |
} | |
} | |
while (!Thread.interrupted()) { | |
try { | |
loop() | |
} catch (e) { | |
send """ | |
JMX monitoring error: unexpected error: $e.message. | |
Stacktrace: """ + e.printStackTrace() | |
e.printStackTrace() | |
} | |
Thread.sleep 5_000 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment