公司在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



