Python monitors windows CPU, Memory, Disk, and pythonmemory
A batch of windows systems need to be monitored. Both zabbix and nagios need to install related plug-ins, Which is troublesome to perform.
Python's psutil module monitors linux, windows, mac, and so on across platforms. Therefore, you can use python to write a monitoring script and then use py2exe to package it into an exe, directly put it in windows and run it directly.
1. Install python2.7 (32-bit)
Download the python suitable for the system in the https://www.python.org/downloads/
After installation, modify the environment variable, and add "C: \ Python27" at the end of "system variable" ---- PATH"
2. Install the psutil module (32-bit)
Download the appropriate psutil system in the https://pypi.python.org/pypi/psutil
Register python2.7 before installation. Otherwise, an error is reported..
Edit registration script register. py
## script to register Python 2.0 or later for use with win32all# and other extensions that require Python registry settings## written by Joakim Loew for Secret Labs AB / PythonWare## source:# http://www.pythonware.com/products/works/articles/regpy20.htm## modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html import sys from _winreg import * # tweak as necessaryversion = sys.version[:3]installpath = sys.prefix regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)installkey = "InstallPath"pythonkey = "PythonPath"pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % ( installpath, installpath, installpath) def RegisterPy(): try: reg = OpenKey(HKEY_CURRENT_USER, regpath) except EnvironmentError as e: try: reg = CreateKey(HKEY_CURRENT_USER, regpath) SetValue(reg, installkey, REG_SZ, installpath) SetValue(reg, pythonkey, REG_SZ, pythonpath) CloseKey(reg) except: print "*** Unable to register!" return print "--- Python", version, "is now registered!" return if (QueryValue(reg, installkey) == installpath and QueryValue(reg, pythonkey) == pythonpath): CloseKey(reg) print "=== Python", version, "is already registered!" return CloseKey(reg) print "*** Unable to register!" print "*** You probably have another Python installation!" if __name__ == "__main__": RegisterPy()
Start dos and run python register. py in the directory where register. py is located.
After the execution is complete, the system prompts "Python 2.7 is already registered !"
3. Write a monitoring script (only CPU, memory, and hard disk usage or idle rate are monitored here. If there are other requirements, please complete them yourself)
Psutil related user documentation see http://pythonhosted.org/psutil/
# Coding = utf8import psutilcpu = {'user': 0, 'system': 0, 'idle': 0, 'percent ': 0} mem = {'Total': 0, 'avaiable': 0, 'percent ': 0, 'used': 0, 'free ': 0} # disk name disk_id = [] # store the total used free percent of each disk to the corresponding listdisk_total = [] disk_used = [] disk_free = [] disk_percent = [] # obtain CPU information def get_cpu_info (): cpu_times = psutil. cpu_times () cpu ['user'] = cpu_times.usercpu ['system'] = cpu_times.systemcpu ['idle'] = cpu_times.idlecpu ['percent '] = psutil. cpu_percent (interval = 2) # obtain the memory information def get_mem_info (): mem_info = psutil. virtual_memory () mem ['Total'] = mem_info.totalmem ['available'] = memory ['percent '] = mem_info.percentmem ['used'] = mem_info.usef8 ['free'] = mem_info.free # obtain disk def get_disk_info (): for id in psutil. disk_partitions (): if 'cdrom 'in id. opts or id. fstype = '': continuedisk_name = id. device. split (':') s = disk_name [0] disk_id.append (s) disk_info = psutil. disk_usage (id. device) Partition (disk_info.total) disk_used.append (disk_info.used) disk_free.append (disk_info.free) Partition (disk_info.percent) if _ name _ = '_ main _': get_cpu_info () cpu_status = cpu ['percent '] print u "CPU usage: % s %" % cpu_statusget_mem_info () mem_status = mem ['percent'] print u "memory usage: % s % "% mem_statusget_disk_info () for I in range (len (disk_id): print U' % s disk idle rate: % s % '% (disk_id [I], 100-disk_percent [I]) raw_input ("Enter key to exit... ")
The execution result is as follows:
4. Package python scripts
To run the monitoring script on other windows systems, you need to install the python environment. However, you can package the monitoring script into an exe program and release it. As long as you run the exe program, you can execute it. How can this problem be achieved?
(1)install package program py2exe-0.6.9.win32-py2.7.exe
Download the appropriate version in the http://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/
Download and install it directly.
(2) Compile a simple compilation script setup_py2exe.py.
from distutils.core import setupimport py2exesetup(console=["monitor.py"])
(3) Packaging
Go to dos and run python setup_py2exe.py in the directory where setup_py2exe.py is located.
After running, two folders will be generated in the current directory: producer is the exe program we need to run, and we need to publish the dist folder.
Note: When we copy the distfile to another Windows machine and directly run monitor.exe, an error may be reported"The application fails to start because the application configuration is incorrect.". This is because the py2exe package program needsMSVCR90.DLL of 9.0.21022.8, We can download it from the Internet and put it under the dist directory for publishing. In addition to MSVCR90.DLL of 9.0.21022.8, we also needMicrosoft. VC90.CRT. manifestPut the file in the dist directory.
Microsoft. VC90.CRT. manifest is as follows:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <noInheritable></noInheritable> <assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity> <file name="msvcr90.dll" /> </assembly>
OK ,we need to upload the dist to another Windows Server and run the monitor.exe program.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.