分布式监控系统Zabbix3.4-针对MongoDB性能监控操作笔记

 

公司在IDC机房的一台服务器上部署了MongoDB,由于所存储的业务数据比较重要,所以对MongoDB的监控显得尤为重要!Zabbix监控MongoDB性能的原理:通过echo "db.serverStatus()" | mongo admin 来查看mongodb的状态。

Zabbix监控MongoDB性能,主要监控以下项目:
- 内存使用情况
- 连接数
- 锁
- 刷新写操作到磁盘
- 每秒执行的查询,插入,删除,更新等操作次数
- 每秒访问的索引次数,每秒命中索引的次数
- MongoDB产生的总的页面错误数量
- MongoDB的网络流量状况

下面简单说下操作过程:

1)MongoDB部署机上的配置(即Zabbix_agnet客户端)

mongodb部署机的ip为10.0.11.60,的端口为28000
 
[root@kevin-mongodb ~]# ps -ef|grep mongo
root     17402 15951  0 01:06 pts/0    00:00:00 grep --color=auto mongo
mongo    31758     1  0 Sep17 ?        00:25:25 mongod -f /data/mongo_28000/conf/mongo.conf
[root@kevin-mongodb ~]# lsof -i:28000
COMMAND   PID   USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
mongod  31758  mongo   11u  IPv4  815849      0t0  TCP *:nxlmd (LISTEN)
 
连接mongodb的一个权限:库名admin,用户名monitor 密码rrrDE3496eb98S98ccff
 
[root@kevin-mongodb ~]# /bin/mongo 127.0.0.1:28000/admin -umonitor -prrrDE3496eb98S98ccff
......
applog:PRIMARY> db.serverStatus();          //查看mongodb的所有状态信息
 
applog:PRIMARY> db.serverStatus().mem;
{
        "bits" : 64,
        "resident" : 112,
        "virtual" : 1480,
        "supported" : true,
        "mapped" : 0,
        "mappedWithJournal" : 0
}
applog:PRIMARY> db.serverStatus().mem.virtual;
1480
 
applog:PRIMARY> db.serverStatus().opcounters;
{
        "insert" : 13,
        "query" : 3272,
        "update" : 11,
        "delete" : 0,
        "getmore" : 94013,
        "command" : 1024869
}
applog:PRIMARY> db.serverStatus().opcounters.query;
3272
 
 
该服务器作为zabbix_agent 客户端,安装目录为/usr/local/zabbix
[root@kevin-mongodb ~]# cat /usr/local/zabbix/etc/zabbix_agentd.conf|grep -v "#"|grep -v "^$"
PidFile=/usr/local/zabbix/logs/zabbix_agentd.pid
LogFile=/usr/local/zabbix/logs/zabbix_agentd.log
Server=10.0.8.40
ListenPort=10050
ServerActive=10.0.8.40
Hostname=kevin-mongodb
Timeout=10
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/
UnsafeUserParameters=1                                           #注意这个一定要配置成1,表示启动自定义脚本功能!
 
 
MongoDB的性能监控的自定义脚本
[root@kevin-mongodb ~]# cat /usr/local/zabbix/monitor_scripts/MongoDB.sh
#!/bin/bash
case $# in
  1)
    output=$(/bin/echo "db.serverStatus().$1" |/bin/mongo 127.0.0.1:28000/admin -umonitor -prrrDE3496eb98S98ccff|sed -n '4p')
    ;;
  2)
    output=$(/bin/echo "db.serverStatus().$1.$2" |/bin/mongo 127.0.0.1:28000/admin -umonitor -prrrDE3496eb98S98ccff|sed -n '4p')
    ;;
  3)
    output=$(/bin/echo "db.serverStatus().$1.$2.$3" |/bin/mongo 127.0.0.1:28000/admin -umonitor -prrrDE3496eb98S98ccff|sed -n '4p')
    ;;
esac
 
#check if the output contains "NumberLong"
if [[ "$output" =~ "NumberLong"   ]];then
  echo $output|sed -n 's/NumberLong(//p'|sed -n 's/)//p'
else
  echo $output
fi
 
================================================================================
手动执行脚本中的命令,检查下:
[root@kevin-mongodb ~]# /bin/echo "db.serverStatus().mem" |/bin/mongo 127.0.0.1:28000/admin -umonitor -prrrDE3496eb98S98ccff
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:28000/admin
MongoDB server version: 3.6.7
{
        "bits" : 64,
        "resident" : 112,
        "virtual" : 1480,
        "supported" : true,
        "mapped" : 0,
        "mappedWithJournal" : 0
}
bye
 
[root@kevin-mongodb ~]# /bin/echo "db.serverStatus().mem.virtual" |/bin/mongo 127.0.0.1:28000/admin -umonitor -prrrDE3496eb98S98ccff
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:28000/admin
MongoDB server version: 3.6.7
1480
bye
 
[root@kevin-mongodb ~]# /bin/echo "db.serverStatus().mem.virtual" |/bin/mongo 127.0.0.1:28000/admin -umonitor -prrrDE3496eb98S98ccff|sed -n "4p"
1480
 
[root@kevin-mongodb ~]# /bin/echo "db.serverStatus().opcounters" |/bin/mongo 127.0.0.1:28000/admin -umonitor -prrrDE3496eb98S98ccff    
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:28000/admin
MongoDB server version: 3.6.7
{
        "insert" : 13,
        "query" : 3274,
        "update" : 11,
        "delete" : 0,
        "getmore" : 94163,
        "command" : 1035856
}
bye
 
[root@kevin-mongodb ~]# /bin/echo "db.serverStatus().opcounters.query" |/bin/mongo 127.0.0.1:28000/admin -umonitor -prrrDE3496eb98S98ccff
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:28000/admin
MongoDB server version: 3.6.7
3274
bye
 
[root@kevin-mongodb ~]# /bin/echo "db.serverStatus().opcounters.query" |/bin/mongo 127.0.0.1:28000/admin -umonitor -prrrDE3496eb98S98ccff|sed -n "4p"
3274
================================================================================
 
接着添加zabbix对应的Key文件(这里后面跟"$1.$2",或者后面直接跟"$1 $2 $3")
[root@kevin-mongodb ~]# vim /usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_MongoDB.conf
UserParameter=MongoDB.Status[*],/usr/local/zabbix/monitor_scripts/MongoDB.sh $1.$2
 
重启zabbix_agent
[root@kevin-mongodb ~]# pkill -9 zabbix_agent
[root@kevin-mongodb ~]# /usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf
 
需要注意的是:一定要将/root/.dbshell文件的执行权限授予zabbix,这步很关键!!
[root@kevin-mongodb ~]# setfacl -Rm u:zabbix:rwx /root
 
然后在zabbix的服务端进行测试,验证是否能采集到监控数据
[root@zabbix01 ~]# /usr/local/zabbix/bin/zabbix_get -s 10.0.11.60 -k mongodb.status[mem.virtual]
1480
[root@zabbix01 ~]# /usr/local/zabbix/bin/zabbix_get -s 10.0.11.60 -k mongodb.status[opcounters.query]
3276

2)Zabbix界面里的配置  

登录zabbix界面,将MongoDB监控模板导入到zabbix的模板中,添加到zabbix模板中的名称为"Template MongoDB"。模板下载地址:https://pan.baidu.com/s/1pnaFgmDdwm7GnURB6Gteuw    提取密码:7vt8

然后配置mongodb的zabbix主机监控时,关联到这个导入的模板。监控效果图如下:

分布式监控系统Zabbix3.4-针对MongoDB性能监控操作笔记_第1张图片

分布式监控系统Zabbix3.4-针对MongoDB性能监控操作笔记_第2张图片

分布式监控系统Zabbix3.4-针对MongoDB性能监控操作笔记_第3张图片

========================另外一种方法==========================
除了上面方法之外,还可以使用下面的监控脚本(其他的操作和上面一致):

[root@kevin-mongodb ~]# cat /usr/local/zabbix/monitor_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  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
# 
##################################################
DB_HOST=127.0.0.1
DB_PORT=28000
DB_USERNAME=monitor
DB_PASSWORD=rrrDE3496eb98S98ccff
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')
#keys=$1'.'$2
#echo $keys
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 2>/dev/null)
#echo $output | jq   $index 2>/dev/null
#value=$(echo $output |jq .$keys 2>/dev/null)
#jq_status=$?
echo $value 


手动测试:
[root@kevin-mongodb ~]# /usr/local/zabbix/monitor_scripts/mongodb.sh mem.virtual     
1480
[root@kevin-mongodb ~]# /usr/local/zabbix/monitor_scripts/mongodb.sh mem,virtual
1480
[root@kevin-mongodb ~]# /usr/local/zabbix/monitor_scripts/mongodb.sh opcounters.query
3283
[root@kevin-mongodb ~]# /usr/local/zabbix/monitor_scripts/mongodb.sh opcounters,query
3283


添加zabbix对应的Key文件
[root@kevin-mongodb ~]# cat /usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_mongodb.conf 
############################################################
# MongoDB - statistics
#
# Author: Neo Chen 
# Website: http://www.netkiller.cn
############################################################

# Discovery

# Return MongoDB statistics
#UserParameter=mongodb.status[*],/bin/bash /usr/local/zabbix/monitor_scripts/mongodb.sh $1 $2 $3 $4 $5
UserParameter=mongodb.status[*], /usr/local/zabbix/monitor_scripts/mongodb.sh  $1  $2 


在zabbix服务端验证是否能采集到监控数据的key
[root@zabbix01 ~]# /usr/local/zabbix/bin/zabbix_get -s 10.0.11.60 -k mongodb.status[mem,virtual]     
1480
[root@zabbix01 ~]# /usr/local/zabbix/bin/zabbix_get -s 10.0.11.60 -k mongodb.status[opcounters,query]
3283


登录zabbix界面,导入模板,这个模板下载地址:https://pan.baidu.com/s/1lZTchsXZmGmxTutaIMVzwg
提取密码:ky1s

分布式监控系统Zabbix3.4-针对MongoDB性能监控操作笔记_第4张图片

分布式监控系统Zabbix3.4-针对MongoDB性能监控操作笔记_第5张图片

分布式监控系统Zabbix3.4-针对MongoDB性能监控操作笔记_第6张图片

你可能感兴趣的:(分布式监控系统Zabbix3.4-针对MongoDB性能监控操作笔记)