zabbix监控进程、日志、主从状态和主从延迟
- 监控进程
- 1、下载服务
- 2、编写脚本
- 3、编写zabbix_agentd.conf
- 4、新建监控项配置触发器
- 5、查看邮件
- 监控日志
- 1、上传log.py的
- 2、编写zabbix_agentd.conf
- 3、新建监控项配置触发器
- 监控数据库主从状态
- 1、编写/etc/hosts(master、slave、zabbix-server、zabbix-agent)
- 2、时钟同步(master、slave)
- 3、下载mariadb(master、slave)
- 4、初始化
- 5、修改配置文件
- 6、配置主从
- 7、下载zabbix-agent包,配置yum源(slave)
- 8、编辑zabbix_agentd.conf
- 9、添加主机、创建空模板
- 10、编写脚本
- 11、编写自配置文件
- 12、验证
- 13、配置监控项、触发器、图形
- 监控数据库主从延迟
- 1、编写脚本
- 2、 配置agentd文件
- 3、验证
- 4、配置监控项、触发器
监控进程
1、下载服务
[root@zabbix-agent ~]# yum -y install httpd
[root@zabbix-agent ~]# systemctl restart httpd
[root@zabbix-agent ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
2、编写脚本
[root@zabbix-agent ~]# mkdir /etc/zabbix/script
[root@zabbix-agent ~]# cd /etc/zabbix/script/
[root@zabbix-agent script]# vim check_httpd.sh
[root@zabbix-agent script]# cat check_httpd.sh
#!/bin/bash
count=$(ps -ef | grep -Ev "grep|$0" | grep -c httpd)
if [ $count -eq 0 ];then
echo '1'
else
echo '0'
fi
[root@zabbix-agent script]# chmod +x check_httpd.sh
[root@zabbix-agent script]# chown -R zabbix.zabbix /etc/zabbix/script/
测试:
[root@zabbix-agent script]# ./check_httpd.sh
0
[root@zabbix-agent script]# systemctl stop httpd
[root@zabbix-agent script]# ./check_httpd.sh
1
[root@zabbix-agent script]# systemctl restart httpd
[root@zabbix-agent script]# ./check_httpd.sh
0
3、编写zabbix_agentd.conf
[root@zabbix-agent script]# vim /etc/zabbix/zabbix_agentd.conf
UserParameter=check_httpd,/bin/bash /etc/zabbix/script/check_httpd.sh
[root@zabbix-agent script]# systemctl restart zabbix-agent.service
4、新建监控项配置触发器
监控项
触发器
5、查看邮件
监控日志
1、上传log.py的
[root@zabbix-agent script]# ls
check_httpd.sh log.py
[root@zabbix-agent script]# yum -y install python3
[root@zabbix-agent script]# chmod +x log.py
[root@zabbix-agent script]# chown zabbix.zabbix log.py
[root@zabbix-agent script]# setfacl -m u:zabbix:r-x /var/log/httpd/
2、编写zabbix_agentd.conf
[root@zabbix-agent script]# vim /etc/zabbix/zabbix_agentd.conf
UserParameter=check_logs[*],/usr/bin/python3 /etc/zabbix/script/log.py $1 $2 $3
[root@zabbix-agent script]# systemctl restart zabbix-agent.service
测试:
[root@zabbix-agent script]# python3 log.py /var/log/httpd/error_log
0
[root@zabbix-agent script]# echo 'Error' >> /var/log/httpd/error_log
[root@zabbix-agent script]# python3 log.py /var/log/httpd/error_log
1
[root@zabbix-agent script]# rm -rf /tmp/logseek
3、新建监控项配置触发器
监控项
触发器
监控数据库主从状态
1、编写/etc/hosts(master、slave、zabbix-server、zabbix-agent)
[root@zabbix-server ~]# vim /etc/hosts
[root@zabbix-server ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.60 zabbix-server.example.com zabbix-server
192.168.100.70 zabbix-agent.example.com zabbix-agent
192.168.100.10 master.example.com master
192.168.100.20 slave.example.com slave
2、时钟同步(master、slave)
[root@slave ~]# systemctl restart chronyd
[root@slave ~]# systemctl enable chronyd
[root@slave ~]# hwclock -w
[root@slave ~]# timedatectl
Local time: Tue 2024-08-13 18:34:46 CST
Universal time: Tue 2024-08-13 10:34:46 UTC
RTC time: Tue 2024-08-13 10:34:46
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
[root@slave ~]#
3、下载mariadb(master、slave)
[root@master ~]# yum install -y mariadb mariadb-server
[root@master ~]# systemctl restart mariadb
[root@master ~]# systemctl enable mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
4、初始化
[root@master ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.
You already have your root account protected, so you can safely answer 'n'.
Switch to unix_socket authentication [Y/n] y
Enabled successfully!
Reloading privilege tables..
... Success!
You already have your root account protected, so you can safely answer 'n'.
Change the root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n
... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
[root@master ~]#
5、修改配置文件
[root@master ~]# vim /etc/my.cnf
[root@master ~]# cat /etc/my.cnf
[mysqld]
log_bin=mysql-bin
server_id=20
[root@master ~]# systemctl restart mariadb
[root@slave ~]# vim /etc/my.cnf
[root@slave ~]# cat /etc/my.cnf
[mysqld]
log_bin=mysql-bin
server_id=30
[root@slave ~]# systemctl restart mariadb
6、配置主从
[root@master ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.22-MariaDB-log 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)]> grant all privileges on *.* to root@'%' identified by "123456";
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> grant replication slave on *.* to 'user'@'slave' identified by '123456';
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]>
[root@slave ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.5.22-MariaDB-log 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)]> grant all privileges on *.* to root@'%' identified by "123456";
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> change master to master_host='master',master_user='user',master_password='123456';
Query OK, 0 rows affected (0.002 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: master
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:
Read_Master_Log_Pos: 4
Relay_Log_File: mariadb-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File:
Slave_IO_Running: Connecting
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: 0
Relay_Log_Space: 256
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1045
Last_IO_Error: error connecting to master 'user@master:3306' - retry-time: 60 maximum-retries: 86400 message: Access denied for user 'user'@'slave.example.com' (using password: YES)
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: optimistic
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Slave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
1 row in set (0.000 sec)
MariaDB [(none)]>
7、下载zabbix-agent包,配置yum源(slave)
[root@slave ~]# ls
1 anaconda-ks.cfg -e -i.bak zabbix-release-7.0-2.el9.noarch.rpm
[root@slave ~]# rpm -Uvh zabbix-release-7.0-2.el9.noarch.rpm
warning: zabbix-release-7.0-2.el9.noarch.rpm: Header V4 RSA/SHA512 Signature, key ID b5333005: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:zabbix-release-7.0-2.el9 ################################# [100%]
[root@slave ~]#
[root@slave ~]# vim /etc/yum.repos.d/zabbix.repo
[root@slave ~]# cat /etc/yum.repos.d/zabbix.repo
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/7.0/rocky/9/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-B5333005
[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/9/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-08EFA7DD
gpgcheck=1
[zabbix-sources]
name=Zabbix Official Repository source code - $basearch
baseurl=https://repo.zabbix.com/zabbix/7.0/rocky/9/SRPMS
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-B5333005
gpgcheck=1
[root@slave ~]# vim /etc/yum.repos.d/epel.repo
excludepkgs=zabbix*
[root@slave ~]# yum -y install zabbix-agent
8、编辑zabbix_agentd.conf
[root@slave ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.100.60
ServerActive=192.168.100.60
Hostname=slave
[root@slave ~]# systemctl restart zabbix-agent.service
[root@slave ~]# systemctl enable zabbix-agent.service
Created symlink /etc/systemd/system/multi-user.target.wants/zabbix-agent.service → /usr/lib/systemd/system/zabbix-agent.service.
9、添加主机、创建空模板
10、编写脚本
[root@slave ~]# mkdir script
[root@slave ~]# cd script/
[root@slave script]# vim mysql_slave_status.sh
#!/bin/bash
USER="root"
PASSWD="redhat"
NAME=$1
function IO {
Slave_IO_Running=`mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Slave_IO_Running |awk '{print $2}'`
if [ $Slave_IO_Running == "Connecting" ];then
echo 0
else
echo 1
fi
}
function SQL {
Slave_SQL_Running=`mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Slave_SQL_Running: |awk '{print $2}'`
if [ $Slave_SQL_Running == "Yes" ];then
echo 0
else
echo 1
fi
}
case $NAME in
io)
IO
;;
sql)
SQL
;;
*)
echo -e "Usage: $0 [io | sql]"
esac
[root@slave script]#
[root@slave script]# chmod +x mysql_slave_status.sh
[root@slave script]# chown -R zabbix.zabbix /etc/zabbix/script/
验证:
[root@slave zabbix]# cd script/
[root@slave script]# ./mysql_slave_status.sh sql
0
[root@slave script]# ./mysql_slave_status.sh io
0
[root@slave script]#
11、编写自配置文件
[root@slave zabbix]# cd /etc/zabbix/zabbix_agentd.d/
[root@slave zabbix_agentd.d]# vim userparameter_mysql_slave.conf
[root@slave zabbix_agentd.d]# cat userparameter_mysql_slave.conf
UserParameter=mysql.slave[*],/etc/zabbix/script/mysql_slave_status.sh $1
[root@slave zabbix_agentd.d]# chown -R zabbix.zabbix /etc/zabbix/zabbix_agentd.d/userparameter_mysql_slave.conf
[root@slave zabbix_agentd.d]# systemctl restart zabbix-agent.service
[root@slave zabbix_agentd.d]# systemctl enable zabbix-agent.service
12、验证
[root@zabbix-server ~]# yum -y install zabbix-get
[root@zabbix-server ~]# zabbix_get -s 192.168.100.20 -k mysql.slave[sql]
0
[root@zabbix-server ~]# zabbix_get -s 192.168.100.20 -k mysql.slave[io]
0
[root@zabbix-server ~]#
13、配置监控项、触发器、图形
监控项
触发器
图形
监控数据库主从延迟
1、编写脚本
[root@slave script]# vim mysql_delay.sh
[root@slave script]# cat mysql_delay.sh
#!/bin/bash
delay=$(mysql -uroot -p123456 -e 'show slave status\G' 2> /dev/null | grep 'Seconds_Behind_Master' | awk '{print $2}')
if [ $delay == "NULL" ];then
echo 0
elif [ $delay -ge 0 ] && [ $delay -le 200 ];then
echo 0
else
echo $delay
fi
[root@slave script]# chown -R zabbix.zabbix mysql_delay.sh
[root@slave script]# chmod +x mysql_delay.sh
2、 配置agentd文件
[root@slave script]# vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql_slave.conf
[root@slave script]# cat /etc/zabbix/zabbix_agentd.d/userparameter_mysql_slave.conf
UserParameter=check_mysql_delay,/bin/bash /etc/zabbix/script/mysql_delay.sh
[root@slave script]# systemctl restart zabbix-agent.service
[root@slave script]#
3、验证
[root@slave script]# ./mysql_delay.sh
0
[root@slave script]#
[root@zabbix-server ~]# zabbix_get -s 192.168.100.20 -k check_mysql_delay
0
[root@zabbix-server ~]#
4、配置监控项、触发器
监控项
触发器