Nagios监控远端的mysql

时间:2022-07-18 23:24:36

工作原理:

利用特定的用户定期访问指定的mysql数据库。当不能访问或连不通时则报警。

1.在生产库上安装nagios插件
    安装略
    备注:编译完显示一定要有mysql支持,不然没有check_mysql插件

2.配置mysql

----建立nagdb专用数据库
mysql> create database nagdb default CHARSET=utf8;
Query OK, 1 row affected (0.01 sec) mysql> grant select on nagdb.* to 'nagios'@'%';
Query OK, 0 rows affected (0.00 sec) mysql> update mysql.user set 'Password' = PASSWORD('nagios') where 'User'='nagios';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Password' = PASSWORD('nagios') where 'User'='nagios'' at line 1
mysql> update mysql.user set Password = PASSWORD('nagios') where user='nagios';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
----用以下命令来验证一下:
[root@node2 etc]# /usr/local/nagios/libexec/check_mysql -H 192.168.1.152 -u nagios -d nagdb -p nagios
Uptime: 3374 Threads: 1 Questions: 11 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 26 Queries per second avg: 0.003
备注:如果出现:/usr/local/nagios/libexec/check_mysql: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
请查看$MYSQL_HOME/lib
[root@node2 lib]# ln -s ./libmysqlclient.so.18.0.0 /usr/lib/libmysqlclient.so.18

3.配置nrpe.cfg

[root@node2 etc]# tail /usr/local/nagios/etc/nrpe.cfg
command[check_mysql]=/usr/local/nagios/libexec/check_mysql -H 192.168.1.152 -u nagios -d nagdb -p nagios //加入此行

4.配置nagios服务端

[root@node1 etc]# cat services.cfg        //加入下面内容
define service{
use local-service
host_name node2
service_description mysql
check_command check_nrpe!check_mysql
notifications_enabled 1
}

5.重启nrpe和服务端的nagios