Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the statistical script for python log analysis

Shulou Source: shulou.com Published: 2022-06-03 05:38:25 10月19日 Update

In this issue, the editor will bring you about the statistical script of python log analysis. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

The script is similar to the tail function of shell. It is used to count the number of status codes of 4xx and 5xx per minute, the number of php execution time less than 1 second, 1-5 seconds, and more than 5 seconds, and the concurrent requests per second. Put the results in the / tmp directory, or you can draw the results with cacti.

#! / usr/bin/env pythonimport time, os#--log_file ='/ var/log/httpd/cmi_access_log'#--log_name = log_file.split ('/') [- 1] file = open (log_file,'r') lt1,to1_5,gt5,status4 Status5,concurrent = 0TIME2 = '0'st_results = os.stat (log_file) st_size = st_results [6] file.seek (st_size) size = os.path.getsize (log_file) while 1: Time = time.strftime ("% Y_%m_%d" Time.localtime (time.time ()-60) where = file.tell () line = file.readline () L = line.split ('") Time1 = time.strftime ("% Y_%m_%d_%H:%M " Time.localtime (time.time ()-60) if not line: size = os.path.getsize (log_file) if size < where: file = open (log_file) 'r') else: time.sleep (1) file.seek (where) else: try: Phptime = float (L [- 2]) url = L [1] status = L [2] [1:4] if'4' in status [0]: status4 + = 1 elif'5' in status [0]: status5 + = 1 if Time1 in Time2: if 'f5.php' not in url: concurrent + = 1 if Phptime < 1: Lt1 + = 1 elif 1 F Out F.close () Time2 = Time1 lt1,to1_5,gt5,status4,status5,concurrent = 0 except: pass above is the statistical script of python log analysis shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Analysis statistics scripts logs content quantity results professional small and medium rich content function that is articles time more status catalogs knowledge articles industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Xiaomi Shulou Technology MySQL Shulou Information