@cryptotaxi247 / netdata-1 / commits / b7a8cccc9

icecast python module added

lgz committed Mar 6, 2018 at 21:36 UTC b7a8cccc9083d139d364ed5eda8e38286c0c6caf
1 file changed +98
python.d/icecast.chart.py new
+98
@@ -0,0 +1,98 @@
1 +# -*- coding: utf-8 -*-
2 +# Description: icecast netdata python.d module
3 +# Author: Ilya Mashchenko (l2isbad)
4 +
5 +import json
6 +
7 +from bases.FrameworkServices.UrlService import UrlService
8 +
9 +
10 +priority = 60000
11 +retries = 60
12 +
13 +# charts order (can be overridden if you want less charts, or different order)
14 +ORDER = ['listeners']
15 +
16 +CHARTS = {
17 + 'listeners': {
18 + 'options': [None, 'Number Of Listeners', 'listeners',
19 + 'listeners', 'listeners.listeners', 'line'],
20 + 'lines': [
21 + ]}
22 +}
23 +
24 +
25 +class Source:
26 + def __init__(self, idx, data):
27 + self.id = idx
28 + self.name = 'source_{0}'.format(idx)
29 + self.data = data
30 +
31 + def is_active(self):
32 + return self.data.get('stream_start') and self.data.get('server_name')
33 +
34 + def listeners(self):
35 + return self.data['listeners']
36 +
37 +
38 +class Service(UrlService):
39 + def __init__(self, configuration=None, name=None):
40 + UrlService.__init__(self, configuration=configuration, name=name)
41 + self.order = ORDER
42 + self.definitions = CHARTS
43 + self.url = self.configuration.get('url')
44 + self._manager = self._build_manager()
45 +
46 + def check(self):
47 + """
48 + Add active sources to the "listeners" chart
49 + :return: bool
50 + """
51 + sources = self.get_sources()
52 + if not sources:
53 + return None
54 +
55 + active_sources = 0
56 + for idx, raw_source in enumerate(sources):
57 + if Source(idx, raw_source).is_active():
58 + active_sources += 1
59 + dim_id = 'source_{0}'.format(idx)
60 + dim = 'source {0}'.format(idx)
61 + self.definitions['listeners']['lines'].append([dim_id, dim])
62 +
63 + return bool(active_sources)
64 +
65 + def _get_data(self):
66 + """
67 + Get number of listeners for every source
68 + :return: dict
69 + """
70 + sources = self.get_sources()
71 + if not sources:
72 + return None
73 +
74 + data = dict()
75 +
76 + for idx, raw_source in enumerate(sources):
77 + source = Source(idx, raw_source)
78 + data[source.name] = source.listeners()
79 +
80 + return data
81 +
82 + def get_sources(self):
83 + """
84 + Format data received from http request and return list of sources
85 + :return: list
86 + """
87 +
88 + raw_data = self._get_raw_data()
89 + if not raw_data:
90 + return None
91 +
92 + try:
93 + data = json.loads(raw_data)
94 + except ValueError as error:
95 + self.error("JSON decode error:", error)
96 + return None
97 +
98 + return data['icestats'].get('source')