Zabbix automatically discovers and monitors remote MongoDB instances based on host and port list
In some cases, you can only use a remote MongoDB instance and cannot log in to the server where the MongoDB instance resides. In this case, you can maintain a list of hosts and ports of a remote MongoDB instance on one server, and then use the low-level discovery feature of zabbix to automatically add corresponding monitoring based on the host and port.
MongoDB host and port discovery step mongodb_discovery.py
Host and port list file mongodb_servers.txt, where the host and port information of each instance is stored
10.4.9.112:28018:root:xxxx10.4.9.2:27017:root10.4.9.3:28018#/usr/bin/python#This script is used to discovery disk on the serverimport subprocessimport jsonargs='''awk-favored 10.4.9.112:28018:root:xxxx10.4.9.2:27017:root10.4.9.3:28018#/usr/bin/python#This script is used to discovery disk on the serverimport subprocessimport jsonargs='''awk'{print $1 ":" $2}'/ usr/local/zabbix/bin/mongodb_servers.txt'''t=subprocess.Popen (args,shell=True Stdout=subprocess.PIPE) .communicate () [0] mongodbs= [] for mongo in t.split ('\ n'): if len (mongo)! = 0: mongodbs.append ({'{# MONGO_HOST}': mongo}) print json.dumps ({'data':mongodbs}, indent=4,separators= (',',':'))
Display the results:
{"data": [{"{# MONGO_HOST}": "192.168.5.7 MONGO_HOST 30000"}, {"{# MONGO_HOST}": "192.168.5.23 MONGO_HOST 30000"}, {"{# MONGO_HOST}": "192.168.5.23 MONGO_HOST 30002"} {"{# MONGO_HOST}": "192.168.5.23virtual 30003"}]}
two。 Get MongoDB port status information script discovery_mongodb_status.sh
Here, according to the value passed by {# MONGO_HOST}, the corresponding line will be found in the mongodb_servers.txt file with a colon ":" to determine the length of the entire line, and then use different mongodb connection methods.
# This script is used to get discovered mongodb servers status#echo "db.serverStatus () .uptime" | mongo 192.168.5.23:30002/admin-uroot-pxxxx#echo "db.serverStatus () .mem.mapped" | mongo 192.168.5.23:30002/admin-uroot-pxxx#echo "db.serverStatus () .globalLock.activeClients.total" | mongo 192.168.5.23:30002/admin-uroot-pxxx# Macro {# MONGO_INFO} "HOSTNAME:PORT:USERNAME:PASSWORD" # Sh discovery_mongodb_status.sh uptime {# MONGO_HOST} # sh discovery_mongodb_status.sh mem mapped {# MONGO_HOST} # sh discovery_mongodb_status.sh globalLock activeClients total {# MONGO_HOST} # one more parametermongo_info= "" command_line= "" function check_mongo_info () {num=$ (echo $mongo_info | awk-F ":"'{print NF}') host=$ (echo $mongo_info | awk-F ":"'{print $1}' ) port=$ (echo $mongo_info | awk-F ":"'{print $2}') username=$ (echo $mongo_info | awk-F ":"'{print $3}') password=$ (echo $mongo_info | awk-F ":"'{print $4}') case $num in 2) command_line= "/ data/app_platform/mongodb/bin/mongo $host:$port/admin" ; 3) command_line= "/ data/app_platform/mongodb/bin/mongo $host:$port/admin-u$username-paired'; 4) command_line=" / data/app_platform/mongodb/bin/mongo $host:$port/admin-u$username-p$password " Esac} case $# in 2) mongo_info=$ (grep $2 / usr/local/zabbix/bin/mongodb_servers.txt) check_mongo_info output=$ (/ bin/echo "db.serverStatus (). $1" | $command_line | sed-n '3p') 3) mongo_info=$ (grep $3 / usr/local/zabbix/bin/mongodb_servers.txt) check_mongo_info output=$ (/ bin/echo "db.serverStatus (). $1.room2" | $command_line | sed-n '3p'); 4) mongo_info=$ (grep $4 / usr/local/zabbix/bin/mongodb_servers.txt) check_mongo_info output=$ (/ bin/echo "db.serverStatus (). $1.room2.room3" | $command_line | sed-n' 3p') Esac#check if the output contains "NumberLong" if [["$output" = ~ "NumberLong"]]; then echo $output | sed-n 's/NumberLong (/ / p' | sed-n's /) / / p'else echo $outputfi
Display the results:
$sh discovery_mongodb_status.sh uptime 192.168.5.23 sh discovery_mongodb_status.sh mem mapped 3000223823151 $192.168.5.23 sh discovery_mongodb_status.sh globalLock activeClients total 3000246691 $192.168.5.23 sh discovery_mongodb_status.sh globalLock activeClients total 300020
3. Add zabbix profile discovery_mongodb_status.conf that is automatically discovered by MongoDB
# echo "db.serverStatus () .mem.mapped" | mongo admin-- host xxxx-- port xxx-uroot-pxxxx#echo "db.serverStatus () .mem.mapped" | mongo 10.4.9.112:28018/admin-uroot-pxxxx#UserParameter=MongoDB.Status [*], / bin/echo "db.serverStatus (). $1.Secret2" | / data/app_platform/mongodb/bin/mongo admin-- port 28018 | sed-n '3p'UserParameter=MongoDB.Discovery / usr/bin/python / usr/local/zabbix/bin/mongodb_discovery.pyUserParameter=MongoDB.Discovery_Status [*], / usr/local/zabbix/bin/discovery_mongodb_status.sh $1 $2 $3 $4
See attachment for template
Attachment: http://down.51cto.com/data/2365000