【python练习】日志分析小工具

1、通过函数实现:

import time
def process(input1_alarm, input2_cdr):
    """
    开发一个小工具:
    对告警发生时间点前后1一个小时内(含1个小时)的进行统计和分析,并把分析结果保存到output.txt 文件内
    :param input1_alarm:  告警文件: 附件alarm.txt文件有2条告警,其中Name: Abnormal income代表的是收入异常的告警,2019-04-03 17:46:54代表的是告警发生时的时间
    :param input2_cdr: 话单文件: 每行代表一条计费,每条计费以|分割,第2个字段代表计费的时间(年月日时分秒),第24个字段代表计费结果(0为正常,其余为异常)。
    :return: 统计结果字典
    """
    # 方法1:获取告警发生时间:
    f = open(input1_alarm, 'r')
    ls = []
    for line in f:
        ls.append(line.strip('
'))
    # print(ls)
    f.close()

    # 列表中查找元素并返回索引
    index = ls.index('Occurrence Time:')
    accureTime = ls[index+1]
    print(accureTime)

    #方法2:过滤话单文件:对告警发生时间点前后1一个小时内(含1个小时)的话单

    # 将格式自字符串转换为时间戳,获取异常起始时间段
    '''
    time.mktime(tupletime):接受时间元组并返回时间戳(1970纪元后经过的浮点秒数)。
    time.strptime(str,fmt='%a %b %d %H:%M:%S %Y'):根据fmt的格式把一个时间字符串解析为时间元组。
    '''

    # 返回时间戳T1
    t1 = time.mktime(time.strptime(accureTime, '%Y-%m-%d %H:%M:%S'))

    # 返回起始时间 前后一小时 当地时间格式:
    # print(t1-3600, t1+3600)
    '''
    time.localtime([secs] 接收时间戳并返回当地时间下的时间元组t(t.tm_isdst可取0或1,取决于当地当时是不是夏令时)。
    time.strftime(fmt[,tupletime]) 接收以时间元组,并返回以可读字符串表示的当地时间,格式由fmt决定。
    '''
    start_time = time.strftime('%Y%m%d%H%M%S', time.localtime(t1 - 3600))
    print(int(start_time))
    end_time = time.strftime('%Y%m%d%H%M%S', time.localtime(t1 + 3600))
    print(int(end_time))


    # 转换话单文件为列表,获取目标时间段和对应的错误码
    cdr = open(input2_cdr)
    cdr_list = []
    cdr_time_result = []
    cdr_error_result = []
    result = {}
    for line in cdr:
        cdr_list.append(line.strip('
'))
    # print(cdr_list)
    for i in range(len(cdr_list)):
        ls_line = cdr_list[i].split('|')
        # 过滤出符合条件的时间和错误码
        if int(start_time) <= int(ls_line[1]) <= int(end_time):
            cdr_time_result.append(ls_line[1])
            cdr_error_result.append(ls_line[23])
    print(cdr_time_result)
    print(cdr_error_result)
    cdr.close()
    # 统计列表元素出现的个数,并返回字典
    for i in cdr_error_result:
        result[i] = cdr_error_result.count(i)
    print(result)
    out_result = open('output.txt', 'w')
    out_result.write(str(result))
    out_result.close()
    return result

if __name__ == '__main__':
    process('input1_alarm.txt', 'input2_cdr.txt')

2、通过类实现:

import time

"""
日志分析小工具:
对告警发生时间点前后1一个小时内(含1个小时)的进行统计和分析,并把分析结果保存到output.txt 文件内
:param input1_alarm: 告警文件: 附件alarm.txt文件有2条告警,其中Name: Abnormal income代表的是异常的告警,2019-04-03 17:46:54代表的是告警发生时的时间
:param input2_cdr: 话单文件: 每行代表一条计费,每条计费以|分割,第2个字段代表计费的时间(年月日时分秒),第24个字段代表计费结果(0为正常,其余为异常)。
:return: 统计结果字典

设计思路:
面向对象
类 Log_finder
方法1:获取告警发生时间
方法2:获取统计时间段:计算异常告警发生时间点的前后1一个小时
方法3:获取目标时间段和对应的错误码并返回结果
"""
class Log_finder():
    def __init__(self, input1_alarm, input2_cdr):
        self.alarmFile = input1_alarm
        self.cdrFile = input2_cdr
        self.alarmList = []
        self.cdr_list = []
        self.cdr_time_result = []
        self.cdr_error_result = []
        self.result = {}

    def getAlarmTime(self):
        f = open(self.alarmFile, 'r')
        for line in f:
            self.alarmList.append(line.strip('
'))
        print(self.alarmList)
        f.close()
        index = self.alarmList.index('Occurrence Time:')
        self.accureTime = self.alarmList[index + 1]
        return self.accureTime

    def getTimeRange(self, accuretime):
        t1 = time.mktime(time.strptime(accuretime, '%Y-%m-%d %H:%M:%S'))
        self.start_time = time.strftime('%Y%m%d%H%M%S', time.localtime(t1 - 3600))
        self.end_time = time.strftime('%Y%m%d%H%M%S', time.localtime(t1 + 3600))
        return self.start_time, self.end_time

    def getcdrList(self):
        cdr = open(self.cdrFile)
        for line in cdr:
            self.cdr_list.append(line.strip('
'))
        print(self.cdr_list)
        cdr.close()
        return self.cdr_list

    def getErrorList(self, cdrlist):
        self.getAlarmTime()
        self.getTimeRange(self.accureTime)
        self.getcdrList()

        for i in range(len(cdrlist)):
            ls_line = self.cdr_list[i].split('|')
            # 过滤出符合条件的时间和错误码
            if int(self.start_time) <= int(ls_line[1]) <= int(self.end_time):
                self.cdr_time_result.append(ls_line[1])
                self.cdr_error_result.append(ls_line[23])
        print(self.cdr_time_result, self.cdr_error_result)

    def OutPut(self):
        self.getErrorList(self.cdr_list)
        # 统计列表元素出现的个数,并返回字典
        for i in self.cdr_error_result:
            self.result[i] = self.cdr_error_result.count(i)
        print(self.result)
        out_result = open('output.txt', 'w')
        out_result.write(str(self.result))
        out_result.close()
        return self.result


if __name__ == '__main__':
    logResult = Log_finder('input1_alarm.txt', 'input2_cdr.txt')
    logResult.OutPut()

3、结果输出:

2019-04-03 17:46:54
20190403164654
20190403184654
['20190403164654', '20190403164655', '20190403164656', '20190403164657', '20190403164658', '20190403164659', '20190403164659', '20190403164700', '20190403164701', '20190403164702', '20190403164703', '20190403164704', '20190403164705', '20190403164706', '20190403164707', '20190403164708', '20190403164709', '20190403164710', '20190403164711', '20190403164712', '20190403164713', '20190403164714', '20190403164715', '20190403164716', '20190403164717', '20190403164718', '20190403164719', '20190403164720', '20190403164721', '20190403164722', '20190403164723', '20190403164724', '20190403164725', '20190403164726', '20190403164727', '20190403164728', '20190403164729', '20190403164730', '20190403164731', '20190403164732', '20190403164733', '20190403164734', '20190403164735', '20190403164736', '20190403164737', '20190403164738', '20190403164739', '20190403164740', '20190403164741', '20190403164742', '20190403164743', '20190403164744', '20190403164745', '20190403164746', '20190403164747', '20190403164748', '20190403164749', '20190403164750', '20190403164751', '20190403164752', '20190403164753', '20190403164754', '20190403164755', '20190403164756', '20190403164757', '20190403164758', '20190403164759', '20190403164759', '20190403174654', '20190403174655', '20190403174656', '20190403174657', '20190403174658', '20190403174659', '20190403174659', '20190403174700', '20190403174701', '20190403174702', '20190403174703', '20190403174704', '20190403174705', '20190403174706', '20190403174707', '20190403174708', '20190403174709', '20190403174710', '20190403174711', '20190403174712', '20190403174713', '20190403174714', '20190403174715', '20190403174716', '20190403174717', '20190403174718', '20190403174719', '20190403174720', '20190403174721', '20190403174722', '20190403174723', '20190403174724', '20190403174725', '20190403174726', '20190403174727', '20190403174728', '20190403174729', '20190403174730', '20190403174731', '20190403174732', '20190403174733', '20190403174734', '20190403174735', '20190403174736', '20190403174737', '20190403174738', '20190403174739', '20190403174740', '20190403174741', '20190403174742', '20190403174743', '20190403174744', '20190403174745', '20190403174746', '20190403174800', '20190403174801', '20190403174802', '20190403174803', '20190403174804', '20190403174805', '20190403174806', '20190403174807', '20190403174808', '20190403174809', '20190403174810', '20190403174811', '20190403174812', '20190403174813', '20190403174814', '20190403174815', '20190403174816', '20190403174817', '20190403174818', '20190403174819', '20190403175100', '20190403175101', '20190403175102', '20190403175103', '20190403175104', '20190403175105', '20190403175106', '20190403175107', '20190403175108', '20190403175109', '20190403175110', '20190403175203', '20190403175204', '20190403175205', '20190403175206', '20190403175207', '20190403175208', '20190403175209', '20190403175210', '20190403175211', '20190403175212', '20190403175213', '20190403175214', '20190403175215', '20190403175216', '20190403175217', '20190403175218', '20190403175219', '20190403175220', '20190403175221', '20190403175222', '20190403175223', '20190403175224', '20190403175225', '20190403180025', '20190403180026', '20190403180027', '20190403180028', '20190403180029', '20190403180030', '20190403180031', '20190403180032', '20190403180033', '20190403180034', '20190403180035', '20190403180036', '20190403180037', '20190403180038', '20190403180039', '20190403180040', '20190403180041', '20190403180042', '20190403180043', '20190403180044', '20190403180100', '20190403180101', '20190403180102', '20190403180103', '20190403180104', '20190403180105', '20190403180106', '20190403180107', '20190403180108', '20190403180109', '20190403180110', '20190403180111', '20190403180112', '20190403180113', '20190403180114', '20190403180115', '20190403184650', '20190403184651', '20190403184652', '20190403184653', '20190403184654']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '100', '100', '100', '100', '100', '126', '1002', '100', '100', '100', '100', '100', '126', '1002', '100', '100', '100', '100', '100', '126', '1002', '100', '100', '100', '100', '100', '126', '1002', '100', '100', '100', '100', '100', '126', '1002', '100', '100', '100', '100', '100', '126', '1002', '100', '100', '100', '100', '100', '126', '1002', '100', '100', '100', '100', '100', '126', '1002', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '100', '100', '100', '100', '100', '126', '1002', '100', '100', '100', '100', '100', '126', '1002', '100', '100', '100', '100', '100', '126', '1002', '100', '100', '100', '100', '100', '126', '1002', '100', '100', '100', '100', '100', '126', '1002', '100', '100', '100', '100', '100', '126', '1002', '100', '100', '100', '100', '100', '126', '1002', '100', '100', '100', '100', '100', '126', '1002', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '100', '100', '100', '100', '100', '126', '1002', '100', '100', '100', '100', '100', '126', '1002', '100', '100', '100', '100']
{'0': 87, '100': 94, '126': 18, '1002': 18}

 

原文地址:https://www.cnblogs.com/zhaoyujiao/p/15407548.html