zabbix监控进程、日志、主从(状态、延迟)

2024-08-25 03:12

本文主要是介绍zabbix监控进程、日志、主从(状态、延迟),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

环境:rocky Linux9虚拟机四台,zabbix端为服务端,node6为客户端,node4为mariadb主,node7为mariadb从

一、zabbix监控进程

httpd服务为例

1、客户端安装httpd

[root@node6 ~]# yum -y install httpd
[root@node6 ~]# systemctl restart httpd
[root@node6 ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.//查看httpd进程信息
[root@node6 ~]# ps -ef | grep httpd
root        1091       1  0 18:25 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      1092    1091  0 18:25 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      1093    1091  0 18:25 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      1094    1091  0 18:25 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache      1095    1091  0 18:25 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root        1306     687  0 18:27 pts/0    00:00:00 grep --color=auto httpd//只查看HTTPD进程信息,过滤掉grep进程信息,并统计httpd进程行数
[root@node6 ~]# ps -ef | grep httpd | grep -v grep | wc -l
5
[root@node6 ~]# ps -ef | grep -v grep | grep -c httpd
5

2、新建脚本存放目录

[root@node6 ~]# mkdir /etc/zabbix/script
[root@node6 ~]# cd /etc/zabbix/script/
[root@node6 script]# vim 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@node6 script]# chmod +x check_httpd.sh 
[root@node6 script]# chown -R zabbix.zabbix /etc/zabbix/script///测试脚本--0是httpd服务开启,1为关闭
[root@node6 script]# ./check_httpd.sh 
0
[root@node6 script]# systemctl stop httpd
[root@node6 script]# ./check_httpd.sh 
1

3、修改客户端zabbix配置文件

[root@node6 script]# vim /etc/zabbix/zabbix_agentd.conf
//找到下面这一行,并在其下面添加一行信息UserParameter=loginusers,who | wc -lUserParameter=check_httpd,/bin/bash /etc/zabbix/script/check_httpd.sh//重启服务
[root@node6 script]# systemctl restart zabbix-agent

4、zabbxi平台配置

在这里插入图片描述
在这里插入图片描述

5、测试

在客户端将httpd服务停止

[root@node6 script]# systemctl stop httpd

在这里插入图片描述
在这里插入图片描述
恢复httpd服务运行

[root@node6 script]# systemctl restart httpd

在这里插入图片描述
在这里插入图片描述

二、自定义监控日志

下载log.py来协助我们进行测试,以httpd服务为例

1、上传配置log.py,

//下载log.py环境
[root@node6 script]# yum -y install python3
[root@node6 script]# rz -E
rz waiting to receive.
[root@node6 script]# ls
check_httpd.sh  log.py
[root@node6 script]# chmod +x log.py 
[root@node6 script]# chown zabbix.zabbix log.py //给zabbix用户对/val/log/httpd/目录及文件有读和执行的权限
[root@node6 script]# setfacl -m u:zabbix:r-x /var/log/httpd/
 log.py作用:检查日志文件中是否有指定的关键字第一个参数为日志文件名(必须有,相对路径、绝对路径均可)第二个参数为seek position文件的路径(可选项,若不设置则默认为/tmp/logseek文件。相对路径、绝对路径均可)第三个参数为搜索关键字,默认为 Error

2、修改zabbix配置文件

[root@node6 script]# vim /etc/zabbix/zabbix_agentd.conf 
//找到以下两行信息,在其下方添加一行信息
UserParameter=loginuser,who | wc -l
UserParameter=check_httpd,/bin/bash /etc/zabbix/script/check_httpd.sh
UserParameter=check_logs[*],/usr/bin/python3 /etc/zabbix/script/log.py $1 $2 $3//重启zabbix-agentd服务
[root@node6 script]# systemctl restart zabbix-agent

3、测试脚本

[root@node6 script]# python3 log.py /var/log/httpd/error_log 
0
[root@node6 script]# echo 'Error' >> /var/log/httpd/error_log 
[root@node6 script]# python3 log.py /var/log/httpd/error_log 
1
[root@node6 script]# rm -rf /tmp/logseek

0为没有Error日志信息,1为有Error日志信息

测试完成后将写入的Error内容删除,而且因文件/tmp/logseek属于root账户,在web端写入写不进去,所以删除。

4、web监控配置

模板中配置监控项
在这里插入图片描述
模板做配置触发器
在这里插入图片描述

5、测试

//插入error信息

[root@node6 script]# echo Error >> /var/log/httpd/error_log

在这里插入图片描述

//删除Error信息
[root@node6 script]# rm -rf /tmp/logseek

在这里插入图片描述

三、监控主从

主从环境:node4为主,node7为从

1、配置/etc/hosts文件

[root@zabbix ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.115 zabbix.example.com zabbix
192.168.100.116 node6.example.com node6
192.168.100.114 node4.example.com node4
192.168.100.117 node7.example.com node7
~ 
[root@zabbix ~]# scp /etc/hosts root@192.168.100.114:/etc/hosts
[root@zabbix ~]# scp /etc/hosts root@192.168.100.116:/etc/hosts
[root@zabbix ~]# scp /etc/hosts root@192.168.100.117:/etc/hosts                                  

2、安装mariadb与时钟同步

两边操作都一样

yum -y install chrony mariadb mariadb-server lrzsz
systemctl restart chronyd
systemctl enable chronyd
hwclock -w
systemctl restart mariadb
systemctl enable mariadb

3、maridb初始化

两边操作都一样

[root@node4 ~]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDBSERVERS 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    //是否允许root用户远程登录... 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!

4、更改mariadb配置文件

主库:

[root@node4 ~]# vim /etc/my.cnf
//在文件最末尾写入
[mysqld]
log_bin=mysql-bin
server_id=20[root@node4 ~]# systemctl restart mariadb

从库

[root@node7 ~]# vim /etc/my.cnf
//在文件最末尾写入
[mysqld]
log_bin=mysql-bin
server_id=30[root@node7 ~]# systemctl restart mariadb

4、配置主从

nide4主库端

[root@node4 ~]# mysql -uroot -predhat
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.22-MariaDB-log MariaDB ServerCopyright (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 "redhat";
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> grant replication slave on *.* to 'user'@'slave' identified by 'redhat';
Query OK, 0 rows affected (0.001 sec)

node7从库端

[root@node7 ~]# mysql -uroot -predhat
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.22-MariaDB-log MariaDB ServerCopyright (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 "redhat";
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> change master to master_host='master',master_user='user',master_password='redhat';
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 masterMaster_Host: masterMaster_User: userMaster_Port: 3306Connect_Retry: 60Master_Log_File: Read_Master_Log_Pos: 4Relay_Log_File: mariadb-relay-bin.000001Relay_Log_Pos: 4Relay_Master_Log_File: Slave_IO_Running: Connecting   //只需要看这两个参数Slave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 0Relay_Log_Space: 256Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULLMaster_SSL_Verify_Server_Cert: NoLast_IO_Errno: 2005Last_IO_Error: error connecting to master 'user@master:3306' - retry-time: 60  maximum-retries: 86400  message: Unknown server host 'master' (-2)Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: NoGtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: optimisticSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesSlave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0Slave_Transactional_Groups: 0
1 row in set (0.000 sec)ERROR: No query specified

5、配置主从yum源,安装zabbix客户端

两者配置一样,从库也是同样操作

[root@node4 ~]# rz -E    
rz waiting to receive.    //将zabbix包文件拖进来
[root@node4 ~]# ls
anaconda-ks.cfg  zabbix-release-7.0-2.el9.noarch.rpm
//升级更新zabbix包文件
[root@node4 ~]# 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%]
vim /etc/yum.repos.d/epel.repo 
//[epel]这个标签的末尾加上一行信息,否则安装会报错
[epel]
......
excludepkgs=zabbix*
vim /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
yum -y install zabbix-agent

6、更改zabbix配置文件,使其被监控

node4与node7主机均同样配置

[root@node4 ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.100.115
ServerActive=192.168.100.115
Hostname=slave[root@node4 ~]# systemctl restart zabbix-agent.service 
[root@node4 ~]# systemctl enable zabbix-agent.service 

7、添加主机到zabbix平台

在这里插入图片描述
在这里插入图片描述

8、在node7主机上配置脚本

[root@node7 ~]# cd /etc/zabbix/
[root@node7 zabbix]# mkdir script
[root@node7 zabbix]# cd script/
[root@node7 script]# vim mysql_slave_status.sh#!/bin/bash
USER="root"
PASSWD="redhat"
NAME=$1function 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" ];thenecho 0 elseecho 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" ];thenecho 0elseecho 1fi}case $NAME inio)IO;;sql)SQL;;*)echo -e "Usage: $0 [io | sql]"
esac[root@node7 script]# chmod +x mysql_slave_status.sh 
[root@node7 script]# chown -R zabbix.zabbix /etc/zabbix/script/
//验证脚本
[root@node7 script]# ./mysql_slave_status.sh io
0
[root@node7 script]# ./mysql_slave_status.sh sql
0

9、编写一个自配置文件,里面指定上面编写的脚本的路径,然后重启服务

[root@node7 script]# cd /etc/zabbix/zabbix_agentd.d/
[root@node7 zabbix_agentd.d]# vim userparameter_mysql_slave.conf
//在文件内写入以下内容
UserParameter=mysql.slave[*],/etc/zabbix/script/mysql_slave_status.sh $1[root@node7 zabbix_agentd.d]# systemctl restart zabbix-agent.service 

10、去zabbix server验证状态

[root@zabbix ~]# yum -y install zabbix-get
//验证的结果如果是0,为正常,如果为1,则异常
[root@zabbix ~]# zabbix_get -s 192.168.100.117 -k mysql.slave[sql]
0
[root@zabbix ~]# zabbix_get -s 192.168.100.117 -k mysql.slave[io]
0

11、给node7主机添加监控项触发器

在这里插入图片描述
在这里插入图片描述
触发器
在这里插入图片描述
在这里插入图片描述
验证

[root@node7 ~]# mysql -uroot -predhat
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 24
Server version: 10.5.22-MariaDB-log MariaDB ServerCopyright (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)]> stop slave;
Query OK, 0 rows affected (0.002 sec)

在这里插入图片描述

这篇关于zabbix监控进程、日志、主从(状态、延迟)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1104384

相关文章

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

MyBatis延迟加载与多级缓存全解析

《MyBatis延迟加载与多级缓存全解析》文章介绍MyBatis的延迟加载与多级缓存机制,延迟加载按需加载关联数据提升性能,一级缓存会话级默认开启,二级缓存工厂级支持跨会话共享,增删改操作会清空对应缓... 目录MyBATis延迟加载策略一对多示例一对多示例MyBatis框架的缓存一级缓存二级缓存MyBat

Java发送SNMP至交换机获取交换机状态实现方式

《Java发送SNMP至交换机获取交换机状态实现方式》文章介绍使用SNMP4J库(2.7.0)通过RCF1213-MIB协议获取交换机单/多路状态,需开启SNMP支持,重点对比SNMPv1、v2c、v... 目录交换机协议SNMP库获取交换机单路状态获取交换机多路状态总结交换机协议这里使用的交换机协议为常

Java 日志中 Marker 的使用示例详解

《Java日志中Marker的使用示例详解》Marker是SLF4J(以及Logback、Log4j2)提供的一个接口,它本质上是一个命名对象,你可以把它想象成一个可以附加到日志语句上的标签或戳... 目录什么是Marker?为什么使用Markejavascriptr?1. 精细化的过滤2. 触发特定操作3

linux查找java项目日志查找报错信息方式

《linux查找java项目日志查找报错信息方式》日志查找定位步骤:进入项目,用tail-f实时跟踪日志,tail-n1000查看末尾1000行,grep搜索关键词或时间,vim内精准查找并高亮定位,... 目录日志查找定位在当前文件里找到报错消息总结日志查找定位1.cd 进入项目2.正常日志 和错误日

C#使用SendMessage实现进程间通信的示例代码

《C#使用SendMessage实现进程间通信的示例代码》在软件开发中,进程间通信(IPC)是关键技术之一,C#通过调用WindowsAPI的SendMessage函数实现这一功能,本文将通过实例介绍... 目录第一章:SendMessage的底层原理揭秘第二章:构建跨进程通信桥梁2.1 定义通信协议2.2

SpringBoot日志级别与日志分组详解

《SpringBoot日志级别与日志分组详解》文章介绍了日志级别(ALL至OFF)及其作用,说明SpringBoot默认日志级别为INFO,可通过application.properties调整全局或... 目录日志级别1、级别内容2、调整日志级别调整默认日志级别调整指定类的日志级别项目开发过程中,利用日志

JWT + 拦截器实现无状态登录系统

《JWT+拦截器实现无状态登录系统》JWT(JSONWebToken)提供了一种无状态的解决方案:用户登录后,服务器返回一个Token,后续请求携带该Token即可完成身份验证,无需服务器存储会话... 目录✅ 引言 一、JWT 是什么? 二、技术选型 三、项目结构 四、核心代码实现4.1 添加依赖(pom

深度剖析SpringBoot日志性能提升的原因与解决

《深度剖析SpringBoot日志性能提升的原因与解决》日志记录本该是辅助工具,却为何成了性能瓶颈,SpringBoot如何用代码彻底破解日志导致的高延迟问题,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言第一章:日志性能陷阱的底层原理1.1 日志级别的“双刃剑”效应1.2 同步日志的“吞吐量杀手”

Linux系统管理与进程任务管理方式

《Linux系统管理与进程任务管理方式》本文系统讲解Linux管理核心技能,涵盖引导流程、服务控制(Systemd与GRUB2)、进程管理(前台/后台运行、工具使用)、计划任务(at/cron)及常用... 目录引言一、linux系统引导过程与服务控制1.1 系统引导的五个关键阶段1.2 GRUB2的进化优