Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the statistical script for python log analysis

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report