自定义脚本监控
1.进程监控
修改配置文件/usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
配置进程监控的脚本
#!/bin/bash
process_status=$(ps -ef|grep $1|grep -Ev "grep|$0"|wc -l)
if [ $process_status -eq 0 ];then
echo '1'
else
echo '0'
fi
添加监控项
关掉postfix服务
[root@longnian etc]# systemctl stop postfix
再次查看
添加触发器
触发结果
2.日志监控
添加内容到配置文件/usr/local/etc/zabbix_agentd.conf
UserParameter=check_logs[*],/usr/bin/python /scripts/log.py $1 $2 $3
添加监控项
添加触发器
触发结果
3.mysql主从状态监控
客户端安装mariadb(做服务端)
[root@longnian ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.65-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> CREATE USER 'repl'@'192.168.159.136' IDENTIFIED BY 'repl123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* to 'repl'@'192.168.159.136';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
添加内容到/etc/my.cnf
server-id = 10
log-bin = mysql_bin
重启服务
[root@longnian ~]# systemctl restart mariadb
查看主库的状态
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 245 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]>
第三台服务器做mariadb的客户端
添加内容到/etc/my.cnf
server-id = 20
relay-log = myrelay
重启服务
[root@longnian ~]# systemctl enable --now mariadb
配置数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> change master to master_host='192.168.159.132',master_user='repl',master_password='repl123',master_log_file
MariaDB [(none)]> change master to master_host='192.168.159.132',master_user='repl',master_password='repl123',master_log_file='mysql-bin.000004',master_log_pos=245;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.159.132
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 245
Relay_Log_File: myrelay.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 245
Relay_Log_Space: 815
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
MariaDB [(none)]>
编译安装zabbix
[root@longnian ~]# cd /usr/src/
[root@longnian src]# ls
debug kernels zabbix-4.4.7 zabbix-4.4.7.tar.gz
[root@longnian src]# tar xf zabbix-4.4.7.tar.gz
[root@longnian src]# cd zabbix-4.4.7
[root@longnian zabbix-4.4.7]# ls
aclocal.m4 compile config.sub depcomp m4 misc src
AUTHORS conf configure frontends Makefile missing
bin config.guess configure.ac include Makefile.am NEWS
build config.log COPYING INSTALL Makefile.in README
ChangeLog config.status database install-sh man sass
[root@longnian zabbix-4.4.7]# ./configure --enable-agent
..........
[root@longnian zabbix-4.4.7]# make install
..........
修改配置文件/usr/local/etc/zabbix_agentd.conf
Server=192.168.159.135
ServerActive=192.168.159.135
Hostname=MySQL_Slave1
创建zabbix用户、启动服务
[root@longnian etc]# useradd -r -M -s /sbin/nologin zabbix
[root@longnian etc]# zabbix_agentd
[root@longnian etc]#
[root@longnian etc]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
添加主机
配置监控脚本
[root@longnian ~]# cd /scripts/
[root@longnian scripts]# vim check_replication.sh
[root@longnian scripts]# cat check_replication.sh
#!/bin/bash
mysql_status=$(mysql -uroot -e 'show slave status\G' 2>/dev/null|grep 'Slave.*Running'|grep -c 'YES')
if [ $mysql_status -ne 2 ];then
echo '1'
else
echo '0'
fi
修改配置文件/usr/local/etc/zabbix_agentd.conf
UserParameter=check_replication,/bin/bash /scripts/check_replication.sh
添加监控项
添加触发器
触发结果
4.mysql主从延时监控
配置监控脚本
[root@longnian scripts]# vim delay_relication.sh
[root@longnian scripts]# cat delay_relication.sh
#!/bin/bash
delay_value=$(mysql -uroot -e 'show slave status\G' 2>/dev/null|grep 'Seconds_Behind_Master'|awk -F'[: ]+' '{print $3}')
echo $delay_value
修改配置文件/usr/local/etc/zabbix_agentd.conf
UserParameter=delay_replication,/bin/bash /scripts/delay_replication.sh
重启服务
[root@longnian scripts]# pkill zabbix
[root@longnian scripts]# zabbix_agentd
[root@longnian scripts]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
添加监控项
添加触发器
触发结果
5.声音监控
添加声音
触发结果