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