2007-07-21 09:42:54 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# Monit status parser plugin for munin
|
|
|
|
|
|
|
|
# This is very raw, but it should work for you out of the box. You of course
|
|
|
|
# need to have monit configured such that the 'monit status' command works.
|
|
|
|
|
|
|
|
# Todd Troxell <ttroxell@debian.org>
|
|
|
|
|
|
|
|
STATUS_CMD = "monit status"
|
|
|
|
|
2016-10-23 01:27:25 +02:00
|
|
|
import os
|
2007-07-21 09:42:54 +02:00
|
|
|
import re
|
2016-10-23 01:27:25 +02:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2007-07-21 09:42:54 +02:00
|
|
|
|
|
|
|
def sanitize(s):
|
|
|
|
OK_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
out = str()
|
|
|
|
for char in s:
|
|
|
|
if char.lower() in OK_CHARS:
|
2016-10-23 00:56:39 +02:00
|
|
|
out += char
|
2007-07-21 09:42:54 +02:00
|
|
|
return out
|
|
|
|
|
|
|
|
procs = dict()
|
|
|
|
|
2016-10-23 01:27:25 +02:00
|
|
|
try:
|
|
|
|
output = subprocess.check_output(STATUS_CMD.split())
|
|
|
|
except (OSError, subprocess.CalledProcessError) as exc:
|
|
|
|
sys.stderr.write("Error running monit status command: %s%s" % (exc, os.linesep))
|
|
|
|
sys.exit(1)
|
2007-07-21 09:42:54 +02:00
|
|
|
|
2016-10-23 01:27:25 +02:00
|
|
|
# python3: the result of command execution is bytes and needs to be converted
|
|
|
|
if not isinstance(output, str):
|
|
|
|
output = output.decode("ascii", "ignore")
|
|
|
|
output = output.splitlines()
|
2007-07-21 09:42:54 +02:00
|
|
|
cur_proc = None
|
|
|
|
for line in output:
|
|
|
|
m = re.match("^Process '(.*)'.*$", line)
|
|
|
|
if m:
|
|
|
|
cur_proc = sanitize(m.group(1))
|
2016-10-23 00:56:39 +02:00
|
|
|
try:
|
|
|
|
procs[cur_proc]
|
|
|
|
except KeyError:
|
|
|
|
procs[cur_proc] = dict()
|
|
|
|
continue
|
2007-07-21 09:42:54 +02:00
|
|
|
m = re.match(" memory kilobytes total\s+([0-9]+).*$", line)
|
|
|
|
if m:
|
|
|
|
procs[cur_proc]["total_memory"] = m.group(1)
|
2016-10-23 00:56:39 +02:00
|
|
|
continue
|
2007-07-21 09:42:54 +02:00
|
|
|
m = re.match(" cpu percent total\s+([.0-9]+)%.*$", line)
|
|
|
|
if m:
|
|
|
|
procs[cur_proc]["total_cpu"] = m.group(1)
|
2016-10-23 00:56:39 +02:00
|
|
|
continue
|
|
|
|
|
2007-07-21 09:42:54 +02:00
|
|
|
if len(sys.argv) > 1 and sys.argv[1] == 'config':
|
2016-10-23 01:27:50 +02:00
|
|
|
print('graph_title Per process stats from Monit')
|
|
|
|
print('graph_vlabel numbers')
|
|
|
|
print('graph_category monit')
|
2007-07-21 09:42:54 +02:00
|
|
|
for process in procs:
|
|
|
|
for stat in procs[process]:
|
2016-10-23 01:27:50 +02:00
|
|
|
print("monit_%s_%s.label %s.%s" % (process, stat, process, stat))
|
2015-06-08 17:10:04 +02:00
|
|
|
if stat == 'total_memory':
|
2016-10-23 01:27:50 +02:00
|
|
|
print("monit_%s_%s.warning 1:" % (process, stat))
|
2007-07-21 09:42:54 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
for process in procs:
|
|
|
|
for stat in procs[process]:
|
2016-10-23 01:27:50 +02:00
|
|
|
print("monit_%s_%s.value %s" % (process, stat, procs[process][stat]))
|