Skip to content

Commit

Permalink
[wmi] instance key has to actually be unique to every instance.
Browse files Browse the repository at this point in the history
[wmi] fixing import typos

[wmi] fixing windows imports.
  • Loading branch information
truthbk committed Jan 21, 2016
1 parent 847c892 commit d1dfe50
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 6 deletions.
4 changes: 3 additions & 1 deletion checks.d/iis.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# project
from checks import AgentCheck
from checks.wmi_check import WinWMICheck, WMIMetric
from utils.containers import hash_mutable


class IIS(WinWMICheck):
Expand Down Expand Up @@ -57,7 +58,8 @@ def check(self, instance):
sites = instance.get('sites', ['_Total'])


instance_key = self._get_instance_key(host, self.NAMESPACE, self.CLASS)
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, [])
Expand Down
4 changes: 3 additions & 1 deletion checks.d/win32_event_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# project
from checks.wmi_check import WinWMICheck, to_time, from_time
from utils.containers import hash_mutable

SOURCE_TYPE_NAME = 'event viewer'
EVENT_TYPE = 'win32_log_event'
Expand Down Expand Up @@ -44,7 +45,8 @@ def check(self, instance):
event_ids = instance.get('event_id', [])
message_filters = instance.get('message_filters', [])

instance_key = self._get_instance_key(host, self.NAMESPACE, self.CLASS)
instance_hash = hash_mutable(instance)
instance_key = self._get_instance_key(host, self.NAMESPACE, self.CLASS, instance_hash)

# Store the last timestamp by instance
if instance_key not in self.last_ts:
Expand Down
4 changes: 3 additions & 1 deletion checks.d/windows_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# project
from checks import AgentCheck
from checks.wmi_check import WinWMICheck
from utils.containers import hash_mutable


class WindowsService(WinWMICheck):
Expand All @@ -29,7 +30,8 @@ def check(self, instance):
password = instance.get('password', "")
services = instance.get('services', [])

instance_key = self._get_instance_key(host, self.NAMESPACE, self.CLASS)
instance_hash = hash_mutable(instance)
instance_key = self._get_instance_key(host, self.NAMESPACE, self.CLASS, instance_hash)

if len(services) == 0:
raise Exception('No services defined in windows_service.yaml')
Expand Down
4 changes: 3 additions & 1 deletion checks.d/wmi_check.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# project
from checks.wmi_check import WinWMICheck
from utils.containers import hash_mutable


class WMICheck(WinWMICheck):
Expand Down Expand Up @@ -32,7 +33,8 @@ def check(self, instance):
constant_tags = instance.get('constant_tags')

# Create or retrieve an existing WMISampler
instance_key = self._get_instance_key(host, namespace, wmi_class)
instance_hash = hash_mutable(instance)
instance_key = self._get_instance_key(host, namespace, wmi_class, instance_hash)

metric_name_and_type_by_property, properties = \
self._get_wmi_properties(instance_key, metrics, tag_queries)
Expand Down
8 changes: 6 additions & 2 deletions checks/wmi_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,14 @@ def _submit_metrics(self, metrics, metric_name_and_type_by_property):

func(metric_name, metric.value, metric.tags)

def _get_instance_key(self, host, namespace, wmi_class):
def _get_instance_key(self, host, namespace, wmi_class, other=None):
"""
Return an index key for a given instance. Usefull for caching.
Return an index key for a given instance. Useful for caching.
"""
if other:
return "{host}:{namespace}:{wmi_class}-{other}".format(
host=host, namespace=namespace, wmi_class=wmi_class, other=other
)
return "{host}:{namespace}:{wmi_class}".format(
host=host, namespace=namespace, wmi_class=wmi_class,
)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
'checks.wmi_check',
'checks.libs.vmware.*',
'httplib2',
'utils.containers',

# pup
'tornado.websocket',
Expand Down
17 changes: 17 additions & 0 deletions utils/containers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@


def freeze(o):
"""
Freezes any mutable object including dictionaries and lists for hashing.
Accepts nested dictionaries.
"""
if isinstance(o, dict):
return frozenset({k:freeze(v) for k,v in o.items()}.items())

if isinstance(o, list):
return tuple([freeze(v) for v in o])

return o

def hash_mutable(m):
return hash(freeze(m))

0 comments on commit d1dfe50

Please sign in to comment.