]> arthur.barton.de Git - netdata.git/blob - python.d/haproxy.chart.py
haproxy_module draft version
[netdata.git] / python.d / haproxy.chart.py
1 # -*- coding: utf-8 -*-
2 # Description: haproxy netdata python.d module
3 # Author: l2isbad
4
5 from base import UrlService
6
7 # default module values (can be overridden per job in `config`)
8 # update_every = 2
9 priority = 60000
10 retries = 60
11
12 # charts order (can be overridden if you want less charts, or different order)
13 ORDER = ['fbin', 'fbout', 'fscur', 'fqcur', 'bbin', 'bbout', 'bscur', 'bqcur']
14 CHARTS = {
15     'fbin': {
16         'options': [None, "Bytes in", "bytes/s", 'Frontend', 'f.bin', 'line'],
17         'lines': [
18         ]},
19     'fbout': {
20         'options': [None, "Bytes out", "bytes/s", 'Frontend', 'f.bout', 'line'],
21         'lines': [
22         ]},
23     'fscur': {
24         'options': [None, "Sessions active", "sessions", 'Frontend', 'f.scur', 'line'],
25         'lines': [
26         ]},
27     'fqcur': {
28         'options': [None, "Session in queue", "sessions", 'Frontend', 'f.qcur', 'line'],
29         'lines': [
30         ]},
31     'bbin': {
32         'options': [None, "Bytes in", "bytes/s", 'Backend', 'b.bin', 'line'],
33         'lines': [
34         ]},
35     'bbout': {
36         'options': [None, "Bytes out", "bytes/s", 'Backend', 'b.bout', 'line'],
37         'lines': [
38         ]},
39     'bscur': {
40         'options': [None, "Sessions active", "sessions", 'Backend', 'b.scur', 'line'],
41         'lines': [
42         ]},
43     'bqcur': {
44         'options': [None, "Sessions in queue", "sessions", 'Backend', 'b.qcur', 'line'],
45         'lines': [
46         ]}
47 }
48
49
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"
54         self.order = ORDER
55         self.order_front = [_ for _ in ORDER if _.startswith('f')]
56         self.order_back = [_ for _ in ORDER if _.startswith('b')]
57         self.definitions = CHARTS
58         self.charts = True
59
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'])
73
74     def _get_data(self):
75         """
76         Format data received from http request
77         :return: dict
78         """
79         if self.url[-4:] != ';csv':
80             self.url += ';csv'
81             self.info('Url rewritten to %s' % self.url)
82
83         try:
84             raw_data = self._get_raw_data().splitlines()
85         except Exception as e:
86             self.error(str(e))
87             return None
88
89         all_instances = [dict(zip(raw_data[0].split(','), raw_data[_].split(','))) for _ in range(1, len(raw_data))]
90
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']
95
96         if self.charts:
97             self.create_charts(front_ends, back_ends)
98             self.charts = False
99
100         to_netdata = dict()
101
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})
105
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})
109
110         return to_netdata