Zabbix对mongodb进行性能监控

在网上检索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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值