-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnginxLogAnalytic.py
More file actions
144 lines (127 loc) · 4.79 KB
/
nginxLogAnalytic.py
File metadata and controls
144 lines (127 loc) · 4.79 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/python
#coding:utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from influxdb import InfluxDBClient
import json
import os
import datetime
import time
from collections import defaultdict
import logging
import logging.handlers
import pdb
import urllib
LOG_FILE = 'nginxacesslog.log'
handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes = 1024*1024*100, backupCount = 5) # 实例化handler
fmt = '%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)s'
formatter = logging.Formatter(fmt) # 实例化formatter
handler.setFormatter(formatter) # 为handler添加formatter
logger = logging.getLogger('tst') # 获取名为tst的logger
logger.addHandler(handler) # 为logger添加handler
logger.setLevel(logging.DEBUG)
# get goaccess result on ip
def getNginxLogJson(ip):
now = datetime.datetime.now().strftime("%Y%m%dT%H%M%SZ")
reportfile="/tmp/" + now + ip + ".json"
cmd = """ssh root@%s 'cd /data/wanglei;
./nginxLogAccess.py /data/ucs-openresty/logs/ucs-api-gateway-upstream.log.1 report.json'
scp root@%s:/data/wanglei/report.json %s
"""%(ip, ip, reportfile)
logger.debug(cmd)
val = os.system(cmd)
if val != 0:
logger.error( now + " getNginxLogJson Fail " + str(val))
return None
f = open(reportfile)
jsonres = json.load(f)
logger.debug(now)
logger.debug( jsonres)
return jsonres
def sendpoint(config):
reslist = []
# {"imagename": {"cost": "total" , "success"}}
totalMap = defaultdict(lambda: defaultdict(int))
now = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
for item in config["nginx"]:
res = getNginxLogJson(item["ip"])
if res == None:
continue
# 1. add point {host:ip, image:k}
for k, v in res.items():
# pdb.set_trace()
bucketNameArr = []
print "origin image name: " + k
logger.debug( "origin image name: " + str(k))
k = urllib.unquote(k)
bucketNameArr = k.split("/")
print "decode and split: " + k, bucketNameArr
bucketNameArrLen = len(bucketNameArr)
logger.debug( "decode and split, k: " + str(k) + ",bucketNameArr:" + str(bucketNameArr) + ", bucketNameArr length:" + str(bucketNameArrLen) )
if bucketNameArrLen < 3:
logger.warn("bucketName err, imageName:" + str(k) + ", bucketNameArr:" + str(bucketNameArr))
continue
bucketName = bucketNameArr[1]
print "bucketName: " + bucketName
logger.debug( "bucketName: " + str(bucketName))
reslist.append({
"measurement": "nginx_request",
"tags": {
"host": item["ip"],
"region": item["set"],
"image": k,
"bucket": bucketName
},
"fields": {
"cost": v["cost"],
"success": float(v["success"]) / v["total"],
"total": v["total"],
"successNum": v["success"]
}
})
totalMap[k]["total"] += v["total"]
totalMap["Allimage"]["total"] += v["total"]
totalMap[k]["success"] += v["success"]
totalMap["Allimage"]["success"] += v["success"]
totalMap[k]["totalcost"] += v["cost"] * v["total"]
totalMap["Allimage"]["totalcost"] += v["cost"] * v["total"]
for k, v in totalMap.items():
# 2. add point {host:all, image:k}
# 3. 3. add point {host:all, image:all}
print "Allhost--------------->"
if "Allimage" == k:
bucketName = k
else:
bucketNameArr = []
print "origin image name: " + k
k = urllib.unquote(k)
bucketNameArr = k.split("/")
print "decode and split: " + k, bucketNameArr
bucketName = bucketNameArr[1]
print "bucketName: " + bucketName
reslist.append({
"measurement": "nginx_request",
"tags": {
"host": "Allhost",
"region": config["region"],
"image": k,
"bucket": bucketName
},
"fields": {
"cost": int(v["totalcost"] / v["total"]),
"success": float(v["success"]) / v["total"],
"total": v["total"],
"successNum": v["success"]
}
})
logger.debug(reslist)
client = InfluxDBClient(config['influxdbAddr'], config['influxdbPort'], config['username'], config['userpasswd'], config['influxdbName'])
ret = client.write_points(reslist)
logger.debug( "write_points " + str(ret))
if __name__ == '__main__':
config = json.load(open("./config.json"))
try:
sendpoint(config)
except Exception,e:
logger.exception( "sendpoint fail " )