参考网站:http://blog.chinaunix.net/uid-7910284-id-5774420.html

参考官方文档:https://docs.microsoft.com/en-us/previous-versions//aa394298(v=vs.85)

获取服务信息

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的更多相关文章

  1. 监控IIS的运行状态

    IIS经常出现假死的情况,具体什么时候会出现假死,我就不说了,今天我要写的是如何监控IIS的状态. 程序的功能是:如果IIS是为运行的状态,就重启IIS,如果IIS的连接数达到了设置的连接数,也重启I ...

  2. Zabbix WMI监控

    检查Windows OS是否激活,5表示处于通知模式,1表示已激活 wmi.get[root\cimv2,select LicenseStatus FROM SoftwareLicensingProd ...

  3. python利用WMI监控windows状态如CPU、内存、硬盘

    安装pywin32库 下载地址: https://sourceforge.net/projects/pywin32/files%2Fpywin32/选择对应python版本的文件.下载后在window ...

  4. 使用WMI监控进程启动与结束

    需要添加引用System.Management 代码: static void Main(string[] args) { //创建WQL事件查询,监视进程开启 var qCreate = new W ...

  5. 监控iis计数器

  6. IIS监控应用程序池和站点假死,自动重启IIS小工具

    文章技术适合初学者.高级的C#开发工程师这些估计都熟悉到烂了,望不要喷. 第一.C#代码要操作IIS 就必须先导入 Microsoft.Web.Administration.dll ,方便控制台程序做 ...

  7. 一个只需要点 「下一步」就完成监控 Windows

    Cloud Insight 此前已然支持 Linux 操作系统,支持20多中数据库中间件等组件,多种操作,多种搭配,服务器监控玩的其乐无穷啊!但想想还有许多 Windows 的小伙伴没有体验过,所以在 ...

  8. 老司机实战Windows Server Docker:2 docker化现有iis应用的正确姿势

    前言 上一篇老司机实战Windows Server Docker:1 初体验之各种填坑介绍了安装docker服务过程中的一些小坑.这一篇,我们来填一些稍大一些的坑:如何docker化一个现有的iis应 ...

  9. IIS Enabling HTTP Keep-Alives

    IIS 6.0 from:https://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/ea116535-8e ...

随机推荐

  1. hive metastore && hiveserver2 . jvm 配置调整优化

    hive-env.sh 添加如下,其中踩坑踩了不少. if [ "$SERVICE" = "metastore" ]; then if [ -z "$ ...

  2. Kafka技术内幕 读书笔记之(四) 新消费者——心跳任务

    消费者拉取数据是在拉取器中完成的,发送心跳是在消费者的协调者上完成的,但并不是说拉取器和消费者的协调者就没有关联关系 . “消费者的协调者”的作用是确保客户端的消费者和服务端的协调者之间的正常通信,如 ...

  3. EJB到底是什么

    EJB到底是什么?   1. 我们不禁要问,什么是"服务集群"?什么是"企业级开发"? 既然说了EJB 是为了"服务集群"和"企业 ...

  4. lasticsearch最佳实践之分片使用优化

    一.遇到的问题 与大多数分布式系统一样,Elasticsearch按照一定的Hash规则把用户数据切分成多个分片,然后打散到不同机器进行存储,从而实现大规模数据的分布式存储. cluster.png ...

  5. G1垃圾收集器的实现原理

    (G1垃圾收集器的实现原理.G1和CMS经常被单独拎出来问) https://tech.meituan.com/g1.html G1太复杂,说下CMS吧

  6. java的TCP和UDP编程

    TCP 客户端: import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter ...

  7. [ASNI C] [常用宏定义] [define技巧]

    1. 打印变量名及其值 #define Inquire(var, mode) fprintf(stdout, #var" = "#mode". \n", var ...

  8. ButterKnife官方使用例子

    Introduction Annotate fields with @BindView and a view ID for Butter Knife to find and automatically ...

  9. 让overflow:auto页面滚动条出现时不跳动

    今天看到一篇张鑫旭的文章,转载过来.https://www.zhangxinxu.com/wordpress/2015/01/css-page-scrollbar-toggle-center-no-j ...

  10. Puppeteer - 谷歌推出的自动化测试工具库

    Puppeteer 是一个 Node 库,它提供了一个高级 API 来通过 DevTools 协议控制无头 Chrome 或 Chromium.它允许你从浏览器之外的环境(即命令行)与Chromium ...