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