自定义监控MySQL进程以及日志

[root@localhost ~]# yum -y install mysql mysql-server
[root@localhost ~]# systemctl start mysqld.service        //开启MySQL服务
[root@localhost ~]# ss -antl
State     Recv-Q     Send-Q         Local Address:Port          Peer Address:Port    
LISTEN    0          32             192.168.122.1:53                 0.0.0.0:*       
LISTEN    0          128                  0.0.0.0:22                 0.0.0.0:*       
LISTEN    0          5                  127.0.0.1:631                0.0.0.0:*       
LISTEN    0          128                  0.0.0.0:10050              0.0.0.0:*       
LISTEN    0          128                  0.0.0.0:111                0.0.0.0:*       
LISTEN    0          128                     [::]:22                    [::]:*       
LISTEN    0          5                      [::1]:631                   [::]:*       
LISTEN    0          70                         *:33060                    *:*       
LISTEN    0          128                        *:3306                     *:*       
LISTEN    0          128                     [::]:111                   [::]:*       

查看一下MySQL的进程

[root@localhost ~]# ps -ef | grep mysql
mysql     186663       1  0 06:52 ?        00:00:01 /usr/libexec/mysqld --basedir=/usr
root      191366  111953  0 06:55 pts/3    00:00:00 grep --color=auto mysql

因为每个服务都是有进程的,基于这个特性我们来写一个脚本

[root@localhost ~]# vim check_process.sh
[root@localhost ~]# cat check_process.sh 
#!/bin/bash

count=$(ps -ef | grep -Ev "grep|$0" | grep -c $1)    //查看MySQL进程并且过滤掉本身,将取到的值给count
if  [ $count -eq 0 ];then         //如果count等于0时打印1
        echo'1'
else         //否则打印0
        echo '0'
fi

修改客户机的zabbix_agentd.conf文件

[root@localhost ~]# cd /usr/local/etc/
[root@localhost etc]# vim zabbix_agentd.conf          //在这个文件中添加下面这行
UserParameter=check_process[*],/bin/bash /scropts/check_process.sh $1

//重启zabixxfu服务让其生效
[root@localhost etc]# pkill zabbix
[root@localhost etc]# zabbix_agentd 
//测试脚本是否能够正常运行
[root@localhost scropts]# ./check_process.sh mysql                //在开启MySQL的时候运行显示的时1表示没问题
1 
[root@localhost scropts]# systemctl stop mysqld.service          //关闭MySQL
[root@localhost scropts]# ./check_process.sh mysql          //在执行脚本发现变成了零 ,就说明脚本没问题
0

回到zabbix网页上添加监控项
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
添加触发器
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
回到客户机然后关闭MySQL服务

[root@localhost scropts]# systemctl stop mysqld.service

在回到zabbix网页就会发现出现了告警
在这里插入图片描述
配置自定义监控日志
配置脚本权限

[root@localhost ~]# cd /scropts/
[root@localhost scropts]# ls
check_process.sh  log.py
[root@localhost scropts]# chmod +x log.py 
[root@localhost scropts]# setfacl -m u:zabbix:r-x /var/log/httpd/error_log
[root@localhost scropts]# getfacl /var/log/httpd/error_log
getfacl: Removing leading '/' from absolute path names
# file: var/log/httpd/error_log
# owner: httpd
# group: httpd
user::rw-
user:zabbix:r-x
group::r--
mask::r-x
other::---

测试脚本

[root@localhost scropts]# python3 log.py /var/log/httpd/error_log
0
[root@localhost scropts]# echo 'Error' >> /var/log/httpd/error_log
[root@localhost scropts]# python3 log.py /var/log/httpd/error_log     //如果mysqld.log文件中有Error这个关键字就会输出1
1
[root@localhost scropts]# python3 log.py /var/log/httpd/error_log //变回零是因为mysqld.log文件是实时更新的,它把Error给刷下去了,所有就会变成0
0
//查看logseek记录查询的位置
[root@localhost scropts]# cat /tmp/logseek 
3701[root@localhost scropts]#             //这时候显示是3701
//再次执行脚本
[root@localhost scroptecho 'Error' >> /var/log/httpd/error_log
[root@localhost scropts]# python3 log.py /var/log/httpd/error_log
1
[root@localhost scropts]# cat /tmp/logseek 
3707[root@localhost scropts]#                   //现在变成了3707
//删除logseek文件
[root@localhost scropts]# rm -rf /tmp/logseek 

在客户机上配置zabbix_agentd.conf文件

[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf      //添加下面这行

UserParameter=check_logs[*],/usr/bin/python3 /scropts/log.py $1 $2 $3

//重启zabbix
[root@localhost ~]# pkill  zabbix_agentd 
[root@localhost ~]# zabbix_agentd 
[root@localhost mysql]# ss -antl
State     Recv-Q     Send-Q         Local Address:Port          Peer Address:Port    
LISTEN    0          128                  0.0.0.0:80                 0.0.0.0:*       
LISTEN    0          32             192.168.122.1:53                 0.0.0.0:*       
LISTEN    0          128                  0.0.0.0:22                 0.0.0.0:*       
LISTEN    0          5                  127.0.0.1:631                0.0.0.0:*       
LISTEN    0          128                  0.0.0.0:10050              0.0.0.0:*       
LISTEN    0          128                  0.0.0.0:111                0.0.0.0:*       
LISTEN    0          128                     [::]:22                    [::]:*       
LISTEN    0          5                      [::1]:631                   [::]:*       
LISTEN    0          70                         *:33060                    *:*       
LISTEN    0          128                        *:3306                     *:*       
LISTEN    0          128                     [::]:111                   [::]:*     

回到zabbix网页
配置监控项
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

添加触发器
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

测试
回到客户机修改mysqld.log文件常看能否告警

[root@localhost scropts]# echo 'Error' >> /var/log/httpd/error_log         //写入Error
[root@localhost scropts]# python3 log.py /var/log/httpd/error_log 
1
[root@localhost scropts]# python3 log.py /var/log/httpd/error_log 
0

回到仪表盘查看告警
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值