文章目录
1自定义监控进程
1.1 自定义监控配置流程
- 写脚本,脚本放到统一位置
- 修改被监控机上的zabbix_agentd.conf配置文件
UnsafeParameters=1
UserParameter=, - 重启zabbix_agent服务
- 在web界面配置监控项和触发器
1.2 监控网站
// 安装httpd
[root@maste ~]# yum -y install httpd
[root@maste ~]# systemctl start httpd
[root@maste ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
访问httpd
1.3 监控httpd进程
// 查看进程
[root@maste ~]# ps -ef | grep httpd
root 2038 1 0 02:54 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 2040 2038 0 02:54 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 2041 2038 0 02:54 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 2042 2038 0 02:54 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 2043 2038 0 02:54 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 2257 1536 0 02:56 pts/0 00:00:00 httpd
root 2259 1536 0 02:56 pts/0 00:00:00 grep --color=auto httpd
监控进程之前先修改agentd配置/usr/local/etc/zabbix_agentd.conf
[root@maste etc]# vim zabbix_agentd.conf
UnsafeUserParameters=1 // 取消注释并将0修改为1
UserParameter=<key>,<shell command> //将此行复制到末尾,key是自定义监控值的key,shell command 是获取值的方式
编写监控进程的脚本(脚本是在客户端执行)
// 创建脚本目录
[root@maste ~]# mkdir /scripts
[root@maste script]# cat check_process.sh
#! /bin/bash
web=$( ps -ef | grep -Ev "grep|$0" | grep -c "$1" )
if [ $web -eq 0 ];then
echo '1'
else
echo '0'
fi
//修改/usr/local/etc/zabbix_agentd.conf
[root@maste ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_process[*],/scripts/check_process.sh $1
//修改完配置文件重启服务
[root@maste ~]# pkill zabbix_agentd
[root@maste ~]# zabbix_agentd
1.4 在zabbix网页上添加监控项
测试key键值是否有效
[root@server ~]# zabbix_get -s 192.168.25.147 -k check_httpd
0
1.5 添加动作
// 关闭httpd触犯动作报警
[root@maste ~]# systemctl stop httpd
[root@maste ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
2 自定义监控日志
// 修改配置文件
[root@maste ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_log[*],/scripts/log.py $1 $2 $3
创建监控项
添加动作
手动触发报警
[root@maste scripts]# echo "Error" >> /var/log/httpd/error_log
监控多个关键字
3 自定义监控mysql主从状态
搭建MySQL主从后,很多时候不知道从的状态是否ok,有时候出现异常不能及时知道,这里通过shell脚本结合zabbix实现监控并告警
一般情况下,在MySQL的从上查看从的运行状态是通过Slave_IO_Running线程和Slave_SQL_Running线程是否ok,通过命令“show slave status\G;”即可查看。所以这里根据这两个值进行判断。
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
// 监控脚本
[root@slave scripts]# cat ./check_replication.sh
#! /bin/bash
count=$(mysql --defaults-file=/scripts/.passwd -e 'show slave status\G' | grep '_Running:' | grep -c 'Yes')
if [ $count -ne 2 ];then
echo '1'
else
echo '0'
fi
修改配置文件
[root@maste ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_replication,/scripts/check_replication.sh
给zabbix账户权限
mysql> grant SUPER, REPLICATION CLIENT on *.* to 'zabbix'@'localhost' identified by '123';
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
创建存放zabbix的密码文件
[root@slave ~]# cat /scripts/.passwd
[client]
user=zabbix
password=123
创建存放root的密码文件
[root@slave ~]# cat .my.cnf
[client]
user=root
password=123456
测试
// 脚本测试
[root@slave scripts]# ./check_replication.sh
0
[root@slave scripts]# ./check_replication.sh
1
// 服务端测试
[root@server ~]# zabbix_get -s 192.168.25.147 -k check_replication
0
[root@server ~]# zabbix_get -s 192.168.25.147 -k check_replication
1
配置zabbix网页监控
测试
4 自定义监控mysql主从延迟
监控脚本
[root@slave scripts]# cat check_replication_delay.sh
#! /bin/bash
delay=$(mysql --defaults-file=/scripts/.passwd -e 'show slave status\G '| grep 'Behind' | awk '{print $2}')
if [ $delay -eq 0 ];then
echo $delay
else
echo '0'
fi
编写配置文件
[root@slave ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_replication_delay,/scripts/check_replication_delay.sh
配置zabbi网页监控
测试(因为本机没有延迟为了能触发报警,所以手动触发)