模板地址:https://share.zabbix.com/databases/mongodb/mongodb-for-zabbix-3-21.安装jq依赖# yum install jq -y2.添加监控用户# 使用admin登录mongo[root@eus_pe_web02:/usr/local/zabbix_agents_3.2.0/scripts]# mongoMongoDB shell version v3.4.6connecting to: mongodb://127.0.0.1:27017MongoDB server version: 3.4.6> use admin> db.auth('admin','admin')# 在admin高权限用户下授权monitor用户,改用户专门用来给zabbix提供监控数据>db.createUser({user: "zabbix",pwd: "zabbix",roles: [{ role: "clusterMonitor", db: "admin" }]})3.修改脚本[root@eus_pe_web02:/usr/local/zabbix_agents_3.2.0/scripts]# cat /usr/local/zabbix_agents_3.2.0/scripts/mongodb.sh #!/bin/bash################################################### Description: zabbix mongodb monitor# Note: Zabbix 3.2# Date: Fri Apr 21 2017# Verion: 1.0# Requirments: mongo, jq, sudo access to mongo_conf.sh## Based on Noescript## Change log:# Mon Apr 24, 2017# - Added checks for mongo and jq# - User --eval to fectch data# - fixed json output to comform# - Use jq to parse json https://stedolan.github.io/jq/# - upated index to handle space or comma betwee values# ##################################################source /etc/profileDB_HOST=127.0.0.1DB_PORT=27017DB_USERNAME=zabbixDB_PASSWORD=zabbixMONGO=`which mongo`JQ=`which jq`EXIT_ERROR=1EXIT_OK=0if [ ! -x "$MONGO" ] ; then echo "mongo not found" exit $EXIT_ERRORelif [ ! -x "$JQ" ] ; then echo "jq not found" exit $EXIT_ERRORelif [ $# -eq 0 ] ; then echo "No values pass" exit $EXIT_ERRORfiindex=.$(echo $@ | sed 's/[ ,]/./g')MONGO_CMD="$MONGO --host ${DB_HOST:-localhost} --port ${DB_PORT:-27017} --authenticationDatabase admin --quiet"[[ "$DB_USERNAME" ]] && MONGO_CMD="${MONGO_CMD} --username ${DB_USERNAME}"[[ "$DB_PASSWORD" ]] && MONGO_CMD="${MONGO_CMD} --password ${DB_PASSWORD}"output=$( $MONGO_CMD <<< "db.runCommand( { serverStatus: 1} )" |\ sed -e 's/NumberLong(\(.*\))/\1/ s/ISODate(\(.*\))/\1/ s/ObjectId(\(.*\))/\1/ s/Timestamp(.*)/"&"/ s/\(BinData(.*\)"\(.*\)")/"\1\2)"/ s/"\([0-9]*\)"/\1/' )mongo_status=${PIPESTATUS[0]}if [ $mongo_status -ne $EXIT_OK ] ; then echo "mongo exec error" exit $EXIT_ERRORfivalue=$(echo $output | jq $index)jq_status=$?echo $value4.添加配置文件# vim /usr/local/zabbix_agents_3.2.0/conf/zabbix_agentd/userparameter_mongodb.conf# Return MongoDB statisticsUserParameter=mongodb.status[*],/usr/local/zabbix_agents_3.2.0/scripts/mongodb.sh $1 $2 $3 $4 $55.导入模板测试[root@aliyun-american-guigu-zabbix:~]# zabbix_get -s 1.1.1.1 -k 'mongodb.status[opcounters,query]'14[root@aliyun-american-guigu-zabbix:~]# zabbix_get -s 1.1.1.1 -k 'mongodb.status[mem,virtual]'933