568数据 568数据


监控windows系统的cpu使用情况,python自动重启IIS

网络编程 监控windows系统的cpu使用情况,python自动重启IIS 09-20

目前业务上有几台windows的老机器,经常IIS使用过高。导致服务器down机,上次查过一次,是IIS占用cpu过高引起的,至于为什么是IIS占用资源过高,自己脑补。。。。。最傻瓜的方式就是:iisreset /restart。

工作日的时候,一般都是在自动化管理平台上去重启一下就可以了,但是周末有时候人在外面,旁边没有电脑,这几台机器cpu又高的话,只能选择用脚本来控制。

下面分享两个脚本,一个是bat的  一个是python的。可以根据各自的需求来修改下面的脚本。

python脚本

# -*- coding:utf-8 -*-# Author:Blacksmithimport psutilimport osdef getcpu():    cpu_percent = psutil.cpu_percent(interval=10,percpu=False)    return cpu_percentdef restart_iis():    cpu_percent = getcpu()    if cpu_percent > 80:        os.system('iisreset /restart')if __name__ == '__main__':    restart_iis()

bat脚本

 @echo offfor /f "tokens=2 delims==" %%a in ('wmic path Win32_PerfFormattedData_PerfOS_Processor get PercentProcessorTime /value^|findstr "PercentProcessorTime"') do (set UseCPU=%%a)echo CPU使用率:%UseCPU%%%if %UseCPU% gtr 90 (iisreset /start)pause

编辑:568数据

标签:脚本,过高,都是,机器,上有