Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iis] adds iis_host tag to metrics #3294

Merged
merged 10 commits into from
Mar 26, 2019
11 changes: 11 additions & 0 deletions iis/datadog_checks/iis/iis.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ class IIS(PDHBaseCheck):
def __init__(self, name, init_config, agentConfig, instances):
PDHBaseCheck.__init__(self, name, init_config, agentConfig, instances=instances, counter_list=DEFAULT_COUNTERS)

def get_iishost(self, instance):
inst_host = instance.get("host")
if inst_host in [".", "localhost", "127.0.0.1", None]:
# Use agent's hostname if connecting to local machine.
iis_host = self.hostname
else:
iis_host = inst_host
return "iis_host:{}".format(self.normalize(iis_host))
ian28223 marked this conversation as resolved.
Show resolved Hide resolved

def check(self, instance):
sites = instance.get('sites')
if sites is None:
Expand All @@ -73,6 +82,7 @@ def check(self, instance):
tags = []
if key in self._tags:
tags = list(self._tags[key])
tags.append(self.get_iishost(instance))

try:
if not counter.is_single_instance():
Expand Down Expand Up @@ -114,5 +124,6 @@ def check(self, instance):
tags = []
if key in self._tags:
tags = list(self._tags[key])
tags.append(self.get_iishost(instance))
tags.append("site:{}".format(self.normalize(site)))
self.service_check(self.SERVICE_CHECK, AgentCheck.CRITICAL, tags)
27 changes: 16 additions & 11 deletions iis/tests/test_iis.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ def test_basic_check(aggregator):
instance = MINIMAL_INSTANCE
c = IIS(CHECK_NAME, {}, {}, [instance])
c.check(instance)
iis_host = c.get_iishost(instance)

site_tags = ['Default_Web_Site', 'Exchange_Back_End', 'Total']
for metric_def in DEFAULT_COUNTERS:
metric = metric_def[3]
for site_tag in site_tags:
aggregator.assert_metric(metric, tags=["site:{0}".format(site_tag)], count=1)
aggregator.assert_metric(metric, tags=["site:{0}".format(site_tag), iis_host], count=1)

for site_tag in site_tags:
aggregator.assert_service_check('iis.site_up', IIS.OK,
tags=["site:{0}".format(site_tag)], count=1)
tags=["site:{0}".format(site_tag), iis_host], count=1)

aggregator.assert_all_metrics_covered()

Expand All @@ -44,19 +45,20 @@ def test_check_on_specific_websites(aggregator):
instance = INSTANCE
c = IIS(CHECK_NAME, {}, {}, [instance])
c.check(instance)
iis_host = c.get_iishost(instance)

site_tags = ['Default_Web_Site', 'Exchange_Back_End', 'Total']
for metric_def in DEFAULT_COUNTERS:
metric = metric_def[3]
for site_tag in site_tags:
aggregator.assert_metric(metric, tags=["site:{0}".format(site_tag)], count=1)
aggregator.assert_metric(metric, tags=["site:{0}".format(site_tag), iis_host], count=1)

for site_tag in site_tags:
aggregator.assert_service_check('iis.site_up', IIS.OK,
tags=["site:{0}".format(site_tag)], count=1)
tags=["site:{0}".format(site_tag), iis_host], count=1)

aggregator.assert_service_check('iis.site_up', IIS.CRITICAL,
tags=["site:{0}".format('Non_Existing_Website')], count=1)
tags=["site:{0}".format('Non_Existing_Website'), iis_host], count=1)

aggregator.assert_all_metrics_covered()

Expand All @@ -66,8 +68,9 @@ def test_service_check_with_invalid_host(aggregator):
instance = INVALID_HOST_INSTANCE
c = IIS(CHECK_NAME, {}, {}, [instance])
c.check(instance)
iis_host = c.get_iishost(instance)

aggregator.assert_service_check('iis.site_up', IIS.CRITICAL, tags=["site:{0}".format('Total')])
aggregator.assert_service_check('iis.site_up', IIS.CRITICAL, tags=["site:{0}".format('Total'), iis_host])


@pytest.mark.usefixtures('pdh_mocks_fixture')
Expand All @@ -78,6 +81,7 @@ def test_check(aggregator):
instance = WIN_SERVICES_CONFIG
c = IIS(CHECK_NAME, {}, {}, [instance])
c.check(instance)
iis_host = c.get_iishost(instance)

# Test metrics
# ... normalize site-names
Expand All @@ -88,13 +92,13 @@ def test_check(aggregator):
for site_name in [default_site_name, ok_site_name, 'Total']:
for metric_def in DEFAULT_COUNTERS:
mname = metric_def[3]
aggregator.assert_metric(mname, tags=["mytag1", "mytag2", "site:{0}".format(site_name)], count=1)
aggregator.assert_metric(mname, tags=["mytag1", "mytag2", "site:{0}".format(site_name), iis_host], count=1)

aggregator.assert_service_check('iis.site_up', status=IIS.OK,
tags=["mytag1", "mytag2", "site:{0}".format(site_name)], count=1)
tags=["mytag1", "mytag2", "site:{0}".format(site_name), iis_host], count=1)

aggregator.assert_service_check('iis.site_up', status=IIS.CRITICAL,
tags=["mytag1", "mytag2", "site:{0}".format(fail_site_name)], count=1)
tags=["mytag1", "mytag2", "site:{0}".format(fail_site_name), iis_host], count=1)

# Check completed with no warnings
# self.assertFalse(logger.warning.called)
Expand All @@ -111,15 +115,16 @@ def test_check_without_sites_specified(aggregator):
instance = WIN_SERVICES_MINIMAL_CONFIG
c = IIS(CHECK_NAME, {}, {}, [instance])
c.check(instance)
iis_host = c.get_iishost(instance)

site_tags = ['Default_Web_Site', 'Exchange_Back_End', 'Total']
for metric_def in DEFAULT_COUNTERS:
mname = metric_def[3]

for site_tag in site_tags:
aggregator.assert_metric(mname, tags=["mytag1", "mytag2", "site:{0}".format(site_tag)], count=1)
aggregator.assert_metric(mname, tags=["mytag1", "mytag2", "site:{0}".format(site_tag), iis_host], count=1)

for site_tag in site_tags:
aggregator.assert_service_check('iis.site_up', status=IIS.OK,
tags=["mytag1", "mytag2", "site:{0}".format(site_tag)], count=1)
tags=["mytag1", "mytag2", "site:{0}".format(site_tag), iis_host], count=1)
aggregator.assert_all_metrics_covered()