File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/__pycache__/win_pdh.cpython-310.pyc
o
�N�g\7 � @ s� d Z ddlZddlZddlZddlmZ zddlZddlZdZ W n e
y+ dZ Y nw e�e�Z
dZdd� ZG d d
� d
�Zdd� Zd
d� Zdd� Zdd� Zddd�Zdd� Zdd� ZdS )a&
Salt Util for getting system information with the Performance Data Helper (pdh).
Counter information is gathered from current activity or log files.
Usage:
.. code-block:: python
import salt.utils.win_pdh
# Get a list of Counter objects
salt.utils.win_pdh.list_objects()
# Get a list of ``Processor`` instances
salt.utils.win_pdh.list_instances('Processor')
# Get a list of ``Processor`` counters
salt.utils.win_pdh.list_counters('Processor')
# Get the value of a single counter
# \Processor(*)\% Processor Time
salt.utils.win_pdh.get_counter('Processor', '*', '% Processor Time')
# Get the values of multiple counters
counter_list = [('Processor', '*', '% Processor Time'),
('System', None, 'Context Switches/sec'),
('Memory', None, 'Pages/sec'),
('Server Work Queues', '*', 'Queue Length')]
salt.utils.win_pdh.get_counters(counter_list)
# Get all counters for the Processor object
salt.utils.win_pdh.get_all_counters('Processor')
� N)�CommandExecutionErrorTFZpdhc C s t jj�� sdS tsdS tS )z8
Only works on Windows systems with the PyWin32
)Fz$salt.utils.win_pdh: Requires Windows)Fz,salt.utils.win_pdh: Missing required modules)�salt�utils�platformZ
is_windows�HAS_WINDOWS_MODULES�__virtualname__� r r �F/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/win_pdh.py�__virtual__; s
r
c @ s� e Zd ZdZdZdZdZdZdZdZ dZ
dZdZd Z
d
ZdZd Zd
ZdZdZd
ZdZdZd ZdZdZdZdZdZdZdZdZdZ dZ!dZ"dZ#dd� Z$e%e$�Z$dd� Z&dd� Z'dd � Z(d!d"� Z)d#d$� Z*d%d&� Z+d'S )(�CounterzU
Counter object
Has enumerations and functions for working with counters
r � i i i i i i i i i i i i i i @ i � i i i i i 0i @c C sD t �d| |d||fd�}t �|�dkrt|| |||�S td|� ���)a�
Makes a fully resolved counter path. Counter names are formatted like
this:
``\Processor(*)\% Processor Time``
The above breaks down like this:
obj = 'Processor'
instance = '*'
counter = '% Processor Time'
Args:
obj (str):
The top level object
instance (str):
The instance of the object
instance_index (int):
The index of the instance. Can usually be 0
counter (str):
The name of the counter
Returns:
Counter: A Counter object with the path if valid
Raises:
CommandExecutionError: If the path is invalid
Nr zInvalid counter specified: )�win32pdhZMakeCounterPathZValidatePathr r )�obj�instance�instance_index�counter�pathr r r �
build_counter� s !�zCounter.build_counterc C s4 || _ || _|| _|| _|| _d | _d | _d | _d S �N)r r r �indexr �handle�info�type)�selfr r r r r r r r �__init__� s
zCounter.__init__c C s t �|| j�| _dS )z�
Add the current path to the query
Args:
query (obj):
The handle to the query to add the counter
N)r
Z
AddCounterr r )r �queryr r r �add_to_query� s zCounter.add_to_queryc C s� | j sDt�| jd�}|d |d |d |d |d |d |d |d d |d d |d d |d d |d d |d d |d d
�| _ | j S )z�
Get information about the counter
.. note::
GetCounterInfo sometimes crashes in the wrapper code. Fewer crashes
if this is called after sampling data.
r � � � � � � � � )r �versionZscaleZ
default_scaleZ user_dataZquery_user_data� full_pathZmachine_nameZobject_nameZ
instance_nameZparent_instancer �counter_nameZexplain_text)r r
ZGetCounterInfor )r Zcir r r �get_info� s$
�zCounter.get_infoc C s t �| jt j�\}}|| _|S )z`
Return the counter value
Returns:
long: The counter value
)r
ZGetFormattedCounterValuer ZPDH_FMT_DOUBLEr )r Zcounter_type�valuer r r r) � s
�z
Counter.valuec C sT | � � d }g }t| �D ]}|�d�r't| |�}|r'||@ r'|�|dd� � q|S )z~
Returns the names of the flags that are set in the Type field
It can be used to format the counter.
r ZPERF_r! N)r( �dir�
startswith�getattr�append)r r Z type_list�member�bitr r r �type_string� s
�zCounter.type_stringc C s | j S r )r )r r r r �__str__� s zCounter.__str__N),�__name__�
__module__�__qualname__�__doc__ZPERF_SIZE_DWORDZPERF_SIZE_LARGEZPERF_SIZE_ZEROZPERF_SIZE_VARIABLE_LENZPERF_TYPE_NUMBERZPERF_TYPE_COUNTERZPERF_TYPE_TEXTZPERF_TYPE_ZEROZPERF_NUMBER_HEXZPERF_NUMBER_DECIMALZPERF_NUMBER_DEC_1000ZPERF_COUNTER_VALUEZPERF_COUNTER_RATEZPERF_COUNTER_FRACTIONZPERF_COUNTER_BASEZPERF_COUNTER_ELAPSEDZPERF_COUNTER_QUEUE_LENZPERF_COUNTER_HISTOGRAMZPERF_TEXT_UNICODEZPERF_TEXT_ASCIIZPERF_TIMER_TICKZPERF_TIMER_100NSZPERF_OBJECT_TIMERZPERF_DELTA_COUNTERZPERF_DELTA_BASEZPERF_INVERSE_COUNTERZPERF_MULTI_COUNTERZPERF_DISPLAY_NO_SUFFIXZPERF_DISPLAY_PER_SECZPERF_DISPLAY_PERCENTZPERF_DISPLAY_SECONDSZPERF_DISPLAY_NO_SHOWr �staticmethodr r r( r) r0 r1 r r r r r H sT (
r c C s t t�dddd��S )zu
Get a list of available counter objects on the system
Returns:
list: A list of counter objects
N���r )�sortedr
ZEnumObjectsr r r r �list_objects� s r9 c C s t �dd| dd�d S )a#
Get a list of counters available for the object
Args:
obj (str):
The name of the counter object. You can get a list of valid names
using the ``list_objects`` function
Returns:
list: A list of counters available to the passed object
Nr7 r �r
�EnumObjectItems�r r r r �
list_counters � r= c C s t �dd| dd�d S )a%
Get a list of instances available for the object
Args:
obj (str):
The name of the counter object. You can get a list of valid names
using the ``list_objects`` function
Returns:
list: A list of instances available to the passed object
Nr7 r r r: r<