mongodb配置zabbix监控

mongodb配置zabbix监控

说明

zabbix 自带监控常用数据库的末模版,可以直接使用mongodb的监控模版,按照官方指导配置宏即可连接mongoDB的数据库,并监控相关数据。

一、添加mongodb模版

二、配置宏

配置宏
{$MONGODB.CONNSTRING}  tcp://192.168.xx.xx:34970
{$MONGODB.USER} admin
{$MONGODB.PASSWORD} xxxxxxx

三测试连接:

Test availability: 
zabbix_get -s mongos.node -k 'mongodb.ping["{$MONGODB.CONNSTRING}","{$MONGODB.USER}","{$MONGODB.PASSWORD}"]"
zabbix_get -s mongos.node -k 'mongodb.ping["tcp://192.168.xx.xx:27017","root","xxxx"]"'
zabbix_get -s mongodb.node -k 'mongodb.ping["tcp://192.168.xx.xx:27017","root","xxxxx"]"'

注意事项

mongoDB 5.0之后的版本不兼容,需要单独安装以下驱动
zabbix-agent2-plugin-mongodb-6.4.14-release1.el7.x86_64.rpm

通过脚本方式添加监控项

1.修改运行zabbix agent脚本的用户

vi /usr/lib/systemd/system/zabbix-agent2.service

systemctl daemon-reload
systemctl restart zabbix-agent2.service

 配置mongodb环境变量
vi /home/mongodb/.bash_profile
export PATH=/data/iuap/middleware/mongodb-34970/bin/:$PATH

2.添加如下用户自定义脚本

vim /etc/zabbix/zabbix_agent2.d/monitor_mongodb.conf

UserParameter=mongo.alter,sh /etc/zabbix/scripts/jkalert.sh
UserParameter=mongo.connect,sh /etc/zabbix/scripts/jkcur_connect.sh  |grep cur_connect
UserParameter=mongo.delay,sh /etc/zabbix/scripts/jkdelay.sh
UserParameter=mongo.alive,sh /etc/zabbix/scripts/jkmongo_alive.sh
UserParameter=mongo.oplog,sh /etc/zabbix/scripts/jkoplog.sh
UserParameter=mongo.replic,sh /etc/zabbix/scripts/jkreplic.sh
MongoBD alter INFO    监控基本告警,错误等
MongoBD connect INFO  监控连接数
MongoBD delay INFO    监控主从延迟
MongoBD alive INFO    监控mongodb是否down掉
MongoBD oplog INFO    监控oplog剩余大小
MongoBD replic INFO   监控复制状态

注意权限问题:

  1. 给mongo.log 文件a+r权限 chmod a+r momgd.log
  2. 将脚本的所有者改成zabbix
    3.测试zabbix用户执行:
    sudo -u zabbix sh jkalert.sh

3.添加如下用户自定义脚本

Zabbix中添加通用模版
mongo模版

Zabbix中添加监控项
mongo监控项

附脚本:

db_monitor.conf

#!/bin/sh
#source ~/.bash_profile
#source /home/mongodb/.bash_profile
export PATH=/data/iuap/middleware/mongodb-34970/bin/:$PATH
export PATH

pass='xxxxxxxxxx'
user=admin
ip='192.xx.xx.xx'
port=34970

mongocmd="mongo admin -u${user} --port=${port} -p${pass}"   ## Access control is enabled

logpath=/data/iuap/middleware/mongodb-34970/log
logfile=mongod.log
ANOTHER_NODE_IP=192.168.xx.xx,192.168.xx.xx

REP_DELAY=Y
DELAY_ALTER=1000

CUR_CONNECT_ALERT=500

jkalert.sh

#/bin/bash
#ERROR_LOG(ALERT=[ERROR])
ALERT_LOG_ALERT="ERROR|Asser|fail|Error|error"
#source ~/.bash_profile 
#source /home/mongodb/.bash_profile
curdir=`dirname $0`
. ${curdir}/db_monitor.conf
cd ${curdir}

cat ${logpath}/${logfile} |grep -E $ALERT_LOG_ALERT|grep -v 'SSL' |grep -v lastErrorObject|grep -v getLastErrorDefaults|grep -v SocketException|grep -v LogicalSessionCacheRefresh > ${curdir}/alert.log 
#cat ${curdir}/alert.log |grep ERROR > ${curdir}/temp.log
#cat ${curdir}/alert.log |grep -E $ALERT_LOG_ALERT > ${curdir}/temp.log

if  [  -s ${curdir}/alert.log ]
     then
	errinf=`diff -a ${curdir}/alert.log ${curdir}/temp.log |grep ^[\<]|sed s/\<//g`
#     else
#	exit 
fi
echo $errinf
#exit
cat ${curdir}/alert.log > ${curdir}/temp.log

jkcur_connect.sh

#/bin/bash
#source ~/.bash_profile
#source /home/mongodb/.bash_profile

curdir=`dirname $0`
. ${curdir}/db_monitor.conf
cd ${curdir}

echo |${mongocmd}
echo "print('cur_connections#',db.serverStatus().connections.current)"|${mongocmd}  > ${curdir}/cur_connections.log

CUR_CONNECT=`cat ${curdir}/cur_connections.log|grep cur_connections|awk '{print $2}'`
echo `date` >> $curdir/tmpfile/connect_number.log
echo $CUR_CONNECT >> $curdir/tmpfile/connect_number.log
#exit

echo $CUR_CONNECT
if [ `awk -v a=${CUR_CONNECT} -v b=${CUR_CONNECT_ALERT} 'BEGIN{print(a>b)?a:b}'` = ${CUR_CONNECT} ];then
alert_info="cur_connect:${CUR_CONNECT}"
echo $alert_info 
fi

jkdelay.sh

#source ~/.bash_profile
#source /home/mongodb/.bash_profile

curdir=`dirname $0`
. ${curdir}/db_monitor.conf
cd ${curdir}

echo "rs.printSlaveReplicationInfo()"|${mongocmd} |grep 'behind the primary'> ${curdir}/cur_delay.log
while read hang
do
cur_delay=`echo ${hang} |awk '{print $1}'`
echo `date` >> $curdir/tmpfile/cur_delay.log
echo $cur_delay >> $curdir/tmpfile/cur_delay.log

if [[ `awk -v a=${cur_delay} -v b=${DELAY_ALTER} 'BEGIN{print(a>b)?a:b}'` = ${cur_delay} ]];then
alert_info="Current delay is:${cur_delay} second"
echo $alert_info 
fi
done<${curdir}/cur_delay.log

jkmongo_alive.sh

#source ~/.bash_profile 
#source /home/mongodb/.bash_profile

curdir=`dirname $0`
. ${curdir}/db_monitor.conf
cd ${curdir}

alert_info=`echo "rs.status()" |${mongocmd}`
if [ $? -eq 1 ] ;then
        alert_info='The mongoDB instance is down!'
fi

jkoplog.sh

#/bin/bash
#SLAVE_STATUS(ALERT=Yes)
#source ~/.bash_profile 
#source /home/mongodb/.bash_profile

curdir=`dirname $0`
. ${curdir}/db_monitor.conf
cd ${curdir}




oplog_length=`echo "rs.printReplicationInfo()" |${mongocmd}  |grep  "log length" |awk -F: '{print $2}'|awk -Fs '{print $1}'`
if [ $oplog_length -le 7200 ];
   then
        alert_info="The oplog length is ${oplog_length}S"
echo ${alert_info}
fi

jkreplic.sh


#/bin/bash
#SLAVE_STATUS(ALERT=Yes)
#source ~/.bash_profile 
#source /home/mongodb/.bash_profile

curdir=`dirname $0`
. ${curdir}/db_monitor.conf
cd ${curdir}

alert_info=`echo "rs.status()" |${mongocmd}  |grep  health |grep -v '"health" : 1'`
if [ -n  "$alert_info" ];
   then
        alert_info='the repset has a node is not health,Please Check'
echo $alert_info
fi

posted @ 2024-12-25 15:03  数据库小白(专注)  阅读(14)  评论(0编辑  收藏  举报