Skip to content

Instantly share code, notes, and snippets.

@taicki
Created January 14, 2012 19:45
Show Gist options
  • Save taicki/1612656 to your computer and use it in GitHub Desktop.
Save taicki/1612656 to your computer and use it in GitHub Desktop.
from itertools import islice
import os
import sys
import subprocess
CMD = "sudo -u hdfs /usr/lib/hadoop/bin/hadoop dfsadmin -report"
def parse(resultstr):
dic = {}
for line in islice(resultstr, 0, 8):
if not line.strip():
continue
namestr, valuestr = line.strip().split(":")
name = namestr.replace(" ", "_").lower()
if "(" in valuestr:
end = valuestr.find("(")
value = valuestr[:end].strip()
else:
value = valuestr.strip()
dic[name] = value
return dic
def check_health(dic):
try:
used = float(dic["dfs_used%"][:-1])
except LookupError:
return "UNKNOWN", 3
if used >= 80:
return "WARNING", 1
elif used > 90:
return "CRITICAL", 2
else:
return "OK", 0
def main():
p = subprocess.Popen(CMD, shell=True, stdout=subprocess.PIPE)
dic = parse(p.stdout.readlines())
health, exitcode = check_health(dic)
print health, "|", " ".join(k+"="+v for k, v in dic.iteritems())
return exitcode
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment