Python monitors linux memory and writes it to mongodb 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 psutil
Import socket
Import time
From pymongo import MongoClient
MongodbIp = '192. 168.200.112'
Export dbport = 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. servermonitor
Dbset = db. memory
Dbset. insert ({
'Time': getCurrentTime (),
'Total': memoryInfo. total,
'Available': memoryInfo. available,
'Percent ': memoryInfo. percent,
'Used': memoryInfo. used,
'Free': memoryInfo. free,
'Active': memoryInfo. active,
'Inactivity': 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/etc/crontab
Add the following content (executed once per minute)
*/1 * root/usr/local/memory_monitor.py
# Service crond reload // reload the configuration # service crond restart // restart the service