1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| from collections import Counter from flask import Flask,render_template from flask_limiter import Limiter from flask_limiter.util import get_remote_address import time,os
app=Flask(__name__)
nginx_log_dir = os.getenv("nginx_log_dir", default="access.log1")
nginx_log_qes = os.getenv("nginx_log_qes", default="2/minute")
def top_n_scores(n, score_dict): if len(score_dict)<10: n = len(score_dict) lot = [(k,v) for k, v in score_dict.items()] nl = [] while len(nl)<n: nl.append(max(lot, key=lambda x: x[1])) lot.remove(nl[-1]) return nl[0:n]
def time_tips(time_name): print(time_name + ":" + time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
limiter = Limiter(app=app, key_func=get_remote_address, default_limits=["30/second"])
@app.route('/nginx/<query_time>') def index(query_time): time_tips("start") with open(nginx_log_dir, "r") as f: url_list = [] log_time_list = [] requesttime_list = [] http_code_list = [] ip_list = [] for line in f: if "HTTP" in line and "2023:" + query_time in line: log_list = line.split() url_list.append(line.split('"')[3].split()[1]) log_time_list.append(log_list[3].split('[')[1]) requesttime_list.append(float(log_list[9].split('"')[1])) http_code_list.append(log_list[10]) ip_list.append(log_list[0])
time_tips("file over")
url_count = Counter(url_list) log_time_count = Counter(log_time_list) http_code_count = Counter(http_code_list) ip_count = Counter(ip_list)
time_tips("uniq over") url_top10 = top_n_scores(10,url_count) qps_top10 = top_n_scores(10,log_time_count) http_code_top10 = top_n_scores(10,http_code_count) ip_top10 = top_n_scores(10,ip_count)
time_tips("list sort over")
url_req_avg = [] for i in url_top10: num = 0.00 for index, value in enumerate(url_list): if i[0] == value: num += requesttime_list[index] url_req_avg_tuple = (i[0],round(num/i[1],3)) url_req_avg.append(url_req_avg_tuple) time_tips("url avg top10 over")
url_qps_top10 = [] for i in url_top10: time_list = [] for index, value in enumerate(url_list): if i[0] == value: time_list.append(log_time_list[index]) time_count = Counter(time_list) time_count = top_n_scores(1,time_count) time_tuple = (i[0],time_count[0][0],time_count[0][1]) url_qps_top10.append(time_tuple)
time_tips("url qps top10 over")
kwargs = { "url_top10": url_top10, "qps_top10": qps_top10, "url_req_avg": url_req_avg, "url_qps_top10": url_qps_top10, "http_code_top10": http_code_top10, "ip_top10": ip_top10 }
time_tips("over")
return render_template("index.html", **kwargs) if __name__=="__main__": app.run(port=8080,host="0.0.0.0")
|