#!/usr/bin/env pythonimport sysfrom optparse import optionparserimport stringok= 0warning=1critical=2unknown=3unit = {' t ': 2**40, ' G ': 2**30, ' m ': 2**20, ' K ': 2**10, ' B ': 1}def opt (): parser = optionparser (usage= "usage: %prog -w warning -c critical ") parser.add_option ("-C ", default=" 100M ", action=" store ", type= "string", dest= "critical") parser.add_option ("-W", default= "500M", action= "Store", type= "string", dest= "warning") return parser.parse _args () Def convertunit (s): s = s.lower () lastchar = s[-1] num = int (S[:-1]) if lastchar in unit: return num*unit[lastchar] else:&nbSp; return int (s) def scaleunit (d): For k,v in unit.items (): num = d / float (v) if (0 < num < 2* *10): return num,kdef Getfreememory (): with open ('/proc/meminfo ', ' R ') as fd: for line in fd.readlines (): if line.startswith (' Memfree '): k, v, u = line.split () return Int (v) *1024def main (): opts, args = opt () w = convertunit ( opts.warning) c = convertunit (opts.critical) free_mem = getfreememory () h_free, h_unit = scaleunit (FREE_MEM) if free_mem > w: print "ok, free:%s %s" % (h_free, h_unit) sys.exit (OK) elif w >= free _mem > c: print "WARNING, free: %s %s " % (h_free, h_unit) sys.exit (WARNING) elif free_mem <= c: print "critical, free: %s %s " % (h_free, h_unit) Sys.exit (CRITICAL) else: print " unknonw, free:%s %s " % (h_free, h_unit) sys.exit (UNKNOWN) if __name__ == "__main__": main ()
This article from "Muzinan Technology blog" blog, declined reprint!
Nagios-plugin (MEM)