自定义监控httpd进程
准备工作
//在客户端上开启自定义监控
[root@localhost ~]# cd /usr/local/etc/
[root@localhost etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@localhost etc]# vim zabbix_agentd.conf
UnsafeUserParameters=1 //搜索Parameters 取消注释 并将值改为1
UserParameter=check_process[],/bin/bash /scropts/check_process.sh $1 //文档最后添加自定义监控
[root@localhost etc]# cd
[root@localhost ~]# pkill zabbix
[root@localhost ~]# zabbix_agentd
[root@localhost ~]# 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 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
//安装httpd用于监控测试
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl start httpd.service
[root@localhost ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd.service(8)
//查看httpd相关的进程
[root@localhost ~]# ps -ef | grep httpd
root 447745 1 0 10:37 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 447757 447745 0 10:37 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 447759 447745 0 10:37 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 447760 447745 0 10:37 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 447761 447745 0 10:37 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 448998 406571 0 10:37 pts/2 00:00:00 grep --color=auto httpd
[root@localhost ~]# ps -ef | grep -v grep| grep -c httpd
5
//编写脚本
[root@localhost ~]# mkdir /scropts
[root@localhost ~]# cd /scropts/
[root@localhost scropts]# vim check_process.sh //判定httpd服务进程数是否为0,如果为0 则有问题输出'1',没问题输出'0'
[root@localhost scropts]# cat check_process.sh
#!bin/bash
count=$(ps -ef|grep -Ev "grep|$0"|grep -c $1)
if [ $count -eq 0 ];then
echo '1'
else
echo '0'
fi
[root@localhost scropts]#
[root@localhost scropts]# chmod +x check_process.sh
[root@localhost scropts]# ll
总用量 4
-rwxr-xr-x. 1 root root 135 7月 10 10:40 check_process.sh
//测试并运行脚本
[root@localhost scropts]# systemctl stop httpd.service
[root@localhost scropts]# ps -ef | grep -v grep| grep -c httpd
0
[root@localhost scropts]# bash check_process.sh
1
[root@server ~]# zabbix_get -s 192.168.201.144 -k check_process[httpd]
1
[root@server ~]# zabbix_get -s 192.168.201.144 -k check_process[zabbix]
0
添加监控项
添加触发器
测试
自定义监控mysql进程
准备工作
//安装mysql服务
[root@localhost ~]# yum -y install mysql mysql-server
[root@localhost ~]# systemctl start mysqld.service
[root@localhost ~]# systemctl status mysqld.service
● mysqld.service - MySQL 8.0 database server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2022-07-10 11:13:36 EDT; 2s ago
添加监控项
添加触发器
测试
//关闭mysql服务
[root@localhost ~]# systemctl stop mysqld.service
自定义监控日志
准备工作
//编写脚本
[root@localhost scropts]# ls
check_process.sh
[root@localhost scropts]# vim log.py
//给脚本log执行权限
[root@localhost scropts]# chmod +x log.py
[root@localhost scropts]# ll
总用量 28
-rwxr-xr-x. 1 root root 132 7月 10 11:04 check_process.sh
-rwxr-xr-x. 1 root root 21374 7月 10 11:23 log.py
[root@localhost scropts]# cd /var/log/httpd/
[root@localhost httpd]# setfacl -m u:zabbix:rx /var/log/httpd
[root@localhost httpd]# 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@localhost httpd]# cd
[root@localhost ~]# yum -y install python3
[root@localhost ~]# cd /scropts/
[root@localhost scropts]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_process[*],/bin/bash /scripts/log.py $1 $2 $3
[root@localhost scripts]# python3 log.py /var/log/httpd/error_log
0
[root@localhost scripts]# echo Error >> /var/log/httpd/error_log
[root@localhost scripts]# python3 log.py /var/log/httpd/error_log
1
[root@localhost scropts]# cat /tmp/logseek //logseek记录查询的位置
3851
[root@localhost scropts]# python3 log.py /var/log/httpd/error_log //执行脚本
0
[root@localhost scropts]# cat /tmp/logseek //再次查看
3873
[root@localhost scropts]# ll /tmp/logseek
-rw-r--r--. 1 root root 5 7月 10 11:32 /tmp/logseek //当网页使用时没有权限
[root@localhost scropts]# rm -rf /tmp/logseek //脚本测试结束后可以删掉 网页会自动创建
[root@localhost scripts]# pkill zabbix_agentd
[root@localhost scripts]# zabbix_agentd
添加监控项
添加触发器
测试
[root@localhost scropts]# echo 'Error' >> /var/log/httpd/error_log
自定义监控mysql主从状态
环境
主机名 | ip | 作用 |
---|---|---|
server | 192.168.201.147 | 服务端 |
slave | 192.168.201.144 | 客户端兼mysql从库 |
master | 192.168.201.143 | mysql主库 |
安装MySQL
参考前面的mysql安装
配置主从并验证是否能成功同步
参考前面的mysql主从
[root@master ~]# mysql -uroot -p'123.com' -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
//从数据库
[root@slave ~]# mysql -uroot -p'123.com!' -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
//在主数据库里创建一个同步账号授权给从数据库使用
mysql> grant replication slave on *.* to 'repl'@'192.168.201.144' identified by 'repl123!';
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
//配置主数据库
[root@master ~]# vim /etc/my.cnf
[root@master ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
server-id = 10
log-bin = mysql-bin
[root@master ~]# systemctl restart mysqld.service //重启mysql服务
[root@master ~]# systemctl disable --now firewalld
[root@master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 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 80 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
mysql> show master status;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 2
Current database: *** NONE ***
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
//配置从数据库
[root@slave ~]# vim /etc/my.cnf
[root@slave ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
server-id = 20
relay-log=mysql-relay-bin
[root@slave ~]# systemctl restart mysqld//重启从库的mysql服务
[root@slave ~]# systemctl disable --now firewalld
[root@slave ~]# 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 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
//配置并启动主从复制
[root@slave ~]# mysql -uroot -p'123.com!'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> change master to
-> master_host='192.168.201.143',
-> master_host='repl',
-> master_password='repl123!',
-> master_log_file='mysql_bin.000001',
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql> show slave status \G //查看从服务器状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.201.143
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
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 //此处必须为Yes
Slave_SQL_Running: Yes //此处必须为Yes
测试
//主服务器
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database school;
Query OK, 1 row affected (0.04 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
5 rows in set (0.00 sec)
//从服务器
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
5 rows in set (0.00 sec)
写脚本获取从库的状态,确保2个YES,如果不是两个YES就报错
[root@slave scropts]# vim mysqlms.sh
[root@slave scropts]# cat mysqlms.sh
#!/bin/bash
count=$( mysql -uroot -p123.com -e 'show slave status\G;' 2>/dev/null | grep -v grep | grep -c 'Yes')
if [ $count -eq 2 ];then //当master_slave不等于2打印1 反之打印0
echo "0"
else
echo "1"
fi
[root@slave scropts]# chmod +x mysqlms.sh
[root@salve scropts]# bash mysqlms.sh
0
配置/usr/local/etc/zabbix_agentd.conf文件自定义监控
[root@slave scropts]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=master_slave,/usr/bin/bash /scropts/master_slave.sh
重启zabbix_agentd
[root@slave ~]# pkill zabbix_agentd
[root@slave ~]# zabbix_agentd
在从库本地执行脚本查看结果是否正常匹配
去服务端用zabbix_get命令手动获取下状态并且确保状态与从库手动执行一致
配置web界面添加监控项和触发器
手动触发(把主库mysql服务停掉)并验证是否报警
自定义监控mysql主从延迟
编写脚本获取从库的状态
[root@slave scropts]# vim mysql_delay.sh
[root@slave scropts]# cat mysql_delay.sh
#!/bin/bash
delay=$( mysql -uroot -p123.com -e "show slave status\G" 2>/dev/null | grep 'Seconds_Behind_Master'| awk '{print $2}')
echo $delay
[root@slave scropts]# chmod +x mysql_delay.sh
[root@salve scropts]# bash mysqlms.sh
0
配置/usr/local/etc/zabbix_agentd.conf文件自定义监控
[root@slave scropts]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=database_delay,/usr/bin/bash /scropts/mysql_delay.sh
重启zabbix_agentd
[root@slave ~]# pkill zabbix_agentd
[root@slave ~]# zabbix_agentd