zabbix自定义监控

zabbix自定义监控

监控指标

  • 系统指标
    • 内存
    • CPU
    • 硬盘
  • 文件监控
  • 网络监控
  • 硬件监控(通过IPMI实现)
    • 硬盘温度
    • 电源是否异常
    • CPU温度
  • 业务监控

自定义监控流程

  • 开启自定义监控的功能

    • 修改agentd.conf文件,修改以下2行配置
      • UnsafeUserParameters=1
      • UserParameters=key,command 格式为:UserParameter=<键值>,<命令>
  • 写脚本

  • web界面配置监控项,触发器

环境说明

环境 IP地址 主机名 需要安装的应用 系统版本
服务端 192.168.110.30 zabbix.example.com lamp架构 zabbix_server zabbix_agent redhat 8.2
客户端 192.168.110.40 zabbix-agent zabbix_agent redhat 8.2

配置服务端和客户端详情参考:zabbix监控服务实例-web界面配置第三方邮箱告警

1. 自定义监控进程

本次监控进程以httpd服务进程为例,编译安装httpd步骤详情见:利用shell脚本实现安装httpd服务

准备工作:

//客户端
#关闭防火墙和selinux
[root@zabbix-agent ~]# systemctl stop firewalld
[root@zabbix-agent ~]# setenforce 0

#通过上面给的链接使用脚本编译安装apache
[root@zabbix-agent ~]# bash httpd.sh

#取消警告,映射软连接
[root@zabbix-agent ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf
[root@zabbix-agent ~]# ln -s /usr/local/apache/bin/apachectl /usr/bin/apachectl

#启动apache服务
[root@zabbix-agent ~]# apachectl start
[root@zabbix-agent ~]# ss -antl
State     Recv-Q    Send-Q         Local Address:Port          Peer Address:Port    
LISTEN    0         128                  0.0.0.0:80                 0.0.0.0:*       
LISTEN    0         128                  0.0.0.0:22                 0.0.0.0:*       
LISTEN    0         128                  0.0.0.0:10050              0.0.0.0:*       
LISTEN    0         128                     [::]:22                    [::]:* 

第一步:编写脚本

//服务端
#创建脚本目录
[root@zabbix-agent ~]# mkdir /scripts

#编写脚本
[root@zabbix-agent ~]# vim /scripts/check_process.sh 

#以查看进程的方式,过滤apache进程,输出1表示进程有问题,0表示没问题
#!/bin/bash

count=$(ps -ef | grep -Ev "grep|$0" | grep -c "$1")
if [ $count -eq 0 ];then
        echo "1"
else
        echo "0"
fi

#给脚本执行权限
[root@zabbix-agent ~]# chmod +x /scripts/check_process.sh 
[root@zabbix-agent ~]# ll /scripts/
total 4
-rwxr-xr-x. 1 root root 118 Apr 29 00:02 check_process.sh

#测试
[root@zabbix-agent ~]# apachectl start
[root@zabbix-agent ~]# bash /scripts/check_process.sh httpd
0
[root@zabbix-agent ~]# apachectl stop
[root@zabbix-agent ~]# bash /scripts/check_process.sh httpd
1

第二步:开启自定义监控功能

//客户端
#开启自定义监控并添加指标
[root@zabbix-agent ~]# vim /usr/local/etc/zabbix_agentd.conf

# Mandatory: no
# Default:
# TLSCipherAll=

#在最后面添加以下内容
UnsafeUserParameters=1
UserParameter=check_apache,/scripts/check_process.sh httpd

#重启zabbix
[root@zabbix-agent ~]# pkill zabbix
[root@zabbix-agent ~]# zabbix_agentd 
[root@zabbix-agent ~]# 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         128                  0.0.0.0:10050              0.0.0.0:*       
LISTEN    0         128                     [::]:22                    [::]:*   

#使用服务端测试是否能获取客户端的指标
[root@zabbix ~]# zabbix_get -s 192.168.110.40 -k check_apache
1

第三步:web界面配置

  1. 添加监控项

点击Configuration ---> Hosts ---> 客户机(192.168.110.40)的Items ---> 右上角Create Items

zabbix自定义监控

  1. 添加触发器

点击 Configuration ---> Hosts ---> 客户机(192.168.110.40)的Triggers ---> 右上角Create triggers

zabbix自定义监控

  1. 配置媒介和动作

配置媒介和动作方法详情请见:zabbix监控服务-邮箱告警的三种配置方式

  1. 触发触发器
//客户端
#停止apache服务
[root@zabbix-agent ~]# apachectl stop
[root@zabbix-agent ~]# 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         128                  0.0.0.0:10050              0.0.0.0:*       
LISTEN    0         128                     [::]:22                    [::]:*  
  1. 邮箱验证

zabbix自定义监控

2. 自定义监控日志

log.py文件详情见:leidazhuang_Github

log.py作用:检查日志文件中是否有指定的关键字

  • 第一个参数为日志文件名(必须有,相对路径、绝对路径均可)
  • 第二个参数为seek position文件的路径(可选项,若不设置则默认为/tmp/logseek文件。相对路径、绝对路径均可)
  • 第三个参数为搜索关键字,默认为 Error
//客户端
#安装python36
[root@zabbix-agent ~]# yum -y install python36

#测试脚本
#监控/etc/httpd/logs/error.log文件,seek position文件为默认的/tmp/logseek,关键字为Error

3. 自定义监控mysql主从状态

环境说明

环境 IP地址 需要安装的应用 系统版本
master 192.168.110.50 mariadb redhat 8.2
slave 192.168.110.40 mariadb redhat 8.2

准备工作

//master端
#安装数据库
[root@master ~]# yum -y install mariadb*

#启动服务
[root@master ~]# systemctl enable --now mariadb

#关闭防火墙和selinux
[root@master ~]# systemctl disenable --now firewalld
Unknown operation disenable.
[root@master ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@master ~]# setenforce 0

//slave端
#安装数据库
[root@slave ~]# yum -y install mariadb*

#启动服务
[root@slave ~]# systemctl enable --now mariadb

#关闭防火墙和selinux
[root@slave ~]# systemctl disenable --now firewalld
Unknown operation disenable.
[root@slave ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@slave ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@slave ~]# setenforce 0

配置master主数据库

//master端
#测试数据库
[root@master ~]# mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.28-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)]> exit
Bye

#配置my.cnf文件
[root@master ~]# vim /etc/my.cnf
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
#最后面加下以下信息
[mysqld]
log-bin=mysql-bin
server-id=1

#重启mysql,查看状态
[root@master ~]# systemctl restart mariadb
[root@master ~]# mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-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)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      328 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)

MariaDB [(none)]> exit
Bye
[root@master ~]#

配置slave从数据库

//slave端
#测试数据库
[root@slave ~]# mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.28-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)]> exit
Bye

#配置my.cnf文件
[root@slave ~]# vim /etc/my.cnf
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
#最后添加以下信息
[mysqld]
server-id=20
relay-log=myrelay

#重启mysql,配置并启动主从复制
[root@slave ~]# mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-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)]> 
MariaDB [(none)]> change master to \
    -> master_host='192.168.110.50',
    -> master_user='repl',
    -> master_password='repl123!',
    -> 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.002 sec)

MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
                Slave_IO_State: Connecting to master
                   Master_Host: 192.168.110.50
                   Master_User: repl
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mysql-bin.000001
           Read_Master_Log_Pos: 328
                Relay_Log_File: myrelay.000001
                 Relay_Log_Pos: 4
         Relay_Master_Log_File: mysql-bin.000001
              Slave_IO_Running: Connecting
             Slave_SQL_Running: Yes
               Replicate_Do_DB: 
           Replicate_Ignore_DB: 
上一篇:lnmp项目案例搭建(sersync, nginx, php, mariadb, web3台)


下一篇:MariaDB完成C轮2700万美金融资,阿里巴巴领投