]> arthur.barton.de Git - netdata.git/blob - python.d/squid.chart.py
callback in `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 = 5
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.request = ""
50         self.host = "localhost"
51         self.port = 3128
52         self.order = ORDER
53         self.definitions = CHARTS
54
55     def _get_data(self):
56         """
57         Get data via http request
58         :return: dict
59         """
60         data = {}
61         try:
62             raw = self._get_raw_data().split('\r\n')[-1]
63             if raw.startswith('<'):
64                 self.error("invalid data received")
65                 return None
66             for row in raw.split('\n'):
67                 if row.startswith(("client", "server.all")):
68                     tmp = row.split("=")
69                     data[tmp[0].replace('.', '_').strip(' ')] = int(tmp[1])
70         except (ValueError, AttributeError, TypeError):
71             self.error("invalid data received")
72             return None
73
74         if len(data) == 0:
75             self.error("no data received")
76             return None
77         else:
78             return data
79
80     def _more_data_available(self):
81         try:
82             ready_to_read, _, in_error = select.select([self._sock, ], [], [], 0.05)
83         except Exception as e:
84             self.debug("select returned exception", str(e))
85         if len(ready_to_read) > 0:
86             return True
87         else:
88             return False
89
90     def check(self):
91         """
92         Parse essential configuration, autodetect squid configuration (if needed), and check if data is available
93         :return: boolean
94         """
95         self._parse_config()
96         # format request
97         req = self.request.decode()
98         if not req.startswith("GET"):
99             req = "GET " + req
100         if not req.endswith(" HTTP/1.0\r\n\r\n"):
101             req += " HTTP/1.0\r\n\r\n"
102         self.request = req.encode()
103         if self._get_data() is not None:
104             return True
105         else:
106             return False