赞
踩
1.自定义监控进程
2.自定义监控日志
3.自定义监控MySQL主从状态
主机说明
主机 | ip |
---|---|
zabbixserver.example.com(zabbix服务端) | 192.168.10.131 |
slave.example.com(zabbix客户端,MySQL主从 从机) | 192.168.10.132 |
node2.example.com(MySQL主从 主机) | 192.168.10.133 |
打开自定义监控功能
#在服务端进入zabbix_agentd.conf配置文件
[root@slave ~]# cd /usr/local/etc/
[root@slave etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@slave etc]# vim zabbix_agentd.conf
#找到UnsafeUserParameters
## Option: UnsafeUserParameters
# Allow all characters to be passed in arguments to user-defined parameters.
# The following characters are not allowed:
# \ ' " ` * ? [ ] { } ~ $ ! & ; ( ) < > | # @
# Additionally, newline characters are not allowed.
# 0 - do not allow
# 1 - allow
#
# Mandatory: no
# Range: 0-1
# Default:
# UnsafeUserParameters=0
UnsafeUserParameters=1 #在下面加这一行
下面这行命令可以查看进程存在的个数
[root@slave srcipt]# ps -ef | grep "agentd" | grep -Ev "grep" | wc -l #名字带有agentd的进程个数有6个
6
[root@slave srcipt]# ps -ef | grep "httpd" | grep -Ev "grep" | wc -l #名字带有httpd的进程个数有0个
0
通过这条命令我们可以在脚本里面判断经常存不存在
创建一个/scripts/目录存放脚本,所有的监控脚本以后都放在这里
[root@slave ~]# mkdir /script
[root@slave ~]# cd /script
[root@slave srcipt]# vim showprocess.sh
[root@slave srcipt]# cat showprocess.sh
#!/bin/bash
process=$(ps -ef | grep "$1" | grep -Ev "grep|$0" | wc -l) #$1位置变量,$0脚本本身
if [ $process -eq 0 ];then #当值等于0时 打印1
echo "1"
else #其他情况打印0
echo "0"
fi
在zabbix_agentd主配置文件中编写(这里直接写到配置文件最后一行)
[root@slave srcipt]# vim /usr/local/etc/zabbix_agentd.conf
......
# ListenBacklog=
UserParameter=show_process[*],/bin/bash /srcipt/showprocess.sh $1
修改配置文件后重启zabbix_agent服务
[root@slave srcipt]# systemctl restart zabbix_agentd [root@slave srcipt]# systemctl status zabbix_agentd ● zabbix_agentd.service - zabbix agentd Loaded: loaded (/usr/lib/systemd/system/zabbix_agentd.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2023-09-27 22:39:21 CST; 13s ago Process: 35328 ExecStop=/usr/bin/pkill zabbix_agentd (code=exited, status=0/SUCCESS) Process: 35330 ExecStart=/usr/local/sbin/zabbix_agentd (code=exited, status=0/SUCCESS) Main PID: 35332 (zabbix_agentd) Tasks: 6 (limit: 24687) Memory: 2.1M CGroup: /system.slice/zabbix_agentd.service ├─35332 /usr/local/sbin/zabbix_agentd ├─35333 /usr/local/sbin/zabbix_agentd: collector [idle 1 sec] ├─35334 /usr/local/sbin/zabbix_agentd: listener #1 [waiting for connection] ├─35335 /usr/local/sbin/zabbix_agentd: listener #2 [waiting for connection] ├─35336 /usr/local/sbin/zabbix_agentd: listener #3 [waiting for connection] └─35337 /usr/local/sbin/zabbix_agentd: active checks #1 [idle 1 sec] Sep 27 22:39:21 slave.example.com systemd[1]: Stopped zabbix agentd. Sep 27 22:39:21 slave.example.com systemd[1]: Starting zabbix agentd... Sep 27 22:39:21 slave.example.com systemd[1]: Started zabbix agentd.
配置监控项
配置触发器
配置完成后提示报错并收到了邮件
进入https://github.com/chendao2015/pyscripts这个网站下载log.py到/script目录下面
#这是一个python脚本,可以用来查看windows和linux中的进程
作用:检查日志文件中是否有指定的关键字
第一个参数为日志文件名(必须有,相对路径、绝对路径均可)
第二个参数为seek position文件的路径(可选项,若不设置则默认为/tmp/logseek文件。相对路径、绝对路径均可)
第三个参数为搜索关键字,默认为 Error
[root@slave script]# ls
log.py showprocess.sh
log.py脚本使用方法
#安装一个python3 [root@slave script]# yum -y install python3 # [root@slave script]# echo -e "hehe\nxixi\njjyy" >> test.log [root@slave script]# cat test.log skhgdkjasgbdjgasdhjgsflo;iugioer hehe xixi jjyy #只没有指定关键字默认Error为关键字,显示为0表示没有关键字 [root@slave script]# python3 /script/log.py /script/test.log 0 [root@slave script]# cat /tmp/logseek #查看脚本读取的位置 48[root@slave script] [root@slave script]# echo "Error" >> test.log #把Error写进文件 [root@slave script]# python3 /script/log.py /script/test.log #执行脚本,发现关键字 1 [root@slave script]# python3 /script/log.py /script/test.log #再次执行脚本 # 这个脚本的好处就是从上次检查的最后开始检查,这样就不会将日志文件中原有的Error重复检查 0 [root@slave script]# cat /tmp/logseek #再次查看脚本读取的位置 54[root@slave script]# [root@slave script]# python3 /script/log.py /script/test.log /tmp/xxx #指定两个参数,seek position文件的路径 1 [root@slave script]# cat /tmp/xxx 54[root@slave script]# rm -rf /tmp/xxx [root@slave script]# touch /tmp/xxx #创建一个新的xxx文件 [root@slave script]# python3 /script/log.py /script/test.log /tmp/xxx failed #指定三个参数,指定关键字为failed 0 [root@slave script]# echo "failed" >> /script/test.log #给文件中添加failed [root@slave script]# python3 /script/log.py /script/test.log /tmp/xxx failed #执行脚本,发现关键字 1 [root@slave script]#
log.py脚本在配置文件中的用法
进入zabbix_agentd配置文件
[root@slave script]# vim /usr/local/etc/zabbix_agentd.conf
......
# ListenBacklog=
UserParameter=show_process[*],/bin/bash /srcipt/showprocess.sh $1
UserParameter=check_logs[*],python3 /script/log.py $1 $2 $3
# 格式: #UserParameter=<key>,<shell command> 自定义键名加上脚本名三个位置变量对应log.py的三个值
配置文件改为后重启zabbix_agentd
[root@slave script]# systemctl restart zabbix_agentd
服务端查看手动触发
# 在服务端查看效果
# 需要先将服务端的/tmp/logseek文件删除,因为其属主不为服务端用户zabbix,无法查看
[root@zabbixserver ~]# zabbix_get -s 192.168.10.132 -k check_logs['/tmp/test.log']
0
手动触发告警,向监控文件中添加Error
[root@c82 scripts]# echo "Error" >> /tmp/test.log
[root@zabbixserver ~]# zabbix_get -s 192.168.10.132 -k check_logs['/tmp/test.log']
1
[root@zabbixserver ~]# zabbix_get -s 192.168.10.132 -k check_logs['/tmp/test.log']
0
# 第二次查看又没有Error了
配置监控项
添加触发器
此时仪表盘没有报警,因为文件中没有Error
手动触发告警,向监控文件中添加Error
[root@slave tmp]# echo "Error" >> test.log
[root@slave tmp]#
添加后30s受到告警信息
下一个30s后,因为检测文件没有Error,告警自动消除
在服务端主机和客户端主机上部署MySQL主从
1、#在两台主机上选择mariadn
2、#启动mariadb服务,无密码登录mysql并设置密码
[root@slave ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 19558
Server version: 10.5.9-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)]> set password = password("Hte_666");
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> exit
由于此时刚刚安装mariadb,所以两台主机上的数据库一定是相同的,因次可以直接配置主从
# 首先在主数据库里创建一个同步账号授权给从数据库使用
MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.10.132' identified by 'Hte_666';
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)
设置主数据库的配置文件
[root@slave ~]# vim /etc/my.cnf.d/mariadb-server.cnf
//找到这个地方
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
//添加这两行
server-id = 10 //数据库服务器唯一标识符,从库的server-id值必须比主库的大,因为方便以后添加新的数据库
log-bin = mysql_bin //启用mysql_bin日志
重启mariadb
[root@zabbixserver ~]# systemctl restart mariadb
查看日志文件mysql_bin存不存在
[root@localhost ~]# ls /var/lib/mysql/ #这个mysql_bin.000001就是日志文件
aria_log.00000001 ibdata1 ibtmp1 mysql_bin.000001 mysql_upgrade_info
aria_log_control ib_logfile0 multi-master.info mysql_bin.index performance_schema
ib_buffer_pool ib_logfile1 mysql mysql.sock
查看主库状态
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql_bin.000001 | 328 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)
[root@slave ~]# vim /etc/my.cnf.d/mariadb-server.cnf
......
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
//添加这两行
server-id = 20 //数据库服务器唯一标识符,从库的server-id值必须比主库的大,因为方便以后添加新的数据库
log-bin = myrelay_bin //启用log_bin日志
重启mariadb
[root@slave ~]# systemctl restart mariadb
MariaDB [(none)]> show master status;
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| myrelay_bin.000001 | 330 | | |
+--------------------+----------+--------------+------------------+
1 row in set (0.000 sec)
MariaDB [(none)]> change master to
-> master_host='192.168.10.131',
-> master_user='repl',
-> master_password='Hte_666',
-> master_log_file='mysql_bin.000001',
-> master_log_pos=328;
Query OK, 0 rows affected (0.003 sec)
开启主从模式
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.001 sec)
查看从服务器状态
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.10.132
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 328
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
......
主库 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.000 sec) MariaDB [(none)]> create database huangtianen; Query OK, 1 row affected (0.000 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | huangtianen | | information_schema | | mysql | | performance_schema | +--------------------+ 4 rows in set (0.000 sec) 从库 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.001 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | huangtianen | | information_schema | | mysql | | performance_schema | +--------------------+ 4 rows in set (0.000 sec)
编写脚本,过滤出查看从机状态中有几个Yes,当Yes出现的次数不等于二时打印1否则就打印2
#以下操作在从库上进行
[root@slave script]# vim mysql_state.sh
#! /bin/bash
mysql_state=$(mysql -uroot -pHte_666 -e"show slave status\G;" | grep "Running:" | grep -c "Yes")
if [ $mysql_state != 2 ];then
echo "1"
else
echo "0"
fi
编写zabbix_agentd配置文件
[root@slave script]# cd /usr/local/etc/
[root@slave etc]# vim zabbix_agentd.conf
......
# ListenBacklog=
UserParameter=show_process[*],/bin/bash /srcipt/showprocess.sh $1
UserParameter=check_logs[*],python3 /script/log.py $1 $2 $3
UserParameter=mysql_state,/bin/bash /script/mysql_state.sh #添加这一行
重启zabbix_agentd服务
[root@slave etc]# systemctl restart zabbix_agentd
配置监控项
配置触发项
手动触发,打开主库机防火墙
30s后,显示告警信息
[root@slave ~]# systemctl start firewalld
编写脚本过滤出延迟信息数据
[root@slave script]# cat mysql_state.sh
#! /bin/bash
mysql_state=$(mysql -uroot -phuangtianen -e"show slave status\G;" | grep "Running:" | grep -c "Yes")
if [ $mysql_state != 2 ];then
echo "1"
else
echo "0"
fi
[root@slave script]# chmod +x mysql_delay.sh
修改配置文件
[root@slave script]# vim /usr/local/etc/zabbix_agentd.conf
......
# ListenBacklog=
UserParameter=show_process[*],/bin/bash /srcipt/showprocess.sh $1
UserParameter=check_logs[*],python3 /script/log.py $1 $2 $3
UserParameter=mysql_state,/bin/bash /script/mysql_state.sh
UserParameter=mysql_delay,/bin/bash /scripts/mysql_delay.sh
[root@slave script]# systemctl restart zabbix_agentd #添加这一行
配置监控项
配置触发项名
主从复制负担超过200,显示报警
修改配置文件
[root@slave script]# vim /usr/local/etc/zabbix_agentd.conf
......
# ListenBacklog=
UserParameter=show_process[*],/bin/bash /srcipt/showprocess.sh $1
UserParameter=check_logs[*],python3 /script/log.py $1 $2 $3
UserParameter=mysql_state,/bin/bash /script/mysql_state.sh
UserParameter=mysql_delay,/bin/bash /scripts/mysql_delay.sh
[root@slave script]# systemctl restart zabbix_agentd #添加这一行
配置监控项
[外链图片转存中…(img-rubUdbB4-1696628290322)]
配置触发项名
[外链图片转存中…(img-PWvMcyCy-1696628290322)]
主从复制负担超过200,显示报警
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。