Zabbix监控MongoDB性能状态

小柒博客 Zabbix26150.1K5字数 6852阅读22分50秒阅读模式

Zabbix监控MongoDB性能的原理:通过echo "db.serverStatus()" | mongo admin 来查看mongodb的状态。

Zabbix监控MongoDB性能,主要监控以下项目:

- 内存使用情况

- 连接数

- 锁

- 刷新写操作到磁盘

- 每秒执行的查询,插入,删除,更新等操作次数

- 每秒访问的索引次数,每秒命中索引的次数

- MongoDB产生的总的页面错误数量

- MongoDB的网络流量状况

监控操作步骤:

1、编写脚本

2、上传脚本到指定位置

3、修改zabbix_agent配置文件

4、zabbix_server的WEB界面导入模版

5、主机关联

1)编辑zabbix_agent配置文件,添加以下内容

[root@localhost ~]# vim /usr/local/zabbix/etc/zabbix_agentd.conf

UserParameter=Mongodb.state[*],/data/scripts/auto_detection_mongodb_state.sh $1
UserParameter=mongodb.status[*],/data/scripts/auto_detection_mongodb_status.sh $1
#保存退出

2)重启zabbix_agentd服务

[root@localhost ~]# service zabbix_agentd restart

3)下载脚本文件(获取监控值)

[root@localhost ~]# cd /data/scripts

[root@localhost scripts]# wget https://mirrors.yangxingzhen.com/shell/auto_detection_mongodb_status.sh

[root@localhost scripts]# wget https://mirrors.yangxingzhen.com/shell/auto_detection_mongodb_state.sh

[root@localhost scripts]# cat auto_detection_mongodb_status.sh

#!/bin/bash
# Description: zabbix ${DB_BIN}/mongodb monitor
# Note: Zabbix 4.0.7
# Date: 2019-7-31 21:20:39
# Verion: 1.0

DB_HOST=127.0.0.1
DB_PORT=27017
DB_BIN="/usr/local/mongodb/bin"

if [[ $# == 1 ]];then
case $1 in
	insert)
	echo "db.serverStatus().opcountersRepl" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "insert"|awk '{print $3}'|sed 's/,//'
	;;
	query)
	echo "db.serverStatus().opcountersRepl" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "query"|awk '{print $3}'|sed 's/,//'
	;;
	delete)
	echo "db.serverStatus().opcountersRepl" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "delete"|awk '{print $3}'|sed 's/,//'
	;;
	getmore)
	echo "db.serverStatus().opcountersRepl" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "getmore"|awk '{print $3}'|sed 's/,//'
	;;
	command)
	echo "db.serverStatus().opcountersRepl" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "command"|awk '{print $3}'|sed 's/,//'
	;;
	update)
	echo "db.serverStatus().opcountersRepl" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "update"|awk '{print $3}'|sed 's/,//'
	;;
	current)
	echo "db.serverStatus().connections"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep -w "current"|awk '{print $4}'|sed 's/,//'
	;;
	available)
	echo "db.serverStatus().connections"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep -w "available"|awk '{print $7}'|sed 's/,//'
	;;
	totalCreated)
	echo "db.serverStatus().connections"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep -w "totalCreated"|awk '{print $10}'|sed 's/,//'
	;;
	total)
	echo "db.serverStatus().globalLock.activeClients"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "total"|awk '{print $4}'|sed 's/,//'
	;;
	readers)
	echo "db.serverStatus().globalLock.activeClients"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "readers"|awk '{print $7}'|sed 's/,//'
	;;
	writers)
	echo "db.serverStatus().globalLock.activeClients"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "writers"|awk '{print $10}'|sed 's/,//'
	;;
	deleted)
	echo "db.serverStatus().metrics.document"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep -w "deleted"|awk -F '(' '{print $2}'|sed 's/),//'
	;;
	inserted)
	echo "db.serverStatus().metrics.document"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep -w "inserted"|awk -F '(' '{print $2}'|sed 's/),//'
	;;
	returned)
	echo "db.serverStatus().metrics.document"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep -w "returned"|awk -F '(' '{print $2}'|sed 's/),//'
	;;
	updated)
	echo "db.serverStatus().metrics.document"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep -w "updated"|awk -F '(' '{print $2}'|sed 's/)//'
	;;
	totalTime)
	echo "db.serverStatus().globalLock"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "totalTime" |awk '{print $3}'|awk -F '"' '{print $2}'
	;;
	bytesIn)
	echo "db.serverStatus().network"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "bytesIn"|awk -F'(' '{print $2}'|sed 's/),//'
	;;
	bytesOut)
	echo "db.serverStatus().network"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "bytesOut"|awk -F'(' '{print $2}'|sed 's/),//'
	;;
	numRequests)
	echo "db.serverStatus().network"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "numRequests"|awk -F'(' '{print $2}'|sed 's/)//'
	;;
	mapped)
	echo "db.serverStatus().mem" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep -w "mapped"|awk '{print $3}'|sed 's/,//'
	;;
	resident)
	echo "db.serverStatus().mem" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep -w "mappedWithJournal"|awk '{print $3}'|sed 's/,//'
	;;
	virtual)
	echo "db.serverStatus().mem" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep -w "virtual"|awk '{print $3}'|sed 's/,//'
	;;
	mappedWithJournal)
	echo "db.serverStatus().mem" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep -w "resident"|awk '{print $3}'|sed 's/,//'
	;;
	*)
	echo -e "\033[33mUsage: $0 {insert|query|delete|getmore|command|update|current|available|totalCreated|total|readers|writers|deleted|inserted|returned|updated|totalTime|bytesIn|bytesOut|numRequests||mapped|resident}\033[0m"
	exit 1
esac
else
	echo -e "\033[33mUsage: $0 {insert|query|delete|getmore|command|update|current|available|totalCreated|total|readers|writers|deleted|inserted|returned|updated|totalTime|bytesIn|bytesOut|numRequests||mapped|resident}\033[0m"
	exit 1
fi

[root@localhost scripts]# cat auto_detection_mongodb_state.sh

#!/bin/bash
# Description: zabbix ${DB_BIN}/mongodb monitor
# Note: Zabbix 4.0.7
# Date: 2019-7-31 21:01:22
# Verion: 1.0

DB_HOST="127.0.0.1"
DB_PORT="27017"
DB_BIN="/usr/local/mongodb/bin"

if [[ $# == 1 ]];then
case $1 in
	insert)
	echo "db.serverStatus().opcounters" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "insert"|awk '{print $3}'|sed 's/,//'
	;;
	query)
	echo "db.serverStatus().opcounters" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "query"|awk '{print $3}'|sed 's/,//'
	;;
	delete)
	echo "db.serverStatus().opcounters" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "delete"|awk '{print $3}'|sed 's/,//'
	;;
	getmore)
	echo "db.serverStatus().opcounters" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "getmore"|awk '{print $3}'|sed 's/,//'
	;;
	command)
	echo "db.serverStatus().opcounters" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "command"|awk '{print $3}'|sed 's/,//'
	;;
	update)
	echo "db.serverStatus().opcounters" |${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "update"|awk '{print $3}'|sed 's/,//'
	;;
	total)
	echo "db.serverStatus().globalLock.currentQueue"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "total"|awk '{print $4}'|sed 's/,//'
	;;
	readers)
	echo "db.serverStatus().globalLock.currentQueue"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "readers"|awk '{print $7}'|sed 's/,//'
	;;
	writers)
	echo "db.serverStatus().globalLock.currentQueue"|${DB_BIN}/mongo ${DB_HOST}:${DB_PORT}/admin |grep "writers"|awk '{print $10}'|sed 's/,//'
	;;
	*)
	echo -e  "\033[33mUsage: $0 {insert|query|delete|getmore|command|update|total|readers|writers}\033[0m"
	exit 1
esac
else
	echo -e  "\033[33mUsage: $0 {insert|query|delete|getmore|command|update|total|readers|writers}\033[0m"
	exit 1
fi
4)脚本授权

[root@localhost scripts]# chmod +x auto_detection_mongodb_status.sh

[root@localhost scripts]# chmod +x auto_detection_mongodb_state.sh

5)验证server端验证是否能获取agent端的key值

#使用zabbix_get命令获取key值

Zabbix监控MongoDB性能状态

Zabbix监控平台配置

1、下载监控模板文件

模板附件下载:

发表评论并刷新页面,方可查看
发表评论并刷新,方可查看
2、导入Mongodb监控模板

进入后台->配置->模板->导入(右上角)->选择下载的模板文件->最后点击导入 。

Zabbix监控MongoDB性能状态

至此Mongodb监控模板已经导入到Zabbix中。

Zabbix监控MongoDB性能状态

3、关联模板

需要把Mongodb监控模板链接到你的监控主机上,进入后台->配置->点击你的主机->模板->选择刚才导入模板,点击添加,最后点击更新即可。

Zabbix监控MongoDB性能状态

4、效果图展示

Zabbix监控MongoDB性能状态

Zabbix监控MongoDB性能状态

Zabbix监控MongoDB性能状态

继续阅读
历史上的今天
7月
31
若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如果喜欢本站,请打赏支持本站,谢谢!
  • 微信号
  • 微信扫一扫加我!
  • weinxin
  • 微信公众号
  • 微信扫一扫关注我!
  • weinxin
Zabbix最后更新:2022-11-23
小柒博客
  • 本文由 发表于 2019-07-31 21:18:02
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
MongoDB常用命令 MongoDB

MongoDB常用命令

# 查看Mongodb版本信息 mongos> db.version() # 关闭mongodb服务 mongos> use admin mongos> shutdown...
Shell脚本获取MongoDB数据量大小 Shell

Shell脚本获取MongoDB数据量大小

此脚本是获取MongoDB数据量大小脚本,有需要朋友可以参考,脚本内容如下: 环境准备: 操作系统:CentOS Linux release 7.8.2003 软件版本: Mong...
Docker 安装 MongoDB Docker

Docker 安装 MongoDB

MongoDB 简介 MongoDB是一个基于分布式文件存储 [1] 的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB是一个介于关系数据库和非关系数...
MongoDB 监控 MongoDB

MongoDB 监控

MongoDB 监控 MongoDB自带了mongostat和mongotop这两个命令来监控MongoDB的运行情况。这两个命令对于我们处理MongoDB数据库变慢等等问题非常有用,能详细的统...
评论  26  访客  22  作者  4
    • sdf
      sdf 1

      导出vdv

      • sdf
        sdf 1

        很好

        • Assange
          Assange 4

          :wink: :wink: 再次评论~~

          • Assange
            Assange 4

            我的赞去哪里了~~~

            • jack
              jack 1

              :wink: :wink:

              • jack
                jack 1

                赞一下

                • wind
                  wind 0

                  再赞一下!!!!!!!!!!!!!!!!!!

                  • 666
                    666 1

                    谢谢

                    • 666
                      666 1

                      nice,谢谢

                      • 依憐旳丶
                        依憐旳丶 2

                        写的太好了

                        • 1
                          1 0

                          谢谢分享

                          • nelson
                            nelson 0

                            谢谢分享

                            • xxd
                              xxd 0

                              看看模板

                              • 追梦
                                追梦 0

                                看看模板

                                • 嗨咯啊哈哼
                                  嗨咯啊哈哼 0

                                  看看模板

                                  • 新手入门
                                    新手入门 0

                                    看看模板

                                    • 肥皂
                                      肥皂 0

                                      看看模板,谢谢

                                      • wuwuw
                                        wuwuw 0

                                        挺不错

                                        • lanlinlan
                                          lanlinlan 1

                                          模板链接都挂了

                                          • lanlinlan
                                            lanlinlan 1

                                            支持zabbix4吗 我感觉现在zabbix更新好快

                                            • 浪子情缘
                                              浪子情缘 0

                                              不错

                                            匿名

                                            发表评论

                                            匿名网友 填写信息

                                            :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: