1+

1. 下载监控配置模板

1.1 从github拉取监控配置文件与脚本

这片文章cosmo给大家讲的主要是对mongod服务的监控。这里我会配置监控我的shard2分片的primary节点。

注意哦:这里最好直接在zabbix-server主机上直接用git拉取项目。由于git的原因,如果在windows机上git拉取下来后再用ftp工具上传python脚本。zabbix在运行脚本时可能会报错 bad interpreter /use/bin/python^M这是因为git在拉取文件的时候会根据当前系统将LF转换位CR-LF。

git clone https://github.com/petrushinvs/mongodb-zabbix-templates-4.0.git

1.2 安装pip

因为这个监控使用了python脚本,目前服务器大多已经安装了python运行环境,如果没有请先安装python,这里的python版本为Python 2.7.5。有了python环境后,我们还需安装pip工具来安装监控脚本中使用的库。

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

如下所示即成功安装了pip

[root@vm3 ~]# curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1734k  100 1734k    0     0  14786      0  0:02:00  0:02:00 --:--:-- 54931
[root@vm3 ~]# python get-pip.py
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting pip
  Downloading https://files.pythonhosted.org/packages/00/b6/9cfa56b4081ad13874b0c6f96af8ce16cfbc1cb06bedf8e9164ce5551ec1/pip-19.3.1-py2.py3-none-any.whl (1.4MB)
     |████████████████████████████████| 1.4MB 19kB/s 
Collecting wheel
  Downloading https://files.pythonhosted.org/packages/00/83/b4a77d044e78ad1a45610eb88f745be2fd2c6d658f9798a15e384b7d57c9/wheel-0.33.6-py2.py3-none-any.whl
Installing collected packages: pip, wheel
Successfully installed pip-19.3.1 wheel-0.33.6
[root@vm3 ~]# 

1.3 安装pymongopy-zabbix

sudo pip install pymongo
sudo pip install py-zabbix

安装成功则如下所示

[root@vm3 ~]# python get-pip.py
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting pip
  Downloading https://files.pythonhosted.org/packages/00/b6/9cfa56b4081ad13874b0c6f96af8ce16cfbc1cb06bedf8e9164ce5551ec1/pip-19.3.1-py2.py3-none-any.whl (1.4MB)
     |████████████████████████████████| 1.4MB 19kB/s 
Collecting wheel
  Downloading https://files.pythonhosted.org/packages/00/83/b4a77d044e78ad1a45610eb88f745be2fd2c6d658f9798a15e384b7d57c9/wheel-0.33.6-py2.py3-none-any.whl
Installing collected packages: pip, wheel
Successfully installed pip-19.3.1 wheel-0.33.6
[root@vm3 ~]# sudo pip install pymongo
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting pymongo
  Downloading https://files.pythonhosted.org/packages/02/c5/4dce8a3f3f08428093d79313d7fbe10a1a925252f0affe6a0369f08c1dec/pymongo-3.10.0-cp27-cp27mu-manylinux1_x86_64.whl (444kB)
     |████████████████████████████████| 450kB 16kB/s 
Installing collected packages: pymongo
Successfully installed pymongo-3.10.0
[root@vm3 ~]# sudo pip install py-zabbix

DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting py-zabbix
  Downloading https://files.pythonhosted.org/packages/c4/4b/18b8876b43e5aa747ae9dcb57a2f8364e9dfe607f4046936d31f7af0351f/py-zabbix-1.1.5.tar.gz
Building wheels for collected packages: py-zabbix
  Building wheel for py-zabbix (setup.py) ... done
  Created wheel for py-zabbix: filename=py_zabbix-1.1.5-cp27-none-any.whl size=11454 sha256=1f63a27f6c362710fac57a22e7aa0c56ad339e6e32416a5a337933636b95ab0a
  Stored in directory: /root/.cache/pip/wheels/f5/25/b1/9a106dc20baf697ccf188d928adb501e4816ad0bc22cbeab0e
Successfully built py-zabbix
Installing collected packages: py-zabbix
Successfully installed py-zabbix-1.1.5
[root@vm3 ~]# 

1.4 将下载下来的python脚本放到zabbix的外部脚本目录下

我的目录在/usr/lib/zabbix/externalscripts/

[root@vm3 ~]# cp ~/mongodb-zabbix-templates-4.0/*.py /usr/lib/zabbix/externalscripts/
[root@vm3 ~]# cd /usr/lib/zabbix/externalscripts/
[root@vm3 externalscripts]# chmod +x *.py

1.5 导入zbx_MongoDB_template.xml模板

点击导入按钮

选择导入的模板zbx_MongoDB_template.xml

导入

1.6 在zabbix管理页面配置host

如果缩略图中的参数看不清,可右键打开新标签来看。

a. 打开host创建页面
创建mongod-host1
b. 配置host名称,分组
配置host
c. 链接模板
配置template
d. 填写mongodb的配置

参数 含义
{$MONGODB_HOSTNAME} mongod服务所在的域名或IP
{$MONGODB_USER} mongodb的用户名
{$MONGODB_PASS} mongodb的密码
{$MONGOS_PORT} mongod服务监听的端口
{$MONGODB_ZABBIX_NAME} 当前配置的zabbix的hostname,就是上面b图中填写的HostName

配置Macros

原项目必须填写mongodb的用户名和密码。因为cosmo这里是测试环境,因此对mongod-db.py做了小小的改动。可以不用填写mongodb的用户名和密码。感兴趣的可以在这里获取我改动后的mongod-db.py

1.7 验证配置是否成功

配置完成后,过几分钟就可以看到最新数据了。

数据采集成功

可能发生的错误

Traceback (most recent call last):
File "/usr/lib/zabbix/externalscripts/mongod-db.py", line 35, in 
r = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
File "/usr/lib64/python2.7/subprocess.py", line 711, in init
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

/usr/bin下面创建mongostat的软链,例如

ln -s /usr/local/mongodb/bin/mongostat /usr/bin/mongostat