python监控linux内存并写入mongodb(推荐)
(需要安装psutil 用来获取服务器资源,以及pymongo驱动)#pip install psutil
#pip install pymongo #vim memory_monitory.py
文件内容如下
#!/usr/bin/env python # -*- coding: UTF-8 -*- import psutil import socket import time from pymongo import MongoClient mongodbIp = '192.168.200.112' mongodbPort = 27017 hostname = 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.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, 'inactive':memoryInfo.inactive, 'buffers':memoryInfo.buffers, 'cached':memoryInfo.cached})
修改文件可以直接执行
#chmod +x memory_monitor.py
使用crontab 定时执行监控程序
#vim vim /etc/crontab
添加如下内容(每分钟执行一次)
*/1 * * * * root /usr/local/memory_monitor.py #service crond reload //重新载入配置 #service crond restart //重启服务
总结
相关推荐
bluecarrot 2020-11-23
Attend 2020-11-17
ZHAICAN 2020-11-16
codedecode 2020-11-14
zhangbingb 2020-11-12
ykmail 2020-11-11
futurezone 2020-11-06
rikeyone 2020-11-04
橄榄 2020-11-04
WenCoo 2020-10-31
liuyuchen 2020-10-27
ROES 2020-10-24
Gexrior 2020-10-22
ZHAICAN 2020-10-20
风语者 2020-10-16
zhongguomin 2020-10-16
净无邪 2020-10-15