方法一
一、mysql从服务器设置
1、mysql从服务器用户添加
代码如下 | 复制代码 |
执行如下语句添加用户: mysql> GRANT REPLICATION CLIENT ON *.* TO monitor@localhost IDENTIFIED BY 'PassWord'; 2、下载check-mysql-slave.pl脚本 cd /usr/local/bin/ wget http://www.centos.bz/wp-content/uploads/2012/10/check-mysql-slave.pl chmod +x check-mysql-slave.pl 3、在mysql从服务器上配置extend mysql-slave 在/etc/snmp/snmpd.conf文件的末尾添加如下代码: extend mysql-slave /usr/local/bin/check-mysql-slave.pl --user monitor --pass PassWord --sock /var/lib/mysql/mysql.sock 注意相关参数修改为自己的。 之后重载snmp: service snmpd reload |
二、监控服务器设置
1、下载check_snmp_extend.sh脚本
代码如下 | 复制代码 |
mkdir /usr/local/nagios/libexec.local cd /usr/local/nagios/libexec.local wget http://www.centos.bz/wp-content/uploads/2012/10/check_snmp_extend.sh chmod +x check_snmp_extend.sh 2、定义USER10变量 在文件/usr/local/nagios/etc/resource.cfg添加如下变量: $USER10$=/usr/local/nagios/libexec.local 3、定义check_snmp_extend命令 在/usr/local/nagios/etc/objects/commands.cfg添加: define command{ command_name check_snmp_extend command_line $USER10$/check_snmp_extend.sh $HOSTADDRESS$ $ARG1$ } |
4、定义监控mysql主从服务
在主机配置文件,如/usr/local/nagios/etc/objects/www.centos.bz.cfg中添加如下service(注意,此www.cnetos.bz.cfg文件已经在nagios.cfg配置文件中包含)
代码如下 | 复制代码 |
define host{ use linux-server host_name www.centos.bz alias www.centos.bz address 142.4.33.74 } ...... ...... define service{ ## This is an example service configured as ## extend servicename /path/to/service-check.sh ## on remote.server in /etc/snmp/snmpd.conf use generic-service host_name www.centos.bz service_description mysql slave status check_command check_snmp_extend!mysql-slave } |
方法二
下面是监控MYSQL复制某个从服务器的输出:
代码如下 | 复制代码 |
mysql> show slave statusG *************************** 1. row*************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.93.16 Master_User: rep1 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 1752541 Relay_Log_File: hy-mysql3-relay-bin.000088 Relay_Log_Pos: 2339 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: |
这个输出,最关键处就是”Slave_IO_Running: Yes“和“Slave_SQL_Running:Yes”,这两个值全是”Yes”就表明主从复制正常,否则就是有问题。
操作步骤:
1、在主数据库服务器增加一个用户,给予较低的权限,操作为
代码如下 | 复制代码 |
mysql > grantReplication client on *.* to ‘nagios’@'%’identified by ‘nagios’; mysql> flush privileges; |
2、登陆从服务器验证一下,看是否正常。操作为 # mysql -unagios -pnagios -e “show slavestutasG” 注意:这个操作是在 shell下操作的!!
3、在从服务器安装nrpe,然后加入一行”command[check_mysql_slave]=/usr/local/nrpe/libexec/chech_mysql_slave”.
4、编写脚本/usr/local/nrpe/libexec/check_mysql_slave(这是监控其作用的核心),其内容如下:
代码如下 | 复制代码 |
#!/bin/sh declare -a slave_is slave_is=($(/usr/local/mysql/bin/mysql -unagios-pnagios -e “show slave statusG”|grep Running|awk ‘{print $2}’)) if [ "${slave_is[0]}” = “Yes” -a “${slave_is[1]}” = “Yes” ] then echo “OK -slave isrunning” exit 0 else echo “Critical -slave iserror” exit 2 fi |
5、手动运行这个脚本,观察输出。
6、执行 check_nrpe -H ip -c check_mysql_slave 观察其输出。
7、修改nagios,并测试。
8、功能测试(略)
方法三
首先定义主机:
代码如下 | 复制代码 |
vim /usr/local/nagios/etc/objects/localhost.cfg define host { host_name joker alias test address 192.168.xx.xx check_command check-host-alive notification_options d,u,r check_interval 1 max_check_attempts 2 contact_groups admins notification_interval 10 notification_period 24x7 } 定义服务: define service { host_name joker service_description nrpe check_period 24x7 normal_check_interval 2 retry_check_interval 1 max_check_attempts 5 notification_period 24x7 notification_options w,u,c,r check_command check_nrpe!check_mysql } 定义command: define command { command_name check_nrpe command_line /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ } 在被监控主机上: 安装nrpe tar fvxz nrpe*.tar.gz ./configure --prefix=/usr/local/nagios useradd nagios make all make install make install-daemon make install-daemon-config make install-xinetd scp /usr/local/nagios/libexec/check_nrpe root@192.168.18.105:/usr/local/nagios/libexec vim /etc/xinetd.d/nrpe service nrpe { flags = REUSE socket_type = stream port = 5666 wait = no user = nagios group = nagios server = /usr/local/nagios/bin/nrpe server_args = -c /usr/local/nagios/etc/nrpe.cfg --inetd log_on_failure += USERID disable = no only_from = 192.168.18.105 } vim /etc/services 在最后添加nrpe 5666/tcp service xinetd restart vim /usri/local/nagios/etc/nrpe.cfg command[check_mysql]=/usr/local/nagios/libexec/check_mysql vim /usr/local/nagios/libexec/check_mysql #!/bin/bash master=`mysql -h 192.168.18.165 -u hello -p123 -e 'show master status' &> /tmp/123 | awk 'NR==2{print $2}' /tmp/123` slave=`mysql -e 'show slave status' &> /tmp/234 | awk =F: '{if($1~/Exec/)print $2}' /tmp/234` if [ $master -eq $slave ]; then echo "ok" rm -fr /tmp/123 rm -fr /tmp/234 exit 0 else echo "error" mv /tmp/123 /tmp/err1 mv /tmp/234 /tmp/err2 exit 2 |
最后进行测试,本人亲测OK~~~
时间: 2024-11-01 01:04:31