]> arthur.barton.de Git - netdata.git/blob - python.d/haproxy.chart.py
change context name
[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, SocketService
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', 'health_down']
14 CHARTS = {
15     'fbin': {
16         'options': [None, "Kilobytes in", "kilobytes in/s", 'Frontend', 'haproxy_f.bin', 'line'],
17         'lines': [
18         ]},
19     'fbout': {
20         'options': [None, "Kilobytes out", "kilobytes out/s", 'Frontend', 'haproxy_f.bout', 'line'],
21         'lines': [
22         ]},
23     'fscur': {
24         'options': [None, "Sessions active", "sessions", 'Frontend', 'haproxy_f.scur', 'line'],
25         'lines': [
26         ]},
27     'fqcur': {
28         'options': [None, "Session in queue", "sessions", 'Frontend', 'haproxy_f.qcur', 'line'],
29         'lines': [
30         ]},
31     'bbin': {
32         'options': [None, "Kilobytes in", "kilobytes in/s", 'Backend', 'haproxy_b.bin', 'line'],
33         'lines': [
34         ]},
35     'bbout': {
36         'options': [None, "Kilobytes out", "kilobytes out/s", 'Backend', 'haproxy_b.bout', 'line'],
37         'lines': [
38         ]},
39     'bscur': {
40         'options': [None, "Sessions active", "sessions", 'Backend', 'haproxy_b.scur', 'line'],
41         'lines': [
42         ]},
43     'bqcur': {
44         'options': [None, "Sessions in queue", "sessions", 'Backend', 'haproxy_b.qcur', 'line'],
45         'lines': [
46         ]},
47     'health_down': {
48         'options': [None, "Servers in DOWN state", "failed servers", 'Health', 'haproxy_h.down', 'line'],
49         'lines': [
50         ]}
51 }
52
53
54 class Service(UrlService, SocketService):
55     def __init__(self, configuration=None, name=None):
56         SocketService.__init__(self, configuration=configuration, name=name)
57         self.user = self.configuration.get('user')
58         self.password = self.configuration.get('pass')
59         self.request = 'show stat\n'
60         self.poll_method = (UrlService, SocketService)
61         self.order = ORDER
62         self.order_front = [_ for _ in ORDER if _.startswith('f')]
63         self.order_back = [_ for _ in ORDER if _.startswith('b')]
64         self.definitions = CHARTS
65         self.charts = True
66
67     def check(self):
68         if self.configuration.get('url'):
69             self.poll_method = self.poll_method[0]
70             url = self.configuration.get('url')
71             if not url.endswith(';csv;norefresh'):
72                 self.error('Bad url(%s). Must be http://<ip.address>:<port>/<url>;csv;norefresh' % url)
73                 return False
74         elif self.configuration.get('socket'):
75             self.poll_method = self.poll_method[1]
76         else:
77             self.error('No configuration is specified')
78             return False
79
80         if self.poll_method.check(self):
81             self.info('Plugin was started succesfully. We are using %s.' % self.poll_method.__name__)
82             return True
83
84     def create_charts(self, front_ends, back_ends):
85         for _ in range(len(front_ends)):
86             self.definitions['fbin']['lines'].append(['_'.join(['fbin', front_ends[_]['# pxname']]), front_ends[_]['# pxname'], 'incremental', 1, 1024])
87             self.definitions['fbout']['lines'].append(['_'.join(['fbout', front_ends[_]['# pxname']]), front_ends[_]['# pxname'], 'incremental', 1, 1024])
88             self.definitions['fscur']['lines'].append(['_'.join(['fscur', front_ends[_]['# pxname']]), front_ends[_]['# pxname'], 'absolute'])
89             self.definitions['fqcur']['lines'].append(['_'.join(['fqcur', front_ends[_]['# pxname']]), front_ends[_]['# pxname'], 'absolute'])
90         
91         for _ in range(len(back_ends)):
92             self.definitions['bbin']['lines'].append(['_'.join(['bbin', back_ends[_]['# pxname']]), back_ends[_]['# pxname'], 'incremental', 1, 1024])
93             self.definitions['bbout']['lines'].append(['_'.join(['bbout', back_ends[_]['# pxname']]), back_ends[_]['# pxname'], 'incremental', 1, 1024])
94             self.definitions['bscur']['lines'].append(['_'.join(['bscur', back_ends[_]['# pxname']]), back_ends[_]['# pxname'], 'absolute'])
95             self.definitions['bqcur']['lines'].append(['_'.join(['bqcur', back_ends[_]['# pxname']]), back_ends[_]['# pxname'], 'absolute'])
96             self.definitions['health_down']['lines'].append(['_'.join(['hdown', back_ends[_]['# pxname']]), back_ends[_]['# pxname'], 'absolute'])
97                 
98     def _get_data(self):
99         """
100         Format data received from http request
101         :return: dict
102         """
103         try:
104             raw_data = self.poll_method._get_raw_data(self).splitlines()
105         except Exception as e:
106             self.error(str(e))
107             return None
108
109         all_instances = [dict(zip(raw_data[0].split(','), raw_data[_].split(','))) for _ in range(1, len(raw_data))]
110
111         back_ends = list(filter(is_backend, all_instances))
112         front_ends = list(filter(is_frontend, all_instances))
113         servers = list(filter(is_server, all_instances))
114
115         if self.charts:
116             self.create_charts(front_ends, back_ends)
117             self.charts = False
118
119         to_netdata = dict()
120
121         for frontend in front_ends:
122             for _ in self.order_front:
123                 to_netdata.update({'_'.join([_, frontend['# pxname']]): int(frontend[_[1:]]) if frontend.get(_[1:]) else 0})
124
125         for backend in back_ends:
126             for _ in self.order_back:
127                 to_netdata.update({'_'.join([_, backend['# pxname']]): int(backend[_[1:]]) if backend.get(_[1:]) else 0})
128
129         for _ in range(len(back_ends)):
130             to_netdata.update({'_'.join(['hdown', back_ends[_]['# pxname']]):
131                            len([server for server in servers if is_server_down(server, back_ends, _)])})
132
133         return to_netdata
134
135     def _check_raw_data(self, data):
136         """
137         Check if all data has been gathered from socket
138         :param data: str
139         :return: boolean
140         """
141         return not bool(data)
142
143 def is_backend(backend):
144     try:
145         return backend['svname'] == 'BACKEND' and backend['# pxname'] != 'stats'
146     except Exception:
147         return False
148
149 def is_frontend(frontend):
150     try:
151         return frontend['svname'] == 'FRONTEND' and frontend['# pxname'] != 'stats'
152     except Exception:
153         return False
154
155 def is_server(server):
156     try:
157         return not server['svname'].startswith(('FRONTEND', 'BACKEND'))
158     except Exception:
159         return False
160
161 def is_server_down(server, back_ends, _):
162     try:
163         return server['# pxname'] == back_ends[_]['# pxname'] and server['status'] != 'UP'
164     except Exception:
165         return False