Zabbix之自定义监控配置

Zabbix之自定义监控配置


环境说明:

环境 IP 主机名

服务端	192.168.132.135	zabbix
客户端	192.168.132.137	localhost

配置监控脚本

在客户端

//首先先更改配置文件
[root@192 ~]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=check_process[*],/bin/bash /scripts/check_process.sh $1
//重启服务
[root@192 ~]# pkill zabbix
[root@192 ~]# zabbix_agentd 
[root@192 ~]# ss -antl | grep 10050
LISTEN 0      128          0.0.0.0:10050      0.0.0.0:*   
创建脚本目录并赋予执行权限
[root@192 ~]# mkdir /scripts
[root@192 ~]# vim /scripts/check_process.sh 
[root@192 ~]# cat /scripts/check_process.sh 
#!/bin/bash
count=`ps -ef| grep $1 |grep -Ev "grep|$0" |wc -l`
if [ $count == 0 ];then
        echo "1"
else
        echo "0"
fi
[root@192 ~]# chmod +x /scripts/check_process.sh 
[root@192 ~]# ll /scripts/check_process.sh 
-rwxr-xr-x 1 root root 122 Sep  7 22:33 /scripts/check_process.sh
来到服务测试
[root@zabbix ~]# zabbix_get -s 192.168.132.137 -k check_process[httpd]
0
[root@zabbix ~]# zabbix_get -s 192.168.132.137 -k check_process[docker]
1

添加监控项

在这里插入图片描述
在这里插入图片描述
输出都为0表示没问题
在这里插入图片描述

添加触发器

在这里插入图片描述
在这里插入图片描述
手动关闭httpd服务,触发报警

[root@192 ~]# systemctl stop httpd.service 

在这里插入图片描述
并且邮箱发来警告
在这里插入图片描述

自定义监控日志

编写脚本
由于我们是初学者所以直接进入这篇文章查看
忘情道长log脚本日志

由于是python脚本,还需按照python环境
[root@192 ~]# dnf -y install python36
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:34:55 ago on Wed Sep  7 22:39:18 2022.
[root@192 ~]# vim /scripts/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@192 scripts]# chmod +x log.py 
更改配置文件类容
[root@192 scripts]# vim /usr/local/etc/zabbix_agentd.conf
UserParameter=check_logs[*],/scripts/log.py $1 $2 $3
//重启服务生效配置
[root@192 scripts]# pkill zabbix_agentd 
[root@192 scripts]# zabbix_agentd 
然后是/var/log/httpd目录没有执行权限,我们需要给他添加权限
[root@192 scripts]# chmod 755 /var/log/httpd/
[root@192 scripts]# ll  /var/log/httpd/
total 64
-rw-r--r-- 1 root root 13569 Sep  2 00:02 access_log
-rw-r--r-- 1 root root 22544 Sep  7 23:07 error_log
//给httpd的error日志,添加一个错误信息
[root@192 scripts]# echo "Error123456" >> /var/log/httpd/error_log
//去服务端检测key是否可用
[root@zabbix ~]# zabbix_get -s 192.168.132.137 -k check_logs[/var/log/httpd/error_log]
1

在web界面添加监控项

跟添加进程监控的步骤流程一致,只需改变些操作
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
手动触发警报

[root@192 scripts]# echo "Error123456" >> /var/log/httpd/error_log

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值