用WMI监控IIS
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用WMI监控IIS相关的知识,希望对你有一定的参考价值。
参考网站:http://blog.chinaunix.net/uid-7910284-id-5774420.html
获取服务信息
from win32com.client import GetObject import wmi,sys,argparse,subprocess parser = argparse.ArgumentParser( usage = ‘check_win_service.py -t xx -n xxxx -cw xx.xx -cc xx.xx -mw xx.xx -mc xx.xx‘, description=‘to check windows iis thrift or process usages‘ ) parser.add_argument( ‘-t‘, dest=‘type‘, metavar=‘string‘, type=str, required=True, help=‘resource type:iis|srv|app‘ ) parser.add_argument( ‘-n‘, dest=‘name‘, metavar=‘string‘, type=str, required=True, help=‘service or app name‘ ) parser.add_argument( ‘-cw‘, dest=‘cpuwarn‘, metavar=‘float‘, type=float, nargs=‘?‘, const=80.00, help=‘warning value for cpu,default 80%‘ ) parser.add_argument( ‘-cc‘, dest=‘cpucrit‘, metavar=‘float‘, type=float, nargs=‘?‘, const=90.00, help=‘critial value for cpu,default 90%‘ ) parser.add_argument( ‘-mw‘, dest=‘memwarn‘, metavar=‘float‘, type=float, nargs=‘?‘, const=15.00, help=‘warning value for mem,default 15%‘ ) parser.add_argument( ‘-mc‘, dest=‘memcrit‘, metavar=‘float‘, type=float, nargs=‘?‘, const=20.00, help=‘critial value for mem,default 20%‘ ) parser.add_argument( ‘-tw‘, dest=‘tcpwarn‘, metavar=‘int‘, type=int, nargs=‘?‘, const=800, help=‘warning value for tcp,default 800‘ ) parser.add_argument( ‘-tc‘, dest=‘tcpcrit‘, metavar=‘int‘, type=int, nargs=‘?‘, const=1000, help=‘critial value for tcp,default 1000‘ ) args = parser.parse_args() def iis_usage(iisname): wmi = GetObject(‘winmgmts:/root/cimv2‘) iisbase = wmi.ExecQuery(‘select * from Win32_PerfFormattedData_W3SVCW3WPCounterProvider_W3SVCW3WP where Name like "%{}"‘.format(iisname)) for item in iisbase: iispid = item.Name.split(‘_‘)[0] iisstatus = [] try: iisinfo = wmi.ExecQuery(‘select * from Win32_PerfFormattedData_PerfProc_Process where IDProcess = "{}"‘.format(iispid)) except UnboundLocalError as nopid: print("CRITIAL - {} was down".format(iisname)) sys.exit(2) for item in iisinfo: iisstatus.append(item.PercentProcessorTime) iisstatus.append(round(float(item.WorkingSetPrivate)/1024/1024,2)) iiscon = wmi.ExecQuery(‘select * from Win32_PerfFormattedData_W3SVC_WebService where Name = "{}"‘.format(iisname)) for item in iiscon: iisstatus.append(item.CurrentConnections) return(iisname,iispid,iisstatus) def srv_usage(srvname): wmi = GetObject(‘winmgmts:/root/cimv2‘) srvbase = wmi.ExecQuery(‘select * from Win32_Service where Name = "{}"‘.format(srvname)) for item in srvbase: srvpid = item.ProcessId srvstatus = [] try: srvinfo = wmi.ExecQuery(‘select * from Win32_PerfFormattedData_PerfProc_Process where IDProcess = "{}"‘.format(srvpid)) except UnboundLocalError as nopid: print("CRITIAL - {} was down".format(srvname)) sys.exit(2) for item in srvinfo: srvstatus.append(item.PercentProcessorTime) srvstatus.append(round(float(item.WorkingSetPrivate)/1024/1024,2)) srvstatus.append(subprocess.getstatusoutput(‘netstat -ano | findstr {} | wc -l‘.format(srvpid))[1]) return(srvname,srvpid,srvstatus) def app_usage(appname): wmi = GetObject(‘winmgmts:/root/cimv2‘) appbase = wmi.ExecQuery(‘select * from Win32_Process where CommandLine like "%{}%" and Caption != "python.exe"‘.format(appname)) for item in appbase: apppid = item.ProcessId appstatus = [] try: appinfo = wmi.ExecQuery(‘select * from Win32_PerfFormattedData_PerfProc_Process where IDProcess = "{}"‘.format(apppid)) except UnboundLocalError as nopid: print("CRITIAL - {} was down".format(appname)) sys.exit(2) for item in appinfo: appstatus.append(item.PercentProcessorTime) appstatus.append(round(float(item.WorkingSetPrivate)/1024/1024,2)) appstatus.append(subprocess.getstatusoutput(‘netstat -ano | findstr {} | wc -l‘.format(apppid))[1]) return(appname,apppid,appstatus) def judgement(judgetype,judgename,judgecpuw,judgecpuc,judgememw,judgememc,judgeconw,judgeconc): wmi = GetObject(‘winmgmts:/root/cimv2‘) totalmem = wmi.ExecQuery(‘select * from Win32_PhysicalMemory‘) memtotal = 0 for item in totalmem: memtotal += round((float(item.Capacity)/1024)/1024,2) if judgetype == ‘iis‘: stu = iis_usage(args.name) elif judgetype == ‘srv‘: stu = srv_usage(args.name) elif judgetype == ‘app‘: stu = app_usage(args.name) else: print("there have not this type") sys.exit(3) statstr = "OK" status = 0 cp = float(stu[2][0]) mp = round((float(stu[2][1])/memtotal)*100, 2) tp = int(stu[2][2]) if cp < judgecpuw and mp < judgememw and tp < judgeconw: pass elif judgecpuw <= cp < judgecpuc or judgememw <= mp < judgememc or judgeconw <= tp < judgeconc: status = 1 statstr = "WARNING" elif judgecpuc <= cp[3][0] or judgememc <= mp or judgeconc <= tp: status = 2 statstr = "CRITIAL" else: status = 3 statstr = "UNKNOWN" print("{0} - {1} PID:{2}, Cpu Usage:{3}%,Mem Usage:{4}% {5}MB,Tcp Sockes:{6}CON | Cpu={3}%;{7};{8} Mem={4}%;{9};{10} MemUsage={5}MB;; Tcp={6}CON;{11};{12}" .format(statstr,stu[0],stu[1],cp,mp,stu[2][1],tp,judgecpuw,judgecpuc,judgememw,judgememc,judgeconw,judgeconc)) sys.exit(status) judgement(args.type,args.name,args.cpuwarn,args.cpucrit,args.memwarn,args.memcrit,args.tcpwarn,args.tcpcrit)
获取资源信息
from win32com.client import GetObject import wmi,sys,argparse parser = argparse.ArgumentParser( usage = ‘check_win_network.py -w xx.xx -c xx.xx‘, description=‘to check linuxs mem or net usages‘ ) parser.add_argument( ‘-t‘, dest=‘type‘, metavar=‘string‘, type=str, required=True, help=‘resource type:mem|net‘ ) parser.add_argument( ‘-d‘, dest=‘devs‘, metavar=‘string‘, type=str, nargs=‘?‘, const="eth0", help=‘network card name,do not define it if type is not "net"‘ ) parser.add_argument( ‘-w‘, dest=‘warn‘, metavar=‘float‘, type=float, nargs=‘?‘, const=80.0, help=‘warning value,default 80‘ ) parser.add_argument( ‘-c‘, dest=‘crit‘, metavar=‘float‘, type=float, nargs=‘?‘, const=90.0, help=‘critial value,default 90‘ ) args = parser.parse_args() def net_usage(dev="eth0", warning=80.00, critical=90.00): wmi = GetObject(‘winmgmts:/root/cimv2‘) networks = wmi.ExecQuery(‘select * from Win32_PerfFormattedData_Tcpip_NetworkInterface‘) netinfo=[] for item in networks: if item.Name == dev: netinfo.append(item.Name) netinfo.append(round(float(item.BytesReceivedPersec)*8/1024/1024,2)) netinfo.append(round(float(item.BytesSentPersec)*8/1024/1024,2)) netinfo.append(round(float(item.BytesTotalPersec)*8/1024/1024,2)) netinfo.append(round(float(item.CurrentBandwidth)/1000/1000,0)) netinfo.append(item.PacketsReceivedPersec) netinfo.append(item.PacketsSentPersec) netinfo.append(item.PacketsPersec) exitflag = 0 status="OK" warning_line = netinfo[4] * (warning/100) critical_line = netinfo[4] * (critical/100) usepec = round((netinfo[3] / netinfo[4]) * 100,2) tp = netinfo[3] if tp < warning_line: pass elif warning_line <= tp < critical_line: exitflag = 1 status="WARNING" elif tp >= critical_line: exitflag = 2 status="CRITICAL" else: print("UNKNOW") sys.exit(3) print("{0} - {1} NetWork Usage: Network wide {2}Mbps, Use_Persent {3}%, Total_RX-TX {4}Mbps, RX {5}Mbps ,RX-package {6}, TX {7}Mbps, TX-package {8} | Use_Persent={3}%;;; Total_RX-TX={4}Mbps;{9};{10}; RX={5}Mbps;;; RX-package={6};;; TX={7}Mbps;;; TX-package={8};;;" .format(status,netinfo[0],netinfo[4],usepec,netinfo[3],netinfo[1],netinfo[5],netinfo[2],netinfo[6],warning_line,critical_line)) sys.exit(exitflag) def mem_usage(warning=80.00, critical=90.00): wmi = GetObject(‘winmgmts:/root/cimv2‘) totalmem = wmi.ExecQuery(‘select * from Win32_PhysicalMemory‘) freemem = wmi.ExecQuery(‘select * from Win32_PerfFormattedData_PerfOS_Memory‘) memtotal = 0.0 memused = 0.0 memfree = 0.0 memcache = 0.0 for item in totalmem: memtotal += round((float(item.Capacity)/1024)/1024,2) for item in freemem: memfree = float(item.AvailableMBytes) memcache = round(float(item.CacheBytes)/1024/1024,2) memused = memtotal - memfree exitflag = 0 status="OK" warning_mem = round((warning/100) * memtotal,2) critical_mem = round((critical/100) * memtotal,2) if memused < warning_mem: pass elif warning_mem <= memused < critical_mem: exitflag = 1 status="WARNING" elif memused >= critical_mem: exitflag = 2 status="CRITICAL" else: print("UNKNOW") sys.exit(3) print("{0} - Mem Usage: Use_Persent {1}%, total {2}MB, free {3}MB, used {4}MB, cache-buffer {5}MB | Use_Persent={1}%;;; total={2}MB;;; free={3}MB;;; used={4}MB;{6};{7}; cache-buffer={5}MB;;;" .format(status,round((memused/memtotal)*100,2),memtotal,memfree,memused,memcache,warning_mem,critical_mem)) sys.exit(exitflag) if args.type == "mem": mem_usage(args.warn, args.crit) elif args.type == "net": net_usage(args.devs,args.warn,args.crit) else: print(args.type) print("no this types -h for help")
以上是关于用WMI监控IIS的主要内容,如果未能解决你的问题,请参考以下文章
WMI IIS 6.0 自定义 HttpError (Powershell)