python 分析日志并发上报

葫芦的运维日志

下一篇 搜索 上一篇

浏览量 4510

2017/11/06 16:42


分析日志并发上报

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/11/6 下午4:42
# @Author  : BrownWang
# @Email   : 277215243@qq.com
# @File    : logtotnm2.py
# @Software: PyCharm

import os
import datetime
import threading
import sys
rootpath='/kkk/logs/bigbang_log/'
modules = os.listdir(rootpath)


def getmonitordir(modules):
    retlist = []
    for module in modules:
        path = '/kkk/logs/bigbang_log/'+module+'/monitor/'
        if os.path.isdir(path):
            retlist.append(path)
    return retlist


# 获取本时段内上一分钟的日志
def logfilterforlastminute(path):
    filename = (datetime.datetime.now() - datetime.timedelta(minutes=1)).strftime("%Y%m%d%H")
    try:
       file = open(path + filename + '.log', 'r')
    except Exception,e:
       print '未找到{0}'.format(path + filename + '.log')
       return []
    lines = file.readlines()
    timelag = (datetime.datetime.now() - datetime.timedelta(minutes=1)).strftime("%Y%m%d %H:%M")
    retlist = []
    for line in lines:
        if timelag in line:
            templist = line.split('|')[4:]
            for item in xrange(len(templist)):
                if item % 2 == 0:
                    retlist.append((templist[item], templist[item + 1].strip()))
    file.close()
    return retlist


# 上报tnm2系统
def agenttotnm2(tnm2list):
    return '|'.join([tnm2list[0], tnm2list[1], os.popen(
        "/kkk/kkk/agenttools/agent/agentRepNum " + tnm2list[0] + " " + tnm2list[1]).read().replace('\n', '')])


# 多线程并发上报tnm2系统
def multithreadtotnm2(result=True):
    threads = []
    retlist = []
    for path in getmonitordir(modules):
        if len(logfilterforlastminute(path))!=0:
            for tnm2list in logfilterforlastminute(path):
                thread = MyThread(agenttotnm2, args=(tnm2list,))
                threads.append(thread)
    for t in threads:
        t.start()
    for t in threads:
        t.join()
        if result == True:
            retlist.append(t.get_result())
    if result == True:
        return "\n".join(retlist) + '\nresults_num=' + str(len(retlist))
    return '已上报成功,如需查看返回值,请加参数 1 '

# 使得线程可通过get_result方法得到返回参数
class MyThread(threading.Thread):
    def __init__(self, func, args=()):
        super(MyThread, self).__init__()
        self.func = func
        self.args = args

    def run(self):
        self.result = self.func(*self.args)

    def get_result(self):
        try:
            return self.result  # 如果子线程不使用join方法,此处可能会报没有self.result的错误
        except Exception:
            return None



if __name__ == '__main__':

    # 关闭结果获取
    if len(sys.argv) == 2:
        switch = sys.argv[1]
        if switch == '1':
            switch = True
        else:
            switch = False
    else:
        switch = False
    results = multithreadtotnm2(result=switch)
    print results

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2021 王梓打赏联系方式 陕ICP备17005322号-1