Erlo

zabbix自定义监控(服务进程和日志)

2022-09-07 15:00:02 发布   151 浏览  
页面报错/反馈
收藏 点赞

zabbix自定义监控

自定义监控进程

测试监控httpd,需要在监控端部署httpd,以方便监控

配置监控脚本

#在监控端
[root@localhost ~]# dnf -y install httpd
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# ss -anlt | grep 80
LISTEN 0      128                *:80               *:*   

#编写脚本
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# vim /scripts/check_process.sh
#!/bin/bash

count=$(ps -ef|grep -Ev "grep|$0" | grep $1 | wc -l)
if [  $count -eq 0 ];then
        echo '1'
else
        echo '0'
fi
[root@localhost ~]# chmod +x /scripts/check_process.sh 
[root@localhost ~]# ll /scripts/check_process.sh
-rwxr-xr-x. 1 root root 119 Sep  6 21:09 /scripts/check_process.sh

#在监控端,修改配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1
UnsafeUserParameters=check_process[*],/bin/bash /scripts/check_process.sh $1

#服务端检查key是否可用
[root@zabbix ~]# zabbix_get -s 192.168.111.137 -k check_process[httpd]
0
[root@zabbix ~]# zabbix_get -s 192.168.111.137 -k check_process[mysql]
1
[root@zabbix ~]# zabbix_get -s 192.168.111.137 -k check_process[zabbix]
0

添加监控项

image
image
image
查看监控数据
image

添加触发器

image
image
image
image

手动关闭httpd,进行测试触发报警

[root@localhost ~]# systemctl stop httpd

查看报错
image

发送邮件
image

查看邮件
image

自定义监控日志

初学者复制的脚本
忘情的log脚本日志

#由于是python的脚本,需要在python环境中
[root@localhost ~]# dnf -y install python36

#编写脚本
[root@localhost ~]# cd /etc/scripts/
[root@localhost scripts]# vim log.py
#!/usr/bin/env python3
import sys
import re

def prePos(seekfile):
    global curpos
    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 
#给httpd的error日志,添加一个错误信息
[root@localhost ~]# echo "Error" >> /var/log/httpd/error_log
#服务端测试
[root@zabbix ~]# zabbix_get -s 192.168.111.137 -k check_logs['/var/log/httpd/error_log']
1
[root@zabbix ~]# zabbix_get -s 192.168.111.137 -k check_logs['/var/log/httpd/error_log']
0

添加监控项

image
image
image

添加触发器

image
image
image
image

手动添加错误日志,触发报警

[root@localhost httpd]# echo "Error123" >> /var/log/httpd/error_log 

image

image

登录查看全部

参与评论

评论留言

还没有评论留言,赶紧来抢楼吧~~

手机查看

返回顶部

给这篇文章打个标签吧~

棒极了 糟糕透顶 好文章 PHP JAVA JS 小程序 Python SEO MySql 确认