Because of the recent server front-end although load balancing, but the back-end Windows host occasionally because the service process CPU to 99 this situation caused the service is not available, although this does not need to process the first time, but also needs to manually log on to the process of kill, Windows can write a. bat script through a DOS script to implement this function, and call this. bat script through configuration, but I've tried it a few times and I don't feel right for me.
So write a script in Python and convert the Py2exe to an. exe program on several windows, because it is not possible for each server to manually deploy the next Python environment.
The code is as follows:
Import Psutil
Import Smtplib
Import Socket,time
From Email.mime.text import Mimetext
mailto_list=[' [email protected]
Hostname=socket.gethostname ()
Subject=hostname+ ' server ' + ' w3wp process CPU accounted for greater than 95% '
#监听w3wp的进程cpu占比并杀进程功能函数
Def moniter_w3wp ():
for proc in Psutil.process_iter (): #通过迭代器遍历进程
Try
If proc.name () = = ' W3wp.exe ' and Proc.cpu_percent (interval=2)/24>=95:
Cpu_threshold=proc.cpu_percent (interval=2)/24 #此处需要除以处理cpu的核心个数
Proc_name=proc.name ()
C_percent=cpu_threshold
S_cpu_percent=str (c_percent)
#print u "Process Name:%s,cpu use Percent:%s%"% (proc_name,c_percent)
#print c_percent
Proc.kill ()
Return s_cpu_percent
Except Psutil. Nosuchprocess:
Pass
#else:
#print ' No process need to kill '
#发送邮件函数
def send_mail (to_list,sub,content):
Me= "[Email protected]"
msg = Mimetext (content,_subtype= ' plain ', _charset= ' gb2312 ')
msg[' Subject ' = Sub
Msg[' from '] = Me
Msg[' to '] = ";". Join (To_list)
Try
Server = Smtplib. SMTP (' localhost ') #因为每台服务器开了smtp邮件服务, so the local address is called here, because I use QQ as SMTP will have SSL authentication error message, but also need to configure, so there is no use of QQ
Server.sendmail (Me, To_list, msg.as_string ())
Server.close ()
Return True
Except Exception, E:
Print str (e)
Return False
If __name__== ' __main__ ':
while (1):
Time.sleep (1)
BKVALUE=MONITER_W3WP ()
If bkvalue>= ' 95 ':
#print "Back value:%s"% Bkvalue
Now_cpuvalue=bkvalue
Emailcontent=hostname+ ' server ' + ' w3wp process CPU is greater than 95%, has been automatically killed; ' + ' now Cpu: ' +now_cpuvalue
#print emailcontent
If Send_mail (mailto_list,subject,emailcontent):
print ' Mail has been sent to the manager! ‘
Else
print ' mail message failed to send! ‘
# # Else:
# # print ' w3wp process CPU status OK! ‘
Define the above script as a windows_monitor_cpu_killer.py file
Then create a new file for compiling setup_py2exe.py content as follows:
# #py2exe需要提前下载,: https://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/
From Distutils.core Import Setup
Import Py2exe
Setup (windows=["windows_monitor_cpu_killer.py"]) Windows GUI can also become console
An explanation of this configuration can be seen in the details of help (Py2exe), with the following excerpt:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7C/63/wKiom1bP07KRXDw_AAESpaoOVqU017.png "title=" Py2exe Describ.png "alt=" Wkiom1bp07krxdw_aaespaoovqu017.png "/>
After the script local test passes, convert. py to an. exe
If your native is Windows system DOS switch to the directory where the. py file is located execute python setup_py2exe.py py2exe
After execution, you will generate two Folders Dist folder and build folder, copy the Dist folder to the server host you need to monitor, execute the. exe file.
Article refer to Related link address:
Http://www.cnblogs.com/dplearning/p/5082074.html
http://tony413.iteye.com/blog/395177
http://blog.csdn.net/xm1331305/article/details/8129244
Http://www.jianshu.com/p/64e265f663f6
This article is from the "Oracle_lover" blog, make sure to keep this source http://oraclelover21.blog.51cto.com/2805838/1745245
Python listens on Windows w3wp process, if cpu>=95% automatically kill