首页 > 代码库 > nagios-plugin(mem)
nagios-plugin(mem)
#!/usr/bin/env python import sys from optparse import OptionParser import string OK=0 WARNING=1 CRITICAL=2 UNKNOWN=3 unit = {‘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: return int(s) def scaleUnit(d): for k,v in unit.items(): num = d / float(v) if (0 < num < 2**10): return num,k def 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)*1024 def 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()
本文出自 “muzinan的技术博客” 博客,谢绝转载!
nagios-plugin(mem)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。