The company has Windows and Linux servers, both of which have a MongoDB replica set and are monitored in the Zabbix platform. Linux systems can be implemented directly using shell scripts, but the Windows system is not very well implemented, and I use Python here. The following script also applies to Linux systems (both Windows Server 2012 and Centos7.3 systems are validated successfully)
Ideas:
1, installation Python2.7
2, using Python's Pymongo module to connect MongoDB database, and authentication authorization
3, the database executes the rs.status () command to view the status of the replica set, you can use the Db._admincommand ("replsetgetstatus") command instead
4, filter rs.status () command after the execution of the value contains primary, secondary, arbiter, and Count
5. Return the count value to Zabbix
Steps:
1, installation Python2.7, step omitted
2. View the results of the rs.status () command after execution
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/96/3D/wKiom1kehg3QuPGjAADArTyyO2c822.png "title=" Qq20170519132600.png "alt=" Wkiom1kehg3qupgjaadartyyo2c822.png "/>
3. Create a new script under the C:\Program Files\zabbix_agents_3.0.0.win directory mongodb-monitor.py
#!/usr/bin/python#coding:utf-8import pymongofrom pymongo import mongoclientclass Check_mongo_repl (): def __init__ (self): self.conn = pymongo. Mongoclient (' mongodb://admin:[email protected]:27018/') self.db = self.conn.admin self.rs = Self.db.command (' Replsetgetstatus ') def get_rs_num (self): list = "value : %s" % self.rs.values () get_list = list.split (', ') a = 0 if " u ' statestr ': u ' PRIMARY ' " in get_list: a = 1 if " u ' stateStr ': u ' SECONDARY ' " in get_list: a += 1 if " u ' statestr ': u ' Arbiter '" in get_list: a += 1 return aif __name__ == "__main__": mongo = check_mongo_repl () print mongo.get_rs_num ()
Description: If the MongoDB replica set three nodes contain PRIMARY, secondary, arbiter instructions, the replica set is normal, the return value is 3
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/96/3D/wKiom1kehiiSPARbAAAFAcedy0o498.png "title=" Qq20170519133230.png "alt=" Wkiom1kehiisparbaaafacedy0o498.png "/>
4. Add in the zabbix_agentd.win.conf configuration file
Userparameter=mongodb.repl.set,python "C:\Program files\zabbix_agents_3.0.0.win\mongodb-monitor.py" |
5. Add monitoring items and triggers in Zabbix background
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/96/3D/wKioL1kehjvAOIFmAABmxM9jTNc636.png "title=" Qq20170519133559.png "alt=" Wkiol1kehjvaoifmaabmxm9jtnc636.png "/>
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/96/3D/wKiom1kehk6gM9n3AACHAi9wxCA294.png "title=" Qq20170519133619.png "alt=" Wkiom1kehk6gm9n3aachai9wxca294.png "/>
6, the latest data can be viewed in the monitoring data
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/96/3D/wKioL1kehmCiYJIBAAA9HWyOeys770.png "title=" Qq20170519133735.png "alt=" Wkiol1kehmciyjibaaa9hwyoeys770.png "/>
Pymongo Connection MongoDB Reference
1. http://api.mongodb.com/python/current/tutorial.html
2. http://stackoverflow.com/questions/13322100/pymongo-how-to-get-status-as-a-python-dictionary-for-rep-sets
3. Http://grokbase.com/t/gg/mongodb-user/132bhfa0jq/getting-mongod-replica-status-via-pymongo
This article is from the "M April Days" blog, please be sure to keep this source http://msiyuetian.blog.51cto.com/8637744/1927508
Zabbix monitoring MongoDB replica set status using Python