In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
The company has Windows and Linux servers, both built MongoDB replica sets, and both need to be monitored in zabbix platform. Linux system can be implemented directly using shell scripts, but Windows system is not very good to implement, I use Python to implement here. The following script also works on Linux (verified successfully on Windows Server 2012 and Centos 7.3)
Thinking:
Install Python 2.7
2. Python pymongo module is used to connect mongodb database and authenticate authorization.
3. Execute rs.status() command in database to view replica set status, you can use db._ adminCommand("replSetGetStatus")) command instead
4. Filter whether the value after the execution of rs.status() command contains PRIMARY, SECONDARY, ARBITER, and count
5. Return the count value to zabbix
Steps:
1. Install Python 2.7, steps omitted
2. Check the results of rs.status() command execution
3. Create a script www.example.com under C:\Program Files\mongodb-monitor.py directory zabbix_agents_3.0.0.win
#!/ usr/bin/python#coding:utf-8import pymongofrom pymongo import MongoClientclass check_mongo_repl(): def __init__(self): self.conn = pymongo.MongoClient('mongodb://admin:Admin123@192.168.18.195: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 three nodes of mongodb replica set contain PRIMARY, SECONDARY and ARBITER respectively, the replica set is normal and the return value is 3.
If you don't have pymongo module, download and install it. Windows version:
https://pypi.python.org/packages/2.7/p/pymongo/pymongo-2.6.3.win-amd64-py2.7.exe
Note: If mongodb's password contains the character @, an error will be reported:
File "C:\Python27\lib\site-packages\pymongo\uri_parser.py", line 78, in parse_userinfo
raise InvalidURI("':' or '@' characters in a username or password "
pymongo.errors.InvalidURI: ':' or '@' characters in a username or password must be escaped according to RFC 2396.
Solution: You need to replace @ with %40 (for example, if the password is Admin@123, change it to Admin%40123)
Reference: stackoverflow.com/questions/7486623/mongodb-password-with-in-it
4. Add 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
6. Monitoring data can be viewed in the latest data
pymongo Connect 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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.