[Python] 一个python日志监控

python-2.6

之前练手写的python日志监控,每秒显示平均Redis、平均HBase响应时间

 

#coding=utf-8
##################################################################
##FUNTION: HBase_dealtime_caculated,5mins
##DATA REGEX: P1000_101008_000000000_CCBCOM_CUSTACT_*ADD_${UDC_BIZ_DATE}_*.dest*
##@author ebda.zh
##【EBDA】基本参数提示(固定的)######################################
##参数编号:0 1 2 3 4 5 6 7
##参数:sh ebda_exec_Sh.sh flowID jobID jobName jobPath udc_Biz_date jobStatus args
##################################################################
##【EBDA】作业参数设置(EDIT)
##################################################################
##参数: arg[0] type minutes
#type: 0 -- once 1-- watch all the time,delay 10s
#minutes: like 5 minutes
import time
import sys
import threading
'''
print "HBase_dealtime_caculated,Started"
print "【EBDA_INFO】Job:"${EBDA_JOBNAME}"###STARTED!#########Date"`date +"%Y-%m-%d %H:%M:%S"`
print "【JOB_INFO】FLOWID["${EBDA_FLOWID}"]JOBID["${EBDA_JOBID}"]JOBNAME["${EBDA_JOBNAME}"]JOBPATH["${EBDA_JOBPATH}
"]UDC_BIZ_DATE["${UDC_BIZ_DATE}"]JOBSTATUS"${EBDA_JOBSTATUS}

EBDA_etc=${EBDA_DIR}/ebda_etc/ebda.env
EBDA_FLOWID=${1}
EBDA_JOBID=${2}
EBDA_JOBNAME=${3}
EBDA_JOBPATH=${4}
EBDA_UDC_BIZ_DATE=${5}
UDC_BIZ_DATE=${EBDA_UDC_BIZ_DATE}
EBDA_JOBSTATUS=${6}
HOST_USER=`whoami`
HOST_NAME=`hostname`
source ${EBDA_etc}
'''

##################################################################
##【EBDA】方法:如果程序执行出错,exit 1(固定的)
##################################################################
def hbase_deal_time_get(time_last,time_current):
File_log =open("/home/ap/user/USERLOG.log","r")
Str_hbasedealtime = "hbase deal time ["
StrLen_hbasedealtime = len(Str_hbasedealtime)
StrLen_timefilter=len(time_last)
ArrayFloat = []
ArrayFloat_Sum=0
ArrayFloat_Num=0
ArrayFloat_Max=0
ArrayFloat_Max2=0
ArrayFloat_Max3=0
ArrayFloat_Max4=0
ArrayFloat_Min=1000
ArrayFloat_Min2=1000
ArrayFloat_Min3=1000
ArrayFloat_Min4=1000
ArrayFloat_Min5=1000
for line in File_log:
if(line[0:int(StrLen_timefilter)] > time_last and line.find("hbase deal time [")!=-1):
dealTime_index=line.find("hbase deal time [")
dealTime_Num = line[dealTime_index+StrLen_hbasedealtime:-2]
#print dealTime_Num
if(float(dealTime_Num) > ArrayFloat_Max):
ArrayFloat_Max=float(dealTime_Num)
if(float(dealTime_Num) < ArrayFloat_Min):
ArrayFloat_Min=float(dealTime_Num)
ArrayFloat_Sum+=float(dealTime_Num)
ArrayFloat_Num+=1
ArrayFloat.append(float(dealTime_Num))
if(ArrayFloat_Num!=0):
print "[HBase DealTime]"+ time_last + "~" + time_current + " TPS:" + str(ArrayFloat_Num) +"tps/m Avg = " + 
str(round(ArrayFloat_Sum/ArrayFloat_Num,2)) + " ms,Max/Min:"+str(ArrayFloat_Max) + "ms/" + str(ArrayFloat_Min)+"ms"

File_log.close()

def hbase_deal_time_filter():
minutes = sys.argv[2]
time_current = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
time_last = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()-float(minutes)*60))
hbase_deal_time_get(time_last,time_current)
######################
##【STEP1】Variables Initialized
######################
#print "【EBDA_INFO】Step1:Initialize Started"

######################
##【STEP2】Data_Clear
######################
#print "【EBDA_INFO】Step2:DataClear Method Started,Status:${EBDA_JOBSTATUS} "
##【STEP3】Analysis
#print "【EBDA_INFO】Step2:DataClear Method Started,Status: ${EBDA_JOBSTATUS}"

if __name__ == '__main__':
print "hbase_deal_time_Get Started"
print "Input Args as Follows,Type:[" + sys.argv[1] + "] Minutes:[" + sys.argv[2] + "]"
str_minutes = sys.argv[2]
if( sys.argv[1] != "1"):
hbase_deal_time_filter()
else:
while True:
hbase_deal_time_filter()
time.sleep(5)

######################
##【STEP3】Variables recycle
######################
#print "【EBDA_INFO】Step4:Vars Destroy Started"

exit(0)
分类上一篇:     分类下一篇:无,已是最新文章

Leave a Reply