首页 > 代码库 > python 正则断言

python 正则断言


#!/usr/bin/python

import re

import os

import urllib

import json


def buffer_line(monitor_log):

    buf = open("/etc/sae/apns_buffer.txt","r").read()

    bytes=os.popen(‘wc -c /data0/logs/%s‘%monitor_log).read().split(" ")[0]

    if int(bytes) < int(buf):

        return 0

    else:

        return int(re.findall("^\d*", buf)[0])


def set_last_pos(pos):

    open("/etc/sae/apns_buffer.txt", "w").write(str(pos))



def monitor_work(monitor_log):

    fh = open(‘/data0/logs/%s‘%monitor_log,‘r‘)

    fh.seek(buffer_line(monitor_log))

    content = fh.read()

    new_total_lines = len(content) + buffer_line(monitor_log)

    set_last_pos(new_total_lines)

    new_lines = content.split("\n")

    return new_lines



def monitor_work_1(content,grade):

    dict_1={‘service‘:‘apns_monitor‘,‘checkpoint‘:‘apns_monitor_log‘,‘title‘:content,‘content‘:content,‘cluster‘:‘public‘,‘grade‘:grade}

    params = urllib.urlencode(dict_1)

    urllib.urlopen(‘****************‘,params)



if __name__ == ‘__main__‘:

    sum=0

    list_except=[]

    list_ERROR=[]

    now_log = monitor_work(‘apns.log‘)

    for i in now_log:

        if i.find(‘count‘) >= 0:

            m=re.search(‘(?<=all msg count:)(\d.*)(?=\.)‘,i)

            f=m.group().split(‘ ‘)

            msg_all_count=f[0].split(‘.‘)[0]

            msg_scu_count=f[2].split(‘:‘)[1].split(‘.‘)[0]

            msg_spend=f[3].split(‘:‘)[1].split(‘s‘)[0]

            msg_error=f[5].split(‘:‘)[1].split(‘.‘)[0]

            msg_sec=f[-1].split(‘:‘)[1]

            print ‘msg_all_count:‘+msg_all_count,‘msg_scu_count:‘+msg_scu_count,‘msg_spend:‘+msg_spend,‘msg_error:‘+msg_error,‘msg_sec‘+msg_sec

            if int(msg_scu_count) > 0:

                if int(msg_spend)/int(msg_scu_count) > 6:

                    spend=int(msg_spend)/int(msg_scu_count)

                    monitor_work_1(‘msg_spend_error:‘+spend,‘2‘)

            if int(msg_sec) < 5:

                    monitor_work_1(‘msg_sleep_error:‘+msg_sec,‘2‘)

        elif i.find(‘ERROR‘) >= 0:

            if i.find(‘exception‘) >= 0:

                mm=re.search(‘(?<=exception: )\w.*‘,i)

                except_line=mm.group()

                list_except.append(except_line)

            else:

                mm=re.search(‘(?<=ERROR] )\w.*‘,i)

                list_ERROR.append(mm.group())

    if len(list_except) > 0:

        date_json=simplejson.dumps(list_except)

        monitor_work_1(date_json,‘1‘)

    elif len(list_ERROR) > 0:

        date_json=simplejson.dumps(list_ERROR)

        monitor_work_1(date_json,‘2‘)


本文出自 “expect批量同步数据” 博客,请务必保留此出处http://4249964.blog.51cto.com/4239964/1579997

python 正则断言