1 # -*- coding: utf-8 -*-
2 # Description: haproxy netdata python.d module
5 from base import UrlService
7 # default module values (can be overridden per job in `config`)
12 # charts order (can be overridden if you want less charts, or different order)
13 ORDER = ['fbin', 'fbout', 'fscur', 'fqcur', 'bbin', 'bbout', 'bscur', 'bqcur']
16 'options': [None, "Bytes in", "bytes/s", 'Frontend', 'f.bin', 'line'],
20 'options': [None, "Bytes out", "bytes/s", 'Frontend', 'f.bout', 'line'],
24 'options': [None, "Sessions active", "sessions", 'Frontend', 'f.scur', 'line'],
28 'options': [None, "Session in queue", "sessions", 'Frontend', 'f.qcur', 'line'],
32 'options': [None, "Bytes in", "bytes/s", 'Backend', 'b.bin', 'line'],
36 'options': [None, "Bytes out", "bytes/s", 'Backend', 'b.bout', 'line'],
40 'options': [None, "Sessions active", "sessions", 'Backend', 'b.scur', 'line'],
44 'options': [None, "Sessions in queue", "sessions", 'Backend', 'b.qcur', 'line'],
50 class Service(UrlService):
51 def __init__(self, configuration=None, name=None):
52 UrlService.__init__(self, configuration=configuration, name=name)
53 self.url = "http://127.0.0.1:7000/haproxy_stats;csv"
55 self.order_front = [_ for _ in ORDER if _.startswith('f')]
56 self.order_back = [_ for _ in ORDER if _.startswith('b')]
57 self.definitions = CHARTS
60 def create_charts(self, front_ends, back_ends):
61 for chart in self.order_front:
62 for _ in range(len(front_ends)):
63 self.definitions[chart]['lines'].append(['_'.join([chart, front_ends[_]['# pxname']]),
64 front_ends[_]['# pxname'],
65 'incremental' if chart.startswith(
66 ('fb', 'bb')) else 'absolute'])
67 for chart in self.order_back:
68 for _ in range(len(back_ends)):
69 self.definitions[chart]['lines'].append(['_'.join([chart, back_ends[_]['# pxname']]),
70 back_ends[_]['# pxname'],
71 'incremental' if chart.startswith(
72 ('fb', 'bb')) else 'absolute'])
76 Format data received from http request
79 if self.url[-4:] != ';csv':
81 self.info('Url rewritten to %s' % self.url)
84 raw_data = self._get_raw_data().splitlines()
85 except Exception as e:
89 all_instances = [dict(zip(raw_data[0].split(','), raw_data[_].split(','))) for _ in range(1, len(raw_data))]
91 back_ends = [backend for backend in all_instances
92 if backend['svname'] == 'BACKEND' and backend['# pxname'] != 'stats']
93 front_ends = [frontend for frontend in all_instances
94 if frontend['svname'] == 'FRONTEND' and frontend['# pxname'] != 'stats']
97 self.create_charts(front_ends, back_ends)
102 for frontend in front_ends:
103 for _ in self.order_front:
104 to_netdata.update({'_'.join([_, frontend['# pxname']]): int(frontend[_[1:]]) if frontend.get(_[1:]) else 0})
106 for backend in back_ends:
107 for _ in self.order_back:
108 to_netdata.update({'_'.join([_, backend['# pxname']]): int(backend[_[1:]]) if backend.get(_[1:]) else 0})