自定义监控配置

自定义监控配置

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

自定义监控流程:

  • 开启自定义监控的功能:

在agentd.conf中设置

UnsafeUserParameters=1

UserParameters=key,command

  • 写脚本

  • 网页上配置监控项、触发器

环境说明

环境 主机名 IP地址 系统版本
服务端 zabbix 192.168.209.130 CentOS stream
客户端、数据库从库 agentd 192.168.209.131 CentOS stream
数据库主库 mysql 192.168.209.135 CentOS stream

zabbix监控部署参见zabbix部署与配置

自定义进程监控

环境配置

//写脚本+开启自定义监控功能
[root@agentd ~]# mkdir /scripts
[root@agentd ~]# vim /scripts/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@agentd ~]# chmod +x /scripts/check_process.sh

//安装并开启httpd服务测试脚本
[root@agentd ~]# yum -y install httpd
[root@agentd ~]# systemctl start httpd.service
[root@agentd ~]# . /scripts/check_process.sh httpd
0

//关闭服务测试
[root@agentd ~]# systemctl stop httpd.service 
[root@agentd ~]# . /scripts/check_process.sh httpd
1

//开启自定义监控并添加指标
[root@agentd ~]# vim /usr/local/etc/zabbix_agentd.conf
在最后一行添加以下内容

UnsafeUserParameters=1     //开启自定义监控
UserParameter=check_apache,/scripts/check_process.sh httpd   //监控指标

[root@agentd ~]# pkill zabbix_agentd
[root@agentd ~]# zabbix_agentd

//在服务端上测试监控是否生效
[root@zabbix ~]# zabbix_get -s 192.168.209.131 -k check_apache
1

网页配置

添加监控项
image
image
image

添加触发器
image
image
image

添加动作
image
image

设置邮箱告警
步骤详见zabbix邮箱告警配置

验证
开启httpd服务后再次关闭

[root@agentd ~]# systemctl start httpd
[root@agentd ~]# systemctl stop httpd

image

自定义日志监控

环境配置

写一个log.py文件,内容如下

[root@agentd scripts]# cat log.py 
#!/usr/bin/env python3
import sys
import re

def prePos(seekfile):
    global curpos
    try:
        cf = open(seekfile)
    except IOError:
        curpos = 0
        return curpos
    except FileNotFoundError:
        curpos = 0
        return curpos
    else:
        try:
            curpos = int(cf.readline().strip())
        except ValueError:
            curpos = 0
            cf.close()
            return curpos
        cf.close()
    return curpos

def lastPos(filename):
    with open(filename) as lfile:
        if lfile.readline():
            lfile.seek(0,2)
        else:
            return 0
        lastPos = lfile.tell()
    return lastPos

def getSeekFile():
    try:
        seekfile = sys.argv[2]
    except IndexError:
        seekfile = '/tmp/logseek'
    return seekfile

def getKey():
    try:
        tagKey = str(sys.argv[3])
    except IndexError:
        tagKey = 'Error'
    return tagKey

def getResult(filename,seekfile,tagkey):
    destPos = prePos(seekfile)
    curPos = lastPos(filename)

    if curPos < destPos:
        curpos = 0

    try:
        f = open(filename)
    except IOError:
        print('Could not open file: %s' % filename)
    except FileNotFoundError:
        print('Could not open file: %s' % filename)
    else:
        f.seek(destPos)

        while curPos != 0 and f.tell() < curPos:
            rresult = f.readline().strip()
            global result
            if re.search(tagkey, rresult):
                result = 1
                break
            else:
                result = 0

        with open(seekfile,'w') as sf:
            sf.write(str(curPos))
    finally:
        f.close()
    return result

if __name__ == "__main__":
    result = 0
    curpos = 0
    tagkey = getKey()
    seekfile = getSeekFile()
    result = getResult(sys.argv[1],seekfile,tagkey)
    print(result)

//添加执行权限
[root@agentd scripts]# chmod +x log.py

log.py作用:检查日志文件中是否有指定的关键字
第一个参数为日志文件名(必须有,相对路径、绝对路径均可)
第二个参数为seek position文件的路径(可选项,若不设置则默认为/tmp/logseek文件。相对路径、绝对路径均可)
第三个参数为搜索关键字,默认为 Error

//安装python36
[root@agentd scripts]# yum -y install python36

//测试脚本
监控/etc/httpd/logs/error.log文件,seek position文件为默认的/tmp/logseek,关键字为Error
[root@agentd scripts]# ./log.py /etc/httpd/logs/error_log 
0
[root@agentd scripts]# cat /tmp/logseek 
1887

[root@agentd scripts]# echo 'Error' >> /etc/httpd/logs/error_log [root@agentd scripts]# ./log.py /etc/httpd/logs/error_log 
1
[root@agentd scripts]# cat /tmp/logseek 
1893

//添加指标
[root@agentd scripts]# vim /usr/local/etc/zabbix_agentd.conf

在最下方添加如下内容
UserParameter=check_logs[*],/scripts/log.py $1 $2 $3

[root@agentd scripts]# pkill zabbix_agentd 
[root@agentd scripts]# zabbix_agentd 
[root@agentd scripts]# chmod o+x /var/log/httpd/

//用服务端测试能否获取客户端指标
[root@zabbix ~]# zabbix_get -s 192.168.209.131 -k check_logs["/etc/httpd/logs/error_log","/tmp/seek","Error"]
1
#测试完成后,删除了日志文件内的Error

网页配置

添加监控项
image
image
image

添加触发器
image
image
image

添加动作
image
image

验证

[root@agentd ~]# echo 'Error' >> /etc/httpd/logs/error_log

image

自定义mysql主从状态监控

环境准备

[root@135 ~]# yum -y install mariadb*
[root@135 ~]# systemctl enable mariadb --now
[root@135 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@135 ~]# setenforce 0
[root@135 ~]# systemctl disable firewalld.service --now
[root@agentd ~]# yum -y install mariadb*

配置mysql主从

详细配置过程请参考mysql主从

配置主库

[root@135 ~]# mysql

MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.209.131' identified by 'repl233';
Query OK, 0 rows affected (0.002 sec)

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

MariaDB [(none)]> quit
Bye

[root@135 ~]# vim /etc/my.cnf
#在最下方添加如下内容
[mysqld]
log-bin=mysql-bin
server-id=1

[root@135 ~]# systemctl restart mariadb.service 

[root@135 ~]# mysql
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@agentd ~]# vim /etc/my.cnf

[mysqld]
server-id=20
relay-log=myrelay

[root@agentd ~]# systemctl restart mariadb.service

[root@agentd ~]# mysql
MariaDB [(none)]> change master to
    -> master_host='192.168.209.135',
    -> master_user='repl',
    -> master_password='repl233',
    -> master_log_file='mysql-bin.000001',
    -> master_log_pos=328;
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: Waiting for master to send event
                   Master_Host: 192.168.209.135
                   Master_User: repl
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mysql-bin.000001
           Read_Master_Log_Pos: 328
                Relay_Log_File: myrelay.000002
                 Relay_Log_Pos: 555
         Relay_Master_Log_File: mysql-bin.000001
              Slave_IO_Running: Yes
             Slave_SQL_Running: Yes

写脚本

[root@agentd ~]# vim /scripts/check_mysql_repl.sh

#!/bin/bash

count=$(mysql -uroot -e 'show slave status\G'|grep 'Running:'|awk '{print $2}'|grep -c 'Yes')

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

[root@agentd ~]# chmod +x /scripts/check_mysql_repl.sh 

//测试
[root@agentd ~]# /scripts/check_mysql_repl.sh
0

添加指标

[root@agentd ~]# vim /usr/local/etc/zabbix_agentd.conf
在最下方添加如下内容
UserParameter=check_mysql_repl,/scripts/check_mysql_repl.sh

[root@agentd ~]# pkill zabbix_agentd
[root@agentd ~]# zabbix_agentd

//用服务端测试能否获取客户端指标
[root@zabbix ~]# zabbix_get -s 192.168.209.131 -k check_mysql_repl
0

网页配置

添加监控项
image
添加触发器
image
添加动作
image
测试

[root@agentd ~]# mysql
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> show slave status \G;
*************************** 1. row ***************************
                Slave_IO_State: 
                   Master_Host: 192.168.209.135
                   Master_User: repl
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mysql-bin.000001
           Read_Master_Log_Pos: 328
                Relay_Log_File: myrelay.000002
                 Relay_Log_Pos: 555
         Relay_Master_Log_File: mysql-bin.000001
              Slave_IO_Running: No
             Slave_SQL_Running: No

image

自定义mysql主从延迟监控

写脚本

[root@agentd ~]# vim /scripts/check_mysql_delay.sh

#!/bin/bash
  
mysql -uroot -e 'show slave status \G'|grep 'Seconds_Behind_Master:'|awk '{print $2}'

[root@agentd ~]# chmod +x /scripts/check_mysql_delay.sh 

[root@agentd ~]# mysql -uroot -e 'show slave status \G'|grep Seconds
         Seconds_Behind_Master: 0
[root@agentd ~]# /scripts/check_mysql_delay.sh
0

添加指标

[root@agentd ~]# vim /usr/local/etc/zabbix_agentd.conf
在最下方添加如下内容
UserParameter=check_mysql_delay,/scripts/check_mysql_delay.sh

[root@agentd ~]# pkill zabbix_agentd 
[root@agentd ~]# zabbix_agentd 

[root@zabbix ~]# zabbix_get -s 192.168.209.131 -k check_mysql_delay
0

网页配置

添加监控项
image
添加触发器
image
添加动作
image
测试
image

posted @ 2021-04-28 14:45  绫璟  阅读(447)  评论(0)    收藏  举报