文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

python读取日志

2023-01-31 06:40

关注

场景:周一到周五早上6点半检查日志中的关键字,并发送相应的成功或者失败短信

用python27版本实现

日志内容如下:

[16-08-04 06:30:39] Init Outer: StkID:20205 Label:7110 Name:02ͨ Type:3 PriceDigit:4 VolUnit:0 FloatIssued:                                                               0 TotalIssued:                                                               0 LastClose:0 AdvStop:0 DecStop:0
[16-08-04 06:30:39] Init Outer: StkID:20206 Label:9802 Name:982 Type:3 PriceDigit:4 VolUnit:0 FloatIssued:                                                               0 TotalIssued:                                                               0 LastClose:0 AdvStop:0 DecStop:0
[16-08-04 06:30:39] IB Recv DCT_STKSTATIC, Stock Total = 20207 Day=20160804, Ver=1470283608

配置文件如下:

[MobileNo]
user1 = num1
user2 = num2
user3 = num3

[code_IB]
keys = Stock Total
filepath = /home/level2/ib/datacollect.log
exepath = /home/level2/ib
exefile = dcib.exe
failmsg = IB init fail!
day_of_week = 0-4
hour = 06
minute = 30

python如下:

#-*- encoding: utf-8 -*-
import re
import sys
import os
import time
import requests
import ConfigParser
import logging
import thread
from logging.handlers import RotatingFileHandler
from apscheduler.schedulers.blocking import BlockingScheduler

#通过logging.basicConfig函数对日志的输出格式及方式做相关配置
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename='search.log',
                    filemode='a')

'''
#定义一个StreamHandler,将INFO级别或更高的日志信息打印到标准错误,并将其添加到当前的日志处理对象
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
'''
'''
#定义一个RotatingFileHandler,最多备份5个日志文件,每个日志文件最大10M
Rthandler = RotatingFileHandler('search.log', maxBytes=1*1024*1024,backupCount=2)
Rthandler.setLevel(logging.INFO)
#formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
#Rthandler.setFormatter(formatter)
logging.getLogger('').addHandler(Rthandler)
'''

#读取配置文件
try:
    conf = ConfigParser.ConfigParser()  #生成config对象
    conf.read("search.ini")  #用config对象读取配置文件
    #keys = conf.get("main","keys")  #指定session,option读取值
    #logpath = conf.get("main","filepath")
    mobileno = conf.items("MobileNo")
    #failmsg = conf.get("msg","fail")
    code = conf.sections()
except Exception as exc:
    pass

def getconfig(section):
    #定义全局变量
    global keys, logpath, exepath, exefile, failmsg, day_of_week, hour ,minute
    keys = conf.get(section,"keys")
    logpath = conf.get(section,"filepath")
    exepath = conf.get(section,"exepath")
    exefile = conf.get(section,"exefile")
    failmsg = conf.get(section,"failmsg")
    day_of_week = conf.get(section,"day_of_week")
    hour = conf.get(section,"hour")
    minute = conf.get(section,"minute")
    print keys, logpath, exepath, exefile, failmsg, day_of_week, hour ,minute




#从前端获取参数,关键字,文件名
'''
def getParameters():
    ret = []
    if len(sys.argv) != 3:
        print 'Please input correct parameter,for example:'
        print 'python search.py keyword filepath configpath'
    else:
        for i in range(1,len(sys.argv)):
            print i, sys.argv[i]
            ret.append(sys.argv[i])
        print '+============================================================================+'
        print '  Keyword = %s'%sys.argv[1]
    return ret
'''
def isFileExists(strfile):
    #检查文件是否存在
    return os.path.isfile(strfile)

def sendMailOk(timefile):
    #初始化正常,发送邮件
    datetimes = timefile.split('[')
    times = datetimes[1].split(']')
    code = timefile.split()
    init = [times[0],code[2],"init success!"]
    message = ' '.join(init) #使用字符串的join方法,可以把列表的各个元素连接起来
    logging.info(message)
    url = 'http:/smsNew/sendMessage.html'
    #payload = {'clientId':'804D0196-6C0D-4CEF-91E1-1BB85E0217DB','Code':'GB2312','toMobileNo':toMobileNo,'message':message}
    #r = requests.post('http:/smsNew/sendMessage.html?clientId=804D0196-6C0D-4CEF-91E1-1BB85E0217DB&Code=GB2312&toMobileNo=18516235206&message=test')
    #r = requests.post(url,params = payload)
    #print r.url,r.text

    for i in range(len(mobileno)):
        toMobileNo = mobileno[i][1]
        payload = {'clientId':'804D0196-6C0D-4CEF-91E1-1BB85E0217DB','Code':'GB2312','toMobileNo':toMobileNo,'message':message}
        r = requests.post(url,params = payload)
        print r.url,r.text
        print toMobileNo
    #print r.text
    #getConfig()

def sendMalFail():
    #初始化失败发送短信
    url = 'http:/smsNew/sendMessage.html'
    for i in range(len(mobileno)):
        toMobileNo = mobileno[i][1]
        payload = {'clientId':'804D0196-6C0D-4CEF-91E1-1BB85E0217DB','Code':'GB2312','toMobileNo':toMobileNo,'message':failmsg}
        r = requests.post(url,params = payload)
        logging.error(failmsg)
        print r.url,r.text
        print toMobileNo


def Search(keyword, filename):
    #在文件中搜索关键字
    if(isFileExists(filename) == False ):
        #print 'Input filepath is wrong,please check agin!'
        logging.error('Input filepath is wrong,please check agin!')
        return False
        #sys.exit()
    linenum = 1
    with open(filename, 'r') as fread:
        lines = fread.readlines()
        for line in lines:
            rs = re.search(keyword, line)
            if rs:
                #打印关键字所在行
                #sys.stdout.write('line:%d '%linenum)
                #print line
                lsstr = line.split(keyword)
                #strlength = len(lsstr)
                #logging.info('DC init success! ')
                sendMailOk(lsstr[0])
                '''
                #打印搜索关键字所在行信息
                for i in range(strlength):
                    if (i < (strlength - 1)):
                        sys.stdout.write(lsstr[i].strip())
                        sys.stdout.write(keyword)
                    else:
                        sys.stdout.write(lsstr[i].strip() + '\n')
                '''
                #关闭打印日志程
                killdc = "pkill " + exefile
                os.system(killdc)
                return True
                #sys.exit()
            linenum = linenum + 1
        logging.debug('DC not init ,tye agin!')
        return False

def executeSearch():
    '''
    ls = getParameters()
    if(len(ls) == 2):
        while True:
            for i in range(5):
                Search(ls[0], ls[1]) #初始化成功退出脚本,否则继续循环
                #print i
                time.sleep(60)
            sendMalFail() #连续5次查找都没有初始化,发送失败短信
    else:
        print 'There is a parameter error occured in executeSearch()!'
    '''
    #print keys,logpath,mobileno

    #os.system('cd /home/level2/ib && /bin/echo > datacollect.log && nohup ./dcib.exe > /dev/null 2>&1 &')
    startdc = "cd " + exepath + " && /bin/echo > datacollect.log && nohup ./" + exefile + " > /dev/null 2>&1 &"
    os.system(startdc)
    time.sleep(3)

    for i in range(5):
        if Search(keys,logpath)== True:
            return True
        time.sleep(60)
    sendMalFail()
    while Search(keys,logpath) == False:
        time.sleep(60)
def cron():
    scheduler = BlockingScheduler()
    scheduler.add_job(executeSearch, 'cron', day_of_week=day_of_week,hour=hour, minute=minute)
    scheduler.start()



def main():
    #scheduler = BlockingScheduler()
    for i in range(0,len(code)):
        if re.search('code',code[i]):
            getconfig(code[i])
            print "keys=",keys, "; logpath=",logpath, "; exepath=",exepath, "; exefile=",exefile, "; failmsg=",failmsg, "; day_of_week=",day_of_week, "; hour=",hour ,"; minute=",minute
            scheduler = BlockingScheduler()
            scheduler.add_job(executeSearch, 'cron', day_of_week=day_of_week,hour=hour, minute=minute)
            scheduler.start()
            #thread.start_new_thread(cron,())
            #time.sleep(3)


    #executeSearch()

if __name__=='__main__':
    main()
#    executeSearch()
阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯