日志文件报警监控脚本(可用于zabbix监控文件)测试中...

因业务要求,需要对某些日志文件中出现的关键字进行监控,所以写了个脚本用于直接用zabbix调用并返回超出阈值的监控项。

主要用来替代zabbix自带的文件监控项。可以对一台机器上的多个日志文件,多个触发阈值进行监控。不用配置多条zabbix监控项及触发器。

也许将来会逐步增加其他奇怪的监控内容

脚本代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: ColinShi

import sys, os
import datetime
import time
import re


class Log_File_Check(object):
    def __init__(self, filename, error_dict):
        self.filename = filename
        self.tmpfilename = '/tmp/'+self.filename.replace('/', '') + '_st_size.tmp'
        self.error_dict = error_dict
        self.res = {}
    def check_st_size(self):
        if os.path.exists(self.tmpfilename):
            with open(self.tmpfilename, 'r') as f:
                pre_size = f.read()
                pre_size.replace('\n', '')
                pre_size = int(pre_size)
            if self.st_size < pre_size:
                return 0
            else:
                return self.st_size
        else:
            return 0

    def write_st_size(self):
        with open(self.tmpfilename, 'w') as f:
            f.write(str(self.st_size))

    def check_content(self):
        if not os.path.exists(self.filename):
            return
        with open(self.filename, 'r') as file:
            st_results = os.stat(self.filename)
            self.st_size = st_results[6]
            pre_size = self.check_st_size()
            file.seek(pre_size)
            content = file.read()
            content = content.replace('\n', '')
        for i in self.error_dict:
            pattern = re.compile(i[0])
            pattern.fullmatch()
            if int(i[1]) <= len(pattern.findall(content)):
                if self.res.get(self.filename):
                    self.res[self.filename].append(i[0])
                else:
                    self.res[self.filename] = []
                    self.res[self.filename].append(i[0])
        self.write_st_size()
        return self.res

    def start_check(self):
        return self.check_content()


def do_error_dict(filename):
    tmpdic = {}
    with open(os.path.dirname(os.path.realpath(sys.argv[0])) + '/' + filename, 'r') as f:
        for i in f.readlines():
            i = i.replace('\n', '')
            i = i.split('||')
            if len(i) == 4:
                i[0] = random_time(i[0], i[-1], '')
            elif len(i) == 5:
                i[0] = random_time(i[0], i[-2], i[-1])
            if i[0] in tmpdic.keys():
                tmpdic[i[0]].append([i[1], i[2]])
            else:
                tmpdic[i[0]] = []
                tmpdic[i[0]].append([i[1], i[2]])
    return tmpdic


def random_time(filename, date_time, ex_name):
    now_time = datetime.datetime.now().strftime(date_time)
    return filename + now_time + ex_name


if __name__ == '__main__':
    '''
    接收1个参数:
    参数一必选参数:需要监控的日志文件的list
    '''
    result = []
    file = sys.argv[1]  # r"/tmp/logdir/odp_locic/log/rpc/rpc-request.log.wf."
    for k, v in do_error_dict(file).items():
        log_check = Log_File_Check(k, v)
        f = log_check.start_check()
        if f:
            result.append(f)
    if result == []:
        print 'ok'
    else:
        print 'Threshold exceeded:', result  

  配置文件格式(前三个为必填参数,后两个为可选,可以同时监控多个日志文件,或者多条触发阈值报警):

一般使用:
被监控文件名称||
需要匹配行的正则表达式||阈值次数
示例:
/logdir/odp_locic/log/order/order.log||^(.*)retry=0/0(.*)MQ_EXECUTE_FAILED(.*)$||10

特殊用法:
文件名前缀(或者文件全名)||需要匹配行的正则表达式||阈值次数||文件特殊格式(日期时间归档)||文件后缀
示例:
/logdir/odp_locic/log/order/order.||^(.*)retry=0/0(.*)MQ_EXECUTE_FAILED(.*)$||10||%Y%m%d%H||.log

 示例实际读取的文件为

/logdir/odp_locic/log/order/order.{当前日期小时}.log文件

备注:

这个脚本需要传入一个参数,为配置文件名称(配置文件需要和运行脚本放置在同一目录下)

该脚本只适用于linux操作系统,并且会在/tmp目录下生成相应的临时文件(日志路径+文件名_st_size.tmp),用于记录上次日志采集位置。

返回ok表示未触发阈值

返回其他表示相应的日志文件,正则表达式被触发。

猜你喜欢

转载自www.cnblogs.com/colinshi/p/10364657.html
今日推荐