]> arthur.barton.de Git - netdata.git/blob - python.d/python_modules/base.py
Merge pull request #587 from Flums/patch-2
[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['update_every'])
40         self.priority = int(config['priority'])
41         self.retries = int(config['retries'])
42         self.retries_left = self.retries
43
44     def create_timetable(self, freq=None):
45         """
46         Create service timetable.
47         `freq` is optional
48         Example:
49             timetable = {'last': 1466370091.3767564,
50                          'next': 1466370092,
51                          'freq': 1}
52         :param freq: int
53         """
54         if freq is None:
55             freq = self.update_every
56         now = time()
57         self.timetable = {'last': now,
58                           'next': now - (now % freq) + freq,
59                           'freq': freq}
60
61     @staticmethod
62     def error(msg, exception=""):
63         if exception != "":
64             exception = " " + str(exception).replace("\n"," ")
65         sys.stderr.write(str(msg)+exception+"\n")
66         sys.stderr.flush()
67
68     def check(self):
69         """
70         check() prototype
71         :return: boolean
72         """
73         self.error("Service " + str(self.__name__) + "doesn't implement check() function")
74         return False
75
76     def create(self):
77         """
78         create() prototype
79         :return: boolean
80         """
81         self.error("Service " + str(self.__name__) + "doesn't implement create() function?")
82         return False
83
84     def update(self):
85         """
86         update() prototype
87         :return: boolean
88         """
89         self.error("Service " + str(self.__name__) + "doesn't implement update() function")
90         return False