Skip to content

Commit

Permalink
Works with out-of-the-box perfdata
Browse files Browse the repository at this point in the history
  • Loading branch information
alq666 committed Jun 12, 2012
1 parent 6a9130c commit 955f077
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
5 changes: 4 additions & 1 deletion checks/datadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,10 @@ def init(cls, logger, config):
@staticmethod
def template_regex(file_template):
try:
regex = re.sub(r'\$([^\$]*)\$', r'(?P<\1>[^\$]*)', file_template)
# Escape characters that will be interpreted as regex bits
# e.g. [ and ] in "[SERVICEPERFDATA]"
regex = re.sub(r'[[\]*]', r'.', file_template)
regex = re.sub(r'\$([^\$]*)\$', r'(?P<\1>[^\$]*)', regex)
return re.compile(regex)
except Exception, e:
raise InvalidDataTemplate("%s (%s)"% (file_template, e))
Expand Down
1 change: 1 addition & 0 deletions tests/host-perfdata
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[HOSTPERFDATA] 1339511443 localhost 0.017 PING OK - Packet loss = 0%, RTA = 0.05 ms rta=0.048000ms;3000.000000;5000.000000;0.000000 pl=0%;80;100;0
4 changes: 4 additions & 0 deletions tests/service-perfdata
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[SERVICEPERFDATA] 1339511383 localhost Current Load 0.003 0.112 (Return code of 127 is out of bounds - plugin may be missing)
[SERVICEPERFDATA] 1339511443 localhost Current Users 0.030 0.182 USERS OK - 1 users currently logged in users=1;20;50;0
[SERVICEPERFDATA] 1339511503 localhost PING 4.006 0.126 PING OK - Packet loss = 0%, RTA = 0.06 ms rta=0.065000ms;100.000000;500.000000;0.000000 pl=0%;20;60;0
[SERVICEPERFDATA] 1339511563 localhost Root Partition 0.009 0.168 DISK OK - free space: / 4467 MB (64% inode=96%): /=2470MB;5852;6583;0;7315
37 changes: 35 additions & 2 deletions tests/test_datadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
import unittest
from tempfile import NamedTemporaryFile
import re
import os

from checks.datadog import Dogstreams, EventDefaults, point_sorter

log = logging.getLogger('datadog.test')
NAGIOS_TEST_HOST = os.path.join(os.path.dirname(__file__), "host-perfdata")
NAGIOS_TEST_SVC = os.path.join(os.path.dirname(__file__), "service-perfdata")
NAGIOS_TEST_HOST_TEMPLATE="[HOSTPERFDATA]\t$TIMET$\t$HOSTNAME$\t$HOSTEXECUTIONTIME$\t$HOSTOUTPUT$\t$HOSTPERFDATA$"
NAGIOS_TEST_SVC_TEMPLATE="[SERVICEPERFDATA]\t$TIMET$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICEEXECUTIONTIME$\t$SERVICELATENCY$\t$SERVICEOUTPUT$\t$SERVICEPERFDATA$"

def parse_stateful(logger, line, state):
"""Simple stateful parser"""
Expand Down Expand Up @@ -604,8 +609,36 @@ def test_host_perfdata(self):

self.assertEquals(expected_output, actual_output)

def test_alt_service_perfdata(self):
from checks.datadog import NagiosServicePerfData

self._write_nagios_config([
"service_perfdata_file=%s" % NAGIOS_TEST_SVC,
"service_perfdata_file_template=%s" % NAGIOS_TEST_SVC_TEMPLATE,
])

dogstream = Dogstreams.init(self.logger, self.agent_config)
self.assertEquals([NagiosServicePerfData], [d.__class__ for d in dogstream.dogstreams])
actual_output = dogstream.check(self.agent_config, move_end=False)

expected_output = {'dogstream': [('nagios.current_users.users', 1339511440, 1.0, {'metric_type': 'gauge', 'warn': '20', 'host_name': 'localhost', 'crit': '50', 'min': '0'}), ('nagios.ping.pl', 1339511500, 0.0, {'warn': '20', 'metric_type': 'gauge', 'host_name': 'localhost', 'min': '0', 'crit': '60', 'unit': '%'}), ('nagios.ping.rta', 1339511500, 0.065, {'warn': '100.000000', 'metric_type': 'gauge', 'host_name': 'localhost', 'min': '0.000000', 'crit': '500.000000', 'unit': 'ms'}), ('nagios.root_partition', 1339511560, 2470.0, {'min': '0', 'max': '7315', 'device_name': '/', 'warn': '5852', 'metric_type': 'gauge', 'host_name': 'localhost', 'crit': '6583', 'unit': 'MB'})]}
self.assertEquals(expected_output, actual_output)

def test_alt_host_perfdata(self):
from checks.datadog import NagiosHostPerfData

self._write_nagios_config([
"host_perfdata_file=%s" % NAGIOS_TEST_HOST,
"host_perfdata_file_template=%s" % NAGIOS_TEST_HOST_TEMPLATE,
])

dogstream = Dogstreams.init(self.logger, self.agent_config)
self.assertEquals([NagiosHostPerfData], [d.__class__ for d in dogstream.dogstreams])
actual_output = dogstream.check(self.agent_config, move_end=False)

expected_output = {'dogstream': [('nagios.host.pl', 1339511440, 0.0, {'warn': '80', 'metric_type': 'gauge', 'host_name': 'localhost', 'min': '0', 'crit': '100', 'unit': '%'}), ('nagios.host.rta', 1339511440, 0.048, {'warn': '3000.000000', 'metric_type': 'gauge', 'host_name': 'localhost', 'min': '0.000000', 'crit': '5000.000000', 'unit': 'ms'})]}
self.assertEquals(expected_output, actual_output)


if __name__ == '__main__':
# logging.basicConfig(format="%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s")
logging.basicConfig(format="%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s")
unittest.main()

0 comments on commit 955f077

Please sign in to comment.