Nagios-plugin (MEM)

Source: Internet
Author: User

#!/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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.