目录
- 监控需求
- 监控脚本
- 总结
监控需求
接到个任务,需要监控mongodb,我们用的mongo是分片加副本集,网上监控的模板与资料太少,于是自己写了个python脚本监控。 满分10分,自己对mongo的熟悉有2分吧/(ㄒoㄒ)/~~,参照自己的理解监控了一些参数,贴出第一批监控的数据
监控脚本
pip install pymongo
#!/usr/bin/python
#coding=utf-8
#author OrangeLoveMilan
from pymongo import MongoClient
import json
import mylog
import sys
def login():
'''
登陆mongo,并执行查询命令
:return:
'''
try:
client = MongoClient('ip', port)
db = client.admin
db.authenticate("user", "pwd")
command = db.command('serverStatus')
return command
except Exception,e:
mylog.logging.error(e)
sys.exit()
def delMongoStatus():
'''
处理数据,转换成便于读写dic格式
:return:
'''
mongoStatus = login()
del mongoStatus["localTime"]
Status = json.loads(json.dumps(mongoStatus))
return Status
def monitor():
'''
监控的参数
:return:
'''
monitorStatus = delMongoStatus()
##启动时间
uptime = int(monitorStatus['uptime'] / 3600 / 24)
##物理内存和虚拟内存
mem = monitorStatus['mem']
phyMem = mem['resident']
virMem = mem['virtual']
##连接数
connections = monitorStatus['connections']
currentCon = connections['current']
availableCon = connections['available']
totalCreatedCon = connections['totalCreated']
##操作数目
opcounters = monitorStatus['opcounters']
getmore = opcounters['getmore']
insert = opcounters['insert']
update = opcounters['update']
command = opcounters['command']
query = opcounters['query']
delete = opcounters['delete']
monitorDic = {'uptime':uptime,'phyMem':phyMem,'virMem':virMem,'currentCon':currentCon,'availableCon':availableCon,'totalCreatedCon':totalCreatedCon,'getmore':getmore,'insert':insert,'update':update,'command':command,'query':query,'delete':delete}
return monitorDic
try:
zabbix = monitor()
for key in zabbix:
if key == sys.argv[1]:
print zabbix[key]
except Exception,e:
mylog.logging.error(e)
sys.exit()
总结
这次是监控的mongos的状态参数,分片的状态和副本级的状态下次完善时添加进去。监控的图就不贴了,脚本写的很简单,主要运用了python很基础的字典来处理数据,给新手一个参考,一起成长