centos7环境下使用python脚本监控mongodb集群复制状态

centos7环境下使用python脚本监控mongodb集群复制状态


centos环境下搭建了 MongoDB 副本集,需要对集群的复制状态进行监控

获取集群的状态信息,集群是3个节点,没有设置仲裁者

[root@eus-image-design-mongo01:/usr/local]# mongo --port 21000
MongoDB shell version v3.4.24
connecting to: mongodb://127.0.0.1:21000/
MongoDB server version: 3.4.24
configs:SECONDARY> use admin
switched to db admin
configs:SECONDARY> db.auth('zabbix','zabbix')
1
configs:SECONDARY> rs.status()
{
        "set" : "configs",
        "date" : ISODate("2020-11-12T08:07:11.216Z"),
        "myState" : 2,
        "term" : NumberLong(4),
        "syncingTo" : "172.30.0.110:21000",
        "syncSourceHost" : "172.30.0.110:21000",
        "syncSourceId" : 2,
        "configsvr" : true,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1605168428, 2),
                        "t" : NumberLong(4)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1605168428, 2),
                        "t" : NumberLong(4)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1605168428, 2),
                        "t" : NumberLong(4)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1605168428, 2),
                        "t" : NumberLong(4)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "172.30.0.109:21000",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 8363759,
                        "optime" : {
                                "ts" : Timestamp(1605168428, 2),
                                "t" : NumberLong(4)
                        },
                        "optimeDate" : ISODate("2020-11-12T08:07:08Z"),
                        "syncingTo" : "172.30.0.110:21000",
                        "syncSourceHost" : "172.30.0.110:21000",
                        "syncSourceId" : 2,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "172.30.0.108:21000",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 8363758,
                        "optime" : {
                                "ts" : Timestamp(1605168428, 2),
                                "t" : NumberLong(4)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1605168428, 2),
                                "t" : NumberLong(4)
                        },
                        "optimeDate" : ISODate("2020-11-12T08:07:08Z"),
                        "optimeDurableDate" : ISODate("2020-11-12T08:07:08Z"),
                        "lastHeartbeat" : ISODate("2020-11-12T08:07:10.523Z"),
                        "lastHeartbeatRecv" : ISODate("2020-11-12T08:07:10.529Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "172.30.0.110:21000",
                        "syncSourceHost" : "172.30.0.110:21000",
                        "syncSourceId" : 2,
                        "infoMessage" : "",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "172.30.0.110:21000",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 8363758,
                        "optime" : {
                                "ts" : Timestamp(1605168428, 2),
                                "t" : NumberLong(4)
                        },
                        "optimeDurable" : {
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "172.30.0.110:21000",
                        "syncSourceHost" : "172.30.0.110:21000",
                        "syncSourceId" : 2,
                        "infoMessage" : "",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "172.30.0.110:21000",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 8363758,
                        "optime" : {
                                "ts" : Timestamp(1605168428, 2),
                                "t" : NumberLong(4)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1605168428, 2),
                                "t" : NumberLong(4)
                        },
                        "optimeDate" : ISODate("2020-11-12T08:07:08Z"),
                        "optimeDurableDate" : ISODate("2020-11-12T08:07:08Z"),
                        "lastHeartbeat" : ISODate("2020-11-12T08:07:10.506Z"),
                        "lastHeartbeatRecv" : ISODate("2020-11-12T08:07:11.097Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1596804656, 1),
                        "electionDate" : ISODate("2020-08-07T12:50:56Z"),
                        "configVersion" : 1
                }
        ],
        "ok" : 1
}
configs:SECONDARY> 

1.编写监控脚本

yum install -y python-pip
pip install pymongo

# vim /usr/local/zabbix_agents/scripts/mongodb-monitor.py 

#coding:utf-8

import pymongo
from pymongo import MongoClient

class check_mongo_repl():
    def __init__(self):
        self.conn = pymongo.MongoClient('mongodb://zabbix:zabbix@127.0.0.1:21000/')
        self.db = self.conn.admin
        self.rs = self.db.command('replSetGetStatus')

    def get_rs_num(self):
        list_values = "Value : %s" %  self.rs.values()
        # 统计 primary和 secondary在字符串中出现的次数,统计主节点和从节点个数之和
        pri_count = list_values.count("PRIMARY")
        #print "pri_count= %s" % pri_count
        sec_count = list_values.count("SECONDARY")
        #print "sec_count= %s" % sec_count
        #get_list = list_values.split(',')
        #a = 0
        #if " u'stateStr': u'SECONDARY'" in get_list:
        #    a += 1
        #if " u'stateStr': u'ARBITER'" in get_list:
        #    a += 1
        return pri_count + sec_count


if __name__ == "__main__":
    mongo = check_mongo_repl()
    print mongo.get_rs_num()

2.在 zabbix_agentd.conf 配置文件中加入监控项

# vim /usr/local/zabbix_agents/conf/zabbix_agentd.conf

UserParameter=mongodb.repl.status,/usr/bin/python /usr/local/zabbix_agents/scripts/mongodb-monitor.py

触发器:
{Template mongodb replication:mongodb.repl.status.count(#5,3,"eq")}=0

 

 

 

实例:据说可以监控mongodb 的分片集群,看了一下能监控复制状态,里面的增删改查的情况

1.下载监控配置模板

从github拉取监控配置文件与脚本

git clone https://github.com/petrushinvs/mongodb-zabbix-templates-4.0.git
cd mongodb-zabbix-templates-4.0
chmod +x *.py
# 拷贝文件到zabbix 服务端调用目录(也可以在Data collector 这个item中查看报错的具体位置进行配置)
cp *.py /usr/local/zabbix/share/zabbix/externalscripts/

也可以从这里下载:https://gitee.com/reblue520/mongodb-zabbix-templates-4.0

导入模板 zbx_MongoDB_template.xml


2.准备环境

使用的是  External check 方式,需要在zabbix_server端安装mongodb命令

zabbix服务端安装
yum install -y python-pip
sudo pip install pymongo
sudo pip install py-zabbix

# 服务端进行测试

# /usr/local/zabbix/share/zabbix/externalscripts/mongod-db.py -u zabbix -s zabbix -h 172.30.0.109 -p 21000 -n eus-image-design-mongo01
History opcounters
1605237495 0 0 0 3651676 0 11703367
{"failed": 2, "chunk": 1, "total": 17, "processed": 15, "time": "0.000179"}

# Zabbix trapper 的测试,在客户端进行验证

# /usr/local/zabbix_agents/bin/zabbix_sender -s 客户端IP -z zabbix服务端IP -k 'mongodb_rs_status' -o test -vv

# /usr/local/zabbix_agents/bin/zabbix_sender -s 172.30.0.109 -z 172.30.0.9 -k 'mongodb_rs_status' -o test -vv

zabbix_sender [4675]: DEBUG: answer [{"response":"success","info":"processed: 0; failed: 1; total: 1; seconds spent: 0.000026"}]
info from server: "processed: 0; failed: 1; total: 1; seconds spent: 0.000026"
sent: 1; skipped: 0; total: 1

 

3.填写mongodb的宏配置

{$MONGODB_HOSTNAME} :mongod服务所在的域名或IP

{$MONGODB_USER} : mongodb的用户名

{$MONGODB_PASS} :mongodb的密码

{$MONGOS_PORT}:mongod服务监听的端口

{$MONGODB_ZABBIX_NAME}:客户端主机名
  

报错的处理

# /usr/local/zabbix/share/zabbix/externalscripts/mongod-db.py -u zabbix -s zabbix -h 172.30.0.109 -p 21000 -n eus-image-design-mongo01
Traceback (most recent call last):
  File "/usr/local/zabbix/share/zabbix/externalscripts/mongod-db.py", line 35, in <module>
    r = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

# 生成软链(需要安装mongdb到zabbix服务端,否则没有 mongostat命令)
ln -s /usr/local/mongodb/bin/mongostat /usr/bin/mongostat

posted @ 2020-11-12 18:03  reblue520  阅读(345)  评论(0编辑  收藏  举报