在网上检索zabbix监控mongodb性能时,大部分出现的是shell脚本,且mongodb版本不一样的话,有的性能参数是没有的。该shell脚本有一个问题就是,如果我有多个监控项目的话,那么我每个监控项目都需要进行取值,这样无形中就给mongodb增加了压力,我这边方式是取值一次然后进行多个监控值推送。
一、Python脚本数据采集
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : mongodb.py
@Time : 2019/06/14 16:31:52
@Author : Peter
@Version : 1.0
@Contact : sky_pipi@163.com
@License : (C)Copyright 2017-2018, Liugroup-NLPR-CASIA
@Desc : None
'''
import json
import sys
import socket
from pymongo import MongoClient
listparasm = [
["connections","available"],
["connections","current"],
["connections","totalCreated"],
["globalLock","activeClients","readers"],
["globalLock","activeClients","total"],
["globalLock","activeClients","writers"],
["globalLock","currentQueue","readers"],
["globalLock","currentQueue","total"],
["globalLock","currentQueue","writers"],
["mem","mappedWithJournal"],
["mem","mapped"],
["mem","resident"],
["mem","virtual"],
["metrics","document","deleted"],
["metrics","document","inserted"],
["metrics","document","returned"],
["metrics","document","updated"],
["network","bytesIn"],
["network","bytesOut"],
["network","numRequests"],
["opcounters","command"],
["opcounters","delete"],
["opcounters","getmore"],
["opcounters","insert"],
["opcounters","query"],
["opcounters","update"],
["opcountersRepl","command"],
["opcountersRepl","delete"],
["opcountersRepl","getmore"],
["opcountersRepl","insert"],
["opcountersRepl","query"],
["opcountersRepl","update"]
]
baseitem = "mongodb.status"
hostname = socket.gethostbyname(socket.getfqdn(socket.gethostname()))
file="mongostatus.txt"
# 清空文件先,避免中途取值有问题,定时任务发送上次的数据
f = open(file,'w')
try:
mo = MongoClient('127.0.0.1', 27017, connectTimeoutMS=5000)
except Exception as e:
print ('Can\'t connect to '+ '127.0.0.1')
print ("ERROR:", e)
sys.exit(1)
res = mo.admin.command('serverStatus')
for item in listparasm:
if len(item) == 2:
value = res[item[0]][item[1]]
elif len(item) == 3:
value = res[item[0]][item[1]][item[2]]
else:
continue
param = ",".join(item)
line = hostname + " "+baseitem+"["+param+"]"+" " + str(value)
f.write(line+"\n")
f.close()
上面的脚本中的列表项就是采集的性能监控指标项,取值后然后按规则放进一个临时文件,最后在服务器上设置一个定时任务每分钟使用zabbix_sender批量发送监控项目值到服务端 。
# check mongodb
* * * * * python /etc/zabbix/scripts/checkmongo.py & zabbix_sender -z 172.31.7.125 -s 172.31.44.135 -i /tmp/mongostatus.txt &>/dev/null
二、模板导入
模板就不贴上来了,直接给个地址下载把。redis 、mysql性能监控都在下面地址上
https://github.com/snjd/db-zabbix-template