zabbix自定义监控(进程、日志、主从)

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

1.监控进程

例:监控httpd的进程,首先得yum安装httpd

[root@centos8-1 ~]# yum -y install httpd
上次元数据过期检查:15:31:35 前,执行于 2021年10月07日 星期四 10时46分24秒。
依赖关系解决。
==========================================================================================================
 软件包                   架构         版本                                         仓库             大小
==========================================================================================================
[root@centos8-1 ~]# systemctl start httpd
[root@centos8-1 ~]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port            Peer Address:Port     Process      
LISTEN      0           128                    0.0.0.0:10050                0.0.0.0:*                     
LISTEN      0           128                    0.0.0.0:22                   0.0.0.0:*                     
LISTEN      0           128                          *:80                         *:*                     
LISTEN      0           128                       [::]:22                      [::]:*                     
[root@centos8-1 ~]# 

更改配置文件

[root@centos8-1 ~]# cd /usr/local/etc/
[root@centos8-1 etc]# vim zabbix_agentd.conf
# Mandatory: no
# Range: 0-1
# Default:
UnsafeUserParameters=1     //取消注释,并改为1
1.1.写脚本
[root@centos8-1 ~]# mkdir /scripts
[root@centos8-1 ~]# cd /scripts/
[root@centos8-1 scripts]# touch httpd.sh
[root@centos8-1 scripts]# chmod +x httpd.sh
[root@centos8-1 scripts]# cat httpd.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@centos8-1 scripts]# 
[root@centos8-1 scripts]# ./httpd.sh httpd
0
[root@centos8-1 scripts]# systemctl stop httpd    
[root@centos8-1 scripts]# ./httpd.sh httpd
1
[root@centos8-1 scripts]# 
1.2.修改配置文件
[root@centos8-1 etc]# vim zabbix_agentd.conf
# ListenBacklog=
UserParameter=check_httpd[*],/scripts/httpd.sh $1  //在文件最后添加改内容
[root@centos8-1 scripts]# pkill zabbix
[root@centos8-1 scripts]# zabbix_agentd    //重启

1.3.添加监控项

在这里插入图片描述

请添加图片描述

[root@node2 ~]# zabbix_get -s 192.168.100.146 -k check_httpd[httpd]
0   //在服务端查看我们得check_httpd是否正确
[root@node2 ~]# 
1.4.添加触发器

请添加图片描述
请添加图片描述
请添加图片描述

[root@centos8-1 etc]# systemctl stop httpd    //此时我们停掉httpd
[root@centos8-1 etc]# 
结果:

请添加图片描述

2.监控日志

2.1,写脚本

写脚本

[root@centos8-1 ~]# cd /scripts/
[root@centos8-1 scripts]# chmod +x log.py 
[root@centos8-1 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@centos8-1 scripts]# 
2.2.修改配置文件
[root@centos8-1 etc]# vim zabbix_agentd.conf
# ListenBacklog=
UserParameter=check_httpd[*],/scripts/httpd.sh $1
UserParameter=check_log[*],/scripts/log.py $1 $2 $3    //添加此内容
[root@centos8-1 scripts]# pkill zabbix
[root@centos8-1 scripts]# zabbix_agentd     //重启
2.3.添加监控项

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

2.4.添加触发器

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

结果:
[root@centos8-1 scripts]# echo "Error" >> /var/log/httpd/error_log 
[root@centos8-1 scripts]# ./log.py /var/log/httpd/error_log 
1
[root@centos8-1 scripts]# 

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lfei5120

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值