zabbix3.2监控mongodb

模板地址:https://share.zabbix.com/databases/mongodb/mongodb-for-zabbix-3-2

1.安装jq依赖
# yum install jq -y

2.添加监控用户
# 使用admin登录mongo
[root@eus_pe_web02:/usr/local/zabbix_agents_3.2.0/scripts]# mongo
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017
MongoDB 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 Noe <netkiller@msn.com> script
#
# 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/profile
DB_HOST=127.0.0.1
DB_PORT=27017
DB_USERNAME=zabbix
DB_PASSWORD=zabbix
MONGO=`which mongo`
JQ=`which jq`
EXIT_ERROR=1
EXIT_OK=0

if [ ! -x "$MONGO" ] ; then
  echo "mongo not found"
  exit $EXIT_ERROR
elif [ ! -x "$JQ" ] ; then
  echo "jq not found"
  exit $EXIT_ERROR
elif [ $# -eq 0 ] ; then
  echo "No values pass"
  exit $EXIT_ERROR
fi
index=.$(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_ERROR
fi
value=$(echo $output | jq $index)
jq_status=$?
echo $value


4.添加配置文件
# vim /usr/local/zabbix_agents_3.2.0/conf/zabbix_agentd/userparameter_mongodb.conf
# Return MongoDB statistics
UserParameter=mongodb.status[*],/usr/local/zabbix_agents_3.2.0/scripts/mongodb.sh  $1 $2 $3 $4 $5

5.导入模板

测试

[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

 

posted @ 2018-10-27 09:59  reblue520  阅读(641)  评论(0编辑  收藏  举报