]> arthur.barton.de Git - netdata.git/blob - python.d/haproxy.chart.py
change bytes to kilobytes
[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, "Kilobytes in", "kilobytes in/s", 'Frontend', 'f.bin', 'line'],
17         'lines': [
18         ]},
19     'fbout': {
20         'options': [None, "Kilobytes out", "kilobytes out/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, "Kilobytes in", "kilobytes in/s", 'Backend', 'b.bin', 'line'],
33         'lines': [
34         ]},
35     'bbout': {
36         'options': [None, "Kilobytes out", "kilobytes out/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 _ in range(len(front_ends)):
62             self.definitions['fbin']['lines'].append(['_'.join(['fbin', front_ends[_]['# pxname']]), front_ends[_]['# pxname'], 'incremental', 1, 1024])
63             self.definitions['fbout']['lines'].append(['_'.join(['fbout', front_ends[_]['# pxname']]), front_ends[_]['# pxname'], 'incremental', 1, 1024])
64             self.definitions['fscur']['lines'].append(['_'.join(['fscur', front_ends[_]['# pxname']]), front_ends[_]['# pxname'], 'absolute'])
65             self.definitions['fqcur']['lines'].append(['_'.join(['fqcur', front_ends[_]['# pxname']]), front_ends[_]['# pxname'], 'absolute'])
66         
67         for _ in range(len(back_ends)):
68             self.definitions['bbin']['lines'].append(['_'.join(['bbin', back_ends[_]['# pxname']]), back_ends[_]['# pxname'], 'incremental', 1, 1024])
69             self.definitions['bbout']['lines'].append(['_'.join(['bbout', back_ends[_]['# pxname']]), back_ends[_]['# pxname'], 'incremental', 1, 1024])
70             self.definitions['bscur']['lines'].append(['_'.join(['bscur', back_ends[_]['# pxname']]), back_ends[_]['# pxname'], 'absolute'])
71             self.definitions['bqcur']['lines'].append(['_'.join(['bqcur', back_ends[_]['# pxname']]), back_ends[_]['# pxname'], 'absolute'])
72                 
73     def _get_data(self):
74         """
75         Format data received from http request
76         :return: dict
77         """
78         if self.url[-4:] != ';csv':
79             self.url += ';csv'
80             self.info('Url rewritten to %s' % self.url)
81
82         try:
83             raw_data = self._get_raw_data().splitlines()
84         except Exception as e:
85             self.error(str(e))
86             return None
87
88         all_instances = [dict(zip(raw_data[0].split(','), raw_data[_].split(','))) for _ in range(1, len(raw_data))]
89
90         back_ends = [backend for backend in all_instances
91                      if backend['svname'] == 'BACKEND' and backend['# pxname'] != 'stats']
92         front_ends = [frontend for frontend in all_instances
93                       if frontend['svname'] == 'FRONTEND' and frontend['# pxname'] != 'stats']
94
95         if self.charts:
96             self.create_charts(front_ends, back_ends)
97             self.charts = False
98
99         to_netdata = dict()
100
101         for frontend in front_ends:
102             for _ in self.order_front:
103                 to_netdata.update({'_'.join([_, frontend['# pxname']]): int(frontend[_[1:]]) if frontend.get(_[1:]) else 0})
104
105         for backend in back_ends:
106             for _ in self.order_back:
107                 to_netdata.update({'_'.join([_, backend['# pxname']]): int(backend[_[1:]]) if backend.get(_[1:]) else 0})
108
109         return to_netdata