自定义监控服务进程及其日志(以mysql为例)
前提
主机名 | ip |
---|---|
dabao-服务端 | 192.168.140.142 |
client-客户端 | 192.168.140.143 |
首先在服务端配置好lamp和zabbix
[root@dabao ~]# ss -antl
[root@dabao ~]# 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 0.0.0.0:10051 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
配置客户机
[root@client ~]# ls
anaconda-ks.cfg zabbix-5.0.25.tar.gz
[root@client ~]# tar xf zabbix-5.0.25.tar.gz
[root@client ~]# useradd -r -M -s /sbin/nologin zabbix
[root@client ~]# yum -y install gcc gcc-c++ vim wegt make
[root@client ~]# yum -y install pcre-devel
[root@client ~]# cd zabbix-5.0.25
[root@client zabbix-5.0.25]# ./configure --enable-agent
[root@client zabbix-5.0.25]# make install
[root@client zabbix-5.0.25]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@client zabbix-5.0.25]# setenforce 0
[root@client zabbix-5.0.25]# vim /etc/selinux/config
[root@client zabbix-5.0.25]# cat /etc/selinux/config
...
SELINUX=disabled
...
[root@client ~]# cd /usr/local/etc/
[root@client etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@client etc]# vim zabbix_agentd.conf
Server=192.168.140.142 #服务端ip
ServerActive=192.168.140.142#服务端ip
Hostname= 192.168.140.143#客户端ip
[root@client ~]# zabbix_agentd
[root@client ~]# yum -y install mysql
[root@client ~]# systemctl start mysqld
[root@client ~]# 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 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 70 *:33060 *:*
设置UnsafeUserParameters
[root@client ~]# cd /usr/local/etc/
[root@client etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@client etc]# vim zabbix_agentd.conf
将UnsafeUserParameters=0 取消注释且值设置为1
在最下面加入这一行
UserParameter=check_process[*],/bin/bash /usr/local/etc/scripts/check_process.sh $1
编辑脚本
[root@client etc]# mkdir scripts
[root@client etc]# vim scripts/check_process.sh
[root@client etc]# cat scripts/check_process.sh #如果有相关服务进程运行,输出0,反之输出1
#!/bin/bash
count=$(ps -ef|grep -Ev "grep|$0"|grep -c $1)
if [ $count -eq 0 ];then
echo '1'
else
echo '0'
fi
[root@client etc]# chmod +x scripts/check_process.sh
[root@client etc]# bash scripts/check_process.sh mysql
[root@client etc]# systemctl stop mysqld
[root@client etc]# bash scripts/check_process.sh mysql
1
[root@dabao ~]# zabbix_get -s 192.168.140.143 -k check_process[mysql]
1
增加监控项
添加触发器
配置日志报错
#将log.py上传
[root@client scripts]# ls
check_process.sh log.py
[root@client mysql]# pwd
/var/log/mysql
[root@client mysql]# ls
mysqld.log
[root@client mysql]# ll
总用量 12
-rw-r----- 1 mysql mysql 9253 7月 10 21:19 mysqld.log
[root@client ~]# ll -d /var/log/mysql/
drwxrwx---+ 2 mysql mysql 24 7月 10 17:44 /var/log/mysql/
安装python
[root@client ~]# dnf -y install python3
[root@client ~]# cd /usr/local/etc/
[root@client etc]# ls
scripts zabbix_agentd.conf zabbix_agentd.conf.d
[root@client etc]# vi zabbix_agentd.conf
在最后一行添加
UserParameter=check_log[*],/usr/bin/python3 /usr/local/etc/scripts/log.py $1 $2 $3
[root@client mysql]# setfacl -m u:zabbix:rwx mysqld.log
[root@client mysql]# setfacl -m u:zabbix:rwx /tmp/logseek
[root@client scripts]# python3 log.py /var/log/mysql/mysqld.log
0
[root@client scripts]# echo 'Error' >> /var/log/mysql/mysqld.log
[root@client scripts]# python3 log.py /var/log/mysql/mysqld.log
1
增加监控项
增加触发器