Press "Enter" to skip to content

Zabbix监控mongodb

主机环境:
[root@test ~]# uname -a
Linux test 3.10.0-693.el7.x86_64 #1 SMP Thu Jul 6 19:56:57 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@test ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.4 (Maipo)
[root@test ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.139.132 netmask 255.255.255.0 broadcast 192.168.139.255
ether 00:0c:29:ca:b7:70 txqueuelen 1000 (Ethernet)
RX packets 356 bytes 41989 (41.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 224 bytes 41057 (40.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Zabbix版本:3.4.9
mongodb版本:
[root@test zabbix_agentd.d]# mongod --version
db version v3.6.5
git version: a20ecd3e3a174162052ff99913bc2ca9a839d618
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel70
distarch: x86_64
target_arch: x86_64
主机防火墙和selinux关闭
Zabbix yum安装参考:https://www.tracymc.cn/archives/614,编译安装可参考https://www.tracymc.cn/archives/218.
mongodb安装:https://www.tracymc.cn/archives/492,https://www.tracymc.cn/archives/168

具体步骤:
1.编写脚本并设置键值
监控脚本:
[root@test ~]# cat /etc/zabbix/scripts/mongodb.sh
#!/bin/bash

index=$(echo $@ | tr " " ".")
status=$(echo "db.serverStatus().${index}" |/usr/bin/mongo --quiet admin)

#check if the output contains "NumberLong"

if [[ "$status" =~ "NumberLong" ]];then
echo $status|sed -n 's/NumberLong(//p'|sed -n 's/)//p'
else
echo $status
fi
[root@test zabbix_agentd.d]# chmod u+x /etc/zabbix/scripts/mongodb.sh //设置权限
设置键值:
[root@test ~]# cat /etc/zabbix/zabbix_agentd.conf |grep conf|grep -v ‘#’ //yum安装zabbix的话agent默认配置文件为/etc/zabbix/zabbix_agentd.conf
Include=/etc/zabbix/zabbix_agentd.d/*.conf //你懂的,包含/etc/zabbix/zabbix_agentd.d下面的配置
[root@test zabbix_agentd.d]# cat /etc/zabbix/zabbix_agentd.d/userparameter_mongodb.conf
UserParameter=mongodb.status[*],sudo sh /etc/zabbix/scripts/mongodb.sh $1 $2 $3 $4 $5
[root@test zabbix_agentd.d]# systemctl restart zabbix-agent //重启agent

[root@test zabbix_agentd.d]# visudo //增加如下行
zabbix ALL=(ALL) NOPASSWD:/bin/sh

2.下载模板
模板地址:zbx_mongodb_templates

3.导入模板
先将模板存到本地,再导入到zabbix-server中.
导入下载模板,如下:

导入成功后可以在配置-模板查看已导入的模板,如下:

4.关联模板
添加模板:
配置-主机点击相关主机进去配置模板,如下:

5.zabbix web验证是否有数据
关联模板后,稍等一会,查看web页面是否有数据.
部分截图如下:

注意:
一开添加模板后出不来数据,有如下报错:
Value "995 2018-06-14T16:45:51.388+0800 E - [main] Error saving history file: FileOpenFailed: Unable to open() file .dbshell: Permission denied" of type "string" is not suitable for value type "Numeric (unsigned)"
意思是zabbix用户没有权限打开.dbshell(全路径为/root/.dbshell)这个文件,解决办法:
[root@test zabbix_agentd.d]# visudo //增加如下行
zabbix ALL=(ALL) NOPASSWD:/bin/sh

7 Comments

  1. mao mao 4月 25, 2019

    我这样子操作了还是报同样的错误.172878227 2019-04-25T19:04:26.628+0800 E – [main] Error saving history file: FileOpenFailed: Unable to open() file .dbshell: Unknown error

    • tracy tracy 4月 26, 2019

      selinux先关了。然后切换到zabbix用户看下有没有权限查看那个文件

    • tracy tracy 4月 26, 2019

      setfacl -Rm u:zabbix:rwx /root 或者试试看这句话

  2. mao mao 4月 25, 2019

    请问/root/.dbshell这个文件是怎么生成的,作为什么用的呢?我看了下里面都是一些mongdb语句。

    • tracy tracy 4月 26, 2019

      这个文件是用来记录命令历史的

  3. lugo lugo 6月 14, 2019

    找遍全网只有你说了关于.dbshell的问题,good.
    难道其他人都没有遇到么?还是他们直接就是把za和mongo装在一个机器上了?

  4. prifi prifi 9月 19, 2020

    setfacl -Rm u:zabbix:rwx /root
    -R 递归设置ACL,把整个 /root 下的文件都添加了acl,其实只需要修改 /root/.dbshell 这个文件的权限就行了。

    setfacl -m u:zabbix:rwx /root/
    setfacl -m u:zabbix:rwx /root/.dbshell
    getfacl /root/.dbshell

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注