2
0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00
contrib-munin/plugins/monit/monit_parser

109 lines
2.9 KiB
Plaintext
Raw Normal View History

#!/usr/bin/python3
2007-07-21 09:42:54 +02:00
"""
=head1 NAME
2007-07-21 09:42:54 +02:00
monit_parser - Monit status parser plugin for munin.
2007-07-21 09:42:54 +02:00
=head1 APPLICABLE SYSTEMS
Any system being able to query monit servers via http.
Monit needs to be configured with the httpd port enabled, e.g.:
set httpd port 2812
=head1 CONFIGURATION
By default the monit instance at localhost is queried:
[monit_parser]
env.port 2812
env.host localhost
=head1 AUTHOR
Todd Troxell <ttroxell@debian.org>
Lars Kruse <devel@sumpfralle.de>
=head1 MAGIC MARKERS
family=auto
capabilities=autoconf
=cut
"""
2007-07-21 09:42:54 +02:00
import xml.dom.minidom
import os
import sys
import urllib.request
MONIT_XML_URL = ("http://{host}:{port}/_status?format=xml"
.format(host=os.getenv("host", "localhost"),
port=os.getenv("port", "2812")))
2007-07-21 09:42:54 +02:00
def sanitize(s):
OK_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789"
2016-10-23 03:20:05 +02:00
return "".join([char for char in s if char in OK_CHARS])
def get_monit_status_xml():
2016-10-23 03:20:05 +02:00
try:
conn = urllib.request.urlopen(MONIT_XML_URL)
except urllib.error.URLError as exc:
conn = None
if conn is None:
raise RuntimeError("Failed to open monit status URL: {}".format(MONIT_XML_URL))
else:
return xml.dom.minidom.parse(conn)
2016-10-23 03:20:05 +02:00
def parse_processes():
dom = get_monit_status_xml()
2016-10-23 03:20:05 +02:00
procs = {}
for item in dom.getElementsByTagName("service"):
if item.getAttribute("type") == "3":
# daemon with memory usage and CPU
name = item.getElementsByTagName("name")[0].childNodes[0].data
memory_usage = item.getElementsByTagName("memory")[0].getElementsByTagName(
"kilobytetotal")[0].childNodes[0].data
cpu_usage = item.getElementsByTagName("cpu")[0].getElementsByTagName(
"percenttotal")[0].childNodes[0].data
procs[name] = {}
procs[name]["total_memory"] = memory_usage
procs[name]["total_cpu"] = cpu_usage
2016-10-23 03:20:05 +02:00
return procs
action = sys.argv[1] if (len(sys.argv) > 1) else None
if action == 'autoconf':
try:
get_monit_status_xml()
2016-10-23 03:20:05 +02:00
print("yes")
except RuntimeError:
2016-10-23 03:20:05 +02:00
print("no (failed to request monit status)")
elif action == 'config':
procs = parse_processes()
print('graph_title Per process stats from Monit')
print('graph_vlabel usage of memory [kB] or cpu [%]')
print('graph_category monit')
2007-07-21 09:42:54 +02:00
for process in procs:
for stat in procs[process]:
print("monit_%s_%s.label %s.%s" % (sanitize(process), stat, process, stat))
2015-06-08 17:10:04 +02:00
if stat == 'total_memory':
2016-10-23 03:20:05 +02:00
# the allocated memory may never be zero
print("monit_%s_%s.warning 1:" % (sanitize(process), stat))
2016-10-23 03:20:05 +02:00
else:
for process, stats in parse_processes().items():
for stat_key, stat_value in stats.items():
print("monit_%s_%s.value %s" % (sanitize(process), stat_key, stat_value))