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

网友投稿 784 2023-03-09

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

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

目前业务上有几台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()

# -*- 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

@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

上一篇:Attach Volume 操作(Part II) - 每天5分钟玩转 OpenStack(54)
下一篇:Linux安装node,pm2
相关文章

 发表评论

暂时没有评论,来抢沙发吧~