def __init__(self, log_path, file_template, logger, hostname, event_func, gauge_func, freq): ''' :param log_path: string, path to the file to parse :param file_template: string, format of the perfdata file :param logger: Logger object :param hostname: string, name of the host this agent is running on :param event_func: function to create event, should accept dict :param gauge_func: function to report a gauge :param freq: int, size of bucket to aggregate perfdata metrics ''' self.log_path = log_path self.log = logger self.gen = None self.tail = None self.hostname = hostname self._event = event_func self._gauge = gauge_func self._line_parsed = 0 self._freq = freq if file_template is not None: self.compile_file_template(file_template) self.tail = TailFile(self.log, self.log_path, self._parse_line) self.gen = self.tail.tail(line_by_line=False, move_end=True) self.gen.next()
def check(self, agentConfig, move_end=True): if self.log_path and os.path.isfile(self.log_path): #reset metric points self._init_metrics() # Build our tail -f if self._gen is None: self._gen = TailFile(self.logger, self.log_path, self._parse_line).tail(line_by_line=False, move_end=move_end) # read until the end of file try: self._gen.next() self.logger.debug("Done ddforwarder check for file %s" % self.log_path) except StopIteration as e: self.logger.exception(e) self.logger.warn("Can't tail %s file" % self.log_path) return {'ddforwarder': self.metrics} else: self.logger.debug("Can't tail datadog forwarder log file: %s" % self.log_path) return {}
def check(self, agentConfig, move_end=True): if self.log_path: self._freq = int(agentConfig.get('check_freq', 15)) self._values = [] self._events = [] # Build our tail -f if self._gen is None: self._gen = TailFile(self.logger, self.log_path, self._line_parser).tail( line_by_line=False, move_end=move_end) # read until the end of file try: self._gen.next() self.logger.debug("Done dogstream check for file {0}".format( self.log_path)) self.logger.debug("Found {0} metric points".format( len(self._values))) except StopIteration as e: self.logger.exception(e) self.logger.warn("Can't tail %s file" % self.log_path) check_output = self._aggregate(self._values) if self._events: check_output.update({"dogstreamEvents": self._events}) self.logger.debug("Found {0} events".format(len(self._events))) return check_output else: return {}
def test_logrotate_copytruncate(self): from utils.tailfile import TailFile def line_parser(l): self.last_line = l tail = TailFile(logging.getLogger(), self.log_file.name, line_parser) self.assertEquals(tail._size, 0) # Write some data to the log file init_string = "hey there, I am a log\n" self.log_file.write(init_string) self.log_file.flush() # Consume from the tail gen = tail.tail(line_by_line=False, move_end=True) gen.next() # Verify that the tail consumed the data I wrote self.assertEquals(tail._size, len(init_string)) try: # Trigger a copytruncate logrotation on the log file self._trigger_logrotate() # Write a new line to the log file new_string = "I am shorter\n" self.log_file.write(new_string) self.log_file.flush() # Verify that the tail recognized the logrotation gen.next() self.assertEquals(self.last_line, new_string[:-1], self.last_line) except OSError: "logrotate is not present"
def __init__(self, log_path, file_template, logger, hostname, event_func, gauge_func, freq): self.log_path = log_path self.log = logger self.gen = None self.tail = None self.hostname = hostname self._event = event_func self._gauge = gauge_func self._line_parsed = 0 self._freq = freq if file_template is not None: self.compile_file_template(file_template) self.tail = TailFile(self.log, self.log_path, self._parse_line) self.gen = self.tail.tail(line_by_line=False, move_end=True) self.gen.next()