目錄
- 監控需求
- 監控腳本
- 總結
監控需求
接到個任務,需要監控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很基礎的字典來處理數據,給新手一個參考,一起成長