From: paulfantom Date: Fri, 1 Jul 2016 10:59:34 +0000 (+0200) Subject: LogService prototype X-Git-Tag: v1.3.0~97^2~7 X-Git-Url: https://arthur.barton.de/gitweb/?a=commitdiff_plain;h=dfed3e01f79879cbac5ad12e60bc0475a9247328;p=netdata.git LogService prototype --- diff --git a/python.d/python_modules/base.py b/python.d/python_modules/base.py index eaee6286..0a52350c 100644 --- a/python.d/python_modules/base.py +++ b/python.d/python_modules/base.py @@ -4,11 +4,13 @@ import time import sys +import os try: from urllib.request import urlopen except ImportError: from urllib2 import urlopen +# from subprocess import STDOUT, PIPE, Popen import threading import msg @@ -414,3 +416,60 @@ class UrlService(SimpleService): return True else: return False + + +class LogService(SimpleService): + def __init__(self, configuration=None, name=None): + # definitions are created dynamically in create() method based on 'charts' dictionary. format: + # definitions = { + # 'chart_name_in_netdata' : [ charts['chart_name_in_netdata']['lines']['name'] ] + # } + self.log_path = "" + self._last_line = 0 + # self._log_reader = None + SimpleService.__init__(self, configuration=configuration, name=name) + + def _get_data(self): + # FIXME find faster solution of reading data. Maybe implement reading in subprocess? + # if self._log_reader is None: + # self._log_reader = Popen(['tail', '-F', self.log_path], stdout=PIPE, stderr=STDOUT) + # if self._log_reader.poll() is not None: + # self._log_reader = Popen(['tail', '-F', self.log_path], stdout=PIPE, stderr=STDOUT) + lines = [] + last = 0 + try: + with open(self.log_path) as fp: + for i, line in enumerate(fp): + if i > self._last_line: + lines.append(line) + last = i + except Exception as e: + msg.error(self.__module__, str(e)) + if last != 0: + self._last_line = last + + if len(lines) != 0: + return lines + return None + + def check(self): + if self.name is None or self.name == str(None): + self.error("Log service doesn't have name.") + return False + else: + self.name = str(self.name) + try: + self.log_path = str(self.configuration['path']) + except (KeyError, TypeError): + self.error("Malformed path to log: '" + self.log_path + "'") + return False + + # FIXME Remove preventing of frequent log parsing + if self.update_every < 3: + self.update_every = 3 + + if os.access(self.log_path, os.R_OK): + return True + else: + self.error("Cannot access file. No read permission.") + return False