1 # -*- coding: utf-8 -*-
2 # Description: prototypes for netdata python.d modules
3 # Author: Pawel Krupa (paulfantom)
9 from urllib.request import urlopen
11 from urllib2 import urlopen
13 # from subprocess import STDOUT, PIPE, Popen
18 class BaseService(threading.Thread):
20 Prototype of Service class.
21 Implemented basic functionality to run jobs by `python.d.plugin`
23 def __init__(self, configuration=None, name=None):
25 This needs to be initialized in child classes
26 :param configuration: dict
29 threading.Thread.__init__(self)
30 self._data_stream = ""
34 self.priority = 140000
37 self.override_name = None
41 if configuration is None:
42 self.error("BaseService: no configuration parameters supplied. Cannot create Service.")
45 self._extract_base_config(configuration)
47 self.create_timetable()
49 def _extract_base_config(self, config):
51 Get basic parameters to run service
53 config = {'update_every':1,
59 self.override_name = config.pop('override_name')
62 self.update_every = int(config.pop('update_every'))
63 self.priority = int(config.pop('priority'))
64 self.retries = int(config.pop('retries'))
65 self.retries_left = self.retries
66 self.configuration = config
68 def create_timetable(self, freq=None):
70 Create service timetable.
73 timetable = {'last': 1466370091.3767564,
79 freq = self.update_every
81 self.timetable = {'last': now,
82 'next': now - (now % freq) + freq,
87 Executes self.update(interval) and draws run time chart.
88 Return value presents exit status of update()
92 # check if it is time to execute job update() function
93 if self.timetable['next'] > t_start:
94 msg.debug(self.chart_name + " will be run in " +
95 str(int((self.timetable['next'] - t_start) * 1000)) + " ms")
98 since_last = int((t_start - self.timetable['last']) * 1000000)
99 msg.debug(self.chart_name +
100 " ready to run, after " + str(int((t_start - self.timetable['last']) * 1000)) +
101 " ms (update_every: " + str(self.timetable['freq'] * 1000) +
102 " ms, latency: " + str(int((t_start - self.timetable['next']) * 1000)) + " ms)")
103 if not self.update(since_last):
106 self.timetable['next'] = t_end - (t_end % self.timetable['freq']) + self.timetable['freq']
108 # draw performance graph
109 run_time = str(int((t_end - t_start) * 1000))
110 run_time_chart = "BEGIN netdata.plugin_pythond_" + self.chart_name + " " + str(since_last) + '\n'
111 run_time_chart += "SET run_time = " + run_time + '\n'
112 run_time_chart += "END\n"
113 sys.stdout.write(run_time_chart)
114 msg.debug(self.chart_name + " updated in " + str(run_time) + " ms")
115 self.timetable['last'] = t_start
120 Runs job in thread. Handles retries.
121 Exits when job failed or timed out.
124 self.timetable['last'] = time.time()
127 status = self._run_once()
128 except Exception as e:
129 msg.error("Something wrong: " + str(e))
132 time.sleep(self.timetable['next'] - time.time())
133 self.retries_left = self.retries
136 if self.retries_left <= 0:
137 msg.error("no more retries. Exiting")
140 time.sleep(self.timetable['freq'])
142 def _line(self, instruction, *params):
144 Converts *params to string and joins them with one space between every one.
145 :param params: str/int/float
147 self._data_stream += instruction
157 self._data_stream += " " + p
158 self._data_stream += "\n"
160 def chart(self, type_id, name="", title="", units="", family="",
161 category="", charttype="line", priority="", update_every=""):
170 :param charttype: str
171 :param priority: int/str
172 :param update_every: int/str
174 self._charts.append(type_id)
175 self._line("CHART", type_id, name, title, units, family, category, charttype, priority, update_every)
177 def dimension(self, id, name=None, algorithm="absolute", multiplier=1, divisor=1, hidden=False):
179 Defines a new dimension for the chart
182 :param algorithm: str
183 :param multiplier: int/str
184 :param divisor: int/str
185 :param hidden: boolean
191 self.error("malformed dimension: multiplier is not a number:", multiplier)
196 self.error("malformed dimension: divisor is not a number:", divisor)
200 if algorithm not in ("absolute", "incremental", "percentage-of-absolute-row", "percentage-of-incremental-row"):
201 algorithm = "absolute"
203 self._dimensions.append(id)
205 self._line("DIMENSION", id, name, algorithm, multiplier, divisor, "hidden")
207 self._line("DIMENSION", id, name, algorithm, multiplier, divisor)
209 def begin(self, type_id, microseconds=0):
213 :param microseconds: int
216 if type_id not in self._charts:
217 self.error("wrong chart type_id:", type_id)
222 self.error("malformed begin statement: microseconds are not a number:", microseconds)
225 self._line("BEGIN", type_id, microseconds)
228 def set(self, id, value):
230 Set value to dimension
232 :param value: int/float
235 if id not in self._dimensions:
236 self.error("wrong dimension id:", id)
239 value = str(int(value))
241 self.error("cannot set non-numeric value:", value)
243 self._line("SET", id, "=", value)
251 Upload new data to netdata
253 print(self._data_stream)
254 self._data_stream = ""
256 def error(self, *params):
258 Show error message on stderr
260 msg.error(self.chart_name, *params)
262 def debug(self, *params):
264 Show debug message on stderr
266 msg.debug(self.chart_name, *params)
268 def info(self, *params):
270 Show information message on stderr
272 msg.info(self.chart_name, *params)
279 msg.error("Service " + str(self.__module__) + "doesn't implement check() function")
287 msg.error("Service " + str(self.__module__) + "doesn't implement create() function?")
290 def update(self, interval):
296 msg.error("Service " + str(self.__module__) + "doesn't implement update() function")
300 class SimpleService(BaseService):
301 def __init__(self, configuration=None, name=None):
303 self.definitions = {}
304 BaseService.__init__(self, configuration=configuration, name=name)
308 Get raw data from http request
313 def _format_data(self):
315 Format data received from http request
331 data = self._format_data()
336 for name in self.order:
337 options = self.definitions[name]['options'] + [self.priority + idx, self.update_every]
338 self.chart(self.__module__ + "_" + self.name + "." + name, *options)
339 # check if server has this datapoint
340 for line in self.definitions[name]['lines']:
342 self.dimension(*line)
348 def update(self, interval):
354 data = self._format_data()
359 for chart in self.order:
360 if self.begin(self.__module__ + "_" + str(self.name) + "." + chart, interval):
362 for dim in self.definitions[chart]['lines']:
364 self.set(dim[0], data[dim[0]])
374 class UrlService(SimpleService):
375 def __init__(self, configuration=None, name=None):
376 # definitions are created dynamically in create() method based on 'charts' dictionary. format:
378 # 'chart_name_in_netdata' : [ charts['chart_name_in_netdata']['lines']['name'] ]
381 SimpleService.__init__(self, configuration=configuration, name=name)
385 Get raw data from http request
390 f = urlopen(self.url, timeout=self.update_every)
391 raw = f.read().decode('utf-8')
392 except Exception as e:
393 msg.error(self.__module__, str(e))
403 Format configuration data and try to connect to server
406 if self.name is None or self.name == str(None):
409 self.name = str(self.name)
411 self.url = str(self.configuration['url'])
412 except (KeyError, TypeError):
415 if self._format_data() is not None:
421 class LogService(SimpleService):
422 def __init__(self, configuration=None, name=None):
423 # definitions are created dynamically in create() method based on 'charts' dictionary. format:
425 # 'chart_name_in_netdata' : [ charts['chart_name_in_netdata']['lines']['name'] ]
428 self._last_position = 0
429 # self._log_reader = None
430 SimpleService.__init__(self, configuration=configuration, name=name)
431 self.retries = 100000 # basically always retry
436 if os.path.getsize(self.log_path) < self._last_position:
437 self._last_position = 0
438 elif os.path.getsize(self.log_path) == self._last_position:
440 with open(self.log_path, "r") as fp:
441 fp.seek(self._last_position)
442 for i, line in enumerate(fp):
444 self._last_position = fp.tell()
445 except Exception as e:
446 msg.error(self.__module__, str(e))
453 if self.name is not None or self.name != str(None):
456 self.name = str(self.name)
458 self.log_path = str(self.configuration['path'])
459 except (KeyError, TypeError):
460 self.error("No path to log specified. Using: '" + self.log_path + "'")
462 if os.access(self.log_path, os.R_OK):
465 self.error("Cannot access file: '" + self.log_path + "'")
469 status = SimpleService.create(self)
470 self._last_position = 0