Skip to content

Instantly share code, notes, and snippets.

@rbramley
Created May 27, 2011 09:28
Show Gist options
  • Save rbramley/994930 to your computer and use it in GitHub Desktop.
Save rbramley/994930 to your computer and use it in GitHub Desktop.
A groovy script to run multiple Opsview checks and aggregate the return value and status / performance data
/**
* Copying and distribution of this file, with or without modification,
* are permitted in any medium without royalty provided the copyright
* notice and this notice are preserved. This file is offered as-is,
* without any warranty.
*
* Aggregate Opsview checks.
* @author Robin Bramley, Opsera Limited (c) 2010
*/
// consts
final int OK = 0
final int WARNING = 1
final int CRITICAL = 2
final int UNKNOWN = 3
def retCode = null
// checks to run
def processList = [
"/usr/local/nagios/libexec/check_load -w 8,5,2, -c 20,9,4",
"/usr/local/nagios/libexec/check_memory -w 90 -c 98",
"/usr/local/nagios/libexec/check_disk -w 5% -c 2% -p /",
"/usr/local/nagios/libexec/check_tcp -p 8080",
"/usr/local/nagios/libexec/check_procs -C jsvc -a tomcat -w 3:3 -c 3:3"
]
def status = new StringBuffer()
def perf = new StringBuffer()
// execute them
processList.each { proc ->
def process = proc.execute()
process.waitForOrKill(10000)
// get output
def str = process.text
// process output
def strings = str.split('\\|')
status.append(strings[0]).append('<br/>')
if (strings.size() > 1) {
perf.append(strings[1]) // will have trailing \n
}
// process return code
def val = process.exitValue()
//println "retCode: ${val}"
if (!retCode || (retCode != CRITICAL && val > retCode)) {
retCode = val
}
}
if (retCode == null) {
retCode = UNKNOWN
}
println "${status}|${perf}"
System.exit(retCode) // return retCode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment