python 监控某个进程的进程数

#!/usr/bin/python 
# _*_ coding:UTF-8 _*_
import socket
import subprocess


def check_proc(ProcName,Warning,Critical):
    CMD = 'pgrep %s' %ProcName
    ProcessInfo =  subprocess.Popen("pgrep %s" %(ProcName),shell=True,stdout=subprocess.PIPE)
    ProcessNum =  len(ProcessInfo.stdout.readlines())
    if ProcessNum > 0 and ProcessNum <= Warning:
        print "%s  OK  %s process" %(ProcName,ProcessNum)
        sys.exit(0)
    elif ProcessNum >= Warning and ProcessNum <= Critical :
        print " %s Warning %s process" %(ProcName,ProcessNum)
        sys.exit(1)
    elif ProcessNum > Critical or ProcessNum == 0:
        print"%s Critical %s process" %(ProcName,ProcessNum)
        sys.exit(2)
    else:
        print "UNkonwn process Error"
        sys.exit(3)
if __name__ == '__main__':
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-p","--process",dest="process",default="httpd",
        help="process for name",metavar="process")
    parser.add_option("-w","--waring",dest="waring",default="10",
        help="process count waring",metavar="waring")
    parser.add_option("-c","--critical",dest="critical",default="12",
        help="process count critical",metavar="critical")
    (options,args) = parser.parse_args()
    #    print "option:%s,args:%s" %(options,args)
    #    print options.process,options.waring,options.critical
    check_proc(options.process,int(options.waring),int(options.critical))
    #    check = check_server(options.address,int(options.port))

你可能感兴趣的:(python)