]> arthur.barton.de Git - netdata.git/blob - python.d/squid.chart.py
non-blocking `SocketService`
[netdata.git] / python.d / squid.chart.py
1 # -*- coding: utf-8 -*-
2 # Description: squid netdata python.d module
3 # Author: Pawel Krupa (paulfantom)
4
5 from base import SocketService
6 import select
7
8 # default module values (can be overridden per job in `config`)
9 # update_every = 2
10 priority = 60000
11 retries = 60
12
13 # charts order (can be overridden if you want less charts, or different order)
14 ORDER = ['clients_net', 'clients_requests', 'servers_net', 'servers_requests']
15
16 CHARTS = {
17     'clients_net': {
18         'options': [None, "Squid Client Bandwidth", "kilobits/s", "clients", "squid.clients.net", "area"],
19         'lines': [
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]
23         ]},
24     'clients_requests': {
25         'options': [None, "Squid Client Requests", "requests/s", "clients", "squid.clients.requests", 'line'],
26         'lines': [
27             ["client_http_requests", "requests", "incremental"],
28             ["client_http_hits", "hits", "incremental"],
29             ["client_http_errors", "errors", "incremental", -1, 1]
30         ]},
31     'servers_net': {
32         'options': [None, "Squid Server Bandwidth", "kilobits/s", "servers", "squid.servers.net", "area"],
33         'lines': [
34             ["server_all_kbytes_in", "in", "incremental", 8, 1],
35             ["server_all_kbytes_out", "out", "incremental", -8, 1]
36         ]},
37     'servers_requests': {
38         'options': [None, "Squid Server Requests", "requests/s", "servers", "squid.servers.requests", 'line'],
39         'lines': [
40             ["server_all_requests", "requests", "incremental"],
41             ["server_all_errors", "errors", "incremental", -1, 1]
42         ]}
43 }
44
45
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
50         self.request = ""
51         self.host = "localhost"
52         self.port = 3128
53         self.order = ORDER
54         self.definitions = CHARTS
55
56     def _get_data(self):
57         """
58         Get data via http request
59         :return: dict
60         """
61         data = {}
62         try:
63             raw = ""
64             for tmp in self._get_raw_data().split('\r\n'):
65                 if tmp.startswith("sample_time"):
66                     raw = tmp
67                     break
68             if raw.startswith('<'):
69                 self.error("invalid data received")
70                 return None
71             for row in raw.split('\n'):
72                 if row.startswith(("client", "server.all")):
73                     tmp = row.split("=")
74                     data[tmp[0].replace('.', '_').strip(' ')] = int(tmp[1])
75         except (ValueError, AttributeError, TypeError):
76             self.error("invalid data received")
77             return None
78
79         if len(data) == 0:
80             self.error("no data received")
81             return None
82         else:
83             return data
84
85     def _check_raw_data(self, data):
86         if "Connection: keep-alive" in data[:1024]:
87             self._keep_alive = True
88         else:
89             self._keep_alive = False
90
91         # if "client" in data and "server.all" in data:
92         #     return True
93         # else:
94         #     return False
95         if data[-7:] == "\r\n0\r\n\r\n":
96             return True
97         else:
98             return False
99     #     # TODO write some parser of "Transfer-Encoding: chunked"
100     #     if "Transfer-Encoding: chunked" in data[:1024]:
101     #         data = data[self.__last:]
102     #
103     #
104     #     print(data)
105     #     import time
106     #     time.sleep(10)
107     #     return False
108     #     supposed = 0
109     #     if length >= supposed:
110     #         return True
111     #     else:
112     #         return False
113     #
114     #     return False
115
116     def check(self):
117         """
118         Parse essential configuration, autodetect squid configuration (if needed), and check if data is available
119         :return: boolean
120         """
121         self._parse_config()
122         # format request
123         req = self.request.decode()
124         if not req.startswith("GET"):
125             req = "GET " + req
126         if not req.endswith(" HTTP/1.1\r\n\r\n"):
127             req += " HTTP/1.1\r\n\r\n"
128         self.request = req.encode()
129         if self._get_data() is not None:
130             return True
131         else:
132             return False