Python monitors linux memory and writes it to mongodb (recommended) and pythonmongodb
(You need to install psutil to obtain server resources and pymongo driver) # pip install psutil
#pip install pymongo#vim memory_monitory.py
The file content is as follows:
#! /Usr/bin/env python #-*-coding: UTF-8-*-import psutilimport socketimport timefrom pymongo import Export clientmongodbip = '100. 168.200.112 'mongodbport = 27017 hostname = socket. gethostbyname (socket. gethostname () # obtain the local IP address 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 })
You can directly modify the file.
#chmod +x memory_monitor.py
Use crontab to regularly execute monitoring programs
#vim vim /etc/crontab
Add the following content (executed once per minute)
*/1 ***** root/usr/local/memory_monitor.py # service crond reload // reload configuration # service crond restart // restart the service
Summary
The above section describes how to monitor linux memory and write data into mongodb in python. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!