In some cases, you can only use a remote MongoDB instance instead of logging on to the server where the MongoDB instance is located. In this case, you can maintain the host and port list of a remote MongoDB instance on one server, then use z
In some cases, you can only use a remote MongoDB instance instead of logging on to the server where the MongoDB instance is located. In this case, you can maintain the host and port list of a remote MongoDB instance on one server, then use z
In some cases, you can only use a remote MongoDB instance instead of logging on to the server where the MongoDB instance is located. In this case, you can maintain the host and port list of a remote MongoDB instance on one server ,, then, the corresponding monitoring is automatically added based on the host and port through the low-level Discovery Function of zabbix.
MongoDB host and port discovery steps mongodb_discovery.py
The host and port information of each instance is stored in the mongodb_servers.txt file.
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-F': ''{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: Invalid DBS. append ({'{# MONGO_HOST}': mongo}) print json. dumps ({'data': mongodbs}, indent = 4, separators = (',',':'))
Display result:
{"Data": [{"{# MONGO_HOST}": "192.168.5.7: 30000" },{ "{# MONGO_HOST}": "192.168.5.7: 30001 "}, {"{# cmd_host}": "192.168.5.23: 30002" },{ "{# cmd_host}": "192.168.5.23: 30003"}]}
2. script for obtaining MongoDB port status information discovery_mongodb_status.sh
Here, we will find the corresponding row in the mongodb_servers.txt file and use the colon ":" to determine the length of the entire row, 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 {# cmd_info}" HOSTNAME: PORT: USERNAME: PASSWORD "# sh discovery_mongodb_status. Sh uptime {# cmd_host} # sh cmdmem mapped {# cmd_host} # sh cmdgloballock activeClients total {# cmd_host} # one more parameter1__info = "" command_line = "" function check_cmd_info () {num = $ (echo $ cmd_info | awk-F ":" '{print NF}') host = $ (echo $ cmd_info | awk-F ": "'{print $1}') port = $ (echo $ pai_info | awk-F": "'{print $2}') username = $ (echo $ mong O_info | awk-F ":" '{print $3}') password = $ (echo $ response _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-p'' "; 4) command_line = "/data/app_platform/mongodb/bin/mongo $ host: $ port/admin-u $ username-p $ password"; esac} case $ # in 2) performance_info = $ (gr Ep $2/usr/local/zabbix/bin/mongodb_servers.txt) check_mongo_info output = $ (/bin/echo "db. serverStatus (). $1 "| $ command_line | sed-n '3'); 3) mongo_info = $ (grep $3/usr/local/zabbix/bin/mongodb_servers.txt) check_mongo_info output = $ (/bin/echo "db. serverStatus (). $1. $2 "| $ command_line | sed-n '3'); 4) mongo_info = $ (grep $4/usr/local/zabbix/bin/mongodb_servers.txt) check_cmd_info output = $ (/bin/ech O "db. serverStatus (). $1. $2. $3 "| $ command_line | sed-n '3'); 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