]> arthur.barton.de Git - netdata.git/blob - python.d/python_modules/base.py
even more comments
[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     """
10     Prototype of Service class.
11     Implemented basic functionality to run jobs by `python.d.plugin`
12     """
13     def __init__(self, configuration=None, name=None):
14         """
15         This needs to be initialized in child classes
16         :param configuration: dict
17         :param name: str
18         """
19         if configuration is None:
20             self.error("BaseService: no configuration parameters supplied. Cannot create Service.")
21             raise RuntimeError
22         else:
23             self._extract_base_config(configuration)
24             self.timetable = {}
25             self.create_timetable()
26             self.execution_name = ""
27
28     def _extract_base_config(self, config):
29         """
30         Get basic parameters to run service
31         Minimum config:
32             config = {'update_every':1,
33                       'priority':100000,
34                       'retries':0}
35         :param config: dict
36         """
37         self.update_every = int(config['update_every'])
38         self.priority = int(config['priority'])
39         self.retries = int(config['retries'])
40         self.retries_left = self.retries
41
42     def create_timetable(self, freq=None):
43         """
44         Create service timetable.
45         `freq` is optional
46         Example:
47             timetable = {'last': 1466370091.3767564,
48                          'next': 1466370092,
49                          'freq': 1}
50         :param freq: int
51         """
52         if freq is None:
53             freq = self.update_every
54         now = time()
55         self.timetable = {'last': now,
56                           'next': now - (now % freq) + freq,
57                           'freq': freq}
58
59     @staticmethod
60     def error(msg, exception=""):
61         if exception != "":
62             exception = " " + str(exception).replace("\n"," ")
63         sys.stderr.write(str(msg)+exception+"\n")
64         sys.stderr.flush()
65
66     def check(self):
67         """
68         check() prototype
69         :return: boolean
70         """
71         self.error("Service " + str(self.__name__) + "doesn't implement check() function")
72         return False
73
74     def create(self):
75         """
76         create() prototype
77         :return: boolean
78         """
79         self.error("Service " + str(self.__name__) + "doesn't implement create() function?")
80         return False
81
82     def update(self):
83         """
84         update() prototype
85         :return: boolean
86         """
87         self.error("Service " + str(self.__name__) + "doesn't implement update() function")
88         return False