Advertisement
Guest User

Untitled

a guest
May 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. """ Collect status information for Windows services
  2. """
  3. # project
  4. from checks import AgentCheck
  5. from checks.wmi_check import WinWMICheck
  6. from utils.containers import hash_mutable
  7. from utils.timeout import TimeoutException
  8.  
  9.  
  10. class WindowsService(WinWMICheck):
  11. STATE_TO_VALUE = {
  12. 'Stopped': AgentCheck.CRITICAL,
  13. 'Start Pending': AgentCheck.WARNING,
  14. 'Stop Pending': AgentCheck.WARNING,
  15. 'Running': AgentCheck.OK,
  16. 'Continue Pending': AgentCheck.WARNING,
  17. 'Pause Pending': AgentCheck.WARNING,
  18. 'Paused': AgentCheck.WARNING,
  19. 'Unknown': AgentCheck.UNKNOWN
  20. }
  21. NAMESPACE = "root\\CIMV2"
  22. CLASS = "Win32_Service"
  23.  
  24. def __init__(self, name, init_config, agentConfig, instances):
  25. WinWMICheck.__init__(self, name, init_config, agentConfig, instances)
  26.  
  27. def check(self, instance):
  28. # Connect to the WMI provider
  29. host = instance.get('host', "localhost")
  30. user = instance.get('username', "")
  31. password = instance.get('password', "")
  32. services = instance.get('services', [])
  33.  
  34. instance_hash = hash_mutable(instance)
  35. instance_key = self._get_instance_key(host, self.NAMESPACE, self.CLASS, instance_hash)
  36. tags = [] if (host == "localhost" or host == ".") else [u'host:{0}'.format(host)]
  37.  
  38. if len(services) == 0:
  39. raise Exception('No services defined in windows_service.yaml')
  40.  
  41. properties = ["Name", "State"]
  42. filters = map(lambda x: {"Name": tuple(('=', x))}, services)
  43. wmi_sampler = self._get_wmi_sampler(
  44. instance_key,
  45. self.CLASS, properties,
  46. filters=filters,
  47. host=host, namespace=self.NAMESPACE,
  48. username=user, password=password
  49. )
  50.  
  51. try:
  52. # Sample, extract & submit metrics
  53. wmi_sampler.sample()
  54. except TimeoutException:
  55. self.log.warning(
  56. u"[WinService] WMI query timed out."
  57. u" class={wmi_class} - properties={wmi_properties} -"
  58. u" filters={filters} - tags={tags}".format(
  59. wmi_class=self.CLASS, wmi_properties=properties,
  60. filters=filters, tags=tags
  61. )
  62. )
  63. else:
  64. self._process_services(wmi_sampler, services, tags)
  65.  
  66. def _process_services(self, wmi_sampler, services, tags):
  67. collected_services_by_names = {sc['Name'].lower(): sc for sc in wmi_sampler}
  68. self.log.debug('collected_services_by_names: {}'.format(collected_services_by_names))
  69.  
  70. for service in services:
  71. service_lower = service.lower()
  72. if service_lower in collected_services_by_names:
  73. wmi_obj = collected_services_by_names[service_lower]
  74. sc_name = wmi_obj['Name']
  75. status = self.STATE_TO_VALUE.get(wmi_obj["state"], AgentCheck.UNKNOWN)
  76. self.service_check("windows_service.state", status,
  77. tags=tags + ['service:{0}'.format(sc_name)])
  78.  
  79. else:
  80. self.service_check("windows_service.state", AgentCheck.CRITICAL,
  81. tags=tags + ['service:{0}'.format(service)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement