File: //lib/check_mk_agent/local/locaweb-nfs
#!/usr/bin/python
import os
import time
import json
from pprint import pprint
stats = "/proc/self/mountstats"
cache = "/tmp/%s-mountstats.cache" % os.getuid()
def parse_mountstats():
mountstats = {"timestamp": time.time(), "datasets": {}}
device = None
ops = {}
for line in [ line.strip().split() for line in open(stats).readlines() ]:
if len(line) == 0: continue
# New device
if line[0] == "device" and line[7] == "nfs" and "/.zfs/" not in line[1] and "/.snapshots" not in line[1]:
if device:
mountstats["datasets"][device] = ops
device = line[1]
ops = {}
if line[0] == "xprt:":
ops.update ({'connect_count': int(line[4]), 'connect_time': int(line[5]), 'rpcsends': int(line[7]), 'rpcreceives': int(line[8]), 'backlogutil': int(line[11]) })
if line[0] in ("READ:", "WRITE:"):
operation = line[0][:-1].lower()
ops.update({operation+'_ops': float(line[1]), operation+'_retrans': float(line[2]) - float(line[1]), operation+'_bytes': (float(line[4]) + float(line[5])) / 1024 / 1024, operation+'_exe_time': float(line[8])})
if device:
mountstats["datasets"][device] = ops
return mountstats
if __name__ == '__main__':
stats = parse_mountstats()
diff = {}
if not os.path.isfile(cache):
open(cache, 'w').write(json.dumps(stats))
else:
try:
old = json.loads(open(cache).read())
except:
pass
else:
timediff = stats["timestamp"] - old["timestamp"]
for dataset, info in stats["datasets"].iteritems():
diff[dataset] = {}
for key, value in info.iteritems():
if old["datasets"].get(dataset, {}).get(key):
valuediff = value - old["datasets"].get(dataset, {}).get(key)
if valuediff == 0:
diff[dataset][key] = valuediff
else:
diff[dataset][key] = valuediff / timediff
open(cache, 'w').write(json.dumps(stats))
for dataset, info in diff.iteritems():
print "0 stats-%s" % dataset, "|".join([ "%s=%s" % (x, y) for x, y in info.iteritems() ]), "OK"