python分析nginx日志,每分钟nginx请求超过10ms的比例

代码如下:
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import time
import datetime
import sys
import os
import os.path
import re
import json
import socket
import requests
import subprocess


class NginxLog(object):
	def __init__(self, log_file, seek_file):
		self.log_file = log_file
		self.seek_file = seek_file


	def hostname(self):
		"""host_name: 主机名"""
		host_name = socket.gethostname()
		return host_name

	def writeSeek(self, seek):
		"""读过的游标写入临时文件"""
		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):
		"""读出新生成的日志
		# 如果第一次运行,或是删除临时文件,从头运行,否则,从上次读取之后运行
		# 0代表从头开始,1代表当前位置,2代表文件最末尾位置
		chunk: 返回一行日志
		"""

		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 = 0
		with open(self.log_file) as f:
			#记录当前最新文件游标
			f.seek(0,2) #最新游标位置
			seek_now = f.tell()
			# 读取上次读完之后的日志
			if seek_now >= seek_old:
				f.seek(seek_old,0) #从文件开头位置偏移
				chunk = f.read(seek_now - seek_old)
			#如果seek_now-seek_old小于0说明日志轮训
			else:
				f.seek(0,0)
				chunk = f.read(seek_now)

		# 将这次的游标写入临时文件
		self.writeSeek(seek_now)
		return chunk

	def Log_percent(self):
		"""获取分钟超过10ms请求数的百分比
		low_request_time: 低于10ms的请求数
		high_request_time: 高于10ms的请求数
		"""

		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)
		# 一分钟请求总数
		count = float(len(low_request_time) + len(high_request_time))

		# 超过10ms的百分比
		if count:
			result = float(len(high_request_time))/count
			
			#只取分子
			percent = int(result * 100)
			return percent
		else:
			return 0	#当一分钟请求数为0时,返回0

	def push_falcon(self, data, url):
		"""数据推送到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_data
		res = requests.post("http://127.0.0.1:1988/v1/push", data=json_data)			

def main():
	# 日志文件位置
	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()


猜你喜欢

转载自blog.51cto.com/haoyonghui/2121694