分散式監控系統Zabbix3.4-針對MongoDB效能監控操作筆記

散盡浮華發表於2018-09-21

 

公司在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主機監控時,關聯到這個匯入的模板。監控效果圖如下:

========================另外一種方法==========================
除了上面方法之外,還可以使用下面的監控指令碼(其他的操作和上面一致):

[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 <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
# 
##################################################
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 <netkiller@msn.com>
# 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

相關文章