Skip to content

Commit

Permalink
Add instance level tags to Nagios Events (#2778)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmuesch authored Dec 18, 2018
1 parent 98aac42 commit 09fb983
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 7 additions & 3 deletions nagios/datadog_checks/nagios/nagios.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def __init__(self, name, init_config, agentConfig, instances=None):
file_template=None,
logger=self.log,
hostname=self.hostname,
tags=custom_tag,
event_func=self.event,
gauge_func=self.gauge,
freq=check_freq,
Expand Down Expand Up @@ -266,7 +267,8 @@ def compile_file_template(self, file_template):


class NagiosEventLogTailer(NagiosTailer):
def __init__(self, log_path, file_template, logger, hostname, event_func, gauge_func, freq, passive_checks=False):
def __init__(self, log_path, file_template, logger, hostname, tags, event_func, gauge_func,
freq, passive_checks=False):
"""
:param log_path: string, path to the file to parse
:param file_template: string, format of the perfdata file
Expand All @@ -279,6 +281,7 @@ def __init__(self, log_path, file_template, logger, hostname, event_func, gauge_
"""

self.passive_checks = passive_checks
self.tags = tags
super(NagiosEventLogTailer, self).__init__(
log_path, file_template, logger, hostname, event_func, gauge_func, freq
)
Expand Down Expand Up @@ -319,7 +322,7 @@ def _parse_line(self, line):
# Chop parts we don't recognize
parts = parts[: len(fields._fields)]

event = self.create_event(tstamp, event_type, self.hostname, fields._make(parts))
event = self.create_event(tstamp, event_type, self.hostname, fields._make(parts), tags=self.tags)

self._event(event)
self.log.debug("Nagios event: %s" % (event))
Expand All @@ -329,7 +332,7 @@ def _parse_line(self, line):
self.log.exception("Unable to create a nagios event from line: [%s]" % (line))
return False

def create_event(self, timestamp, event_type, hostname, fields):
def create_event(self, timestamp, event_type, hostname, fields, tags=None):
"""Factory method called by the parsers
"""
# Agent6 expects a specific set of fields, so we need to place all
Expand All @@ -355,6 +358,7 @@ def create_event(self, timestamp, event_type, hostname, fields):
'event_type': event_type,
'msg_text': msg_text,
'source_type_name': SOURCE_TYPE_NAME,
'tags': tags
})

# if host is localhost, turn that into the internal host name
Expand Down
3 changes: 2 additions & 1 deletion nagios/tests/test_nagios.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_line_parser(self, aggregator):
"""

# Get the config
config, nagios_cfg = get_config("log_file={}\n".format(NAGIOS_TEST_LOG), events=True)
config, nagios_cfg = get_config("log_file={}\n".format(NAGIOS_TEST_LOG), events=True, tags=CUSTOM_TAGS)

# Set up the check
nagios = Nagios(CHECK_NAME, {}, {}, config['instances'])
Expand Down Expand Up @@ -70,6 +70,7 @@ def test_line_parser(self, aggregator):
elif t == "HOST DOWNTIME ALERT":
assert event["host"] is not None
assert event["downtime_start_stop"] in ("STARTED", "STOPPED")
assert CUSTOM_TAGS == event['tags']

assert counters["SERVICE ALERT"] == 301
assert counters["SERVICE NOTIFICATION"] == 120
Expand Down

0 comments on commit 09fb983

Please sign in to comment.