zabbix自定义监控(进程、日志文件、mysql)

本文介绍了如何使用bash脚本监控MySQL主从复制的健康状况,包括检查httpd进程、配置zabbix监控,以及创建专门的脚本来检测主从延迟。关键步骤包括设置自定义UserParameter在Zabbix Agent配置中,确保权限管理和脚本安全执行。
摘要由CSDN通过智能技术生成

自定义监控进程

//查看httpd进程,如果启动最少有一个进程
[root@localhost ~]# ps -ef|grep httpd

//获取数量,通过数量来判断是非有问题
[root@localhost ~]# ps -ef|grep -v grep | grep httpd | wc -l 

//写个脚本,判断是否有问题
[root@localhost ~]# mkdir /scripts    //创建一个放脚本的目录
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# ls
[root@localhost scripts]# chmod +x check_process.sh 
[root@localhost scripts]# 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 scripts]# ./check_process.sh 
0

// 去客户端的zabbix监控配置文件里面去加东西
[root@localhost ~]# ls /usr/local/etc/
zabbix_agentd.conf  zabbix_agentd.conf.d
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
322 UnsafeUserParameters=1    // 是否启用自定义监控项,可选值为{1|0},1是启用,0是关闭
326 #       Format: UserParameter=<key>,<shell command>   //把UserParameter=<key>,<shell command>复制到末尾
525 UserParameter=check_process[*],/scripts/check_process.sh $1
[root@localhost etc]# pkill zabbix
[root@localhost etc]# zabbix_agentd

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

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

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

MySQL主从复制

// 主库
[root@localhost ~]# yum -y install mariadb*
[root@localhost ~]# systemctl start mariadb   //开机自启
[root@localhost ~]# systemctl disable --now firewall.service   //关闭防火墙
[root@localhost ~]# getenforce 0   //关闭selinux
Disabled
[root@localhost ~]# mysql   //进入数据库
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.17-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("123");   //设置密码
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> quit
Bye

// 从库
[root@localhost ~]# yum -y install mariadb*
[root@localhost ~]# systemctl start mariadb   //开机自启
[root@localhost ~]# systemctl disable --now firewall.service   //关闭防火墙[root@slave ~]# getenforce 0   //关闭selinux
Disabled
[root@localhost ~]# mysql   //进入数据库
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.17-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("123");   //设置密码
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> quit
Bye

在主数据库创建用户给从数据库用

[root@localhost ~]# mysql -uroot -p123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.17-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 replication slave on *.* to 'zrz'@'192.168.14.12' identified by 'zrz123!';
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> quit
Bye

配置主数据库

[root@localhost ~]# vim /etc/my.cnf.d/mariadb-server.cnf 
 16 [mysqld]
 17 datadir=/var/lib/mysql
 18 socket=/var/lib/mysql/mysql.sock
 19 log-error=/var/log/mariadb/mariadb.log
 20 pid-file=/run/mariadb/mariadb.pid
 21 
 22 server-id = 10
 23 log-bin = mysql_bin

配置从

[root@localhost ~]# vim /etc/my.cnf.d/mariadb-server.cnf 
 16 [mysqld]
 17 datadir=/var/lib/mysql
 18 socket=/var/lib/mysql/mysql.sock
 19 log-error=/var/log/mariadb/mariadb.log
 20 pid-file=/run/mariadb/mariadb.pid
 21 
 22 server-id = 20
 23 relay-log = myrelay
[root@localhost ~]# systemctl restart mariadb.service 

//配置并启动主从复制
MariaDB [(none)]> change master to
    -> master_host='192.168.14.11',
    -> master_user='zrz',
    -> master_password='zrz123!',
    -> master_log_file='mysql_bin.000001',
    -> master_log_pos=328;
Query OK, 0 rows affected (0.008 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.001 sec)

从上写脚本

[root@localhost ~]# cd /scripts/
[root@localhost scripts]# touch check_replication.sh
[root@localhost scripts]# chmod +x check_replication.sh 
[root@localhost scripts]# ls
check_replication.sh

//为了让脚本更加安全,需要授权一个zabbix账户,除了要给select权限外还得给super,replication client权限
MariaDB [(none)]> grant select on *.* to 'zabbix'@'localhost' identified by 'zabbix123!';    
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> grant super,replication client on *.* to 'zabbix'@'localhost' identified by 'zabbix123!';
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec)

[root@slave scripts]# cat check_replication.sh 
#!/bin/bash

count=$(mysql -uzabbix -p'zabbix123!' -e 'show slave status\G'|grep '_Running'|grep -c 'Yes')

if [ $count -ne 2 ];then
        echo '1'
else
        echo '0'
fi

// 如果在从服务器上关闭主从,再去执行脚本,就会显示1
MariaDB [(none)]> stop slave; 
Query OK, 0 rows affected (0.002 sec)

[root@localhost scripts]# ./check_replication.sh 
1

[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf   //修改zabbix_agentd.conf配置文件
322 UnsafeUserParameters=1
525 UserParameter=check_replication,/scripts/check_replication.sh   //在配置文件末尾加上这一行
[root@localhost ~]# pkill zabbix
[root@localhost ~]# zabbix_agentd 

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

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

/ 写一个监控只从延迟的脚本
[root@localhost scripts]# chmod +x check_replication_delay.sh 
[root@localhost scripts]# cat check_replication.sh 
#!/bin/bash
  
delay=$(mysql -uzabbix -p'zabbix123!' -e 'show slave status\G'|grep 'Behind'|awk '{print $2}')
if [ $delay != NULL ];then    //如果不是NULL就打印$delay,如果是就打印0
        echo $delay
else    
        echo '0'
fi      
[root@localhost scripts]# ./check_replication_delay.sh 
0

//修改zabbix_agentd.conf配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
525 UserParameter=check_replication,/scripts/check_replication.sh
526 UserParameter=check_replication_delay,/scripts/check_replicatio    n_delay.sh
[root@localhost ~]# pkill zabbix
[root@localhost ~]# zabbix_agentd 

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值