-
Notifications
You must be signed in to change notification settings - Fork 2
/
haproxy_2_graphite.py
executable file
·268 lines (252 loc) · 9.12 KB
/
haproxy_2_graphite.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python
import sys
from datetime import datetime, date, timedelta, time
import subprocess
import pprint
from socket import socket
pp = pprint.PrettyPrinter(indent=4)
#
# SETTINGS
#
CARBON_SERVER = '127.0.0.1' # Your graphite server
CARBON_PORT = 2003 # Your graphite port
LOG_STATS=1 # Log the stats sent to graphite to a text file to see what is being sent
log_path="/tmp/haproxy_stats" # The file to log the stats too
test=1 # Turns off sending to graphite
one_levels=['creative', 'TOPLEVEL'] # What to report on. TOPLEVEL is '/' in the log line. All others are ignored.
#################################
################################################################
# SUBS
################################################################
def rollup():
lines=[]
sock = socket()
f = open("%s/%s" % (log_path, schema), 'a')
try:
sock.connect( (CARBON_SERVER,CARBON_PORT) )
except:
print "Couldn't connect to %(server)s on port %(port)d, is carbon-agent.py running?" % { 'server':CARBON_SERVER, 'port':CARBON_PORT }
sys.exit(1)
for key in server:
#
# Aborted
#
if key == 'aborted':
lines.append("%s.aborted %s %d" % (schema, server[key], unix_time))
continue
#
# hits per sec or min
#
if key == 'hps':
if int(delta) == 60:
key='hpm'
num=server['hps']
lines.append("%s.hpm %s %d" % (schema, num, unix_time))
continue
else:
num=server[key] / int(delta)
lines.append("%s.hps %s %d" % (schema, num, unix_time))
continue
#
# one level URL
#
if key[0] == 'one_level':
num=server[key[0], key[1]] / int(delta)
lines.append("%s.%s %s %d" % (schema, key[1], num, unix_time))
continue
if key[1] == 'lines':
hps=server[key[0],'lines'] / int(delta)
name=key[0] + '.hps'
lines.append("%s.%s %s %d" % (schema, name , hps, unix_time))
if key[1] == 'total_Tq':
avg=server[key[0], 'total_Tq'] / server[key[0], 'lines']
lines.append("%s.%s.avgTq %s %d" % (schema, key[0], avg, unix_time))
if key[1] == 'total_Tw':
avg=server[key[0], 'total_Tw'] / server[key[0], 'lines']
lines.append("%s.%s.avgTw %s %d" % (schema, key[0], avg, unix_time))
if key[1] == 'total_Tc':
avg=server[key[0], 'total_Tc'] / server[key[0], 'lines']
lines.append("%s.%s.avgTc %s %d" % (schema, key[0], avg, unix_time))
if key[1] == 'total_Tr':
avg=server[key[0], 'total_Tr'] / server[key[0], 'lines']
lines.append("%s.%s.avgTr %s %d" % (schema, key[0], avg, unix_time))
if key[1] == 'total_Tt':
avg=server[key[0], 'total_Tt'] / server[key[0], 'lines']
lines.append("%s.%s.avgTt %s %d" % (schema, key[0], avg, unix_time))
if key[1] == 'max_Tq':
lines.append("%s.%s.maxTq %s %d" % (schema, key[0], server[key], unix_time))
if key[1] == 'max_Tw':
lines.append("%s.%s.maxTw %s %d" % (schema, key[0], server[key], unix_time))
if key[1] == 'max_Tc':
lines.append("%s.%s.max_Tc %s %d" % (schema, key[0], server[key], unix_time))
if key[1] == 'max_Tr':
lines.append("%s.%s.maxTr %s %d" % (schema, key[0], server[key], unix_time))
if key[1] == 'max_Tt':
lines.append("%s.%s.maxTt %s %d" % (schema, key[0], server[key], unix_time))
message = '\n'.join(lines) + '\n' #all lines must end in a newline
if test == 0:
if LOG_STATS == 1:
f.write("sending message.....")
f.write(message)
sock.sendall(message)
#print message
else:
print message
#f.write(message)
f.write("sent.\n")
def run_total(total, new):
if (backend_srv, total) in server:
server[(backend_srv, total)]= server[(backend_srv, total)] + int(new)
else:
server[(backend_srv, total)]=int(new)
def max(max, new):
if (backend_srv, max) in server:
if int(new) > server[(backend_srv, max)]:
server[(backend_srv, max)]=int(new)
else:
server[(backend_srv, max)]=int(new)
################################################################################
#MAIN
################################################################################
try:
only_for=sys.argv[1]
delta=sys.argv[2]
check_vip=1
except:
pass
td=timedelta(seconds=+int(delta))
dtFirstLine=None
server={}
for line in sys.stdin:
#
# Parse Line
#
if 'GET' not in line and 'POST' not in line:
print "Not a GET or a POST? "
print line
continue
split_array=line.split() #split_array -- original split of line
log_time=split_array[6] #time -- the time of the event
backend=split_array[8].split("/") #the backend -- creative_backend/creative01
backend_srv=backend[1] #the adserver -- creative01
stats=split_array[9].split("/") #the stats -- 26/0/0/125/255
url_array=line.split("}") #the URL element
url=url_array[1].split()
ols=url[1].split("?")
one_level=ols[0].replace('/', '')
if one_level == '':
one_level='TOPLEVEL'
host=split_array[17].replace('{',' ').replace('|',' ').split()[0].split('.')
#print host
if len(host) > 3 or len(host) < 2: #has to be a hostname not 209.81
print 'ship: ' + host
continue
schema=host[0] + '.' + host[1] #graphite schema
try:
if check_vip == 1:
#print schema, only_for
if schema != only_for:
print "skipping " + schema, only_for
continue
except:
pass
#
#Get details of connections
#
Tq=stats[0]
Tw=stats[1]
Tc=stats[2]
Tr=stats[3]
Tt=stats[4]
if (Tw == "-1" or Tq == '-1' or Tc == '-1' or Tr == '-1' or Tt =='-1'):
if 'aborted' in server:
#f.write("ABORTED in SERVER %s" % server['aborted'])
server['aborted']= server['aborted'] + 1
else:
server['aborted']=1
continue
dtLine = datetime.strptime(log_time, "[%d/%b/%Y:%H:%M:%S.%f]")
int_unix_time=dtLine.strftime("%s")
unix_time=int(int_unix_time)
#
# Running Total
#
run_total('total_Tq', Tq)
run_total('total_Tc', Tc)
run_total('total_Tw', Tw)
run_total('total_Tr', Tr)
run_total('total_Tt', Tt)
#
# MAX's
#
max('max_Tq', Tq)
max('max_Tc', Tc)
max('max_Tw', Tw)
max('max_Tr', Tr)
max('max_Tt', Tt)
#
# Everything total
#
if 'hps' in server:
server['hps']=server['hps'] + 1
else:
server['hps']=server['hps'] = 1
if (backend_srv,'lines') in server:
server[(backend_srv,'lines')]=server[(backend_srv,'lines')] + 1
else:
server[(backend_srv,'lines')]= 1
if one_level in one_levels:
try:
server['one_level', one_level] = server['one_level',one_level] + 1
except KeyError:
server['one_level', one_level] = 1
#else:
#print "not reporting on: " + line
if dtFirstLine is None:
dtFirstLine=dtLine
dtStop=dtFirstLine + td
if dtLine > dtStop:
dtFirstLine=None
import time
now=int(time.time())
#pp.pprint(server)
rollup()
server={}
## 6 Tq '/' Tw '/' Tc '/' Tr '/' Tt* 10/0/30/69/109
#"Tq" total time in milliseconds spent waiting for the client to send a full HTTP request, not counting data.
#"Tc" total time in milliseconds spent waiting for the connection to establish to the final server, including retries.
#"Tw" total time in milliseconds spent waiting in the various queues.
#"Tr" total time in milliseconds spent waiting for the server to send a full HTTP response, not counting data.
#"Tt" is the total time in milliseconds elapsed between the accept and the last close
# 12 actconn '/' feconn '/' beconn '/' srv_conn '/' retries* 1/1/1/1/0
#f.write (server + Tq + "\n")
#0[ 'Aug',
#1 '28',
#2 '12:49:37',
#3 '10.10.106.202',
#4 'haproxy[28233]:',
#5 '174.62.109.81:54907',
#6 '[28/Aug/2012:12:49:37.487]',
#7 'frontend name',
#8 'backend name',
#9 '26/0/0/125/255',
#10 '200',
#11 '287',
#12 '-',
#13 '-',
#14 '----',
#15 '12/12/10/10/0',
# '0/0',
# 'acl name',
# '(iPad;',
# 'CPU',
# 'OS',
# '5_1_1',
# 'like',
# 'Mac',
# 'OS',
# 'X)',
# 'App}',
# '"GET',
# 'url'
# 'HTTP/1.1"']