X-Git-Url: https://arthur.barton.de/gitweb/?a=blobdiff_plain;f=python.d%2Fpython_modules%2Fbase.py;h=2d275beca6523ed4b8def05faf821051430c5233;hb=77563deeed3129bbe1fd5a9996633b874bbffeff;hp=a60afb26f2550e5d71d72931935ef8b18ec1589c;hpb=575a5a8641d1723802eea2b7b6fa671c384e2b2b;p=netdata.git diff --git a/python.d/python_modules/base.py b/python.d/python_modules/base.py index a60afb26..2d275bec 100644 --- a/python.d/python_modules/base.py +++ b/python.d/python_modules/base.py @@ -1,9 +1,13 @@ # -*- coding: utf-8 -*- -# Description: base for netdata python.d plugins +# Description: prototypes for netdata python.d modules # Author: Pawel Krupa (paulfantom) from time import time import sys +try: + from urllib.request import urlopen +except ImportError: + from urllib2 import urlopen class BaseService(object): @@ -62,7 +66,7 @@ class BaseService(object): @staticmethod def error(msg, exception=""): if exception != "": - exception = " " + str(exception).replace("\n"," ") + exception = " " + str(exception).replace("\n", " ") sys.stderr.write(str(msg)+exception+"\n") sys.stderr.flush() @@ -71,7 +75,7 @@ class BaseService(object): check() prototype :return: boolean """ - self.error("Service " + str(self.__name__) + "doesn't implement check() function") + self.error("Service " + str(self.__module__) + "doesn't implement check() function") return False def create(self): @@ -79,13 +83,136 @@ class BaseService(object): create() prototype :return: boolean """ - self.error("Service " + str(self.__name__) + "doesn't implement create() function?") + self.error("Service " + str(self.__module__) + "doesn't implement create() function?") return False - def update(self): + def update(self, interval): """ update() prototype + :param interval: int :return: boolean """ - self.error("Service " + str(self.__name__) + "doesn't implement update() function") + self.error("Service " + str(self.__module__) + "doesn't implement update() function") return False + + +class UrlService(BaseService): + charts = {} + # charts definitions in format: + # charts = { + # 'chart_name_in_netdata': ( + # "parameters defining chart (passed to CHART statement)", + # [ # dimensions (lines) definitions + # ("dimension_name", "dimension parameters (passed to DIMENSION statement)") + # ]) + # } + order = [] + definitions = {} + # definitions are created dynamically in create() method based on 'charts' dictionary. format: + # definitions = { + # 'chart_name_in_netdata' : (charts['chart_name_in_netdata'][1], charts['chart_name_in_netdata'][0]) + # } + url = "" + + def _get_data(self): + """ + Get raw data from http request + :return: str + """ + try: + f = urlopen(self.url) + raw = f.read().decode('utf-8') + f.close() + except Exception as e: + self.error(self.__module__, str(e)) + return None + return raw + + def _formatted_data(self): + """ + Format data received from http request + :return: dict + """ + return {} + + def check(self): + """ + Format configuration data and try to connect to server + :return: boolean + """ + if self.name is None: + self.name = 'local' + else: + self.name = str(self.name) + try: + self.url = str(self.configuration['url']) + except (KeyError, TypeError): + pass + + if self._formatted_data() is not None: + return True + else: + return False + + def create(self): + """ + Create charts + :return: boolean + """ + for name in self.order: + if name not in self.charts: + continue + self.definitions[name] = [] + for line in self.charts[name]['lines']: + self.definitions[name].append(line['name']) + + idx = 0 + data = self._formatted_data() + if data is None: + return False + for name in self.order: + header = "CHART " + \ + self.__module__ + "_" + \ + self.name + "." + \ + name + " " + \ + self.charts[name]['options'] + " " + \ + str(self.priority + idx) + " " + \ + str(self.update_every) + content = "" + # check if server has this datapoint + for line in self.charts[name]['lines']: + if line['name'] in data: + content += "DIMENSION " + line['name'] + " " + line['options'] + "\n" + + if len(content) > 0: + print(header) + print(content) + idx += 1 + + if idx == 0: + return False + return True + + def update(self, interval): + """ + Update charts + :param interval: int + :return: boolean + """ + data = self._formatted_data() + if data is None: + return False + + for chart, dimensions in self.definitions.items(): + header = "BEGIN " + self.__module__ + "_" + str(self.name) + "." + chart + " " + str(interval) + c = "" + for dim in dimensions: + try: + c += "\nSET " + dim + " = " + str(data[dim]) + except KeyError: + pass + if len(c) != 0: + print(header + c) + print("END") + + return True