mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Fix win_pdh.get_counters failing when no data are available
This commit is contained in:
parent
d4e2a00bd2
commit
a2f0296b35
3 changed files with 36 additions and 7 deletions
1
changelog/58327.fixed
Normal file
1
changelog/58327.fixed
Normal file
|
@ -0,0 +1 @@
|
|||
Return empty dict on win_pdh.get_counters rather than raising exception when no data are available
|
|
@ -1,4 +1,3 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
r"""
|
||||
Salt Util for getting system information with the Performance Data Helper (pdh).
|
||||
Counter information is gathered from current activity or log files.
|
||||
|
@ -35,7 +34,6 @@ Usage:
|
|||
|
||||
# https://docs.microsoft.com/en-us/windows/desktop/perfctrs/using-the-pdh-functions-to-consume-counter-data
|
||||
# Import python libs
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
# https://www.cac.cornell.edu/wiki/index.php?title=Performance_Data_Helper_in_Python_with_win32pdh
|
||||
import logging
|
||||
|
@ -74,7 +72,7 @@ def __virtual__():
|
|||
return __virtualname__
|
||||
|
||||
|
||||
class Counter(object):
|
||||
class Counter:
|
||||
"""
|
||||
Counter object
|
||||
Has enumerations and functions for working with counters
|
||||
|
@ -172,7 +170,7 @@ class Counter(object):
|
|||
)
|
||||
if win32pdh.ValidatePath(path) == 0:
|
||||
return Counter(path, obj, instance, instance_index, counter)
|
||||
raise CommandExecutionError("Invalid counter specified: {0}".format(path))
|
||||
raise CommandExecutionError("Invalid counter specified: {}".format(path))
|
||||
|
||||
build_counter = staticmethod(build_counter)
|
||||
|
||||
|
@ -421,6 +419,14 @@ def get_counters(counter_list):
|
|||
else:
|
||||
raise
|
||||
|
||||
except pywintypes.error as exc:
|
||||
if exc.strerror == "No data to return.":
|
||||
# Sometimess, win32pdh.CollectQueryData can err
|
||||
# so just ignore it
|
||||
return {}
|
||||
else:
|
||||
raise
|
||||
|
||||
finally:
|
||||
win32pdh.CloseQuery(query)
|
||||
|
||||
|
|
|
@ -1,12 +1,18 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
import salt.utils.platform
|
||||
import salt.utils.win_pdh as win_pdh
|
||||
from tests.support.helpers import slowTest
|
||||
from tests.support.mock import patch
|
||||
from tests.support.unit import TestCase, skipIf
|
||||
|
||||
try:
|
||||
import pywintypes
|
||||
|
||||
HAS_WIN32 = True
|
||||
except ImportError:
|
||||
HAS_WIN32 = False
|
||||
|
||||
|
||||
@skipIf(not HAS_WIN32, "Requires pywin32")
|
||||
@skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
|
||||
class WinPdhTestCase(TestCase):
|
||||
@slowTest
|
||||
|
@ -95,3 +101,19 @@ class WinPdhTestCase(TestCase):
|
|||
def test_get_counter(self):
|
||||
results = win_pdh.get_counter("Processor", "*", "% Processor Time")
|
||||
self.assertTrue("\\Processor(*)\\% Processor Time" in results)
|
||||
|
||||
@patch("win32pdh.CollectQueryData")
|
||||
def test_get_counters_no_data_to_return(self, mock_query):
|
||||
mock_query.side_effect = pywintypes.error(
|
||||
-2147481643, "CollectQueryData", "No data to return."
|
||||
)
|
||||
counter_list = [
|
||||
("Memory", None, "Available Bytes"),
|
||||
("Paging File", "*", "% Usage"),
|
||||
("Processor", "*", "% Processor Time"),
|
||||
("Server", None, "Work Item Shortages"),
|
||||
("Server Work Queues", "*", "Queue Length"),
|
||||
("System", None, "Context Switches/sec"),
|
||||
]
|
||||
results = win_pdh.get_counters(counter_list)
|
||||
assert results == {}
|
||||
|
|
Loading…
Add table
Reference in a new issue