Python analysis Nginx log, more than 10ms per minute Nginx request ratio

Source: Internet
Author: User

The code is as follows: #!/usr/bin/python# --*-- coding:utf-8 --*--import timeimport datetimeimport  sysimport osimport os.pathimport reimport jsonimport socketimport  Requestsimport subprocessclass nginxlog (object):d ef __init__ (self, log_file, seek_ File): Self.log_file = log_fileself.seek_file = seek_filedef hostname (self): "" "Host_ name:  host Name "" "Host_name = socket.gethostname () Return host_namedef writeseek (self,  Seek): "" "read cursor written to temp file" "" With open (Self.seek_file, ' W ')  as f:f.write (Time.strftime ("%y-%m-%d  %h:%m:%s ",  time.localtime (Time.time ()))  +  ' \ n ') F.write (str (seek)  + " \ n ") def  Logread (self): "" "reads the newly generated log #  if the first run, or delete the temporary file, run from the beginning, otherwise, from the last read run # 0 representative from the beginning, 1 for the current position, 2 for the file at the end of the location chunk:   returns one line of log "" "If os.path.exists (self.seek_file): With open (self.seek_file)  as f:seek_tmp  = f.readlines () seek_old  = int (Seek_tmp[1].strip ()) Else:seek_old = 0with open (self.log_file)  as f:# Records the current latest file cursor F.seek (0,2)   #最新游标位置seek_now  = f.tell () #  read the log after the last read if seek_now >=  seek_old:f.seek (seek_old,0)   #从文件开头位置偏移chunk  = f.read (seek_now - seek_old) # If Seek_now-seek_old is less than 0 description log rotation else:f.seek (0,0) chunk = f.read (Seek_now) #  Write this cursor to the temporary file Self.writeseek (seek_now) return chunkdef log_percent (self): "" gets the percentage of minutes exceeding 10ms requests Low_ request_time:  requests less than 10ms high_request_time:  higher than 10ms requests "" "Low_request_time = []high_request_ Time = []for line in self. Logread (). Split (' \ n '): Tmp_time = line.split ('   ') [ -1]if tmp_time:tmp_data = float ('%.3f '  % float (tmp_time)) Request_time = int (tmp_data * 1000) if request_ Time > 10:high_request_time.append (request_time) else:low_request_time.append (request_time) #  One minute requestTotal Count = float (len (low_request_time)  + len (high_request_time)) #  over 10ms% if  Count:result = float (Len (high_request_time))/count# only takes the molecule Percent = int (result *  return percentelse:return 0# when the number of requests for one minute is 0 o'clock, return 0def push_falcon (Self, data, url): "" " Data push to Openfalcon "" "Host = self.hostname () Current_time = int (Time.time ()) payload =  [{"endpoint":  host, "metric":  "nginx_request_percent", "timestamp":  current_time, "step":  60, "value":  data, "CounterType":  "GAUGE", "tags":  "nginx_request_percent=10ms",}]json_data= Json.dumps (payload) print json_datares = requests.post ("Http://127.0.0.1:1988/v1/push",  Data=json_data) def main ():#  log file location log_file =  "/root/access.log" seek_file =  "/ Root/seek_temp.log "url = " Http://127.0.0.1:1988/v1/push "Nginx_log = nginxlog (Log_file,seek _file) Percent = nginx_log. Log_percent () Nginx_log.push_falcon (percent,url) if __name__ ==  ' __main__ ': Main ()


Python analysis Nginx log, more than 10ms per minute Nginx request ratio

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.