zabbix监控部署之自定义监控

zabbix监控部署之自定义监控

自定义监控进程

自定义监控日志

自定义监控MySQL主从状态

自定义监控MySQL主从延迟

主机名系统版本ip地址主机zabbix版本
kiwi.server.comCentOS8192.168.234.33服务端zabbix-6.4.6
kiwi111.client.comCentOS8192.168.234.22客户端zabbix-6.4.6

1.配置自定义监控进程

1.1 在客户端打开自定义监控功能
[root@kiwi111 ~]# vim /usr/local/etc/zabbix_agentd.conf

··························略

# Mandatory: no
# Range: 0-1
# Default:
# UnsafeUserParameters=0
UnsafeUserParameters=1        ## 添加这一行

### Option: UserParameter

····················略


## 重启服务
[root@kiwi111 ~]# systemctl restart zabbix_agentd
[root@kiwi111 ~]# 
1.2 编写检查进程的脚本
## 创建一个专门用来放脚本的目录
[root@kiwi111 ~]# mkdir /kiwi_scripts
[root@kiwi111 ~]# cd /kiwi_scripts/
[root@kiwi111 kiwi_scripts]# vim process.sh
[root@kiwi111 kiwi_scripts]# cat process.sh 
#!/bin/bash

content=$(ps -ef | grep -Ev "grep|$0" | grep -c "$1")

if [ $content -eq 0 ];then
    echo 1
else
    echo 0
fi
[root@kiwi111 kiwi_scripts]# chmod +x process.sh 
[root@kiwi111 kiwi_scripts]# ./process.sh httpd
1
[root@kiwi111 kiwi_scripts]# ./process.sh zabbix
0

## 结果为1就是服务进程有问题,0则没有问题

1.3 修改zabbix配置文件

让脚本能够绑定一个key

[root@kiwi111 kiwi_scripts]# vim /usr/local/etc/zabbix_agentd.conf

································略

### Option: UserParameter
#       User-defined parameter to monitor. There can be several user-defined parameters.
#       Format: UserParameter=<key>,<shell command>
#       See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
# UserParameter=

····································略

UserParameter=check_process[*],/bin/bash /kiwi_scripts/process.sh $1  ## 在此文件加入这一行,$1表示要监控的进程,路径为脚本路径
## 此行可以在上面复制这一行UserParameter=<key>,<shell command>

## 重启服务
[root@kiwi111 kiwi_scripts]# systemctl restart zabbix_agentd
1.4 在服务端进行测试
[root@kiwi etc]# zabbix_get -s 192.168.234.22 -k check_process[mysql]
0
[root@kiwi etc]# zabbix_get -s 192.168.234.22 -k check_process[httpd]
1
[root@kiwi etc]# 
1.5 在web端添加监控项

创建监控项

image-20230926155049182

image-20230926155117098

image-20230926160239521

完成之后点添加

这样就是添加好了

image-20230926160401221

接着再去创建触发器

image-20230926160459468

image-20230926160510036

image-20230926160818206

image-20230926160934896

image-20230926160919731

填完之后点insert

image-20230926161007027

完成之后再点add

添加完成

image-20230926161102074

现在将客户端的MySQL服务给关闭

[root@kiwi111 ~]# systemctl stop mysqld
[root@kiwi111 ~]# 

成功报警

image-20230926161303388

注:这里邮件发送不了是因为我把邮箱告警给关闭了

监控完成

2.配置自定义监控日志

2.1 在客户端配置一个py脚本
## 注意:该脚本检测到一个Error之后为0,当它再检查一遍之后发现同一个Error就不会再为0

[root@kiwi111 kiwi_scripts]# vim log.py
[root@kiwi111 kiwi_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@kiwi111 kiwi_scripts]# 
[root@kiwi111 kiwi_scripts]# chmod +x log.py 
[root@kiwi111 kiwi_scripts]# 
2.2 在客户端修改配置文件

让脚本能够绑定一个key

[root@kiwi111 kiwi_scripts]# vim /usr/local/etc/zabbix_agentd.conf

······························略

UserParameter=check_process[*],/bin/bash /kiwi_scripts/process.sh $1
UserParameter=check_logs[*],python3 /kiwi_scripts/log.py $1 $2 $3   ## 添加此行


## 安装Python3
[root@kiwi111 kiwi_scripts]# yum install -y python3
## 重启服务
[root@kiwi111 kiwi_scripts]# systemctl restart zabbix_agentd
[root@kiwi111 kiwi_scripts]# 
2.3 测试
## 在客户端给权限
[root@kiwi111 ~]# chmod 755 /var/log/mysql/mysqld.log 

## 在服务端测试
[root@kiwi ~]# zabbix_get -s 192.168.234.22 -k check_logs['/var/log/mysql/mysqld.log']
0
[root@kiwi ~]# 

## 当日志文件里有Error时则为出现问题即为1
2.4 在web端配置

添加监控项

image-20230926164431404

image-20230926164446812

image-20230926164653446

这样就是添加好了

image-20230926164722179

添加触发器

image-20230926164753465

image-20230926164759735

image-20230926164936525

选择一个监控项image-20230926165002975

image-20230926165015495

将值修改为1

image-20230926165044000

添加

image-20230926165057292

添加好了

image-20230926165139964

手动添加Error进行测试

[root@kiwi111 kiwi_scripts]# echo Error >> /var/log/mysql/mysqld.log

触发成功

image-20230926165955868

监控完成

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

注:需先部署MySQL的主从服务,可以看本篇博客作者的另一个博客《MySQL主从》。在此不多做赘述

《MySQL主从》

3.1编写脚本
## 这个脚本能够查看数据库主从是否正常

[root@kiwi111 kiwi_scripts]# vim mysql_master-slave.sh
[root@kiwi111 kiwi_scripts]# cat mysql_master-slave.sh 
#!/bin/bash


count=$(mysql -uroot -p'1' -e 'show slave status\G' | grep -i 'running:' | grep -c 'Yes')

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

[root@kiwi111 kiwi_scripts]# chmod +x mysql_master-slave.sh 
[root@kiwi111 kiwi_scripts]# 
3.2 在zabbix客户端修改配置文件

让脚本能够绑定一个key

[root@kiwi111 kiwi_scripts]# vim /usr/local/etc/zabbix_agentd.conf

······························略

UserParameter=check_mysql_master-slave,/kiwi_scripts/mysql_master-slave.sh  ## 追加此行


##重启服务
[root@kiwi111 kiwi_scripts]# systemctl restart zabbix_agentd

在服务端测试

[root@kiwi ~]# zabbix_get -s 192.168.234.22 -k check_mysql_master-slave
0
[root@kiwi ~]# 

## 服务正常为0,异常为1
3.3 在zabbix web端配置

添加监控项

image-20230927093532980

image-20230927093544736

image-20230927093747444

这样就是添加好了

image-20230927093831007

添加触发器

image-20230927093903252

image-20230927093911796

image-20230927094043115

添加完成

image-20230927094134319

3.4 测试
## 在从服务器关闭主从同步
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.001 sec)

可以看到已经触发

image-20230927094823562

监控完成

4. 自定义监控MySQL主从延迟

当主从延迟达到了一定值,就会报警

《MySQL主从》

4.1 编写脚本
[root@kiwi111 kiwi_scripts]# vim mysql_delay.sh
[root@kiwi111 kiwi_scripts]# cat mysql_delay.sh 
#!/bin/bash

mysql -uroot -p'1' -e 'show slave status\G' | grep 'Seconds_Behind_Master' | awk '{print $NF}'
[root@kiwi111 kiwi_scripts]# chmod +x mysql_delay.sh
4.2 修改客户端配置文件

给脚本绑定key

[root@kiwi111 ~]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_mysql_delay,/kiwi_scripts/mysql_delay.sh      ##添加此行

##重启服务
[root@kiwi111 ~]# systemctl restart zabbix_agentd
[root@kiwi111 ~]# 

在服务端进行测试

[root@kiwi ~]# zabbix_get -s 192.168.234.22 -k check_mysql_delay
0
[root@kiwi ~]# 
4.3 在web端添加监控项与触发器

监控项

image-20230927100553483

添加成功

image-20230927100613404

触发器

image-20230927101237404

当延迟超过两百报警

4.4 手动触发进行测试

image-20230927101441053

可以看到已经报警

部署完成

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值