Skip to content

Commit

Permalink
Merge pull request #2379 from DataDog/jaime/iis_fix
Browse files Browse the repository at this point in the history
[IIS] Deal with BytesTransfered vs BytesTransferred 2008sp2 typo
  • Loading branch information
olivielpeau committed Mar 28, 2016
2 parents dc62b5c + 024a7af commit f4c3456
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 5 deletions.
25 changes: 20 additions & 5 deletions checks.d/iis.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
'''
Check the performance counters from IIS
'''
# 3p
import pythoncom

# project
from checks import AgentCheck
from checks.wmi_check import WinWMICheck, WMIMetric
from config import _is_affirmative
from utils.containers import hash_mutable
from utils.timeout import TimeoutException

Expand Down Expand Up @@ -57,13 +61,18 @@ def check(self, instance):
password = instance.get('password', "")
instance_tags = instance.get('tags', [])
sites = instance.get('sites', ['_Total'])
is_2008 = _is_affirmative(instance.get('is_2008', False))


instance_hash = hash_mutable(instance)
instance_key = self._get_instance_key(host, self.NAMESPACE, self.CLASS, instance_hash)
filters = map(lambda x: {"Name": tuple(('=', x))}, sites)

metrics_by_property, properties = self._get_wmi_properties(instance_key, self.METRICS, [])
if is_2008:
for idx, prop in enumerate(properties):
if prop == "TotalBytesTransferred".lower():
properties[idx] = "TotalBytesTransfered"

wmi_sampler = self._get_wmi_sampler(
instance_key,
Expand All @@ -87,6 +96,11 @@ def check(self, instance):
filters=filters, instance_tags=instance_tags
)
)
except pythoncom.com_error as e:
if '0x80041017' in str(e):
self.warning("You may be running IIS6/7 which reports metrics a \
little differently. Try enabling the is_2008 flag for this instance.")
raise e
else:
self._submit_events(wmi_sampler, sites)
self._submit_metrics(metrics, metrics_by_property)
Expand Down Expand Up @@ -153,13 +167,14 @@ def _submit_events(self, wmi_sampler, sites):

def _submit_metrics(self, wmi_metrics, metrics_by_property):
for m in wmi_metrics:
if m.name == "TotalBytesTransfered":
m.name = "TotalBytesTransferred"
elif m.name == "TotalConnectionAttemptsallinstances":
m.name = "TotalConnectionAttemptsAllinstances"
metric_name = m.name
# Windows 2008 sp2 reports it as TotalbytesTransfered
# instead of TotalBytesTransferred (single r)
if metric_name.lower() == "totalbytestransfered":
metric_name = "totalbytestransferred"
elif m.name not in metrics_by_property:
continue

metric, mtype = metrics_by_property[m.name]
metric, mtype = metrics_by_property[metric_name]
submittor = getattr(self, mtype)
submittor(metric, m.value, m.tags)
1 change: 1 addition & 0 deletions checks/libs/wmi/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def __init__(self, logger, class_name, property_names, filters="", host="localho
# - Frequency_PerfTime
# - Frequency_Object"
])

self.class_name = class_name
self.property_names = property_names
self.filters = filters
Expand Down
4 changes: 4 additions & 0 deletions conf.d/iis.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ instances:
# - host: MYREMOTESERVER
# username: MYREMOTESERVER\fred
# password: mysecretpassword
# is_2008: false # NOTE: because of a typo in IIS6/7 (typically on W2K8)
# where perfmon reports TotalBytesTransferred as
# TotalBytesTransfered, you may have to enable this
# to grab the IIS metrics in that environment.
# tags:
# - myapp2
# - east
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ServiceUptime 251
TotalBytesSent 0
TotalBytesReceived 0
TotalBytesTransfered 0
CurrentConnections 0
TotalFilesSent 0
TotalFilesReceived 0
TotalConnectionAttemptsallinstances 0
TotalGetRequests 0
TotalPostRequests 0
TotalHeadRequests 0
TotalPutRequests 0
TotalDeleteRequests 0
TotalOptionsRequests 0
TotalTraceRequests 0
TotalNotFoundErrors 0
TotalLockedErrors 0
TotalAnonymousUsers 0
TotalNonAnonymousUsers 0
TotalCGIRequests 0
TotalISAPIExtensionRequests 0
28 changes: 28 additions & 0 deletions tests/checks/mock/test_iis.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,31 @@ def test_check(self):
tags=["site:{0}".format(fail_site_name)], count=1)

self.coverage_report()

def test_check_2008(self):
"""
Returns the right metrics and service checks for 2008 IIS
"""
# Run check
config = {
'instances': [self.WIN_SERVICES_CONFIG]
}
config['instances'][0]['is_2008'] = True

self.run_check_twice(config)

# Test metrics

# normalize site-names
ok_site_name = re.sub(r"[,\+\*\-/()\[\]{}\s]", "_", config['instances'][0]['sites'][0])
fail_site_name = re.sub(r"[,\+\*\-/()\[\]{}\s]", "_", config['instances'][0]['sites'][1])
for mname in self.IIS_METRICS:
self.assertMetric(mname, tags=["mytag1", "mytag2", "site:{0}".format(ok_site_name)], count=1)

# Test service checks
self.assertServiceCheck('iis.site_up', status=AgentCheck.OK,
tags=["site:{0}".format(ok_site_name)], count=1)
self.assertServiceCheck('iis.site_up', status=AgentCheck.CRITICAL,
tags=["site:{0}".format(fail_site_name)], count=1)

self.coverage_report()
3 changes: 3 additions & 0 deletions tests/core/test_wmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ def ExecQuery(self, query, query_language, flags):
"TotalISAPIExtensionRequests from Win32_PerfFormattedData_W3SVC_WebService WHERE ( Name = 'Failing site' ) OR ( Name = 'Default Web Site' )"): # noqa
results += load_fixture("win32_perfformatteddata_w3svc_webservice", ("Name", "Default Web Site")) # noqa

if query == ("Select * from Win32_PerfFormattedData_W3SVC_WebService WHERE ( Name = 'Failing site' ) OR ( Name = 'Default Web Site' )"): # noqa
results += load_fixture("win32_perfformatteddata_w3svc_webservice_2008", ("Name", "Default Web Site")) # noqa

if query == ("Select Name,State from Win32_Service WHERE ( Name = 'WSService' ) OR ( Name = 'WinHttpAutoProxySvc' )"): # noqa
results += load_fixture("win32_service_up", ("Name", "WinHttpAutoProxySvc"))
results += load_fixture("win32_service_down", ("Name", "WSService"))
Expand Down

0 comments on commit f4c3456

Please sign in to comment.