This article mainly describes the Python monitoring of Linux memory and write the relevant information of MongoDB, the need for friends can refer to the following
(Need to install Psutil to get server resources, as well as Pymongo drivers) #pip Install Psutil
#pip Install Pymongo#vim memory_monitory.py
The file contents are as follows
#!/usr/bin/env python#-*-coding:utf-8-*-import psutilimport socketimport timefrom Pymongo Import MongoClientmongodbIp = ' 192.168.200.112 ' mongodbport = 27017hostname = Socket.gethostbyname (Socket.gethostname ()) #获取本地IP地址def GetCurrentTime (): return Time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime (Time.time ())) Memoryinfo = Psutil.virtual_memory () conn = mongoclient (mongodbip,mongodbport) db = Conn.servermonitordbset = Db.memorydbset.insert ({ ' time ': GetCurrentTime (), ' total ': memoryinfo.total, ' available ': memoryinfo.available, ' Percent ': memoryinfo.percent, ' used ': memoryinfo.used, ' free ': memoryinfo.free, ' active ': Memoryinfo.active, ' inactive ': memoryinfo.inactive, ' buffers ': memoryinfo.buffers, ' cached ': Memoryinfo.cached})
Modified files can be executed directly
#chmod +x memory_monitor.py
Using crontab to perform the monitoring program regularly
#vim Vim/etc/crontab
Add the following (executed once per minute)
*/1 * * * * root/usr/local/memory_monitor.py#service Crond Reload //Reload configuration #service crond Restart//Restart service