Skip to content
49 changes: 37 additions & 12 deletions collective/check_munin/check_munin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@
import sys
import time


def readRRDData(fn):
def readRRDData(fn, continueonstale):
try:
s = subprocess.check_output(['rrdtool', 'lastupdate', fn, ])
except subprocess.CalledProcessError:
print sys.exc_value
print(sys.exc_value)
sys.exit(3)
mtime, value = s.split('\n')[2].split(':')
mtime, value = s.decode().split('\n')[2].split(':')
if time.time() - float(mtime) > 600:
print "%s has stale data." % fn
sys.exit(3)
return float(value.strip())
# print("continueonstale: %s" % continueonstale)
if continueonstale < 1:
print("%s has stale data." % fn)
sys.exit(3)

v = value.strip()
if v.find("0x") == 0 :
v = int(v, 16)
return float(v)


class RRD(nagiosplugin.Resource):
Expand All @@ -35,35 +40,54 @@ def __init__(self, args):
self.module = args.module
self.only = args.only
self.ignore = args.ignore
self.rrdpath = os.path.join(args.datadir, args.domain)
self.continueonstale = args.continueonstale
domain = args.domain
if not domain:
domain = re.sub(r'^[^\.]+\.', '', args.hostname)
self.rrdpath = os.path.join(args.datadir, domain)


def probe(self):
myglob = os.path.join(
self.rrdpath,
'%s-%s-%s-g.rrd' % (self.hostname, self.module, self.only)
'%s-%s-%s-*.rrd' % (self.hostname, self.module, self.only)
)
logging.info('checking %s', myglob)
myfiles = glob.glob(myglob)
myfiles.sort()
for fn in myfiles:
component_pattern = re.compile(r"^.+/%s-%s-(.+)-[a-z]\.rrd$" % (self.hostname, self.module))
# print('file: %s' % fn)
component_pattern = re.compile(r"^.+/%s-%s-(.+)-([a-z])\.rrd$" % (self.hostname, self.module))
# print(component_pattern)
mo = component_pattern.match(fn)
if mo:
component = mo.group(1)
typedata = mo.group(2)
# print('component: %s' % component)
# print('typedata: %s' % typedata)
# print('ignore: %s' % self.ignore)
# print('continueonstale: %s' % self.continueonstale)
if component not in self.ignore:
component = component.replace('_', '/')
# note that we check ignore again in case it was specified with /s
if component not in self.ignore:
logging.info('reading %s', component)
yield nagiosplugin.Metric(component, readRRDData(fn), min=0, context='munin')
data = readRRDData(fn, self.continueonstale)
# print('data: %s' % data)

if typedata == 'd':
data = data / (1024 * 1024) / 127 * 100
yield nagiosplugin.Metric(component, data, min=0, context='munin')


@nagiosplugin.guarded
def main():
argp = argparse.ArgumentParser(description=__doc__)
argp.add_argument('-d', '--domain', required=True, default='')
argp.add_argument('-d', '--domain', default='')
argp.add_argument('-H', '--hostname', required=True, default='')
argp.add_argument('-M', '--module', required=True, default='')
argp.add_argument('-C', '--continueonstale', action='count', default=0,
help='keep on running if stale data is found)')
argp.add_argument('-o', '--only', default='*',
help='restrict to a subcomponent of module')
argp.add_argument('-i', '--ignore', action='append', default=[],
Expand All @@ -84,6 +108,7 @@ def main():
nagiosplugin.ScalarContext('munin', args.warning, args.critical),
)
check.name = args.name or args.module
check.continueonstale = args.continueonstale
check.main(verbose=args.verbose)

if __name__ == '__main__':
Expand Down