-
Notifications
You must be signed in to change notification settings - Fork 12
/
haproxy.py
414 lines (336 loc) · 17.9 KB
/
haproxy.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
"""
*******************
*Copyright 2017, MapleLabs, All Rights Reserved.
*
********************
"""
import json
import time
import collectd
import subprocess
import re
import signal
import traceback
from collections import defaultdict
from copy import deepcopy
# user imports
import utils
from constants import *
HAPROXY_DOCS = ["frontendStats", "backendStats", "haproxyStats"]
class haproxyStats(object):
"""Plugin object will be created only once and collects utils
and available CPU/RAM usage info every interval."""
def __init__(self, interval=1, utilize_type="CPU", maximum_grep=5, process_name='*'):
"""Initializes interval."""
self.interval = DEFAULT_INTERVAL
self.socket_path = None
self.documentsTypes = []
self.pollCounter = 0
self.prev_frontend_data = {}
self.prev_backend_data = {}
self.prev_haproxy_data = {}
def config(self, cfg):
"""Initializes variables from conf files."""
for children in cfg.children:
if children.key == INTERVAL:
self.interval = children.values[0]
if children.key == DOCUMENTSTYPES:
self.documentsTypes = children.values[0]
if children.key == "socket_path":
self.socket_path = children.values[0]
def get_keys(self, key_mapping, lines):
"""Gets keys of metrics as specified by the haproxy csv stats exposed"""
key_buf = lines[0].strip("# \n").split(",")[2:]
for key in key_buf:
if key:
key_mapping.append(key)
def format_stats(self, dict_stats, lines):
"""Formats the stats exposed by haproxy stats socket to form a dictionary"""
for line in lines:
line = line.strip("\n")
if line != b'':
# print line
if line[0] == "#":
continue
buf = line.split(",")
name = buf[0] + buf[1]
dict_stats[name] = buf[:-1]
def get_frontend_data(self, key_mapping, dict_stats, haproxy_data):
"""Get data for frontend metrics"""
try:
metrics_tmp = {}
for index in dict_stats.keys():
if dict_stats[index][1] == "FRONTEND":
i = 2
for key in key_mapping:
metrics_tmp[key] = dict_stats[index][i]
if metrics_tmp[key] == '':
metrics_tmp[key] = 0
i += 1
pxname = dict_stats[index][0]
if metrics_tmp:
haproxy_data['frontendStats'][pxname] = {}
haproxy_data['frontendStats'][pxname]['pxName'] = dict_stats[index][0]
haproxy_data['frontendStats'][pxname]['reqRate'] = float(metrics_tmp['req_rate'])
haproxy_data['frontendStats'][pxname]['rate'] = float(metrics_tmp['rate'])
haproxy_data['frontendStats'][pxname]['ereq'] = int(metrics_tmp['ereq'])
haproxy_data['frontendStats'][pxname]['dreq'] = int(metrics_tmp['dreq'])
haproxy_data['frontendStats'][pxname]['hrsp_4xx'] = int(metrics_tmp['hrsp_4xx'])
haproxy_data['frontendStats'][pxname]['hrsp_5xx'] = int(metrics_tmp['hrsp_5xx'])
haproxy_data['frontendStats'][pxname]['bin'] = float(metrics_tmp['bin']) / 1024
haproxy_data['frontendStats'][pxname]['bout'] = float(metrics_tmp['bout']) / 1024
haproxy_data['frontendStats'][pxname]['sutil'] = (float(metrics_tmp['scur']) / float(metrics_tmp['slim'])) * 100
haproxy_data['frontendStats'][pxname]['_documentType'] = 'frontendStats'
self.add_common_params(haproxy_data['frontendStats'][pxname], 'frontendStats')
else:
collectd.info("Plugin haproxy: Error collecting frontend stats for %s proxy" % pxname)
return
except Exception as err:
collectd.error("Plugin haproxy: Exception in get_frontend_data due to %s" % err)
collectd.error("Traceback: %s" % traceback.format_exc())
return
def get_backend_data(self, key_mapping, dict_stats, haproxy_data):
"""Get data for backend metrics"""
try:
metrics_tmp = {}
for index in dict_stats.keys():
if dict_stats[index][1] == "BACKEND":
i = 2
for key in key_mapping:
metrics_tmp[key] = dict_stats[index][i]
if metrics_tmp[key] == '':
metrics_tmp[key] = 0
i += 1
pxname = dict_stats[index][0]
if metrics_tmp:
haproxy_data['backendStats'][pxname] = {}
haproxy_data['backendStats'][pxname]['pxName'] = pxname
haproxy_data['backendStats'][pxname]['rtime'] = int(metrics_tmp['rtime'])
haproxy_data['backendStats'][pxname]['econ'] = int(metrics_tmp['econ'])
haproxy_data['backendStats'][pxname]['dresp'] = int(metrics_tmp['dresp'])
haproxy_data['backendStats'][pxname]['eresp'] = int(metrics_tmp['eresp'])
haproxy_data['backendStats'][pxname]['qcur'] = int(metrics_tmp['qcur'])
haproxy_data['backendStats'][pxname]['qtime'] = int(metrics_tmp['qtime'])
haproxy_data['backendStats'][pxname]['wredis'] = int(metrics_tmp['wredis'])
haproxy_data['backendStats'][pxname]['wretr'] = int(metrics_tmp['wretr'])
haproxy_data['backendStats'][pxname]['hrsp_4xx'] = int(metrics_tmp['hrsp_4xx'])
haproxy_data['backendStats'][pxname]['hrsp_5xx'] = int(metrics_tmp['hrsp_5xx'])
haproxy_data['backendStats'][pxname]['_documentType'] = 'backendStats'
self.add_common_params(haproxy_data['backendStats'][pxname], 'backendStats')
else:
collectd.info("Plugin haproxy: Error collecting backend stats for %s proxy" % pxname)
return
except Exception as err:
collectd.error("Plugin haproxy: Exception in get_backend_data due to %s" % err)
collectd.error("Traceback: %s" % traceback.format_exc())
def get_haproxy_data(self, lines, haproxy_data):
"""Get general haproxy stats"""
dict_stats = {}
try:
for line in lines:
line = line.strip("\n")
if line != b'':
buf = line.split(":")
dict_stats[buf[0]] = buf[1].strip(" ")
haproxy_data['haproxyStats']['version'] = dict_stats['Version']
haproxy_data['haproxyStats']['upTime'] = int(dict_stats['Uptime_sec'])
haproxy_data['haproxyStats']['currConns'] = int(dict_stats['CurrConns'])
haproxy_data['haproxyStats']['connRate'] = int(dict_stats['ConnRate'])
haproxy_data['haproxyStats']['nbproc'] = int(dict_stats['Nbproc'])
haproxy_data['haproxyStats']['pipesUsed'] = int(dict_stats['PipesUsed'])
haproxy_data['haproxyStats']['sslCacheMisses'] = int(dict_stats['SslCacheMisses'])
haproxy_data['haproxyStats']['sslCacheLookups'] = int(dict_stats['SslCacheLookups'])
haproxy_data['haproxyStats']['sessRate'] = int(dict_stats['SessRate'])
haproxy_data['haproxyStats']['_documentType'] = "haproxyStats"
self.add_common_params(haproxy_data['haproxyStats'], 'haproxyStats')
except Exception as err:
collectd.error("Plugin haproxy: Exception in get_haproxy_data due to %s" % err)
def collect_haproxy_data(self, doc):
"""Collect haproxy data for various doc types"""
try:
haproxy_data = defaultdict(dict)
if doc == "frontendStats" or doc == "backendStats":
dict_stats = defaultdict(list)
key_mapping = []
cmnd = "echo 'show stat' | nc -U /var/lib/haproxy/stats"
process = subprocess.Popen(cmnd, shell=True, stdout=subprocess.PIPE)
lines = process.stdout.readlines()
self.get_keys(key_mapping, lines)
self.format_stats(dict_stats, lines)
self.get_frontend_data(key_mapping, dict_stats, haproxy_data)
self.get_backend_data(key_mapping, dict_stats, haproxy_data)
if doc == "haproxyStats":
cmnd = "echo 'show info' | nc -U /var/lib/haproxy/stats"
process = subprocess.Popen(cmnd, shell=True, stdout=subprocess.PIPE)
lines = process.stdout.readlines()
self.get_haproxy_data(lines, haproxy_data)
return haproxy_data
except Exception as err:
collectd.error("Plugin haproxy: Exception in collect_haproxy_data due to %s" % err)
def add_default_diff_value(self, doc_stats, doc):
"""Add default diff values for first poll"""
if doc == 'frontendStats':
keylist = ['ereq', 'dreq', 'hrsp_4xx', 'hrsp_5xx', 'bin', 'bout']
elif doc == 'backendStats':
keylist = ['econ', 'dresp', 'eresp', 'wredis', 'wretr', 'hrsp_4xx', 'hrsp_5xx']
elif doc == 'haproxyStats':
keylist = ['sslCacheMisses', 'sslCacheLookups']
for key in keylist:
doc_stats[key] = 0
def get_diff(self, key, curr_data, prev_data):
"""Get diff values"""
diff = NAN
if not prev_data:
return diff
if key not in prev_data:
collectd.error("%s key not in previous data. Shouldn't happen." % key)
return diff
if TIMESTAMP not in curr_data or TIMESTAMP not in prev_data:
collectd.error("%s key not in previous data. Shouldn't happen." % key)
return diff
curr_time = curr_data[TIMESTAMP]
prev_time = prev_data[TIMESTAMP]
if curr_time <= prev_time:
collectd.error("Current data time: %s is less than previous data time: %s. "
"Shouldn't happen." % (curr_time, prev_time))
return diff
diff = curr_data[key] - prev_data[key]
# rate can get negative if the topic(s) are deleted and created again with the same name
# intializing rate to 0 if rates are negative value.
# if diff < 0:
# rate = 0.0
return diff
def add_diff(self, doc_stats, doc, pxname):
"""Add the diff values to metrics dictionary"""
if doc == 'frontendStats':
diff = self.get_diff('ereq', doc_stats, self.prev_frontend_data[pxname])
if diff != NAN:
doc_stats['ereq'] = diff
diff = self.get_diff('dreq', doc_stats, self.prev_frontend_data[pxname])
if diff != NAN:
doc_stats['dreq'] = diff
diff = self.get_diff('hrsp_4xx', doc_stats, self.prev_frontend_data[pxname])
if diff != NAN:
doc_stats['hrsp_4xx'] = diff
diff = self.get_diff('hrsp_5xx', doc_stats, self.prev_frontend_data[pxname])
if diff != NAN:
doc_stats['hrsp_5xx'] = diff
diff = self.get_diff('bin', doc_stats, self.prev_frontend_data[pxname])
if diff != NAN:
doc_stats['bin'] = round(diff, 2)
diff = self.get_diff('bout', doc_stats, self.prev_frontend_data[pxname])
if diff != NAN:
doc_stats['bout'] = round(diff, 2)
if doc == 'backendStats':
diff = self.get_diff('econ', doc_stats, self.prev_backend_data[pxname])
if diff != NAN:
doc_stats['econ'] = diff
diff = self.get_diff('dresp', doc_stats, self.prev_backend_data[pxname])
if diff != NAN:
doc_stats['dresp'] = diff
diff = self.get_diff('eresp', doc_stats, self.prev_backend_data[pxname])
if diff != NAN:
doc_stats['eresp'] = diff
diff = self.get_diff('wredis', doc_stats, self.prev_backend_data[pxname])
if diff != NAN:
doc_stats['wredis'] = diff
diff = self.get_diff('wretr', doc_stats, self.prev_backend_data[pxname])
if diff != NAN:
doc_stats['wretr'] = diff
diff = self.get_diff('hrsp_4xx', doc_stats, self.prev_backend_data[pxname])
if diff != NAN:
doc_stats['hrsp_4xx'] = diff
diff = self.get_diff('hrsp_5xx', doc_stats, self.prev_backend_data[pxname])
if diff != NAN:
doc_stats['hrsp_5xx'] = diff
if doc == 'haproxyStats':
diff = self.get_diff('sslCacheMisses', doc_stats, self.prev_haproxy_data)
if diff != NAN:
doc_stats['sslCacheMisses'] = diff
diff = self.get_diff('sslCacheLookups', doc_stats, self.prev_haproxy_data)
if diff != NAN:
doc_stats['sslCacheLookups'] = diff
def add_dispatch_haproxy(self, doc_stats, doc):
"""Add difference values to haproxyStats and dispatch values"""
if doc == 'haproxyStats':
if self.pollCounter == 1:
self.prev_haproxy_data = deepcopy(doc_stats)
self.add_default_diff_value(doc_stats, doc)
else:
self.add_diff(doc_stats, doc, 'haproxy')
self.prev_haproxy_data = deepcopy(doc_stats)
self.dispatch_data(deepcopy(doc_stats), doc)
def add_dispatch_fbstats(self, doc_stats, doc):
"""Add difference values to frontend and backend stats and dispatch values"""
try:
if doc == 'frontendStats':
for pxname in doc_stats.keys():
if self.pollCounter==1 or pxname not in self.prev_frontend_data.keys():
self.prev_frontend_data[pxname] = deepcopy(doc_stats)
self.add_default_diff_value(doc_stats[pxname], doc)
else:
self.add_diff(doc_stats[pxname], doc, pxname)
self.prev_frontend_data[pxname] = deepcopy(doc_stats[pxname])
elif doc == 'backendStats':
for pxname in doc_stats.keys():
if self.pollCounter == 1 or pxname not in self.prev_backend_data.keys():
self.prev_backend_data[pxname] = deepcopy(doc_stats)
self.add_default_diff_value(doc_stats[pxname], doc)
else:
self.add_diff(doc_stats[pxname], doc, pxname)
self.prev_backend_data[pxname] = deepcopy(doc_stats[pxname])
self.dispatch_data(deepcopy(doc_stats), doc)
except Exception as err:
collectd.error("Plugin haproxy: Error in add_dispatch_fbstats due to %s" % err)
def add_common_params(self, dict_stats, doc):
"""Adds TIMESTAMP, PLUGIN, PLUGITYPE to dictionary."""
timestamp = int(round(time.time() * 1000))
dict_stats[TIMESTAMP] = timestamp
dict_stats[PLUGIN] = HAPROXY
dict_stats[PLUGINTYPE] = doc
dict_stats[ACTUALPLUGINTYPE] = HAPROXY
# dict_jmx[PLUGIN_INS] = doc
collectd.info("Plugin haproxy: Added common parameters successfully for %s doctype" % doc)
def read(self):
"""Collects all data."""
try:
self.pollCounter += 1
for doc in HAPROXY_DOCS:
haproxy_stats = self.collect_haproxy_data(doc)
if not haproxy_stats:
collectd.error("Plugin haproxy: Unable to fetch data for document type: %s." % doc)
return
else:
#self.documentsTypes = ['frontendStats', 'backendStats', 'haproxyStats']
if doc not in self.documentsTypes:
del haproxy_stats[doc]
if doc == 'haproxyStats' and doc in self.documentsTypes:
self.add_dispatch_haproxy(haproxy_stats[doc], doc)
elif (doc == 'frontendStats' or doc == 'backendStats') and (doc in self.documentsTypes):
self.add_dispatch_fbstats(haproxy_stats[doc], doc)
except Exception as err:
collectd.error("Plugin haproxy: Couldn't read and gather the metrics due to the exception %s in %s" % (err, traceback.format_exc()))
def dispatch_data(self, result, doc):
"""Dispatch data to collectd."""
if doc == "haproxyStats":
collectd.info("Plugin haproxy: Succesfully sent %s doctype to collectd." % doc)
collectd.debug("Plugin haproxy: Values dispatched =%s" % json.dumps(result))
utils.dispatch(result)
elif doc == "frontendStats" or doc == "backendStats":
for pxname in result.keys():
collectd.info("Plugin haproxy: Succesfully sent %s of %s to collectd." % (doc, pxname))
collectd.debug("Plugin haproxy: Values dispatched =%s" % json.dumps(result[pxname]))
utils.dispatch(result[pxname])
def read_temp(self):
"""Collectd first calls register_read. At that time default interval is taken,
hence temporary function is made to call, the read callback is unregistered
and read() is called again with interval obtained from conf by register_config callback."""
collectd.unregister_read(self.read_temp)
collectd.register_read(self.read, interval=int(self.interval))
def init():
"""When new process is formed, action to SIGCHLD is reset to default behavior."""
signal.signal(signal.SIGCHLD, signal.SIG_DFL)
OBJ = haproxyStats()
collectd.register_init(init)
collectd.register_config(OBJ.config)
collectd.register_read(OBJ.read_temp)