自定义监控:业务场景监控
自定义监控进程
1.在客服端打开自定义监控功能
2.在客服端写一个通用脚本
3.在客服端添加脚本监控配置
4.在客服端重启zabbix_agentd
5.在服务端 zabbix_get -s 192.168.11.131 -k
6.在web上配置监控项和触发器
自定义监控功能开启
[root@150 etc]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1 //取消注释
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1 //添加
[root@150 etc]# mkdir /scripts/
[root@150 etc]# cd /scripts/
[root@150 scripts]# touch check_process.sh
[root@150 scripts]# chmod +x check_process.sh
//脚本
[root@150 scripts]# vim check_process.sh
#!/bin/bash
count=$(ps -ef|grep -Ev "grep|$0"|grep $1|wc -l)
if [ $count -eq 0 ];then
echo '1'
else
echo '0'
fi
[root@zabbix ~]# zabbix_get -s 192.168.11.131 -k check_process[hhtpd]
1
[root@zabbix ~]# zabbix_get -s 192.168.11.131 -k check_process[vsftpd]
0
添加触发器
2.自定义监控日志
脚本
[root@150 log]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_log[*],/usr/local/python36/bin/python3/scripts/log.py $1 $2 $3
check_process.sh log.py
[root@150 scripts]#
[root@150 scripts]# pyvenv-3.6 /usr/local/python36
WARNING: the pyenv script is deprecated in favour of `platform-python -m venv`
[root@150 scripts]#vim/usr/local/etc/zabbix_agentd.conf
[root@150 scripts]# pkill zabbix
[root@150 scripts]# zabbix ag
//给权限
[root@150 log]# setfacl -m u:zabbix:rx /var/log/httpd
[root@150 log]# getfacl /var/log/httpd/
getfacl: Removing leading '/' from absolute path names
# file: var/log/httpd/
# owner: root
# group: root
user::rwx
user:zabbix:r-x
group::---
mask::r-x
other::---
[root@zabbix ~]# zabbix_get -s 192.168.11.131 -k check_log[/var/log/httpd/error_log]
0
[root@150 log]# ls /tmp/
logseek
zabbix_agentd.log
zabbix_agentd.pid
[root@150 log]# echo 'Error' >> httpd/error_log
[root@zabbix ~]# zabbix_get -s 192.168.11.131 -k check_log[/var/log/httpd/error_log]
1
[root@zabbix ~]#
添加监控项
6137[root@150 loecho 'error' >> httpd/error_log
3.监听mysql
//安装
[root@mastrt ~]# yum -y install mariadb*
[root@sleve ~]# yum -y install mariadb*
//启动
[root@mastrt ~]# systemctl start mariadb
[root@sleve ~]# systemctl start mariadb
[root@mastrt ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 80 0.0.0.0:3306 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 32 *:21 *:*
LISTEN 0 128 [::]:22 [::]:*
//授权
MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.11.130' identified by 'repl123';
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.008 sec)
MariaDB [(none)]>
[root@mastrt ~]# vim /etc/my.cnf
[mysqld]
server-id = 10
log-bin = mysql_bin
[root@mastrt ~]# systemctl restart mariadb
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql_bin.000001 | 328 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)
MariaDB [(none)]>
[root@sleve ~]# vim /etc/my.cnf
[client-server]
[mysqld]
server-id = 20
relay-log = myrelay
[root@sleve ~]# systemctl restart mariadb
MariaDB [(none)]> change master to master_host='192.168.11.131',master_user='repl',master_password='repl123',master_log_file='mysql_bin.000001',master_log_pos=328;
Query OK, 0 rows affected (0.007 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.056 sec)
MariaDB [(none)]>
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)
MariaDB [(none)]>
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)
//解压
[root@sleve ~]# tar xf zabbix-5.2.0.tar.gz
[root@sleve ~]# yum -y install gcc gcc-c++ make
[root@sleve ~]# yum -y install pcre-devel
//编译
[root@sleve zabbix-5.2.0]# ./configure --enable-agent
[root@sleve zabbix-5.2.0]# make install
[root@sleve zabbix-5.2.0]# cd /usr/local/etc/
[root@sleve etc]# vim zabbix_agentd.conf
Server=192.168.11.150
ServerActive=192.168.11.150
Hostname=mysql_slave1
//关闭防火墙seliunx
[root@sleve ~]# vim /etc/selinux/config
[root@sleve ~]# systemctl stop firewalld
[root@sleve ~]# setenforce 0
[root@sleve ~]# mysql -e 'show slave status\G'|grep -E 'IO_Running:|SQL_Running:'
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@sleve ~]#
//脚本
[sleve scripts]# vim mysql_msstatus.sh
#!/bin/bash
yes_count=$(mysql -e 'show slave status\G'|grep -E 'IO_Running:|SQL_Running:'|grep -c 'Yes')
if [ $yes_count -ne 2 ];then
echo '1'
else
echo '0'
fi
//给脚本权限
[root@sleve scripts]# chmod +x mysql_msstatus.sh
[root@sleve scripts]# ./mysql_msstatus.sh
0
[root@sleve scripts]#
//该属组
[root@sleve ~]# chown -R zabbix.zabbix /scripts/
[root@sleve ~]# ll /scripts/
total 4
-rwxr-xr-x. 1 zabbix zabbix 174 Nov 8 21:02 mysql_msstatus.sh
[root@sleve ~]#
[root@sleve ~]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=show_master_slave_status,/bin/bash/scripts/mysql_msstatus.sh
[root@sleve ~]# pkill zabbix
[root@sleve ~]# zabbix_agentd
[root@sleve ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
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 80 0.0.0.0:3306 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@sleve ~]#
[root@zabbix ~]# zabbix_get -s 192.168.11.130 -k show_master_slave_status
0
[root@zabbix ~]#
[root@sleve scripts]# mv mysqld_de mysqld_delay.sh
[root@sleve scripts]# vim mysqld_delay.sh
[root@sleve scripts]# chmod +x mysqld_delay.sh
[root@sleve scripts]# ll
total 8
-rwxr-xr-x. 1 root root 114 Nov 8 22:42 mysqld_delay.sh
-rwxr-xr-x. 1 zabbix zabbix 174 Nov 8 21:02 mysql_msstatus.sh
脚本
[root@sleve scripts]# vim mysql_delay.sh
#!/bin/bash
delay=$(mysql -e 'show slave status\G'|grep 'Seconds_Behind_Master'|awk '{pri
nt $2}')
echo $delay