]> arthur.barton.de Git - netdata.git/blob - python.d/python_modules/base.py
Code cleanup
[netdata.git] / python.d / python_modules / base.py
1 # Description: base for netdata python.d plugins
2 # Author: Pawel Krupa (paulfantom)
3
4 from time import time
5 import sys
6
7
8 class BaseService(object):
9     def __init__(self, configuration=None):
10         if configuration is None:
11             self.error("BaseService: no configuration parameters supplied. Cannot create Service.")
12             raise RuntimeError
13         else:
14             self._extract_base_config(configuration)
15             self.timetable = {}
16             self.create_timetable()
17             self.execution_name = ""
18
19     def _extract_base_config(self, config):
20         self.update_every = int(config['update_every'])
21         self.priority = int(config['priority'])
22         self.retries = int(config['retries'])
23         self.retries_left = self.retries
24
25     def create_timetable(self, freq=None):
26         if freq is None:
27             freq = self.update_every
28         now = time()
29         self.timetable = {'last': now,
30                           'next': now - (now % freq) + freq,
31                           'freq': freq}
32
33     @staticmethod
34     def error(msg, exception=""):
35         if exception != "":
36             exception = " " + str(exception).replace("\n"," ")
37         sys.stderr.write(str(msg)+exception+"\n")
38         sys.stderr.flush()
39
40     def check(self):
41         # TODO notify about not overridden function
42         self.error("Where is your check()?")
43         return False
44
45     def create(self):
46         # TODO notify about not overridden function
47         self.error("Where is your create()?")
48         return False
49
50     def update(self):
51         # TODO notify about not overridden function
52         self.error("Where is your update()?")
53         return False