#!/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 ')
This article is from the "Expect batch Sync data" blog, so be sure to keep this source http://4249964.blog.51cto.com/4239964/1579997
Python Regular assertion