1 # -*- coding: utf-8 -*-
2 # Description: prototypes for netdata python.d modules
3 # Author: Pawel Krupa (paulfantom)
8 from urllib.request import urlopen
10 from urllib2 import urlopen
16 class BaseService(threading.Thread):
18 Prototype of Service class.
19 Implemented basic functionality to run jobs by `python.d.plugin`
21 def __init__(self, configuration=None, name=None):
23 This needs to be initialized in child classes
24 :param configuration: dict
27 threading.Thread.__init__(self)
28 self._data_stream = ""
32 self.priority = 140000
35 self.override_name = None
39 if configuration is None:
40 self.error("BaseService: no configuration parameters supplied. Cannot create Service.")
43 self._extract_base_config(configuration)
45 self.create_timetable()
47 def _extract_base_config(self, config):
49 Get basic parameters to run service
51 config = {'update_every':1,
57 self.override_name = config.pop('override_name')
60 self.update_every = int(config.pop('update_every'))
61 self.priority = int(config.pop('priority'))
62 self.retries = int(config.pop('retries'))
63 self.retries_left = self.retries
64 self.configuration = config
66 def create_timetable(self, freq=None):
68 Create service timetable.
71 timetable = {'last': 1466370091.3767564,
77 freq = self.update_every
79 self.timetable = {'last': now,
80 'next': now - (now % freq) + freq,
85 Executes self.update(interval) and draws run time chart.
86 Return value presents exit status of update()
90 # check if it is time to execute job update() function
91 if self.timetable['next'] > t_start:
92 msg.debug(self.chart_name + " will be run in " +
93 str(int((self.timetable['next'] - t_start) * 1000)) + " ms")
96 since_last = int((t_start - self.timetable['last']) * 1000000)
97 msg.debug(self.chart_name +
98 " ready to run, after " + str(int((t_start - self.timetable['last']) * 1000)) +
99 " ms (update_every: " + str(self.timetable['freq'] * 1000) +
100 " ms, latency: " + str(int((t_start - self.timetable['next']) * 1000)) + " ms)")
101 if not self.update(since_last):
104 self.timetable['next'] = t_end - (t_end % self.timetable['freq']) + self.timetable['freq']
106 # draw performance graph
107 run_time = str(int((t_end - t_start) * 1000))
108 run_time_chart = "BEGIN netdata.plugin_pythond_" + self.chart_name + " " + str(since_last) + '\n'
109 run_time_chart += "SET run_time = " + run_time + '\n'
110 run_time_chart += "END\n"
111 sys.stdout.write(run_time_chart)
112 msg.debug(self.chart_name + " updated in " + str(run_time) + " ms")
113 self.timetable['last'] = t_start
118 Runs job in thread. Handles retries.
119 Exits when job failed or timed out.
122 self.timetable['last'] = time.time()
125 status = self._run_once()
126 except Exception as e:
127 msg.error("Something wrong: " + str(e))
130 time.sleep(self.timetable['next'] - time.time())
131 self.retries_left = self.retries
134 if self.retries_left <= 0:
135 msg.error("no more retries. Exiting")
138 time.sleep(self.timetable['freq'])
140 def _line(self, instruction, *params):
142 Converts *params to string and joins them with one space between every one.
143 :param params: str/int/float
145 self._data_stream += instruction
155 self._data_stream += " " + p
156 self._data_stream += "\n"
158 def chart(self, type_id, name="", title="", units="", family="",
159 category="", charttype="line", priority="", update_every=""):
168 :param charttype: str
169 :param priority: int/str
170 :param update_every: int/str
172 self._charts.append(type_id)
173 self._line("CHART", type_id, name, title, units, family, category, charttype, priority, update_every)
175 def dimension(self, id, name=None, algorithm="absolute", multiplier=1, divisor=1, hidden=False):
177 Defines a new dimension for the chart
180 :param algorithm: str
181 :param multiplier: int/str
182 :param divisor: int/str
183 :param hidden: boolean
189 self.error("malformed dimension: multiplier is not a number:", multiplier)
194 self.error("malformed dimension: divisor is not a number:", divisor)
198 if algorithm not in ("absolute", "incremental", "percentage-of-absolute-row", "percentage-of-incremental-row"):
199 algorithm = "absolute"
201 self._dimensions.append(id)
203 self._line("DIMENSION", id, name, algorithm, multiplier, divisor, "hidden")
205 self._line("DIMENSION", id, name, algorithm, multiplier, divisor)
207 def begin(self, type_id, microseconds=0):
211 :param microseconds: int
214 if type_id not in self._charts:
215 self.error("wrong chart type_id:", type_id)
220 self.error("malformed begin statement: microseconds are not a number:", microseconds)
223 self._line("BEGIN", type_id, microseconds)
226 def set(self, id, value):
228 Set value to dimension
230 :param value: int/float
233 if id not in self._dimensions:
234 self.error("wrong dimension id:", id)
237 value = str(int(value))
239 self.error("cannot set non-numeric value:", value)
241 self._line("SET", id, "=", value)
249 Upload new data to netdata
251 print(self._data_stream)
252 self._data_stream = ""
254 def error(self, *params):
256 Show error message on stderr
258 msg.error(self.chart_name, *params)
260 def debug(self, *params):
262 Show debug message on stderr
264 msg.debug(self.chart_name, *params)
266 def info(self, *params):
268 Show information message on stderr
270 msg.info(self.chart_name, *params)
277 msg.error("Service " + str(self.__module__) + "doesn't implement check() function")
285 msg.error("Service " + str(self.__module__) + "doesn't implement create() function?")
288 def update(self, interval):
294 msg.error("Service " + str(self.__module__) + "doesn't implement update() function")
298 class SimpleService(BaseService):
299 def __init__(self, configuration=None, name=None):
301 self.definitions = {}
302 BaseService.__init__(self, configuration=configuration, name=name)
306 Get raw data from http request
311 def _formatted_data(self):
313 Format data received from http request
329 data = self._formatted_data()
334 for name in self.order:
335 options = self.definitions[name]['options'] + [self.priority + idx, self.update_every]
336 self.chart(self.__module__ + "_" + self.name + "." + name, *options)
337 # check if server has this datapoint
338 for line in self.definitions[name]['lines']:
340 self.dimension(*line)
346 def update(self, interval):
352 data = self._formatted_data()
357 for chart in self.order:
358 if self.begin(self.__module__ + "_" + str(self.name) + "." + chart, interval):
360 for dim in self.definitions[chart]['lines']:
362 self.set(dim[0], data[dim[0]])
372 class UrlService(SimpleService):
373 def __init__(self, configuration=None, name=None):
374 # definitions are created dynamically in create() method based on 'charts' dictionary. format:
376 # 'chart_name_in_netdata' : [ charts['chart_name_in_netdata']['lines']['name'] ]
379 SimpleService.__init__(self, configuration=configuration, name=name)
383 Get raw data from http request
388 f = urlopen(self.url, timeout=self.update_every)
389 raw = f.read().decode('utf-8')
390 except Exception as e:
391 msg.error(self.__module__, str(e))
401 Format configuration data and try to connect to server
404 if self.name is None or self.name == str(None):
407 self.name = str(self.name)
409 self.url = str(self.configuration['url'])
410 except (KeyError, TypeError):
413 if self._formatted_data() is not None: