zabbix自定义监控

本文详细介绍了如何在Zabbix中设置自定义监控,包括开启自定义监控功能,创建脚本目录,监控进程状态,检查日志文件错误,以及监测MySQL主从状态和延迟。步骤覆盖了从编写脚本到在Zabbix服务端和客户端的测试验证。

zabbix自定义监控
开启自定义监控功能
在zabbix客户端上将zabbix_agent.conf文件中将自定义监控的功能打开

[root@localhost ~]# vi /usr/local/etc/zabbix_agentd.conf
# UnsafeUserParameters=1                //0表示此功能关闭1表示此功能打开

在两台被监控的主机上创建脚本存放目录

[root@localhost ~]#  mkdir /scripts

自定义监控进程
编写判断进程是否开启的脚本

[root@localhost ~]# vi /scripts/check_process.sh 
#!/bin/bash
process_status=$(ps -elf | grep -Ev "grep|$0" | grep -c $1)
if  [ $process_status!=0 ];then
        echo '1'
else
        echo '0'
fi

给脚本加执行权限

[root@localhost ~]# chmod +x /scripts/check_process.sh

测试zabbix用户是否能使用此脚本

[root@localhost ~]# su - zabbix -s /bin/bash
su: warning: cannot change directory to /home/zabbix: No such file or directory
[zabbix@localhost root]$ /scripts/check_process.sh httpd
1
[root@localhost ~]# /scripts/check_process.sh httpd
0

在zabbix客户端端写入此条自定义监控并且在服务端测试是否可用
客户端:

[root@localhost ~]# vi /usr/local/etc/zabbix_agentd.conf
# UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
[root@localhost ~]# pkill zabbix
[root@localhost ~]# zabbix_agentd 
[root@localhost ~]#  ss -anlt
State   Recv-Q  Send-Q    Local Address:Port      Peer Address:Port  Process  
LISTEN  0       128             0.0.0.0:111            0.0.0.0:*              
LISTEN  0       128             0.0.0.0:80             0.0.0.0:*              
LISTEN  0       32        192.168.122.1:53             0.0.0.0:*              
LISTEN  0       128             0.0.0.0:22             0.0.0.0:*              
LISTEN  0       5             127.0.0.1:631            0.0.0.0:*              
LISTEN  0       128             0.0.0.0:10050          0.0.0.0:*              
LISTEN  0       80                    *:3306                 *:*              
LISTEN  0       128                [::]:111               [::]:*              
LISTEN  0       128                [::]:22                [::]:*              
LISTEN  0       5                 [::1]:631               [::]:*

服务端:

[root@localhost ~]#  zabbix_get -s 192.168.11.137 -k check_process['httpd']
1

在zabbix页面上创建监控项
在这里插入图片描述
创建触发器
在这里插入图片描述
测试验证

[root@localhost ~]# apachectl stop
[root@localhost ~]#  ss -anlt
State   Recv-Q   Send-Q     Local Address:Port      Peer Address:Port  Process  
LISTEN  0        128              0.0.0.0:111            0.0.0.0:*              
LISTEN  0        32         192.168.122.1:53             0.0.0.0:*              
LISTEN  0        128              0.0.0.0:22             0.0.0.0:*              
LISTEN  0        5              127.0.0.1:631            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:10050          0.0.0.0:*              
LISTEN  0        80                     *:3306                 *:*              
LISTEN  0        128                 [::]:111               [::]:*              
LISTEN  0        128                 [::]:22                [::]:*              
LISTEN  0        5                  [::1]:631               [::]:*     

在这里插入图片描述
在这里插入图片描述
自定义监控日志文件
编写判断日志文件是否有报错

[root@localhost ~]#  vi /scripts/log.py 
i#!/usr/bin/python3
import sys
import re
def prePos(seekfile):
    global curpos
    try:
        cf = open(seekfile)
    except IOError:
        curpos = 0
        return curpos
    except FileNotFoundError:
    else:
        try:
            curpos = int(cf.readline().strip())
            cf.close()
            return curpos
        cf.close()
    return curpos
        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@localhost ~]# chmod +x /scripts/log.py 
[root@localhost ~]#  ll /scripts/log.py 
-rwxr-xr-x. 1 root root 3608 May 20 01:09 /scripts/log.py

测试zabbix用户是否能使用此脚本

[root@localhost ~]# dnf -y  module install  python38
[root@localhost ~]# setfacl -m u:zabbix:rx /var/log/httpd/
[root@localhost ~]#  mkdir /zabbix_item_log
[root@localhost ~]# chown -R zabbix.zabbix /zabbix_item_log/
[zabbix@localhost root]$ python3 /scripts/log.py /var/log/httpd/error_log /zabbix_item_log/logseek Failed
0

在zabbix客户端写入此条自定义监控并且在服务端测试是否可用
客户端:

[root@localhost ~]# vi /usr/local/etc/zabbix_agentd.conf
UserParameter=check_logs[*],/scripts/log.py $1 $2 $3
[root@localhost ~]#  pkill zabbix_agent
[root@localhost ~]#  zabbix_agentd 
[root@localhost ~]# ss -anlt
State   Recv-Q   Send-Q     Local Address:Port      Peer Address:Port  Process  
LISTEN  0        128              0.0.0.0:111            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:80             0.0.0.0:*              
LISTEN  0        32         192.168.122.1:53             0.0.0.0:*              
LISTEN  0        128              0.0.0.0:22             0.0.0.0:*              
LISTEN  0        5              127.0.0.1:631            0.0.0.0:*              
LISTEN  0        128              0.0.0.0:10050          0.0.0.0:*              
LISTEN  0        80                     *:3306                 *:*              
LISTEN  0        128                 [::]:111               [::]:*              
LISTEN  0        128                 [::]:22                [::]:*              
LISTEN  0        5                  [::1]:631               [::]:*    

服务端:

[root@localhost ~]# zabbix_get -s 192.168.11.137 -k check_logs['/var/log/httpd/error_log','zabbix_item_log/logseek','Failed']
0

客户端:

[root@localhost ~]# echo 'Failed' >> /var/log/httpd/error_log 

服务端:

[root@localhost ~]# zabbix_get -s 192.168.11.137 -k check_logs['/var/log/httpd/error_log','zabbix_item_log/logseek','Failed']
1

客户端:

[root@localhost ~]#  cat /zabbix_item_log/logseek 
6985

在zabbix页面上创建监控项
在这里插入图片描述
创建触发器
在这里插入图片描述
测试验证

[root@localhost ~]#  echo 'Failed' >> /var/log/httpd/error_log 

在这里插入图片描述

在这里插入图片描述

mysql主从状态的监控

|192.168.11.136| zabbix服务端server,MySQL主 |
|192.168.11.137|zabbix客户端client,MySQL从|

sever:

mysql> create user 'root'@'192.168.11.137' identified by 'root' ;
Query OK, 0 rows affected (0.03 sec)

mysql>  grant all on *.* to 'root'@'192.168.11.137';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

client:

[root@localhost ~]# vi /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/mysql_data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/mysql_data/mysql.pid
user = mysql
skip-name-resolve
server-id=2
relay-log=slave_relay_bin
[client]
user=root
password=root
~                     

编写判断主从状态的脚本
client:

[root@localhost ~]#  vi /etc/my.cnf
#!/bin/bash
status=$(mysql -e 'show slave status\G' | grep "Running: Yes" |awk '{print $2}' | grep -c 'Yes')
if  [ $status == 2 ];then
        echo '0'
else
        echo '1'
fi

给脚本加执行权限

[root@localhost ~]# chmod +x /scripts/check_mysql_replistatus.sh 

测试zabbix用户是否能使用此脚本

[root@client ~]# su - zabbix -s /bin/bash
[zabbix@client root]$ /scripts/check_mysql_replistatus.sh 
0

在zabbix客户端写入此条自定义监控并且在服务端测试是否可用
client:

[root@client ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql_replication,/scripts/check_mysql_replistatus.sh
[root@client ~]# pkill zabbix_agent
[root@client ~]# zabbix_agentd 

zabbix:

[root@zabbix ~]# zabbix_get -s 192.168.11.137 -k check_mysql_replication
0

在zabbix页面上创建监控项
在这里插入图片描述
创建触发器
在这里插入图片描述
测试验证
client:

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
             Slave_IO_Running: No
            Slave_SQL_Running: No

在这里插入图片描述
mysql主从延迟的监控
编写查看延迟的脚本

[root@client ~]# vim /scripts/check_mysql_replistatus.sh
#!/bin/bash
delay=$(mysql -e 'show slave status\G' | grep "Seconds_Behind_Master" |awk '{print $2}')
echo $delay

给脚本加执行权限

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

测试zabbix用户是否能使用此脚本

[root@client ~]# su - zabbix -s /bin/bash
[zabbix@client root]$ /scripts/check_mysql_delay.sh 

在zabbix客户端写入此条自定义监控并且在服务端测试是否可用
client:

[root@client ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql_delay,/scripts/check_mysql_delay.sh
[root@client ~]# pkill zabbix_agent
[root@client ~]# zabbix_agentd 

zabbix:

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

在zabbix页面上创建监控项

在这里插入图片描述

创建触发器
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值