1 # -*- coding: utf-8 -*-
2 # Description: squid netdata python.d module
3 # Author: Pawel Krupa (paulfantom)
5 from base import SocketService
8 # default module values (can be overridden per job in `config`)
13 # charts order (can be overridden if you want less charts, or different order)
14 ORDER = ['clients_net', 'clients_requests', 'servers_net', 'servers_requests']
18 'options': [None, "Squid Client Bandwidth", "kilobits/s", "clients", "squid.clients_net", "area"],
20 ["client_http_kbytes_in", "in", "incremental", 8, 1],
21 ["client_http_kbytes_out", "out", "incremental", -8, 1],
22 ["client_http_hit_kbytes_out", "hits", "incremental", -8, 1]
25 'options': [None, "Squid Client Requests", "requests/s", "clients", "squid.clients_requests", 'line'],
27 ["client_http_requests", "requests", "incremental"],
28 ["client_http_hits", "hits", "incremental"],
29 ["client_http_errors", "errors", "incremental", -1, 1]
32 'options': [None, "Squid Server Bandwidth", "kilobits/s", "servers", "squid.servers_net", "area"],
34 ["server_all_kbytes_in", "in", "incremental", 8, 1],
35 ["server_all_kbytes_out", "out", "incremental", -8, 1]
38 'options': [None, "Squid Server Requests", "requests/s", "servers", "squid.servers_requests", 'line'],
40 ["server_all_requests", "requests", "incremental"],
41 ["server_all_errors", "errors", "incremental", -1, 1]
46 class Service(SocketService):
47 def __init__(self, configuration=None, name=None):
48 SocketService.__init__(self, configuration=configuration, name=name)
49 self._keep_alive = True
51 self.host = "localhost"
54 self.definitions = CHARTS
58 Get data via http request
61 response = self._get_raw_data()
66 for tmp in response.split('\r\n'):
67 if tmp.startswith("sample_time"):
71 if raw.startswith('<'):
72 self.error("invalid data received")
75 for row in raw.split('\n'):
76 if row.startswith(("client", "server.all")):
78 data[tmp[0].replace('.', '_').strip(' ')] = int(tmp[1])
80 except (ValueError, AttributeError, TypeError):
81 self.error("invalid data received")
85 self.error("no data received")
90 def _check_raw_data(self, data):
91 header = data[:1024].lower()
93 if "connection: keep-alive" in header:
94 self._keep_alive = True
96 self._keep_alive = False
98 if data[-7:] == "\r\n0\r\n\r\n" and "transfer-encoding: chunked" in header: # HTTP/1.1 response
99 self.debug("received full response from squid")
102 self.debug("waiting more data from squid")
107 Parse essential configuration, autodetect squid configuration (if needed), and check if data is available
112 req = self.request.decode()
113 if not req.startswith("GET"):
115 if not req.endswith(" HTTP/1.1\r\n\r\n"):
116 req += " HTTP/1.1\r\n\r\n"
117 self.request = req.encode()
118 if self._get_data() is not None: