Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _query_counters(self):
# https://docs.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhenumobjectitemsa#remarks
try:
# https://docs.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhenumobjectsa
# http://timgolden.me.uk/pywin32-docs/win32pdh__EnumObjects_meth.html
# https://mhammond.github.io/pywin32/win32pdh__EnumObjects_meth.html
win32pdh.EnumObjects(None, self._connection.server, win32pdh.PERF_DETAIL_WIZARD, True)
except pywintypes.error as error:
message = 'Error refreshing performance objects: {}'.format(error.strerror)
Expand Down Expand Up @@ -81,7 +81,7 @@ def _query_counters(self):

try:
# https://docs.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhcollectquerydata
# http://timgolden.me.uk/pywin32-docs/win32pdh__CollectQueryData_meth.html
# https://mhammond.github.io/pywin32/win32pdh__CollectQueryData_meth.html
win32pdh.CollectQueryData(self._connection.query_handle)
except pywintypes.error as error:
message = 'Error querying performance counters: {}'.format(error.strerror)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def add(self, resource, username, password):
name = resource.lpRemoteName
if name not in self.__resources:
# https://docs.microsoft.com/en-us/windows/win32/api/winnetwk/nf-winnetwk-wnetaddconnection2a
# http://timgolden.me.uk/pywin32-docs/win32wnet__WNetAddConnection2_meth.html
# https://mhammond.github.io/pywin32/win32wnet__WNetAddConnection2_meth.html
win32wnet.WNetAddConnection2(resource, password, username, 0)

self.__resources[name] += 1
Expand All @@ -38,7 +38,7 @@ def remove(self, resource):
del self.__resources[name]

# https://docs.microsoft.com/en-us/windows/win32/api/winnetwk/nf-winnetwk-wnetcancelconnection2a
# http://timgolden.me.uk/pywin32-docs/win32wnet__WNetCancelConnection2_meth.html
# https://mhammond.github.io/pywin32/win32wnet__WNetCancelConnection2_meth.html
win32wnet.WNetCancelConnection2(name, 0, 1)


Expand Down Expand Up @@ -75,7 +75,7 @@ def __init__(self, config):
server = f'{server}.ipv6-literal.net'

# https://docs.microsoft.com/en-us/windows/win32/api/winnetwk/ns-winnetwk-netresourcea
# http://timgolden.me.uk/pywin32-docs/PyNETRESOURCE.html
# https://mhammond.github.io/pywin32/PyNETRESOURCE.html
self.network_resource = win32wnet.NETRESOURCE()
self.network_resource.lpRemoteName = fr'\\{server}'

Expand All @@ -90,12 +90,12 @@ def connect(self):
self.network_resources.add(self.network_resource, self.username, self.password)

# https://docs.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhopenquerya
# http://timgolden.me.uk/pywin32-docs/win32pdh__OpenQuery_meth.html
# https://mhammond.github.io/pywin32/win32pdh__OpenQuery_meth.html
self.__query_handle = win32pdh.OpenQuery()

def disconnect(self):
# https://docs.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhclosequery
# http://timgolden.me.uk/pywin32-docs/win32pdh__CloseQuery_meth.html
# https://mhammond.github.io/pywin32/win32pdh__CloseQuery_meth.html
win32pdh.CloseQuery(self.__query_handle)

if self.network_resource is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def collect(self):

def refresh(self):
# https://docs.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhenumobjectitemsa
# http://timgolden.me.uk/pywin32-docs/win32pdh__EnumObjectItems_meth.html
# https://mhammond.github.io/pywin32/win32pdh__EnumObjectItems_meth.html
counters, instances = win32pdh.EnumObjectItems(
None, self.connection.server, self.name, win32pdh.PERF_DETAIL_WIZARD
)
Expand Down Expand Up @@ -149,11 +149,11 @@ def _configure_counters(self, available_counters, available_instances):

if self.use_localized_counters:
# https://docs.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhaddcountera
# http://timgolden.me.uk/pywin32-docs/win32pdh__AddCounter_meth.html
# https://mhammond.github.io/pywin32/win32pdh__AddCounter_meth.html
counter_selector = win32pdh.AddCounter
else:
# https://docs.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhaddenglishcountera
# http://timgolden.me.uk/pywin32-docs/win32pdh__AddEnglishCounter_meth.html
# https://mhammond.github.io/pywin32/win32pdh__AddEnglishCounter_meth.html
counter_selector = win32pdh.AddEnglishCounter

if available_instances:
Expand All @@ -165,7 +165,7 @@ def _configure_counters(self, available_counters, available_instances):
)

# https://docs.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhvalidatepatha
# http://timgolden.me.uk/pywin32-docs/win32pdh__ValidatePath_meth.html
# https://mhammond.github.io/pywin32/win32pdh__ValidatePath_meth.html
if win32pdh.ValidatePath(possible_path) == 0:
counter_type = SingleCounter
self.has_multiple_instances = False
Expand Down Expand Up @@ -319,7 +319,7 @@ def handle_counter_value_error(self, error, instance=None):
# Counter requires at least 2 data points to return a meaningful value, see:
# https://docs.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhgetformattedcountervalue#remarks
#
# http://timgolden.me.uk/pywin32-docs/error.html
# https://mhammond.github.io/pywin32/error.html
if error.strerror != 'The data is not valid.':
raise

Expand Down Expand Up @@ -369,7 +369,7 @@ def clear(self):

try:
# https://docs.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhremovecounter
# http://timgolden.me.uk/pywin32-docs/win32pdh__RemoveCounter_meth.html
# https://mhammond.github.io/pywin32/win32pdh__RemoveCounter_meth.html
win32pdh.RemoveCounter(self.counter_handle)
except Exception as e:
self.logger.warning(
Expand Down Expand Up @@ -498,7 +498,7 @@ def refresh(self, instance_counts):

try:
# https://docs.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhremovecounter
# http://timgolden.me.uk/pywin32-docs/win32pdh__RemoveCounter_meth.html
# https://mhammond.github.io/pywin32/win32pdh__RemoveCounter_meth.html
win32pdh.RemoveCounter(counter_handle)
except Exception as e:
self.logger.warning(
Expand Down Expand Up @@ -538,7 +538,7 @@ def clear(self):
counter_handle = counter_handles.pop()
try:
# https://docs.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhremovecounter
# http://timgolden.me.uk/pywin32-docs/win32pdh__RemoveCounter_meth.html
# https://mhammond.github.io/pywin32/win32pdh__RemoveCounter_meth.html
win32pdh.RemoveCounter(counter_handle)
except Exception as e:
self.logger.warning(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def construct_counter_path(*, machine_name, object_name, counter_name, instance_
# More info: https://docs.microsoft.com/en-us/windows/win32/perfctrs/specifying-a-counter-path
#
# https://docs.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhmakecounterpatha
# http://timgolden.me.uk/pywin32-docs/win32pdh__MakeCounterPath_meth.html
# https://mhammond.github.io/pywin32/win32pdh__MakeCounterPath_meth.html
return win32pdh.MakeCounterPath((machine_name, object_name, instance_name, None, instance_index, counter_name))


def get_counter_value(counter_handle):
# https://docs.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhgetformattedcountervalue
# http://timgolden.me.uk/pywin32-docs/win32pdh__GetFormattedCounterValue_meth.html
# https://mhammond.github.io/pywin32/win32pdh__GetFormattedCounterValue_meth.html
return win32pdh.GetFormattedCounterValue(counter_handle, COUNTER_VALUE_FORMAT)[1]
24 changes: 12 additions & 12 deletions win32_event_log/datadog_checks/win32_event_log/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def collect_sid(self, event_payload, rendered_event, event_object):

try:
# https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-lookupaccountsida
# http://timgolden.me.uk/pywin32-docs/win32security__LookupAccountSid_meth.html
# https://mhammond.github.io/pywin32/win32security__LookupAccountSid_meth.html
user, domain, _ = win32security.LookupAccountSid(
None if self._session is None else event_payload['host'], value
)
Expand All @@ -257,7 +257,7 @@ def render_event(self, event, context):

# https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtrender
# https://docs.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_render_flags
# http://timgolden.me.uk/pywin32-docs/win32evtlog__EvtRender_meth.html
# https://mhammond.github.io/pywin32/win32evtlog__EvtRender_meth.html
return win32evtlog.EvtRender(event, win32evtlog.EvtRenderEventValues, Context=context)

def consume_events(self):
Expand All @@ -283,7 +283,7 @@ def poll_events(self):
# IMPORTANT: the subscription starts immediately so you must consume before waiting for the first signal
while True:
# https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtnext
# http://timgolden.me.uk/pywin32-docs/win32evtlog__EvtNext_meth.html
# https://mhammond.github.io/pywin32/win32evtlog__EvtNext_meth.html
#
# An error saying EvtNext: The operation identifier is not valid happens
# when you call the method and there are no events to read (i.e. polling).
Expand All @@ -303,7 +303,7 @@ def poll_events(self):
yield event

# https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobjectex
# http://timgolden.me.uk/pywin32-docs/win32event__WaitForSingleObjectEx_meth.html
# https://mhammond.github.io/pywin32/win32event__WaitForSingleObjectEx_meth.html
wait_signal = win32event.WaitForSingleObjectEx(self._event_handle, self.config.timeout, True)

# No more events, end check run
Expand All @@ -314,11 +314,11 @@ def update_bookmark(self, event):
# See https://docs.microsoft.com/en-us/windows/win32/wes/bookmarking-events

# https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtupdatebookmark
# http://timgolden.me.uk/pywin32-docs/win32evtlog__EvtUpdateBookmark_meth.html
# https://mhammond.github.io/pywin32/win32evtlog__EvtUpdateBookmark_meth.html
win32evtlog.EvtUpdateBookmark(self._bookmark_handle, event)

# https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtrender
# http://timgolden.me.uk/pywin32-docs/win32evtlog__EvtRender_meth.html
# https://mhammond.github.io/pywin32/win32evtlog__EvtRender_meth.html
bookmark_xml = win32evtlog.EvtRender(self._bookmark_handle, win32evtlog.EvtRenderBookmark)

self.write_persistent_cache('bookmark', bookmark_xml)
Expand Down Expand Up @@ -355,12 +355,12 @@ def create_session(self):
return

# https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtopensession
# http://timgolden.me.uk/pywin32-docs/win32evtlog__EvtOpenSession_meth.html
# https://mhammond.github.io/pywin32/win32evtlog__EvtOpenSession_meth.html
self._session = win32evtlog.EvtOpenSession(session_struct, win32evtlog.EvtRpcLogin, 0, 0)

def create_subscription(self):
# https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-createeventa
# http://timgolden.me.uk/pywin32-docs/win32event__CreateEvent_meth.html
# https://mhammond.github.io/pywin32/win32event__CreateEvent_meth.html
self._event_handle = win32event.CreateEvent(None, 0, 0, self.check_id)

bookmark = self.read_persistent_cache('bookmark')
Expand All @@ -373,11 +373,11 @@ def create_subscription(self):
bookmark = None

# https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtcreatebookmark
# http://timgolden.me.uk/pywin32-docs/win32evtlog__EvtCreateBookmark_meth.html
# https://mhammond.github.io/pywin32/win32evtlog__EvtCreateBookmark_meth.html
self._bookmark_handle = win32evtlog.EvtCreateBookmark(bookmark)

# https://docs.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtsubscribe
# http://timgolden.me.uk/pywin32-docs/win32evtlog__EvtSubscribe_meth.html
# https://mhammond.github.io/pywin32/win32evtlog__EvtSubscribe_meth.html
self._subscription = win32evtlog.EvtSubscribe(
self.config.path,
flags,
Expand Down Expand Up @@ -406,11 +406,11 @@ def get_session_struct(self):
password = self.instance.get('password')

# https://docs.microsoft.com/en-us/windows/win32/api/winevt/ns-winevt-evt_rpc_login
# http://timgolden.me.uk/pywin32-docs/PyEVT_RPC_LOGIN.html
# https://mhammond.github.io/pywin32/PyEVT_RPC_LOGIN.html
return server, user, domain, password, self.LOGIN_FLAGS[auth_type]

def log_windows_error(self, exc):
# http://timgolden.me.uk/pywin32-docs/error.html
# https://mhammond.github.io/pywin32/error.html
#
# Occasionally the Windows function returns some extra data after a colon which we don't need
self.log.debug('Error code %d when calling `%s`: %s', exc.winerror, exc.funcname.split(':')[0], exc.strerror)
Expand Down
2 changes: 1 addition & 1 deletion win32_event_log/datadog_checks/win32_event_log/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ def get_last_error_message(): # no cov
"""
# https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror
# https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessage
# http://timgolden.me.uk/pywin32-docs/win32api__FormatMessage_meth.html
# https://mhammond.github.io/pywin32/win32api__FormatMessage_meth.html
return win32api.FormatMessage(0)
6 changes: 3 additions & 3 deletions win32_event_log/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _report(self, message, wait=1, level='info', event_type=None):
time.sleep(wait)

# https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-reporteventa
# http://timgolden.me.uk/pywin32-docs/win32evtlog__ReportEvent_meth.html
# https://mhammond.github.io/pywin32/win32evtlog__ReportEvent_meth.html
win32evtlog.ReportEvent(
self.log_handle,
event_type if event_type is not None else self.EVENT_TYPES[level],
Expand All @@ -50,13 +50,13 @@ def __enter__(self):
# win32evtlogutil.AddSourceToRegistry(self.source)

# https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-registereventsourcea
# http://timgolden.me.uk/pywin32-docs/win32evtlog__RegisterEventSource_meth.html
# https://mhammond.github.io/pywin32/win32evtlog__RegisterEventSource_meth.html
self.log_handle = win32evtlog.RegisterEventSource(None, self.source)
return self

def __exit__(self, exc_type, exc_value, traceback):
# https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-deregistereventsource
# http://timgolden.me.uk/pywin32-docs/win32evtlog__DeregisterEventSource_meth.html
# https://mhammond.github.io/pywin32/win32evtlog__DeregisterEventSource_meth.html
win32evtlog.DeregisterEventSource(self.log_handle)

# This requires that tests are executed in an administrator shell, useful for testing handling of Error 15027
Expand Down