首页  :: 新随笔  :: 管理

MongoDB监控工具之mongo_monitor

Posted on 2022-01-10 14:49  高&玉  阅读(1361)  评论(0编辑  收藏  举报

1 介绍

GitHub地址:https://github.com/hcymysql/mongo_monitor

       一款面向研发人员查看的MongoDB图形可视化监控工具,借鉴了Percona PMM Grafana以及官方自带的mongostat工具输出的监控指标项,去掉了一些不必要看不懂的监控项,目前采集了数据库连接数、QPS/TPS、内存使用率统计,副本集replset状态信息和同步复制延迟时长。

      采用远程连接方式获取数据,所以无需要在数据库服务器端部署相关agent或计划任务,可实现微信和邮件报警。

注意:

  • 监控环境为MongoDB 3.2以上版本,2.X版本未测试。
  • mongo_monitor使用MySQL数据库存储监控数据。

 

MongoDB监控状态展示:

 

点击图表,可以查看历史曲线图

连接数:

 

QPS:

2 安装使用

2.1 准备环境

安装php-mysql驱动

[root]# yum install -y php-pear php-devel php gcc openssl openssl-devel cyrus-sasl cyrus-sasl-devel httpd mysql php-mysql

 

安装php-mongo驱动

[root]# pecl install mongo

添加如下内容到最后一行

[root]# vi /etc/php.ini
extension=mongo.so

 

重启httpd服务

[root]# systemctl restart httpd

 

MongoDB端创建超级用户(用于监控采集)

       首先我们在被监控的数据库端创建授权帐号,允许采集器服务器能连接到Mongodb数据库。由于需要执行命令db.runCommand({serverStatus:1,repl:1}).repl和db.adminCommand( { replSetGetStatus: 1 } ).members,所以需要授予root角色,授权方式如下所示:

mongos> use admin
mongos> db.createUser({user:"root",pwd:"Passwd",roles:[{role:"root",db:"admin"}]})

提示:mongo的密码不能包含“@”符号。

2.2 部署mongo_monitor

2.2.1 下载安装包

[root]# cd /var/www/html/
[root]# wget https://github.com/hcymysql/mongo_monitor/archive/refs/heads/master.zip
[root]# unzip mongo_monitor-master.zip
[root]# mv mongo_monitor-master mongo_monitor

2.2.2 脚本授权

邮件脚本授权

[root]# chmod 755 /var/www/html/mongo_monitor/mail/sendEmail

微信脚本授权

[root]# chmod 755 /var/www/html/mongo_monitor/weixin/wechat.py

2.2.3 导入Mongo Monitor监控表结构

导入Mongo Monitor监控工具表结构到MySQL数据库中,生成mongo_monitor库:

[root]# cd /var/www/html/mongo_monitor/schema_sql
[root]# mysql -uroot -p < mongo_monitor_schema.sql
MySQL [(none)]> use mongo_monitor;

MySQL [mongo_monitor]> show tables;
+-------------------------+
| Tables_in_mongo_monitor |
+-------------------------+
| mongo_repl_status       |
| mongo_status            |
| mongo_status_history    |
| mongo_status_info       |
+-------------------------+

 

插入被监控MongoDB主机的信息

MySQL> INSERT INTO `mongo_status_info`(ip,tag,USER,pwd,PORT,authdb,threshold_alarm_connection,threshold_alarm_repl)VALUES('10.150.1.107','MongoDB测试','root','Gaoyu@029','37011','admin',1000,60);

 注释:

  • ip字段含义:输入被监控Mongo的IP地址。
  • tag字段含义:输入被监控Mongo的业务名字。
  • user字段含义:输入被监控Mongo的用户名(ROOT权限)。
  • pwd字段含义:输入被监控Mongo的密码。
  • port字段含义:输入被监控MySQL的端口号。
  • authdb字段含义:输入被监控Mongo的数据库登录权限认证库名。
  • monitor字段含义:0为关闭监控(也不采集数据,直接跳过);1为开启监控(采集数据)。
  • send_mail字段含义:0为关闭邮件报警;1为开启邮件报警。
  • send_mail_to_list字段含义:邮件人列表。
  • send_weixin字段含义:0为关闭微信报警;1为开启微信报警。
  • send_weixin_to_list字段含义:微信公众号。
  • threshold_alarm_connection字段含义:设置连接数阀值(单位个)。
  • threshold_alarm_repl字段含义:设置主从复制延迟阀值(单位秒)。

 

创建mongo用户用于监控

MySQL> create user 'mongo'@'%' identified by 'Password';
MySQL> grant all privileges on mongo_monitor.* to 'mongo'@'%';
MySQL> flush privileges;

 2.2.4 配置MySQL连接

 配置mongo_monitor连接MySQL数据库。

[root]# vi /var/www/html/mongo_monitor/conn.php
 <?php
//https://github.com/hcymysql/mongo_monitor

     $con = mysqli_connect("10.150.1.107","mongo","Password","mongo_monitor","3306") or die("数据库链接错误".mysqli_error($con));
     mysqli_query($con,"set names utf8");
?>

 2.2.5 邮件报警

可以将报警信息通过邮件形式发出。

[root]# vi /var/www/html/mongo_monitor/mail/mail.php
system("./mail/sendEmail -f chunyang_he@139.com -t '{$this->send_mail_to_list}' -s smtp.139.com:25 -u '{$this->alarm_subject}' -o message-charset=utf8 -o message-content-type=html -m '报警信息:
{$this->alarm_info}' -xu chunyang_he@139.com -xp '123456' -o tls=no");

注:按需修改发件人地址,账号密码,里面的变量不用修改。

2.2.6 微信报警

微信企业号设置参考:

https://github.com/X-Mars/Zabbix-Alert-WeChat/blob/master/README.md

2.2.7 设置定时任务

用于抓取监控信息

[root]# crontab -e
*/1 * * * * cd /var/www/html/mongo_monitor; /usr/bin/php /var/www/html/mongo_monitor/check_mongo_status.php > /dev/null 2 >&1
*/1 * * * * cd /var/www/html/mongo_monitor; /usr/bin/php /var/www/html/mongo_monitor/check_mongo_repl.php > /dev/null 2 >&1
  • check_mongo_status.php:用来采集被监控端Mongo状态信息和触发报警。
  • check_mongo_repl.php:用来采集被监控端Mongo主从复制信息和触发报警。

2.2.8 设置页面刷新频率

[root]# vi /var/www/html/mongo_monitor/mongo_replset_monitor.php
<meta http-equiv="refresh" content="10" />  <!-- 页面刷新时间600秒 -->

2.2.9 查看监控页面

http://10.150.1.107/mongo_monitor/mongo_replset_monitor.php

 

连接数:

 

QPS: