@cryptotaxi247 / netdata-1 / commits / dfed3e01f

LogService prototype

paulfantom committed Jul 1, 2016 at 12:59 UTC dfed3e01f79879cbac5ad12e60bc0475a9247328
1 file changed +59
python.d/python_modules/base.py
+59
@@ -4,11 +4,13 @@
4
5 import time
6 import sys
7 +import os
8 try:
9 from urllib.request import urlopen
10 except ImportError:
11 from urllib2 import urlopen
12
13 +# from subprocess import STDOUT, PIPE, Popen
14 import threading
15 import msg
16
@@ -414,3 +416,60 @@ class UrlService(SimpleService):
416 return True
417 else:
418 return False
419 +
420 +
421 +class LogService(SimpleService):
422 + def __init__(self, configuration=None, name=None):
423 + # definitions are created dynamically in create() method based on 'charts' dictionary. format:
424 + # definitions = {
425 + # 'chart_name_in_netdata' : [ charts['chart_name_in_netdata']['lines']['name'] ]
426 + # }
427 + self.log_path = ""
428 + self._last_line = 0
429 + # self._log_reader = None
430 + SimpleService.__init__(self, configuration=configuration, name=name)
431 +
432 + def _get_data(self):
433 + # FIXME find faster solution of reading data. Maybe implement reading in subprocess?
434 + # if self._log_reader is None:
435 + # self._log_reader = Popen(['tail', '-F', self.log_path], stdout=PIPE, stderr=STDOUT)
436 + # if self._log_reader.poll() is not None:
437 + # self._log_reader = Popen(['tail', '-F', self.log_path], stdout=PIPE, stderr=STDOUT)
438 + lines = []
439 + last = 0
440 + try:
441 + with open(self.log_path) as fp:
442 + for i, line in enumerate(fp):
443 + if i > self._last_line:
444 + lines.append(line)
445 + last = i
446 + except Exception as e:
447 + msg.error(self.__module__, str(e))
448 + if last != 0:
449 + self._last_line = last
450 +
451 + if len(lines) != 0:
452 + return lines
453 + return None
454 +
455 + def check(self):
456 + if self.name is None or self.name == str(None):
457 + self.error("Log service doesn't have name.")
458 + return False
459 + else:
460 + self.name = str(self.name)
461 + try:
462 + self.log_path = str(self.configuration['path'])
463 + except (KeyError, TypeError):
464 + self.error("Malformed path to log: '" + self.log_path + "'")
465 + return False
466 +
467 + # FIXME Remove preventing of frequent log parsing
468 + if self.update_every < 3:
469 + self.update_every = 3
470 +
471 + if os.access(self.log_path, os.R_OK):
472 + return True
473 + else:
474 + self.error("Cannot access file. No read permission.")
475 + return False