1 # -*- coding: utf-8 -*-
2 # Description: prototypes for netdata python.d modules
3 # Author: Pawel Krupa (paulfantom)
8 from urllib.request import urlopen
10 from urllib2 import urlopen
13 class BaseService(object):
15 Prototype of Service class.
16 Implemented basic functionality to run jobs by `python.d.plugin`
18 def __init__(self, configuration=None, name=None):
20 This needs to be initialized in child classes
21 :param configuration: dict
25 if configuration is None:
26 self.error("BaseService: no configuration parameters supplied. Cannot create Service.")
29 self._extract_base_config(configuration)
31 self.create_timetable()
34 def _extract_base_config(self, config):
36 Get basic parameters to run service
38 config = {'update_every':1,
43 self.update_every = int(config.pop('update_every'))
44 self.priority = int(config.pop('priority'))
45 self.retries = int(config.pop('retries'))
46 self.retries_left = self.retries
47 self.configuration = config
49 def create_timetable(self, freq=None):
51 Create service timetable.
54 timetable = {'last': 1466370091.3767564,
60 freq = self.update_every
62 self.timetable = {'last': now,
63 'next': now - (now % freq) + freq,
67 def error(msg, exception=""):
69 exception = " " + str(exception).replace("\n", " ")
70 sys.stderr.write(str(msg)+exception+"\n")
78 self.error("Service " + str(self.__module__) + "doesn't implement check() function")
86 self.error("Service " + str(self.__module__) + "doesn't implement create() function?")
89 def update(self, interval):
95 self.error("Service " + str(self.__module__) + "doesn't implement update() function")
99 class UrlService(BaseService):
100 def __init__(self, configuration=None, name=None):
102 # charts definitions in format:
104 # 'chart_name_in_netdata': {
105 # 'options': "parameters defining chart (passed to CHART statement)",
107 # { 'name': 'dimension_name',
108 # 'options': 'dimension parameters (passed to DIMENSION statement)"
113 self.definitions = {}
114 # definitions are created dynamically in create() method based on 'charts' dictionary. format:
116 # 'chart_name_in_netdata' : [ charts['chart_name_in_netdata']['lines']['name'] ]
119 BaseService.__init__(self, configuration=configuration, name=name)
124 Get raw data from http request
128 f = urlopen(self.url)
129 raw = f.read().decode('utf-8')
131 except Exception as e:
132 self.error(self.__module__, str(e))
136 def _formatted_data(self):
138 Format data received from http request
145 Format configuration data and try to connect to server
148 if self.name is None:
151 self.name = str(self.name)
153 self.url = str(self.configuration['url'])
154 except (KeyError, TypeError):
157 if self._formatted_data() is not None:
167 for name in self.order:
168 if name not in self.charts:
170 self.definitions[name] = []
171 for line in self.charts[name]['lines']:
172 self.definitions[name].append(line['name'])
175 data = self._formatted_data()
178 for name in self.order:
179 header = "CHART " + \
180 self.__module__ + "_" + \
183 self.charts[name]['options'] + " " + \
184 str(self.priority + idx) + " " + \
185 str(self.update_every)
187 # check if server has this datapoint
188 for line in self.charts[name]['lines']:
189 if line['name'] in data:
190 content += "DIMENSION " + line['name'] + " " + line['options'] + "\n"
201 def update(self, interval):
207 data = self._formatted_data()
211 for chart, dimensions in self.definitions.items():
212 header = "BEGIN " + self.__module__ + "_" + str(self.name) + "." + chart + " " + str(interval)
214 for dim in dimensions:
216 c += "\nSET " + dim + " = " + str(data[dim])