Created
September 18, 2009 22:07
-
-
Save allaryin/189323 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
import sys | |
from subprocess import * | |
ERR_OK = 0 | |
ERR_WARN = 1 | |
ERR_CRIT = 2 | |
ERR_UNKNOWN = 3 | |
if len(sys.argv) == 1: | |
print "UNKNOWN:", sys.argv[0], "cmd[:min[:max]] ..." | |
sys.exit( ERR_UNKNOWN ) | |
err_lvl = ERR_OK | |
err_msg = [] | |
cmds = [] | |
raw = [] | |
for v in sys.argv[1:]: | |
opts = v.split(":") | |
cmd = opts[0] | |
cmds = cmds + [cmd] | |
if len(opts) > 1: | |
min_procs = int(opts[1]) | |
else: | |
min_procs = 1 | |
if len(opts) > 2: | |
max_procs = int(opts[2]) | |
else: | |
max_procs = -1 | |
pids = Popen(["ps", "--no-heading", "-opid", "-C"+cmd], stdout=PIPE).communicate()[0].split("\n")[:-1] | |
num_procs = len(pids) | |
raw = raw + [cmd+"="+str(num_procs)+";"+str(min_procs)+";"+str(max_procs)+";"] | |
if num_procs < min_procs: | |
err_msg = err_msg + [cmd+"<"+str(min_procs)] | |
err_lvl = ERR_CRIT | |
elif max_procs > -1 and num_procs > max_procs: | |
err_msg = err_msg + [cmd+">"+str(max_procs)] | |
if err_lvl < ERR_CRIT: | |
err_lvl = ERR_WARN | |
if err_lvl: | |
err_msg.sort() | |
if err_lvl == ERR_WARN: | |
err_msg = ["WARN:"] + err_msg | |
else: | |
err_msg = ["CRITICAL:"] + err_msg | |
print " ".join(err_msg), | |
else: | |
cmds.sort() | |
print "OK:", " ".join(cmds), | |
print "|"+" ".join(raw) | |
sys.exit( err_lvl ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment