diff --git a/helpers/windows/pdh/defs_pdh_windows.go b/helpers/windows/pdh/defs_pdh_windows.go new file mode 100644 index 0000000..f98e171 --- /dev/null +++ b/helpers/windows/pdh/defs_pdh_windows.go @@ -0,0 +1,234 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// go run mkpdh_defs.go +// MACHINE GENERATED BY THE ABOVE COMMAND; DO NOT EDIT + +//go:build ignore + +package pdh + +/* +#include +#include +#cgo LDFLAGS: -lpdh +*/ +import "C" + +type PdhErrno uintptr + +// PDH Error Codes +const ( + PDH_CSTATUS_VALID_DATA PdhErrno = C.PDH_CSTATUS_VALID_DATA + PDH_CSTATUS_NEW_DATA PdhErrno = C.PDH_CSTATUS_NEW_DATA + PDH_CSTATUS_NO_MACHINE PdhErrno = C.PDH_CSTATUS_NO_MACHINE + PDH_CSTATUS_NO_INSTANCE PdhErrno = C.PDH_CSTATUS_NO_INSTANCE + PDH_MORE_DATA PdhErrno = C.PDH_MORE_DATA + PDH_CSTATUS_ITEM_NOT_VALIDATED PdhErrno = C.PDH_CSTATUS_ITEM_NOT_VALIDATED + PDH_RETRY PdhErrno = C.PDH_RETRY + PDH_NO_DATA PdhErrno = C.PDH_NO_DATA + PDH_CALC_NEGATIVE_DENOMINATOR PdhErrno = C.PDH_CALC_NEGATIVE_DENOMINATOR + PDH_CALC_NEGATIVE_TIMEBASE PdhErrno = C.PDH_CALC_NEGATIVE_TIMEBASE + PDH_CALC_NEGATIVE_VALUE PdhErrno = C.PDH_CALC_NEGATIVE_VALUE + PDH_DIALOG_CANCELLED PdhErrno = C.PDH_DIALOG_CANCELLED + PDH_END_OF_LOG_FILE PdhErrno = C.PDH_END_OF_LOG_FILE + PDH_ASYNC_QUERY_TIMEOUT PdhErrno = C.PDH_ASYNC_QUERY_TIMEOUT + PDH_CANNOT_SET_DEFAULT_REALTIME_DATASOURCE PdhErrno = C.PDH_CANNOT_SET_DEFAULT_REALTIME_DATASOURCE + PDH_CSTATUS_NO_OBJECT PdhErrno = C.PDH_CSTATUS_NO_OBJECT + PDH_CSTATUS_NO_COUNTER PdhErrno = C.PDH_CSTATUS_NO_COUNTER + PDH_CSTATUS_INVALID_DATA PdhErrno = C.PDH_CSTATUS_INVALID_DATA + PDH_MEMORY_ALLOCATION_FAILURE PdhErrno = C.PDH_MEMORY_ALLOCATION_FAILURE + PDH_INVALID_HANDLE PdhErrno = C.PDH_INVALID_HANDLE + PDH_INVALID_ARGUMENT PdhErrno = C.PDH_INVALID_ARGUMENT + PDH_FUNCTION_NOT_FOUND PdhErrno = C.PDH_FUNCTION_NOT_FOUND + PDH_CSTATUS_NO_COUNTERNAME PdhErrno = C.PDH_CSTATUS_NO_COUNTERNAME + PDH_CSTATUS_BAD_COUNTERNAME PdhErrno = C.PDH_CSTATUS_BAD_COUNTERNAME + PDH_INVALID_BUFFER PdhErrno = C.PDH_INVALID_BUFFER + PDH_INSUFFICIENT_BUFFER PdhErrno = C.PDH_INSUFFICIENT_BUFFER + PDH_CANNOT_CONNECT_MACHINE PdhErrno = C.PDH_CANNOT_CONNECT_MACHINE + PDH_INVALID_PATH PdhErrno = C.PDH_INVALID_PATH + PDH_INVALID_INSTANCE PdhErrno = C.PDH_INVALID_INSTANCE + PDH_INVALID_DATA PdhErrno = C.PDH_INVALID_DATA + PDH_NO_DIALOG_DATA PdhErrno = C.PDH_NO_DIALOG_DATA + PDH_CANNOT_READ_NAME_STRINGS PdhErrno = C.PDH_CANNOT_READ_NAME_STRINGS + PDH_LOG_FILE_CREATE_ERROR PdhErrno = C.PDH_LOG_FILE_CREATE_ERROR + PDH_LOG_FILE_OPEN_ERROR PdhErrno = C.PDH_LOG_FILE_OPEN_ERROR + PDH_LOG_TYPE_NOT_FOUND PdhErrno = C.PDH_LOG_TYPE_NOT_FOUND + PDH_NO_MORE_DATA PdhErrno = C.PDH_NO_MORE_DATA + PDH_ENTRY_NOT_IN_LOG_FILE PdhErrno = C.PDH_ENTRY_NOT_IN_LOG_FILE + PDH_DATA_SOURCE_IS_LOG_FILE PdhErrno = C.PDH_DATA_SOURCE_IS_LOG_FILE + PDH_DATA_SOURCE_IS_REAL_TIME PdhErrno = C.PDH_DATA_SOURCE_IS_REAL_TIME + PDH_UNABLE_READ_LOG_HEADER PdhErrno = C.PDH_UNABLE_READ_LOG_HEADER + PDH_FILE_NOT_FOUND PdhErrno = C.PDH_FILE_NOT_FOUND + PDH_FILE_ALREADY_EXISTS PdhErrno = C.PDH_FILE_ALREADY_EXISTS + PDH_NOT_IMPLEMENTED PdhErrno = C.PDH_NOT_IMPLEMENTED + PDH_STRING_NOT_FOUND PdhErrno = C.PDH_STRING_NOT_FOUND + PDH_UNABLE_MAP_NAME_FILES PdhErrno = C.PDH_UNABLE_MAP_NAME_FILES + PDH_UNKNOWN_LOG_FORMAT PdhErrno = C.PDH_UNKNOWN_LOG_FORMAT + PDH_UNKNOWN_LOGSVC_COMMAND PdhErrno = C.PDH_UNKNOWN_LOGSVC_COMMAND + PDH_LOGSVC_QUERY_NOT_FOUND PdhErrno = C.PDH_LOGSVC_QUERY_NOT_FOUND + PDH_LOGSVC_NOT_OPENED PdhErrno = C.PDH_LOGSVC_NOT_OPENED + PDH_WBEM_ERROR PdhErrno = C.PDH_WBEM_ERROR + PDH_ACCESS_DENIED PdhErrno = C.PDH_ACCESS_DENIED + PDH_LOG_FILE_TOO_SMALL PdhErrno = C.PDH_LOG_FILE_TOO_SMALL + PDH_INVALID_DATASOURCE PdhErrno = C.PDH_INVALID_DATASOURCE + PDH_INVALID_SQLDB PdhErrno = C.PDH_INVALID_SQLDB + PDH_NO_COUNTERS PdhErrno = C.PDH_NO_COUNTERS + PDH_SQL_ALLOC_FAILED PdhErrno = C.PDH_SQL_ALLOC_FAILED + PDH_SQL_ALLOCCON_FAILED PdhErrno = C.PDH_SQL_ALLOCCON_FAILED + PDH_SQL_EXEC_DIRECT_FAILED PdhErrno = C.PDH_SQL_EXEC_DIRECT_FAILED + PDH_SQL_FETCH_FAILED PdhErrno = C.PDH_SQL_FETCH_FAILED + PDH_SQL_ROWCOUNT_FAILED PdhErrno = C.PDH_SQL_ROWCOUNT_FAILED + PDH_SQL_MORE_RESULTS_FAILED PdhErrno = C.PDH_SQL_MORE_RESULTS_FAILED + PDH_SQL_CONNECT_FAILED PdhErrno = C.PDH_SQL_CONNECT_FAILED + PDH_SQL_BIND_FAILED PdhErrno = C.PDH_SQL_BIND_FAILED + PDH_CANNOT_CONNECT_WMI_SERVER PdhErrno = C.PDH_CANNOT_CONNECT_WMI_SERVER + PDH_PLA_COLLECTION_ALREADY_RUNNING PdhErrno = C.PDH_PLA_COLLECTION_ALREADY_RUNNING + PDH_PLA_ERROR_SCHEDULE_OVERLAP PdhErrno = C.PDH_PLA_ERROR_SCHEDULE_OVERLAP + PDH_PLA_COLLECTION_NOT_FOUND PdhErrno = C.PDH_PLA_COLLECTION_NOT_FOUND + PDH_PLA_ERROR_SCHEDULE_ELAPSED PdhErrno = C.PDH_PLA_ERROR_SCHEDULE_ELAPSED + PDH_PLA_ERROR_NOSTART PdhErrno = C.PDH_PLA_ERROR_NOSTART + PDH_PLA_ERROR_ALREADY_EXISTS PdhErrno = C.PDH_PLA_ERROR_ALREADY_EXISTS + PDH_PLA_ERROR_TYPE_MISMATCH PdhErrno = C.PDH_PLA_ERROR_TYPE_MISMATCH + PDH_PLA_ERROR_FILEPATH PdhErrno = C.PDH_PLA_ERROR_FILEPATH + PDH_PLA_SERVICE_ERROR PdhErrno = C.PDH_PLA_SERVICE_ERROR + PDH_PLA_VALIDATION_ERROR PdhErrno = C.PDH_PLA_VALIDATION_ERROR + PDH_PLA_VALIDATION_WARNING PdhErrno = C.PDH_PLA_VALIDATION_WARNING + PDH_PLA_ERROR_NAME_TOO_LONG PdhErrno = C.PDH_PLA_ERROR_NAME_TOO_LONG + PDH_INVALID_SQL_LOG_FORMAT PdhErrno = C.PDH_INVALID_SQL_LOG_FORMAT + PDH_COUNTER_ALREADY_IN_QUERY PdhErrno = C.PDH_COUNTER_ALREADY_IN_QUERY + PDH_BINARY_LOG_CORRUPT PdhErrno = C.PDH_BINARY_LOG_CORRUPT + PDH_LOG_SAMPLE_TOO_SMALL PdhErrno = C.PDH_LOG_SAMPLE_TOO_SMALL + PDH_OS_LATER_VERSION PdhErrno = C.PDH_OS_LATER_VERSION + PDH_OS_EARLIER_VERSION PdhErrno = C.PDH_OS_EARLIER_VERSION + PDH_INCORRECT_APPEND_TIME PdhErrno = C.PDH_INCORRECT_APPEND_TIME + PDH_UNMATCHED_APPEND_COUNTER PdhErrno = C.PDH_UNMATCHED_APPEND_COUNTER + PDH_SQL_ALTER_DETAIL_FAILED PdhErrno = C.PDH_SQL_ALTER_DETAIL_FAILED + PDH_QUERY_PERF_DATA_TIMEOUT PdhErrno = C.PDH_QUERY_PERF_DATA_TIMEOUT +) + +var pdhErrors = map[PdhErrno]struct{}{ + PDH_CSTATUS_VALID_DATA: struct{}{}, + PDH_CSTATUS_NEW_DATA: struct{}{}, + PDH_CSTATUS_NO_MACHINE: struct{}{}, + PDH_CSTATUS_NO_INSTANCE: struct{}{}, + PDH_MORE_DATA: struct{}{}, + PDH_CSTATUS_ITEM_NOT_VALIDATED: struct{}{}, + PDH_RETRY: struct{}{}, + PDH_NO_DATA: struct{}{}, + PDH_CALC_NEGATIVE_DENOMINATOR: struct{}{}, + PDH_CALC_NEGATIVE_TIMEBASE: struct{}{}, + PDH_CALC_NEGATIVE_VALUE: struct{}{}, + PDH_DIALOG_CANCELLED: struct{}{}, + PDH_END_OF_LOG_FILE: struct{}{}, + PDH_ASYNC_QUERY_TIMEOUT: struct{}{}, + PDH_CANNOT_SET_DEFAULT_REALTIME_DATASOURCE: struct{}{}, + PDH_CSTATUS_NO_OBJECT: struct{}{}, + PDH_CSTATUS_NO_COUNTER: struct{}{}, + PDH_CSTATUS_INVALID_DATA: struct{}{}, + PDH_MEMORY_ALLOCATION_FAILURE: struct{}{}, + PDH_INVALID_HANDLE: struct{}{}, + PDH_INVALID_ARGUMENT: struct{}{}, + PDH_FUNCTION_NOT_FOUND: struct{}{}, + PDH_CSTATUS_NO_COUNTERNAME: struct{}{}, + PDH_CSTATUS_BAD_COUNTERNAME: struct{}{}, + PDH_INVALID_BUFFER: struct{}{}, + PDH_INSUFFICIENT_BUFFER: struct{}{}, + PDH_CANNOT_CONNECT_MACHINE: struct{}{}, + PDH_INVALID_PATH: struct{}{}, + PDH_INVALID_INSTANCE: struct{}{}, + PDH_INVALID_DATA: struct{}{}, + PDH_NO_DIALOG_DATA: struct{}{}, + PDH_CANNOT_READ_NAME_STRINGS: struct{}{}, + PDH_LOG_FILE_CREATE_ERROR: struct{}{}, + PDH_LOG_FILE_OPEN_ERROR: struct{}{}, + PDH_LOG_TYPE_NOT_FOUND: struct{}{}, + PDH_NO_MORE_DATA: struct{}{}, + PDH_ENTRY_NOT_IN_LOG_FILE: struct{}{}, + PDH_DATA_SOURCE_IS_LOG_FILE: struct{}{}, + PDH_DATA_SOURCE_IS_REAL_TIME: struct{}{}, + PDH_UNABLE_READ_LOG_HEADER: struct{}{}, + PDH_FILE_NOT_FOUND: struct{}{}, + PDH_FILE_ALREADY_EXISTS: struct{}{}, + PDH_NOT_IMPLEMENTED: struct{}{}, + PDH_STRING_NOT_FOUND: struct{}{}, + PDH_UNABLE_MAP_NAME_FILES: struct{}{}, + PDH_UNKNOWN_LOG_FORMAT: struct{}{}, + PDH_UNKNOWN_LOGSVC_COMMAND: struct{}{}, + PDH_LOGSVC_QUERY_NOT_FOUND: struct{}{}, + PDH_LOGSVC_NOT_OPENED: struct{}{}, + PDH_WBEM_ERROR: struct{}{}, + PDH_ACCESS_DENIED: struct{}{}, + PDH_LOG_FILE_TOO_SMALL: struct{}{}, + PDH_INVALID_DATASOURCE: struct{}{}, + PDH_INVALID_SQLDB: struct{}{}, + PDH_NO_COUNTERS: struct{}{}, + PDH_SQL_ALLOC_FAILED: struct{}{}, + PDH_SQL_ALLOCCON_FAILED: struct{}{}, + PDH_SQL_EXEC_DIRECT_FAILED: struct{}{}, + PDH_SQL_FETCH_FAILED: struct{}{}, + PDH_SQL_ROWCOUNT_FAILED: struct{}{}, + PDH_SQL_MORE_RESULTS_FAILED: struct{}{}, + PDH_SQL_CONNECT_FAILED: struct{}{}, + PDH_SQL_BIND_FAILED: struct{}{}, + PDH_CANNOT_CONNECT_WMI_SERVER: struct{}{}, + PDH_PLA_COLLECTION_ALREADY_RUNNING: struct{}{}, + PDH_PLA_ERROR_SCHEDULE_OVERLAP: struct{}{}, + PDH_PLA_COLLECTION_NOT_FOUND: struct{}{}, + PDH_PLA_ERROR_SCHEDULE_ELAPSED: struct{}{}, + PDH_PLA_ERROR_NOSTART: struct{}{}, + PDH_PLA_ERROR_ALREADY_EXISTS: struct{}{}, + PDH_PLA_ERROR_TYPE_MISMATCH: struct{}{}, + PDH_PLA_ERROR_FILEPATH: struct{}{}, + PDH_PLA_SERVICE_ERROR: struct{}{}, + PDH_PLA_VALIDATION_ERROR: struct{}{}, + PDH_PLA_VALIDATION_WARNING: struct{}{}, + PDH_PLA_ERROR_NAME_TOO_LONG: struct{}{}, + PDH_INVALID_SQL_LOG_FORMAT: struct{}{}, + PDH_COUNTER_ALREADY_IN_QUERY: struct{}{}, + PDH_BINARY_LOG_CORRUPT: struct{}{}, + PDH_LOG_SAMPLE_TOO_SMALL: struct{}{}, + PDH_OS_LATER_VERSION: struct{}{}, + PDH_OS_EARLIER_VERSION: struct{}{}, + PDH_INCORRECT_APPEND_TIME: struct{}{}, + PDH_UNMATCHED_APPEND_COUNTER: struct{}{}, + PDH_SQL_ALTER_DETAIL_FAILED: struct{}{}, + PDH_QUERY_PERF_DATA_TIMEOUT: struct{}{}, +} + +type PdhCounterFormat uint32 + +// PDH Counter Formats +const ( + // PdhFmtDouble returns data as a double-precision floating point real. + PdhFmtDouble PdhCounterFormat = C.PDH_FMT_DOUBLE + // PdhFmtLarge returns data as a 64-bit integer. + PdhFmtLarge PdhCounterFormat = C.PDH_FMT_LARGE + // PdhFmtLong returns data as a long integer. + PdhFmtLong PdhCounterFormat = C.PDH_FMT_LONG + + // Use bitwise operators to combine these values with the counter type to scale the value. + + // Do not apply the counter's default scaling factor. + PdhFmtNoScale PdhCounterFormat = C.PDH_FMT_NOSCALE + // Counter values greater than 100 (for example, counter values measuring + // the processor load on multiprocessor computers) will not be reset to 100. + // The default behavior is that counter values are capped at a value of 100. + PdhFmtNoCap100 PdhCounterFormat = C.PDH_FMT_NOCAP100 + // Multiply the actual value by 1,000. + PdhFmtMultiply1000 PdhCounterFormat = C.PDH_FMT_1000 +) diff --git a/helpers/windows/pdh/defs_pdh_windows_386.go b/helpers/windows/pdh/defs_pdh_windows_386.go new file mode 100644 index 0000000..e794050 --- /dev/null +++ b/helpers/windows/pdh/defs_pdh_windows_386.go @@ -0,0 +1,217 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Code generated by cmd/cgo -godefs; DO NOT EDIT. +// cgo.exe -godefs defs_pdh_windows.go + +package pdh + +type PdhErrno uintptr + +const ( + PDH_CSTATUS_VALID_DATA PdhErrno = 0x0 + PDH_CSTATUS_NEW_DATA PdhErrno = 0x1 + PDH_CSTATUS_NO_MACHINE PdhErrno = 0x800007d0 + PDH_CSTATUS_NO_INSTANCE PdhErrno = 0x800007d1 + PDH_MORE_DATA PdhErrno = 0x800007d2 + PDH_CSTATUS_ITEM_NOT_VALIDATED PdhErrno = 0x800007d3 + PDH_RETRY PdhErrno = 0x800007d4 + PDH_NO_DATA PdhErrno = 0x800007d5 + PDH_CALC_NEGATIVE_DENOMINATOR PdhErrno = 0x800007d6 + PDH_CALC_NEGATIVE_TIMEBASE PdhErrno = 0x800007d7 + PDH_CALC_NEGATIVE_VALUE PdhErrno = 0x800007d8 + PDH_DIALOG_CANCELLED PdhErrno = 0x800007d9 + PDH_END_OF_LOG_FILE PdhErrno = 0x800007da + PDH_ASYNC_QUERY_TIMEOUT PdhErrno = 0x800007db + PDH_CANNOT_SET_DEFAULT_REALTIME_DATASOURCE PdhErrno = 0x800007dc + PDH_CSTATUS_NO_OBJECT PdhErrno = 0xc0000bb8 + PDH_CSTATUS_NO_COUNTER PdhErrno = 0xc0000bb9 + PDH_CSTATUS_INVALID_DATA PdhErrno = 0xc0000bba + PDH_MEMORY_ALLOCATION_FAILURE PdhErrno = 0xc0000bbb + PDH_INVALID_HANDLE PdhErrno = 0xc0000bbc + PDH_INVALID_ARGUMENT PdhErrno = 0xc0000bbd + PDH_FUNCTION_NOT_FOUND PdhErrno = 0xc0000bbe + PDH_CSTATUS_NO_COUNTERNAME PdhErrno = 0xc0000bbf + PDH_CSTATUS_BAD_COUNTERNAME PdhErrno = 0xc0000bc0 + PDH_INVALID_BUFFER PdhErrno = 0xc0000bc1 + PDH_INSUFFICIENT_BUFFER PdhErrno = 0xc0000bc2 + PDH_CANNOT_CONNECT_MACHINE PdhErrno = 0xc0000bc3 + PDH_INVALID_PATH PdhErrno = 0xc0000bc4 + PDH_INVALID_INSTANCE PdhErrno = 0xc0000bc5 + PDH_INVALID_DATA PdhErrno = 0xc0000bc6 + PDH_NO_DIALOG_DATA PdhErrno = 0xc0000bc7 + PDH_CANNOT_READ_NAME_STRINGS PdhErrno = 0xc0000bc8 + PDH_LOG_FILE_CREATE_ERROR PdhErrno = 0xc0000bc9 + PDH_LOG_FILE_OPEN_ERROR PdhErrno = 0xc0000bca + PDH_LOG_TYPE_NOT_FOUND PdhErrno = 0xc0000bcb + PDH_NO_MORE_DATA PdhErrno = 0xc0000bcc + PDH_ENTRY_NOT_IN_LOG_FILE PdhErrno = 0xc0000bcd + PDH_DATA_SOURCE_IS_LOG_FILE PdhErrno = 0xc0000bce + PDH_DATA_SOURCE_IS_REAL_TIME PdhErrno = 0xc0000bcf + PDH_UNABLE_READ_LOG_HEADER PdhErrno = 0xc0000bd0 + PDH_FILE_NOT_FOUND PdhErrno = 0xc0000bd1 + PDH_FILE_ALREADY_EXISTS PdhErrno = 0xc0000bd2 + PDH_NOT_IMPLEMENTED PdhErrno = 0xc0000bd3 + PDH_STRING_NOT_FOUND PdhErrno = 0xc0000bd4 + PDH_UNABLE_MAP_NAME_FILES PdhErrno = 0x80000bd5 + PDH_UNKNOWN_LOG_FORMAT PdhErrno = 0xc0000bd6 + PDH_UNKNOWN_LOGSVC_COMMAND PdhErrno = 0xc0000bd7 + PDH_LOGSVC_QUERY_NOT_FOUND PdhErrno = 0xc0000bd8 + PDH_LOGSVC_NOT_OPENED PdhErrno = 0xc0000bd9 + PDH_WBEM_ERROR PdhErrno = 0xc0000bda + PDH_ACCESS_DENIED PdhErrno = 0xc0000bdb + PDH_LOG_FILE_TOO_SMALL PdhErrno = 0xc0000bdc + PDH_INVALID_DATASOURCE PdhErrno = 0xc0000bdd + PDH_INVALID_SQLDB PdhErrno = 0xc0000bde + PDH_NO_COUNTERS PdhErrno = 0xc0000bdf + PDH_SQL_ALLOC_FAILED PdhErrno = 0xc0000be0 + PDH_SQL_ALLOCCON_FAILED PdhErrno = 0xc0000be1 + PDH_SQL_EXEC_DIRECT_FAILED PdhErrno = 0xc0000be2 + PDH_SQL_FETCH_FAILED PdhErrno = 0xc0000be3 + PDH_SQL_ROWCOUNT_FAILED PdhErrno = 0xc0000be4 + PDH_SQL_MORE_RESULTS_FAILED PdhErrno = 0xc0000be5 + PDH_SQL_CONNECT_FAILED PdhErrno = 0xc0000be6 + PDH_SQL_BIND_FAILED PdhErrno = 0xc0000be7 + PDH_CANNOT_CONNECT_WMI_SERVER PdhErrno = 0xc0000be8 + PDH_PLA_COLLECTION_ALREADY_RUNNING PdhErrno = 0xc0000be9 + PDH_PLA_ERROR_SCHEDULE_OVERLAP PdhErrno = 0xc0000bea + PDH_PLA_COLLECTION_NOT_FOUND PdhErrno = 0xc0000beb + PDH_PLA_ERROR_SCHEDULE_ELAPSED PdhErrno = 0xc0000bec + PDH_PLA_ERROR_NOSTART PdhErrno = 0xc0000bed + PDH_PLA_ERROR_ALREADY_EXISTS PdhErrno = 0xc0000bee + PDH_PLA_ERROR_TYPE_MISMATCH PdhErrno = 0xc0000bef + PDH_PLA_ERROR_FILEPATH PdhErrno = 0xc0000bf0 + PDH_PLA_SERVICE_ERROR PdhErrno = 0xc0000bf1 + PDH_PLA_VALIDATION_ERROR PdhErrno = 0xc0000bf2 + PDH_PLA_VALIDATION_WARNING PdhErrno = 0x80000bf3 + PDH_PLA_ERROR_NAME_TOO_LONG PdhErrno = 0xc0000bf4 + PDH_INVALID_SQL_LOG_FORMAT PdhErrno = 0xc0000bf5 + PDH_COUNTER_ALREADY_IN_QUERY PdhErrno = 0xc0000bf6 + PDH_BINARY_LOG_CORRUPT PdhErrno = 0xc0000bf7 + PDH_LOG_SAMPLE_TOO_SMALL PdhErrno = 0xc0000bf8 + PDH_OS_LATER_VERSION PdhErrno = 0xc0000bf9 + PDH_OS_EARLIER_VERSION PdhErrno = 0xc0000bfa + PDH_INCORRECT_APPEND_TIME PdhErrno = 0xc0000bfb + PDH_UNMATCHED_APPEND_COUNTER PdhErrno = 0xc0000bfc + PDH_SQL_ALTER_DETAIL_FAILED PdhErrno = 0xc0000bfd + PDH_QUERY_PERF_DATA_TIMEOUT PdhErrno = 0xc0000bfe +) + +var pdhErrors = map[PdhErrno]struct{}{ + PDH_CSTATUS_VALID_DATA: struct{}{}, + PDH_CSTATUS_NEW_DATA: struct{}{}, + PDH_CSTATUS_NO_MACHINE: struct{}{}, + PDH_CSTATUS_NO_INSTANCE: struct{}{}, + PDH_MORE_DATA: struct{}{}, + PDH_CSTATUS_ITEM_NOT_VALIDATED: struct{}{}, + PDH_RETRY: struct{}{}, + PDH_NO_DATA: struct{}{}, + PDH_CALC_NEGATIVE_DENOMINATOR: struct{}{}, + PDH_CALC_NEGATIVE_TIMEBASE: struct{}{}, + PDH_CALC_NEGATIVE_VALUE: struct{}{}, + PDH_DIALOG_CANCELLED: struct{}{}, + PDH_END_OF_LOG_FILE: struct{}{}, + PDH_ASYNC_QUERY_TIMEOUT: struct{}{}, + PDH_CANNOT_SET_DEFAULT_REALTIME_DATASOURCE: struct{}{}, + PDH_CSTATUS_NO_OBJECT: struct{}{}, + PDH_CSTATUS_NO_COUNTER: struct{}{}, + PDH_CSTATUS_INVALID_DATA: struct{}{}, + PDH_MEMORY_ALLOCATION_FAILURE: struct{}{}, + PDH_INVALID_HANDLE: struct{}{}, + PDH_INVALID_ARGUMENT: struct{}{}, + PDH_FUNCTION_NOT_FOUND: struct{}{}, + PDH_CSTATUS_NO_COUNTERNAME: struct{}{}, + PDH_CSTATUS_BAD_COUNTERNAME: struct{}{}, + PDH_INVALID_BUFFER: struct{}{}, + PDH_INSUFFICIENT_BUFFER: struct{}{}, + PDH_CANNOT_CONNECT_MACHINE: struct{}{}, + PDH_INVALID_PATH: struct{}{}, + PDH_INVALID_INSTANCE: struct{}{}, + PDH_INVALID_DATA: struct{}{}, + PDH_NO_DIALOG_DATA: struct{}{}, + PDH_CANNOT_READ_NAME_STRINGS: struct{}{}, + PDH_LOG_FILE_CREATE_ERROR: struct{}{}, + PDH_LOG_FILE_OPEN_ERROR: struct{}{}, + PDH_LOG_TYPE_NOT_FOUND: struct{}{}, + PDH_NO_MORE_DATA: struct{}{}, + PDH_ENTRY_NOT_IN_LOG_FILE: struct{}{}, + PDH_DATA_SOURCE_IS_LOG_FILE: struct{}{}, + PDH_DATA_SOURCE_IS_REAL_TIME: struct{}{}, + PDH_UNABLE_READ_LOG_HEADER: struct{}{}, + PDH_FILE_NOT_FOUND: struct{}{}, + PDH_FILE_ALREADY_EXISTS: struct{}{}, + PDH_NOT_IMPLEMENTED: struct{}{}, + PDH_STRING_NOT_FOUND: struct{}{}, + PDH_UNABLE_MAP_NAME_FILES: struct{}{}, + PDH_UNKNOWN_LOG_FORMAT: struct{}{}, + PDH_UNKNOWN_LOGSVC_COMMAND: struct{}{}, + PDH_LOGSVC_QUERY_NOT_FOUND: struct{}{}, + PDH_LOGSVC_NOT_OPENED: struct{}{}, + PDH_WBEM_ERROR: struct{}{}, + PDH_ACCESS_DENIED: struct{}{}, + PDH_LOG_FILE_TOO_SMALL: struct{}{}, + PDH_INVALID_DATASOURCE: struct{}{}, + PDH_INVALID_SQLDB: struct{}{}, + PDH_NO_COUNTERS: struct{}{}, + PDH_SQL_ALLOC_FAILED: struct{}{}, + PDH_SQL_ALLOCCON_FAILED: struct{}{}, + PDH_SQL_EXEC_DIRECT_FAILED: struct{}{}, + PDH_SQL_FETCH_FAILED: struct{}{}, + PDH_SQL_ROWCOUNT_FAILED: struct{}{}, + PDH_SQL_MORE_RESULTS_FAILED: struct{}{}, + PDH_SQL_CONNECT_FAILED: struct{}{}, + PDH_SQL_BIND_FAILED: struct{}{}, + PDH_CANNOT_CONNECT_WMI_SERVER: struct{}{}, + PDH_PLA_COLLECTION_ALREADY_RUNNING: struct{}{}, + PDH_PLA_ERROR_SCHEDULE_OVERLAP: struct{}{}, + PDH_PLA_COLLECTION_NOT_FOUND: struct{}{}, + PDH_PLA_ERROR_SCHEDULE_ELAPSED: struct{}{}, + PDH_PLA_ERROR_NOSTART: struct{}{}, + PDH_PLA_ERROR_ALREADY_EXISTS: struct{}{}, + PDH_PLA_ERROR_TYPE_MISMATCH: struct{}{}, + PDH_PLA_ERROR_FILEPATH: struct{}{}, + PDH_PLA_SERVICE_ERROR: struct{}{}, + PDH_PLA_VALIDATION_ERROR: struct{}{}, + PDH_PLA_VALIDATION_WARNING: struct{}{}, + PDH_PLA_ERROR_NAME_TOO_LONG: struct{}{}, + PDH_INVALID_SQL_LOG_FORMAT: struct{}{}, + PDH_COUNTER_ALREADY_IN_QUERY: struct{}{}, + PDH_BINARY_LOG_CORRUPT: struct{}{}, + PDH_LOG_SAMPLE_TOO_SMALL: struct{}{}, + PDH_OS_LATER_VERSION: struct{}{}, + PDH_OS_EARLIER_VERSION: struct{}{}, + PDH_INCORRECT_APPEND_TIME: struct{}{}, + PDH_UNMATCHED_APPEND_COUNTER: struct{}{}, + PDH_SQL_ALTER_DETAIL_FAILED: struct{}{}, + PDH_QUERY_PERF_DATA_TIMEOUT: struct{}{}, +} + +type PdhCounterFormat uint32 + +const ( + PdhFmtDouble PdhCounterFormat = 0x200 + + PdhFmtLarge PdhCounterFormat = 0x400 + + PdhFmtLong PdhCounterFormat = 0x100 + + PdhFmtNoScale PdhCounterFormat = 0x1000 + + PdhFmtNoCap100 PdhCounterFormat = 0x8000 + + PdhFmtMultiply1000 PdhCounterFormat = 0x2000 +) diff --git a/helpers/windows/pdh/defs_pdh_windows_amd64.go b/helpers/windows/pdh/defs_pdh_windows_amd64.go new file mode 100644 index 0000000..e794050 --- /dev/null +++ b/helpers/windows/pdh/defs_pdh_windows_amd64.go @@ -0,0 +1,217 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Code generated by cmd/cgo -godefs; DO NOT EDIT. +// cgo.exe -godefs defs_pdh_windows.go + +package pdh + +type PdhErrno uintptr + +const ( + PDH_CSTATUS_VALID_DATA PdhErrno = 0x0 + PDH_CSTATUS_NEW_DATA PdhErrno = 0x1 + PDH_CSTATUS_NO_MACHINE PdhErrno = 0x800007d0 + PDH_CSTATUS_NO_INSTANCE PdhErrno = 0x800007d1 + PDH_MORE_DATA PdhErrno = 0x800007d2 + PDH_CSTATUS_ITEM_NOT_VALIDATED PdhErrno = 0x800007d3 + PDH_RETRY PdhErrno = 0x800007d4 + PDH_NO_DATA PdhErrno = 0x800007d5 + PDH_CALC_NEGATIVE_DENOMINATOR PdhErrno = 0x800007d6 + PDH_CALC_NEGATIVE_TIMEBASE PdhErrno = 0x800007d7 + PDH_CALC_NEGATIVE_VALUE PdhErrno = 0x800007d8 + PDH_DIALOG_CANCELLED PdhErrno = 0x800007d9 + PDH_END_OF_LOG_FILE PdhErrno = 0x800007da + PDH_ASYNC_QUERY_TIMEOUT PdhErrno = 0x800007db + PDH_CANNOT_SET_DEFAULT_REALTIME_DATASOURCE PdhErrno = 0x800007dc + PDH_CSTATUS_NO_OBJECT PdhErrno = 0xc0000bb8 + PDH_CSTATUS_NO_COUNTER PdhErrno = 0xc0000bb9 + PDH_CSTATUS_INVALID_DATA PdhErrno = 0xc0000bba + PDH_MEMORY_ALLOCATION_FAILURE PdhErrno = 0xc0000bbb + PDH_INVALID_HANDLE PdhErrno = 0xc0000bbc + PDH_INVALID_ARGUMENT PdhErrno = 0xc0000bbd + PDH_FUNCTION_NOT_FOUND PdhErrno = 0xc0000bbe + PDH_CSTATUS_NO_COUNTERNAME PdhErrno = 0xc0000bbf + PDH_CSTATUS_BAD_COUNTERNAME PdhErrno = 0xc0000bc0 + PDH_INVALID_BUFFER PdhErrno = 0xc0000bc1 + PDH_INSUFFICIENT_BUFFER PdhErrno = 0xc0000bc2 + PDH_CANNOT_CONNECT_MACHINE PdhErrno = 0xc0000bc3 + PDH_INVALID_PATH PdhErrno = 0xc0000bc4 + PDH_INVALID_INSTANCE PdhErrno = 0xc0000bc5 + PDH_INVALID_DATA PdhErrno = 0xc0000bc6 + PDH_NO_DIALOG_DATA PdhErrno = 0xc0000bc7 + PDH_CANNOT_READ_NAME_STRINGS PdhErrno = 0xc0000bc8 + PDH_LOG_FILE_CREATE_ERROR PdhErrno = 0xc0000bc9 + PDH_LOG_FILE_OPEN_ERROR PdhErrno = 0xc0000bca + PDH_LOG_TYPE_NOT_FOUND PdhErrno = 0xc0000bcb + PDH_NO_MORE_DATA PdhErrno = 0xc0000bcc + PDH_ENTRY_NOT_IN_LOG_FILE PdhErrno = 0xc0000bcd + PDH_DATA_SOURCE_IS_LOG_FILE PdhErrno = 0xc0000bce + PDH_DATA_SOURCE_IS_REAL_TIME PdhErrno = 0xc0000bcf + PDH_UNABLE_READ_LOG_HEADER PdhErrno = 0xc0000bd0 + PDH_FILE_NOT_FOUND PdhErrno = 0xc0000bd1 + PDH_FILE_ALREADY_EXISTS PdhErrno = 0xc0000bd2 + PDH_NOT_IMPLEMENTED PdhErrno = 0xc0000bd3 + PDH_STRING_NOT_FOUND PdhErrno = 0xc0000bd4 + PDH_UNABLE_MAP_NAME_FILES PdhErrno = 0x80000bd5 + PDH_UNKNOWN_LOG_FORMAT PdhErrno = 0xc0000bd6 + PDH_UNKNOWN_LOGSVC_COMMAND PdhErrno = 0xc0000bd7 + PDH_LOGSVC_QUERY_NOT_FOUND PdhErrno = 0xc0000bd8 + PDH_LOGSVC_NOT_OPENED PdhErrno = 0xc0000bd9 + PDH_WBEM_ERROR PdhErrno = 0xc0000bda + PDH_ACCESS_DENIED PdhErrno = 0xc0000bdb + PDH_LOG_FILE_TOO_SMALL PdhErrno = 0xc0000bdc + PDH_INVALID_DATASOURCE PdhErrno = 0xc0000bdd + PDH_INVALID_SQLDB PdhErrno = 0xc0000bde + PDH_NO_COUNTERS PdhErrno = 0xc0000bdf + PDH_SQL_ALLOC_FAILED PdhErrno = 0xc0000be0 + PDH_SQL_ALLOCCON_FAILED PdhErrno = 0xc0000be1 + PDH_SQL_EXEC_DIRECT_FAILED PdhErrno = 0xc0000be2 + PDH_SQL_FETCH_FAILED PdhErrno = 0xc0000be3 + PDH_SQL_ROWCOUNT_FAILED PdhErrno = 0xc0000be4 + PDH_SQL_MORE_RESULTS_FAILED PdhErrno = 0xc0000be5 + PDH_SQL_CONNECT_FAILED PdhErrno = 0xc0000be6 + PDH_SQL_BIND_FAILED PdhErrno = 0xc0000be7 + PDH_CANNOT_CONNECT_WMI_SERVER PdhErrno = 0xc0000be8 + PDH_PLA_COLLECTION_ALREADY_RUNNING PdhErrno = 0xc0000be9 + PDH_PLA_ERROR_SCHEDULE_OVERLAP PdhErrno = 0xc0000bea + PDH_PLA_COLLECTION_NOT_FOUND PdhErrno = 0xc0000beb + PDH_PLA_ERROR_SCHEDULE_ELAPSED PdhErrno = 0xc0000bec + PDH_PLA_ERROR_NOSTART PdhErrno = 0xc0000bed + PDH_PLA_ERROR_ALREADY_EXISTS PdhErrno = 0xc0000bee + PDH_PLA_ERROR_TYPE_MISMATCH PdhErrno = 0xc0000bef + PDH_PLA_ERROR_FILEPATH PdhErrno = 0xc0000bf0 + PDH_PLA_SERVICE_ERROR PdhErrno = 0xc0000bf1 + PDH_PLA_VALIDATION_ERROR PdhErrno = 0xc0000bf2 + PDH_PLA_VALIDATION_WARNING PdhErrno = 0x80000bf3 + PDH_PLA_ERROR_NAME_TOO_LONG PdhErrno = 0xc0000bf4 + PDH_INVALID_SQL_LOG_FORMAT PdhErrno = 0xc0000bf5 + PDH_COUNTER_ALREADY_IN_QUERY PdhErrno = 0xc0000bf6 + PDH_BINARY_LOG_CORRUPT PdhErrno = 0xc0000bf7 + PDH_LOG_SAMPLE_TOO_SMALL PdhErrno = 0xc0000bf8 + PDH_OS_LATER_VERSION PdhErrno = 0xc0000bf9 + PDH_OS_EARLIER_VERSION PdhErrno = 0xc0000bfa + PDH_INCORRECT_APPEND_TIME PdhErrno = 0xc0000bfb + PDH_UNMATCHED_APPEND_COUNTER PdhErrno = 0xc0000bfc + PDH_SQL_ALTER_DETAIL_FAILED PdhErrno = 0xc0000bfd + PDH_QUERY_PERF_DATA_TIMEOUT PdhErrno = 0xc0000bfe +) + +var pdhErrors = map[PdhErrno]struct{}{ + PDH_CSTATUS_VALID_DATA: struct{}{}, + PDH_CSTATUS_NEW_DATA: struct{}{}, + PDH_CSTATUS_NO_MACHINE: struct{}{}, + PDH_CSTATUS_NO_INSTANCE: struct{}{}, + PDH_MORE_DATA: struct{}{}, + PDH_CSTATUS_ITEM_NOT_VALIDATED: struct{}{}, + PDH_RETRY: struct{}{}, + PDH_NO_DATA: struct{}{}, + PDH_CALC_NEGATIVE_DENOMINATOR: struct{}{}, + PDH_CALC_NEGATIVE_TIMEBASE: struct{}{}, + PDH_CALC_NEGATIVE_VALUE: struct{}{}, + PDH_DIALOG_CANCELLED: struct{}{}, + PDH_END_OF_LOG_FILE: struct{}{}, + PDH_ASYNC_QUERY_TIMEOUT: struct{}{}, + PDH_CANNOT_SET_DEFAULT_REALTIME_DATASOURCE: struct{}{}, + PDH_CSTATUS_NO_OBJECT: struct{}{}, + PDH_CSTATUS_NO_COUNTER: struct{}{}, + PDH_CSTATUS_INVALID_DATA: struct{}{}, + PDH_MEMORY_ALLOCATION_FAILURE: struct{}{}, + PDH_INVALID_HANDLE: struct{}{}, + PDH_INVALID_ARGUMENT: struct{}{}, + PDH_FUNCTION_NOT_FOUND: struct{}{}, + PDH_CSTATUS_NO_COUNTERNAME: struct{}{}, + PDH_CSTATUS_BAD_COUNTERNAME: struct{}{}, + PDH_INVALID_BUFFER: struct{}{}, + PDH_INSUFFICIENT_BUFFER: struct{}{}, + PDH_CANNOT_CONNECT_MACHINE: struct{}{}, + PDH_INVALID_PATH: struct{}{}, + PDH_INVALID_INSTANCE: struct{}{}, + PDH_INVALID_DATA: struct{}{}, + PDH_NO_DIALOG_DATA: struct{}{}, + PDH_CANNOT_READ_NAME_STRINGS: struct{}{}, + PDH_LOG_FILE_CREATE_ERROR: struct{}{}, + PDH_LOG_FILE_OPEN_ERROR: struct{}{}, + PDH_LOG_TYPE_NOT_FOUND: struct{}{}, + PDH_NO_MORE_DATA: struct{}{}, + PDH_ENTRY_NOT_IN_LOG_FILE: struct{}{}, + PDH_DATA_SOURCE_IS_LOG_FILE: struct{}{}, + PDH_DATA_SOURCE_IS_REAL_TIME: struct{}{}, + PDH_UNABLE_READ_LOG_HEADER: struct{}{}, + PDH_FILE_NOT_FOUND: struct{}{}, + PDH_FILE_ALREADY_EXISTS: struct{}{}, + PDH_NOT_IMPLEMENTED: struct{}{}, + PDH_STRING_NOT_FOUND: struct{}{}, + PDH_UNABLE_MAP_NAME_FILES: struct{}{}, + PDH_UNKNOWN_LOG_FORMAT: struct{}{}, + PDH_UNKNOWN_LOGSVC_COMMAND: struct{}{}, + PDH_LOGSVC_QUERY_NOT_FOUND: struct{}{}, + PDH_LOGSVC_NOT_OPENED: struct{}{}, + PDH_WBEM_ERROR: struct{}{}, + PDH_ACCESS_DENIED: struct{}{}, + PDH_LOG_FILE_TOO_SMALL: struct{}{}, + PDH_INVALID_DATASOURCE: struct{}{}, + PDH_INVALID_SQLDB: struct{}{}, + PDH_NO_COUNTERS: struct{}{}, + PDH_SQL_ALLOC_FAILED: struct{}{}, + PDH_SQL_ALLOCCON_FAILED: struct{}{}, + PDH_SQL_EXEC_DIRECT_FAILED: struct{}{}, + PDH_SQL_FETCH_FAILED: struct{}{}, + PDH_SQL_ROWCOUNT_FAILED: struct{}{}, + PDH_SQL_MORE_RESULTS_FAILED: struct{}{}, + PDH_SQL_CONNECT_FAILED: struct{}{}, + PDH_SQL_BIND_FAILED: struct{}{}, + PDH_CANNOT_CONNECT_WMI_SERVER: struct{}{}, + PDH_PLA_COLLECTION_ALREADY_RUNNING: struct{}{}, + PDH_PLA_ERROR_SCHEDULE_OVERLAP: struct{}{}, + PDH_PLA_COLLECTION_NOT_FOUND: struct{}{}, + PDH_PLA_ERROR_SCHEDULE_ELAPSED: struct{}{}, + PDH_PLA_ERROR_NOSTART: struct{}{}, + PDH_PLA_ERROR_ALREADY_EXISTS: struct{}{}, + PDH_PLA_ERROR_TYPE_MISMATCH: struct{}{}, + PDH_PLA_ERROR_FILEPATH: struct{}{}, + PDH_PLA_SERVICE_ERROR: struct{}{}, + PDH_PLA_VALIDATION_ERROR: struct{}{}, + PDH_PLA_VALIDATION_WARNING: struct{}{}, + PDH_PLA_ERROR_NAME_TOO_LONG: struct{}{}, + PDH_INVALID_SQL_LOG_FORMAT: struct{}{}, + PDH_COUNTER_ALREADY_IN_QUERY: struct{}{}, + PDH_BINARY_LOG_CORRUPT: struct{}{}, + PDH_LOG_SAMPLE_TOO_SMALL: struct{}{}, + PDH_OS_LATER_VERSION: struct{}{}, + PDH_OS_EARLIER_VERSION: struct{}{}, + PDH_INCORRECT_APPEND_TIME: struct{}{}, + PDH_UNMATCHED_APPEND_COUNTER: struct{}{}, + PDH_SQL_ALTER_DETAIL_FAILED: struct{}{}, + PDH_QUERY_PERF_DATA_TIMEOUT: struct{}{}, +} + +type PdhCounterFormat uint32 + +const ( + PdhFmtDouble PdhCounterFormat = 0x200 + + PdhFmtLarge PdhCounterFormat = 0x400 + + PdhFmtLong PdhCounterFormat = 0x100 + + PdhFmtNoScale PdhCounterFormat = 0x1000 + + PdhFmtNoCap100 PdhCounterFormat = 0x8000 + + PdhFmtMultiply1000 PdhCounterFormat = 0x2000 +) diff --git a/helpers/windows/pdh/doc.go b/helpers/windows/pdh/doc.go new file mode 100644 index 0000000..736bde4 --- /dev/null +++ b/helpers/windows/pdh/doc.go @@ -0,0 +1,24 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package pdh + +//go:generate go run mkpdh_defs.go +//go:generate go run ../run.go -cmd "go tool cgo -godefs defs_pdh_windows.go" -goarch amd64 -output defs_pdh_windows_amd64.go +//go:generate go run ../run.go -cmd "go tool cgo -godefs defs_pdh_windows.go" -goarch 386 -output defs_pdh_windows_386.go +//go:generate go run $GOROOT/src/syscall/mksyscall_windows.go -output zpdh_windows.go pdh_windows.go +//go:generate goimports -w defs_pdh_windows_amd64.go defs_pdh_windows_386.go zpdh_windows.go diff --git a/helpers/windows/pdh/mkpdh_defs.go b/helpers/windows/pdh/mkpdh_defs.go new file mode 100644 index 0000000..6bb3d95 --- /dev/null +++ b/helpers/windows/pdh/mkpdh_defs.go @@ -0,0 +1,165 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//go:build ignore + +package main + +import ( + "bufio" + "bytes" + "flag" + "fmt" + "os" + "os/exec" + "regexp" + "text/template" +) + +var ( + output = flag.String("output", "defs_pdh_windows.go", "output file") +) + +const includes = ` +#include +` + +type TemplateParams struct { + Errors []string +} + +const fileTemplate = ` +// go run mkpdh_defs.go +// MACHINE GENERATED BY THE ABOVE COMMAND; DO NOT EDIT + +// +build ignore + +package pdh + +/* +#include +#include +#cgo LDFLAGS: -lpdh +*/ +import "C" + +type PdhErrno uintptr + +// PDH Error Codes +const ( +{{- range $i, $errorCode := .Errors }} + {{ $errorCode }} PdhErrno = C.{{ $errorCode }} +{{- end }} +) + +var pdhErrors = map[PdhErrno]struct{}{ +{{- range $i, $errorCode := .Errors }} + {{ $errorCode }}: struct{}{}, +{{- end }} +} + +type PdhCounterFormat uint32 + +// PDH Counter Formats +const ( + // PdhFmtDouble returns data as a double-precision floating point real. + PdhFmtDouble PdhCounterFormat = C.PDH_FMT_DOUBLE + // PdhFmtLarge returns data as a 64-bit integer. + PdhFmtLarge PdhCounterFormat = C.PDH_FMT_LARGE + // PdhFmtLong returns data as a long integer. + PdhFmtLong PdhCounterFormat = C.PDH_FMT_LONG + + // Use bitwise operators to combine these values with the counter type to scale the value. + + // Do not apply the counter's default scaling factor. + PdhFmtNoScale PdhCounterFormat = C.PDH_FMT_NOSCALE + // Counter values greater than 100 (for example, counter values measuring + // the processor load on multiprocessor computers) will not be reset to 100. + // The default behavior is that counter values are capped at a value of 100. + PdhFmtNoCap100 PdhCounterFormat = C.PDH_FMT_NOCAP100 + // Multiply the actual value by 1,000. + PdhFmtMultiply1000 PdhCounterFormat = C.PDH_FMT_1000 +) +` + +var ( + tmpl = template.Must(template.New("defs_pdh_windows").Parse(fileTemplate)) + + pdhErrorRegex = regexp.MustCompile(`^#define (PDH_[\w_]+)`) +) + +func main() { + errors, err := getErrorDefines() + if err != nil { + fmt.Fprintln(os.Stderr, "error:", err) + os.Exit(1) + } + + t := TemplateParams{ + Errors: errors, + } + + if err := writeOutput(t); err != nil { + fmt.Fprintln(os.Stderr, "error:", err) + os.Exit(1) + } + + if err := gofmtOutput(); err != nil { + fmt.Fprintln(os.Stderr, "error:", err) + os.Exit(1) + } +} + +func getErrorDefines() ([]string, error) { + cmd := exec.Command("gcc", "-E", "-dD", "-") + cmd.Stdin = bytes.NewBuffer([]byte(includes)) + out, err := cmd.Output() + if err != nil { + return nil, err + } + + var errors []string + s := bufio.NewScanner(bytes.NewBuffer(out)) + for s.Scan() { + line := s.Text() + matches := pdhErrorRegex.FindStringSubmatch(line) + if len(matches) > 1 { + errors = append(errors, matches[1]) + } + } + + return errors, nil +} + +func writeOutput(p TemplateParams) error { + // Create output file. + f, err := os.Create(*output) + if err != nil { + return err + } + defer f.Close() + + if err := tmpl.Execute(f, p); err != nil { + return err + } + return nil +} + +func gofmtOutput() error { + _, err := exec.Command("gofmt", "-w", *output).Output() + return err +} diff --git a/helpers/windows/pdh/pdh_query_windows.go b/helpers/windows/pdh/pdh_query_windows.go new file mode 100644 index 0000000..94270af --- /dev/null +++ b/helpers/windows/pdh/pdh_query_windows.go @@ -0,0 +1,377 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//go:build windows + +package pdh + +import ( + "errors" + "fmt" + "regexp" + "runtime" + "strings" + "syscall" + "unsafe" + + "golang.org/x/sys/windows" +) + +var ( + instanceNameRegexp = regexp.MustCompile(`(\(.+\))\\`) + objectNameRegexp = regexp.MustCompile(`(?:^\\\\[^\\]+\\|^\\)([^\\]+)`) +) + +// Counter object will contain the handle and format of each performance counter. +type Counter struct { + handle PdhCounterHandle + format PdhCounterFormat + instanceName string +} + +// Query contains the pdh. +type Query struct { + Handle PdhQueryHandle + Counters map[string]*Counter +} + +// CounterValue contains the performance counter values. +type CounterValue struct { + Instance string + Measurement interface{} + Err CounterValueError +} + +// CounterValueError contains the performance counter error. +type CounterValueError struct { + Error error + CStatus uint32 +} + +// Open creates a new query. +func (q *Query) Open() error { + h, err := PdhOpenQuery("", 0) + if err != nil { + return err + } + q.Handle = h + q.Counters = make(map[string]*Counter) + return nil +} + +// // AddEnglishCounter adds the specified counter to the query. +func (q *Query) AddEnglishCounter(counterPath string) (PdhCounterHandle, error) { + h, err := PdhAddEnglishCounter(q.Handle, counterPath, 0) + return h, err +} + +// AddCounter adds the specified counter to the query. +func (q *Query) AddCounter(counterPath string, instance string, format string, wildcard bool) error { + if _, found := q.Counters[counterPath]; found { + return nil + } + var err error + var instanceName string + // Extract the instance name from the counterPath. + if instance == "" || wildcard { + instanceName, err = matchInstanceName(counterPath) + if err != nil { + return err + } + } else { + instanceName = instance + } + h, err := PdhAddCounter(q.Handle, counterPath, 0) + if err != nil { + return err + } + + q.Counters[counterPath] = &Counter{ + handle: h, + instanceName: instanceName, + format: getPDHFormat(format), + } + return nil +} + +// GetCounterPaths func will check the computer or log file and return the counter paths that match the given counter path which contains wildcard characters. +func (q *Query) GetCounterPaths(counterPath string) ([]string, error) { + paths, err := q.ExpandWildCardPath(counterPath) + if err == nil { + return paths, err + } + //check if Windows installed language is not ENG, the ExpandWildCardPath will return either one of the errors below. + if err == PDH_CSTATUS_NO_OBJECT || err == PDH_CSTATUS_NO_COUNTER { + handle, err := q.AddEnglishCounter(counterPath) + if err != nil { + return nil, err + } + defer PdhRemoveCounter(handle) + info, err := PdhGetCounterInfo(handle) + if err != nil { + return nil, err + } + path := UTF16PtrToString(info.SzFullPath) + if path != counterPath { + return q.ExpandWildCardPath(path) + } + } + return nil, err +} + +// RemoveUnusedCounters will remove all counter handles for the paths that are not found anymore +func (q *Query) RemoveUnusedCounters(counters []string) error { + // check if the expandwildcard func did expand th wildcard queries, if not, no counters will be removed + for _, counter := range counters { + if strings.Contains(counter, "*") { + return nil + } + } + unused := make(map[string]*Counter) + for counterPath, counter := range q.Counters { + if !matchCounter(counterPath, counters) { + unused[counterPath] = counter + } + } + if len(unused) == 0 { + return nil + } + for counterPath, cnt := range unused { + err := PdhRemoveCounter(cnt.handle) + if err != nil { + return err + } + delete(q.Counters, counterPath) + } + return nil +} + +func matchCounter(counterPath string, counterList []string) bool { + for _, cn := range counterList { + if cn == counterPath { + return true + } + } + return false +} + +// CollectData collects the value for all counters in the query. +func (q *Query) CollectData() error { + return PdhCollectQueryData(q.Handle) +} + +// CollectData collects the value for all counters in the query. +func (q *Query) CollectDataEx(interval uint32, event windows.Handle) error { + return PdhCollectQueryDataEx(q.Handle, interval, event) +} + +// GetFormattedCounterValues returns an array of formatted values for a query. +func (q *Query) GetFormattedCounterValues() (map[string][]CounterValue, error) { + if q.Counters == nil || len(q.Counters) == 0 { + return nil, errors.New("no counter list found") + } + rtn := make(map[string][]CounterValue, len(q.Counters)) + for path, counter := range q.Counters { + rtn[path] = append(rtn[path], getCounterValue(counter)) + } + return rtn, nil +} + +// GetCountersAndInstances returns a list of counters and instances for a given object +func (q *Query) GetCountersAndInstances(objectName string) ([]string, []string, error) { + counters, instances, err := PdhEnumObjectItems(objectName) + if err != nil { + return nil, nil, fmt.Errorf("Unable to retrieve counter and instance list for %s: %w", objectName, err) + } + if len(counters) == 0 && len(instances) == 0 { + return nil, nil, fmt.Errorf("Unable to retrieve counter and instance list for %s", objectName) + } + return UTF16ToStringArray(counters), UTF16ToStringArray(instances), nil +} + +// ExpandWildCardPath examines local computer and returns those counter paths that match the given counter path which contains wildcard characters. +func (q *Query) ExpandWildCardPath(wildCardPath string) ([]string, error) { + if wildCardPath == "" { + return nil, errors.New("no query path given") + } + utfPath, err := syscall.UTF16PtrFromString(wildCardPath) + if err != nil { + return nil, err + } + var expdPaths []uint16 + + // PdhExpandWildCardPath will not return the counter paths for windows 32 bit systems but PdhExpandCounterPath will. + if runtime.GOARCH == "386" { + if expdPaths, err = PdhExpandCounterPath(utfPath); err != nil { + return nil, err + } + if expdPaths == nil { + return nil, errors.New("no counter paths found") + } + return UTF16ToStringArray(expdPaths), nil + } else { + if expdPaths, err = PdhExpandWildCardPath(utfPath); err != nil { + if err == PDH_MORE_DATA { + if expdPaths, err = PdhExpandWildCardPath(utfPath); err != nil { + return nil, err + } + } else { + return nil, err + } + } + paths := UTF16ToStringArray(expdPaths) + // in several cases ExpandWildCardPath win32 api seems to return initial wildcard without any errors, adding some waiting time between the 2 ExpandWildCardPath api calls seems to be succesfull but that will delay data retrieval + // A call is triggered again + if len(paths) == 1 && strings.Contains(paths[0], "*") && paths[0] == wildCardPath { + expdPaths, err = PdhExpandWildCardPath(utfPath) + if err == nil { + return paths, err + } + } else { + return paths, err + } + } + + return nil, PdhErrno(syscall.ERROR_NOT_FOUND) +} + +// Close closes the query and all of its counters. +func (q *Query) Close() error { + return PdhCloseQuery(q.Handle) +} + +// matchInstanceName will check first for instance and then for any objects names. +func matchInstanceName(counterPath string) (string, error) { + matches := instanceNameRegexp.FindStringSubmatch(counterPath) + if len(matches) == 2 { + return returnLastInstance(matches[1]), nil + } + matches = objectNameRegexp.FindStringSubmatch(counterPath) + if len(matches) == 2 { + return matches[1], nil + } + return "", errors.New("query doesn't contain an instance name. In this case you have to define 'instance_name'") +} + +// returnLastInstance will return the content from the last parentheses, this covers cases as `\WF (System.Workflow) 4.0.0.0(*)\Workflows Created`. +func returnLastInstance(match string) string { + var openedParanth int + var innerMatch string + var matches []string + runeMatch := []rune(match) + for i := 0; i < len(runeMatch); i++ { + char := string(runeMatch[i]) + + // check if string ends between parentheses + if char == ")" { + openedParanth -= 1 + } + if openedParanth > 0 { + innerMatch += char + } + if openedParanth == 0 && innerMatch != "" { + matches = append(matches, innerMatch) + innerMatch = "" + } + // check if string starts between parentheses + if char == "(" { + openedParanth += 1 + } + } + if len(matches) > 0 { + return matches[len(matches)-1] + } + return match +} + +// getCounterValue will retrieve the counter value based on the format applied in the config options +func getCounterValue(counter *Counter) CounterValue { + counterValue := CounterValue{Instance: counter.instanceName, Err: CounterValueError{CStatus: 0}} + switch counter.format { + case PdhFmtLong: + _, value, err := PdhGetFormattedCounterValueLong(counter.handle) + if err != nil { + counterValue.Err.Error = err + if value != nil { + counterValue.Err.CStatus = value.CStatus + } + } else { + counterValue.Measurement = value.Value + } + case PdhFmtLarge: + _, value, err := PdhGetFormattedCounterValueLarge(counter.handle) + if err != nil { + counterValue.Err.Error = err + if value != nil { + counterValue.Err.CStatus = value.CStatus + } + } else { + counterValue.Measurement = value.Value + } + case PdhFmtDouble: + _, value, err := PdhGetFormattedCounterValueDouble(counter.handle) + if err != nil { + counterValue.Err.Error = err + if value != nil { + counterValue.Err.CStatus = value.CStatus + } + } else { + counterValue.Measurement = value.Value + fmt.Println(value.Value) + } + default: + counterValue.Err.Error = fmt.Errorf("initialization failed: format '%#v' "+ + "for instance '%s' is invalid (must be PdhFmtDouble, PdhFmtLarge or PdhFmtLong)", + counter.format, counter.instanceName) + } + + return counterValue +} + +// getPDHFormat calculates data pdhformat. +func getPDHFormat(format string) PdhCounterFormat { + switch format { + case "long": + return PdhFmtLong + case "large": + return PdhFmtLarge + default: + return PdhFmtDouble + } +} + +// UTF16ToStringArray converts list of Windows API NULL terminated strings to Go string array. +func UTF16ToStringArray(buf []uint16) []string { + var strings []string + nextLineStart := 0 + stringLine := UTF16PtrToString(&buf[0]) + for stringLine != "" { + strings = append(strings, stringLine) + nextLineStart += len([]rune(stringLine)) + 1 + remainingBuf := buf[nextLineStart:] + stringLine = UTF16PtrToString(&remainingBuf[0]) + } + return strings +} + +// UTF16PtrToString converts Windows API LPTSTR (pointer to string) to Go string. +func UTF16PtrToString(s *uint16) string { + if s == nil { + return "" + } + return syscall.UTF16ToString((*[1 << 29]uint16)(unsafe.Pointer(s))[0:]) +} diff --git a/helpers/windows/pdh/pdh_query_windows_test.go b/helpers/windows/pdh/pdh_query_windows_test.go new file mode 100644 index 0000000..3a09d6d --- /dev/null +++ b/helpers/windows/pdh/pdh_query_windows_test.go @@ -0,0 +1,195 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package pdh + +import ( + "syscall" + "testing" + + "github.com/stretchr/testify/assert" +) + +// TestOpenSuccessful will open query successfully. +func TestOpenSuccessful(t *testing.T) { + var q Query + err := q.Open() + assert.NoError(t, err) + defer q.Close() +} + +// TestAddCounterInvalidArgWhenQueryClosed will check if addcounter func fails when query is closed. +func TestAddCounterInvalidArgWhenQueryClosed(t *testing.T) { + var q Query + queryPath, err := q.GetCounterPaths(validQuery) + // if windows os language is ENG then err will be nil, else the GetCounterPaths will execute the AddCounter + if assert.NoError(t, err) { + err = q.AddCounter(queryPath[0], "TestInstanceName", "float", false) + assert.Error(t, err, PDH_INVALID_HANDLE) + } else { + assert.Error(t, err, PDH_INVALID_ARGUMENT) + } +} + +// func TestGetFormattedCounterValuesEmptyCounterList will check if getting the counter values will fail when no counter handles are added. +func TestGetFormattedCounterValuesEmptyCounterList(t *testing.T) { + var q Query + list, err := q.GetFormattedCounterValues() + assert.Nil(t, list) + assert.EqualValues(t, err.Error(), "no counter list found") +} + +// TestExpandWildCardPathWithEmptyString will check for a valid path string. +func TestExpandWildCardPathWithEmptyString(t *testing.T) { + var q Query + list, err := q.ExpandWildCardPath("") + assert.Nil(t, list) + assert.EqualValues(t, err.Error(), "no query path given") +} + +// TestSuccessfulQuery retrieves a per counter successfully. +func TestSuccessfulQuery(t *testing.T) { + var q Query + err := q.Open() + if err != nil { + t.Fatal(err) + } + defer q.Close() + queryPath, err := q.GetCounterPaths(validQuery) + if err != nil { + t.Fatal(err) + } + err = q.AddCounter(queryPath[0], "TestInstanceName", "floar", false) + if err != nil { + t.Fatal(err) + } + //Some counters, such as rate counters, require two counter values in order to compute a displayable value. In this case we must call PdhCollectQueryData twice before calling PdhGetFormattedCounterValue. + // For more information, see Collecting Performance Data (https://docs.microsoft.com/en-us/windows/desktop/PerfCtrs/collecting-performance-data). + err = q.CollectData() + err = q.CollectData() + if err != nil { + t.Fatal(err) + } + list, err := q.GetFormattedCounterValues() + assert.NoError(t, err) + assert.NotNil(t, list) +} + +func TestMatchInstanceName(t *testing.T) { + query := "\\SQLServer:Databases(*)\\Log File(s) Used Size (KB)" + match, err := matchInstanceName(query) + assert.NoError(t, err) + assert.Equal(t, match, "*") + + query = " \\\\desktop-rfooe09\\per processor network interface card activity(3, microsoft wi-fi directvirtual (gyfyg) adapter #2)\\dpcs queued/sec" + match, err = matchInstanceName(query) + assert.NoError(t, err) + assert.Equal(t, match, "3, microsoft wi-fi directvirtual (gyfyg) adapter #2") + + query = " \\\\desktop-rfooe09\\ (test this scenario) per processor network interface card activity(3, microsoft wi-fi directvirtual (gyfyg) adapter #2)\\dpcs queued/sec" + match, err = matchInstanceName(query) + assert.NoError(t, err) + assert.Equal(t, match, "3, microsoft wi-fi directvirtual (gyfyg) adapter #2") + + query = "\\RAS\\Bytes Received By Disconnected Clients" + match, err = matchInstanceName(query) + assert.NoError(t, err) + assert.Equal(t, match, "RAS") + + query = `\\Process (chrome.exe#4)\\Bytes Received By Disconnected Clients` + match, err = matchInstanceName(query) + assert.NoError(t, err) + assert.Equal(t, match, "chrome.exe#4") + + query = "\\BranchCache\\Local Cache: Cache complete file segments" + match, err = matchInstanceName(query) + assert.NoError(t, err) + assert.Equal(t, match, "BranchCache") + + query = `\Synchronization(*)\Exec. Resource no-Waits AcqShrdStarveExcl/sec` + match, err = matchInstanceName(query) + assert.NoError(t, err) + assert.Equal(t, match, "*") + + query = `\.NET CLR Exceptions(test hellp (dsdsd) #rfsfs #3)\# of Finallys / sec` + match, err = matchInstanceName(query) + assert.NoError(t, err) + assert.Equal(t, match, "test hellp (dsdsd) #rfsfs #3") +} + +// TestInstanceNameRegexp tests regular expression for instance. +func TestInstanceNameRegexp(t *testing.T) { + queryPaths := []string{`\SQLServer:Databases(*)\Log File(s) Used Size (KB)`, `\Search Indexer(*)\L0 Indexes (Wordlists)`, + `\Search Indexer(*)\L0 Merges (flushes) Now.`, `\NUMA Node Memory(*)\Free & Zero Page List MBytes`} + for _, path := range queryPaths { + matches := instanceNameRegexp.FindStringSubmatch(path) + if assert.Len(t, matches, 2, "regular expression did not return any matches") { + assert.Equal(t, matches[1], "(*)") + } + } +} + +// TestObjectNameRegexp tests regular expression for object. +func TestObjectNameRegexp(t *testing.T) { + queryPaths := []string{`\Web Service Cache\Output Cache Current Flushed Items`, + `\Web Service Cache\Output Cache Total Flushed Items`, `\Web Service Cache\Total Flushed Metadata`, + `\Web Service Cache\Kernel: Current URIs Cached`} + for _, path := range queryPaths { + matches := objectNameRegexp.FindStringSubmatch(path) + if assert.Len(t, matches, 2, "regular expression did not return any matches") { + assert.Equal(t, matches[1], "Web Service Cache") + } + } +} + +func TestReturnLastInstance(t *testing.T) { + query := "(*)" + match := returnLastInstance(query) + assert.Equal(t, match, "*") + + query = "(3, microsoft wi-fi directvirtual (gyfyg) adapter #2)" + match = returnLastInstance(query) + assert.Equal(t, match, "3, microsoft wi-fi directvirtual (gyfyg) adapter #2") + + query = "(test this scenario) per processor network interface card activity(3, microsoft wi-fi directvirtual (gyfyg) adapter #2)" + match = returnLastInstance(query) + assert.Equal(t, match, "3, microsoft wi-fi directvirtual (gyfyg) adapter #2") + + query = `(chrome.exe#4)` + match = returnLastInstance(query) + assert.Equal(t, match, "chrome.exe#4") + + query = `(test hellp (dsdsd) #rfsfs #3)` + match = returnLastInstance(query) + assert.Equal(t, match, "test hellp (dsdsd) #rfsfs #3") +} + +func TestUTF16ToStringArray(t *testing.T) { + var array = []string{"\\\\DESKTOP-RFOOE09\\Physikalischer Datenträger(0 C:)\\Schreibvorgänge/s", "\\\\DESKTOP-RFOOE09\\Physikalischer Datenträger(_Total)\\Schreibvorgänge/s", ""} + var unicode []uint16 + for _, i := range array { + uni, err := syscall.UTF16FromString(i) + assert.NoError(t, err) + unicode = append(unicode, uni...) + } + response := UTF16ToStringArray(unicode) + assert.NotNil(t, response) + assert.Equal(t, len(response), 2) + for _, res := range response { + assert.Contains(t, array, res) + } +} diff --git a/helpers/windows/pdh/pdh_windows.go b/helpers/windows/pdh/pdh_windows.go new file mode 100644 index 0000000..6c4bcb9 --- /dev/null +++ b/helpers/windows/pdh/pdh_windows.go @@ -0,0 +1,326 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//go:build windows + +package pdh + +import ( + "strconv" + "syscall" + "unicode/utf16" + "unsafe" + + "golang.org/x/sys/windows" +) + +// Windows API calls +//sys _PdhOpenQuery(dataSource *uint16, userData uintptr, query *PdhQueryHandle) (errcode error) [failretval!=0] = pdh.PdhOpenQueryW +//sys _PdhAddEnglishCounter(query PdhQueryHandle, counterPath string, userData uintptr, counter *PdhCounterHandle) (errcode error) [failretval!=0] = pdh.PdhAddEnglishCounterW +//sys _PdhAddCounter(query PdhQueryHandle, counterPath string, userData uintptr, counter *PdhCounterHandle) (errcode error) [failretval!=0] = pdh.PdhAddCounterW +//sys _PdhRemoveCounter(counter PdhCounterHandle) (errcode error) [failretval!=0] = pdh.PdhRemoveCounter +//sys _PdhCollectQueryData(query PdhQueryHandle) (errcode error) [failretval!=0] = pdh.PdhCollectQueryData +//sys _PdhCollectQueryDataEx(query PdhQueryHandle, interval uint32, event windows.Handle) (errcode error) [failretval!=0] = pdh.PdhCollectQueryDataEx +//sys _PdhGetFormattedCounterValueDouble(counter PdhCounterHandle, format PdhCounterFormat, counterType *uint32, value *PdhCounterValueDouble) (errcode error) [failretval!=0] = pdh.PdhGetFormattedCounterValue +//sys _PdhGetFormattedCounterValueLarge(counter PdhCounterHandle, format PdhCounterFormat, counterType *uint32, value *PdhCounterValueLarge) (errcode error) [failretval!=0] = pdh.PdhGetFormattedCounterValue +//sys _PdhGetFormattedCounterValueLong(counter PdhCounterHandle, format PdhCounterFormat, counterType *uint32, value *PdhCounterValueLong) (errcode error) [failretval!=0]= pdh.PdhGetFormattedCounterValue +//sys _PdhCloseQuery(query PdhQueryHandle) (errcode error) [failretval!=0] = pdh.PdhCloseQuery +//sys _PdhExpandWildCardPath(dataSource *uint16, wildcardPath *uint16, expandedPathList *uint16, pathListLength *uint32) (errcode error) [failretval!=0] = pdh.PdhExpandWildCardPathW +//sys _PdhExpandCounterPath(wildcardPath *uint16, expandedPathList *uint16, pathListLength *uint32) (errcode error) [failretval!=0] = pdh.PdhExpandCounterPathW +//sys _PdhGetCounterInfo(counter PdhCounterHandle, text uint16, size *uint32, lpBuffer *byte) (errcode error) [failretval!=0] = pdh.PdhGetCounterInfoW +//sys _PdhEnumObjectItems(dataSource uint16, machineName uint16, objectName *uint16, counterList *uint16, counterListSize *uint32, instanceList *uint16, instanceListSize *uint32, detailLevel uint32, flags uint32) (errcode error) [failretval!=0] = pdh.PdhEnumObjectItemsW + +type PdhQueryHandle uintptr + +var InvalidQueryHandle = ^PdhQueryHandle(0) + +type PdhCounterHandle uintptr + +var InvalidCounterHandle = ^PdhCounterHandle(0) + +// PerformanceDetailWizard is the counter detail level +const PerformanceDetailWizard = 400 + +// PdhCounterInfo struct contains the performance counter details +type PdhCounterInfo struct { + DwLength uint32 + DwType uint32 + CVersion uint32 + CStatus uint32 + LScale int32 + LDefaultScale int32 + DwUserData *uint32 + DwQueryUserData *uint32 + SzFullPath *uint16 // pointer to a string + SzMachineName *uint16 // pointer to a string + SzObjectName *uint16 // pointer to a string + SzInstanceName *uint16 // pointer to a string + SzParentInstance *uint16 // pointer to a string + DwInstanceIndex uint32 // pointer to a string + SzCounterName *uint16 // pointer to a string + Padding [4]byte + SzExplainText *uint16 // pointer to a string + DataBuffer [1]uint32 // pointer to an extra space +} + +// PdhCounterValueDouble for double values +type PdhCounterValueDouble struct { + CStatus uint32 + Pad_cgo_0 [4]byte + Value float64 + Pad_cgo_1 [4]byte +} + +// PdhCounterValueLarge for 64 bit integer values +type PdhCounterValueLarge struct { + CStatus uint32 + Pad_cgo_0 [4]byte + Value int64 + Pad_cgo_1 [4]byte +} + +// PdhCounterValueLong for long values +type PdhCounterValueLong struct { + CStatus uint32 + Pad_cgo_0 [4]byte + Value int32 + Pad_cgo_1 [4]byte +} + +// PdhOpenQuery creates a new query. +func PdhOpenQuery(dataSource string, userData uintptr) (PdhQueryHandle, error) { + var dataSourcePtr *uint16 + if dataSource != "" { + var err error + dataSourcePtr, err = syscall.UTF16PtrFromString(dataSource) + if err != nil { + return InvalidQueryHandle, err + } + } + + var handle PdhQueryHandle + if err := _PdhOpenQuery(dataSourcePtr, userData, &handle); err != nil { + return InvalidQueryHandle, PdhErrno(err.(syscall.Errno)) + } + return handle, nil +} + +// PdhAddEnglishCounter adds the specified counter to the query. +func PdhAddEnglishCounter(query PdhQueryHandle, counterPath string, userData uintptr) (PdhCounterHandle, error) { + var handle PdhCounterHandle + if err := _PdhAddEnglishCounter(query, counterPath, userData, &handle); err != nil { + return InvalidCounterHandle, PdhErrno(err.(syscall.Errno)) + } + + return handle, nil +} + +// PdhAddCounter adds the specified counter to the query. +func PdhAddCounter(query PdhQueryHandle, counterPath string, userData uintptr) (PdhCounterHandle, error) { + var handle PdhCounterHandle + if err := _PdhAddCounter(query, counterPath, userData, &handle); err != nil { + return InvalidCounterHandle, PdhErrno(err.(syscall.Errno)) + } + + return handle, nil +} + +// PdhRemoveCounter removes the specified counter to the query. +func PdhRemoveCounter(counter PdhCounterHandle) error { + if err := _PdhRemoveCounter(counter); err != nil { + return PdhErrno(err.(syscall.Errno)) + } + + return nil +} + +// PdhCollectQueryData collects the current raw data value for all counters in the specified query. +func PdhCollectQueryData(query PdhQueryHandle) error { + if err := _PdhCollectQueryData(query); err != nil { + return PdhErrno(err.(syscall.Errno)) + } + + return nil +} + +// PdhCollectQueryDataEx collects the current raw data value for all counters in the specified query. +func PdhCollectQueryDataEx(query PdhQueryHandle, interval uint32, event windows.Handle) error { + if err := _PdhCollectQueryDataEx(query, interval, event); err != nil { + return PdhErrno(err.(syscall.Errno)) + } + return nil +} + +// PdhGetFormattedCounterValueDouble computes a displayable double value for the specified counter. +func PdhGetFormattedCounterValueDouble(counter PdhCounterHandle) (uint32, *PdhCounterValueDouble, error) { + var counterType uint32 + var value PdhCounterValueDouble + if err := _PdhGetFormattedCounterValueDouble(counter, PdhFmtDouble|PdhFmtNoCap100, &counterType, &value); err != nil { + return 0, &value, PdhErrno(err.(syscall.Errno)) + } + + return counterType, &value, nil +} + +// PdhGetFormattedCounterValueLarge computes a displayable large value for the specified counter. +func PdhGetFormattedCounterValueLarge(counter PdhCounterHandle) (uint32, *PdhCounterValueLarge, error) { + var counterType uint32 + var value PdhCounterValueLarge + if err := _PdhGetFormattedCounterValueLarge(counter, PdhFmtLarge|PdhFmtNoCap100, &counterType, &value); err != nil { + return 0, &value, PdhErrno(err.(syscall.Errno)) + } + + return counterType, &value, nil +} + +// PdhGetFormattedCounterValueLong computes a displayable long value for the specified counter. +func PdhGetFormattedCounterValueLong(counter PdhCounterHandle) (uint32, *PdhCounterValueLong, error) { + var counterType uint32 + var value PdhCounterValueLong + if err := _PdhGetFormattedCounterValueLong(counter, PdhFmtLong|PdhFmtNoCap100, &counterType, &value); err != nil { + return 0, &value, PdhErrno(err.(syscall.Errno)) + } + + return counterType, &value, nil +} + +// PdhExpandWildCardPath returns counter paths that match the given counter path. +func PdhExpandWildCardPath(utfPath *uint16) ([]uint16, error) { + var bufferSize uint32 + if err := _PdhExpandWildCardPath(nil, utfPath, nil, &bufferSize); err != nil { + if PdhErrno(err.(syscall.Errno)) != PDH_MORE_DATA { + return nil, PdhErrno(err.(syscall.Errno)) + } + expandPaths := make([]uint16, bufferSize) + if err = _PdhExpandWildCardPath(nil, utfPath, &expandPaths[0], &bufferSize); err != nil { + return nil, PdhErrno(err.(syscall.Errno)) + } + return expandPaths, err + } + return nil, PdhErrno(syscall.ERROR_NOT_FOUND) +} + +// PdhExpandCounterPath returns counter paths that match the given counter path, for 32 bit windows. +func PdhExpandCounterPath(utfPath *uint16) ([]uint16, error) { + var bufferSize uint32 + if err := _PdhExpandCounterPath(utfPath, nil, &bufferSize); err != nil { + if PdhErrno(err.(syscall.Errno)) != PDH_MORE_DATA { + return nil, PdhErrno(err.(syscall.Errno)) + } + expandPaths := make([]uint16, bufferSize) + if err := _PdhExpandCounterPath(utfPath, &expandPaths[0], &bufferSize); err != nil { + return nil, PdhErrno(err.(syscall.Errno)) + } + return expandPaths, nil + } + return nil, nil +} + +// PdhGetCounterInfo returns the counter information for given handle +func PdhGetCounterInfo(handle PdhCounterHandle) (*PdhCounterInfo, error) { + var bufSize uint32 + var buff []byte + if err := _PdhGetCounterInfo(handle, 0, &bufSize, nil); err != nil { + if PdhErrno(err.(syscall.Errno)) != PDH_MORE_DATA { + return nil, PdhErrno(err.(syscall.Errno)) + } + buff = make([]byte, bufSize) + bufSize = uint32(len(buff)) + + if err = _PdhGetCounterInfo(handle, 0, &bufSize, &buff[0]); err == nil { + counterInfo := (*PdhCounterInfo)(unsafe.Pointer(&buff[0])) + if counterInfo != nil { + return counterInfo, nil + } + } + } + return nil, nil +} + +// PdhCloseQuery closes all counters contained in the specified query. +func PdhCloseQuery(query PdhQueryHandle) error { + if err := _PdhCloseQuery(query); err != nil { + return PdhErrno(err.(syscall.Errno)) + } + + return nil +} + +// PdhEnumObjectItems returns the counters and instance info for given object +func PdhEnumObjectItems(objectName string) ([]uint16, []uint16, error) { + var ( + cBuff = make([]uint16, 1) + cBuffSize = uint32(0) + iBuff = make([]uint16, 1) + iBuffSize = uint32(0) + ) + obj := windows.StringToUTF16Ptr(objectName) + if err := _PdhEnumObjectItems( + 0, + 0, + obj, + &cBuff[0], + &cBuffSize, + &iBuff[0], + &iBuffSize, + PerformanceDetailWizard, + 0); err != nil { + if PdhErrno(err.(syscall.Errno)) != PDH_MORE_DATA { + return nil, nil, PdhErrno(err.(syscall.Errno)) + } + cBuff = make([]uint16, cBuffSize) + iBuff = make([]uint16, iBuffSize) + + if err = _PdhEnumObjectItems( + 0, + 0, + obj, + &cBuff[0], + &cBuffSize, + &iBuff[0], + &iBuffSize, + PerformanceDetailWizard, + 0); err != nil { + return nil, nil, err + } + return cBuff, iBuff, nil + } + return nil, nil, nil +} + +// Error returns a more explicit error message. +func (e PdhErrno) Error() string { + // If the value is not one of the known PDH errors then assume its a + // general windows error. + if _, found := pdhErrors[e]; !found { + return syscall.Errno(e).Error() + } + + // Use FormatMessage to convert the PDH errno to a string. + // Example: https://msdn.microsoft.com/en-us/library/windows/desktop/aa373046(v=vs.85).aspx + var flags uint32 = windows.FORMAT_MESSAGE_FROM_HMODULE | windows.FORMAT_MESSAGE_ARGUMENT_ARRAY | windows.FORMAT_MESSAGE_IGNORE_INSERTS + b := make([]uint16, 300) + n, err := windows.FormatMessage(flags, modpdh.Handle(), uint32(e), 0, b, nil) + if err != nil { + return "pdh error #" + strconv.Itoa(int(e)) + } + + // Trim terminating \r and \n + for ; n > 0 && (b[n-1] == '\n' || b[n-1] == '\r'); n-- { + } + return string(utf16.Decode(b[:n])) +} diff --git a/helpers/windows/pdh/pdh_windows_test.go b/helpers/windows/pdh/pdh_windows_test.go new file mode 100644 index 0000000..1ff28f1 --- /dev/null +++ b/helpers/windows/pdh/pdh_windows_test.go @@ -0,0 +1,130 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package pdh + +import ( + "syscall" + "testing" + + "github.com/stretchr/testify/assert" +) + +var validQuery = `\Processor Information(_Total)\% Processor Time` + +// TestPdhErrno checks that PdhError provides the correct message for known +// PDH errors and also falls back to Windows error messages for non-PDH errors. +func TestPdhErrno_Error(t *testing.T) { + assert.Contains(t, PdhErrno(PDH_CSTATUS_BAD_COUNTERNAME).Error(), "Unable to parse the counter path.") + assert.Contains(t, PdhErrno(15).Error(), "The system cannot find the drive specified.") +} + +// TestPdhOpenQueryInvalidQuery will check for file source and throw exception. +func TestPdhOpenQueryInvalidQuery(t *testing.T) { + handle, err := PdhOpenQuery("invalid string", 0) + assert.EqualValues(t, handle, InvalidQueryHandle) + assert.EqualValues(t, err, PDH_FILE_NOT_FOUND) +} + +// TestPdhAddCounterInvalidCounter checks for invalid query. +func TestPdhAddCounterInvalidCounter(t *testing.T) { + handle, err := PdhAddCounter(InvalidQueryHandle, validQuery, 0) + assert.EqualValues(t, handle, InvalidCounterHandle) + assert.EqualValues(t, err, PDH_INVALID_HANDLE) +} + +// TestPdhGetFormattedCounterValueInvalidCounter will test for invalid counters. +func TestPdhGetFormattedCounterValueInvalidCounter(t *testing.T) { + counterType, counterValue, err := PdhGetFormattedCounterValueDouble(InvalidCounterHandle) + assert.EqualValues(t, counterType, 0) + assert.NotNil(t, counterValue) + assert.Equal(t, counterValue.Value, float64(0)) + assert.EqualValues(t, err, PDH_INVALID_HANDLE) +} + +// TestPdhExpandWildCardPathInvalidPath will test for invalid query path. +func TestPdhExpandWildCardPathInvalidPath(t *testing.T) { + utfPath, err := syscall.UTF16PtrFromString("sdfhsdhfd") + assert.NoError(t, err) + queryList, err := PdhExpandWildCardPath(utfPath) + assert.Nil(t, queryList) + assert.EqualValues(t, err, PDH_INVALID_PATH) +} + +// TestPdhCollectQueryDataInvalidQuery will check for invalid query. +func TestPdhCollectQueryDataInvalidQuery(t *testing.T) { + err := PdhCollectQueryData(InvalidQueryHandle) + assert.EqualValues(t, err, PDH_INVALID_HANDLE) +} + +// TestPdhCloseQueryInvalidQuery will check for invalid query. +func TestPdhCloseQueryInvalidQuery(t *testing.T) { + err := PdhCloseQuery(InvalidQueryHandle) + assert.EqualValues(t, err, PDH_INVALID_HANDLE) +} + +// TestPdhSuccessfulCounterRetrieval will execute the PDH functions successfully. +func TestPdhSuccessfulCounterRetrieval(t *testing.T) { + queryHandle, err := PdhOpenQuery("", 0) + if err != nil { + t.Fatal(err) + } + defer PdhCloseQuery(queryHandle) + utfPath, err := syscall.UTF16PtrFromString(validQuery) + if err != nil { + t.Fatal(err) + } + queryList, err := PdhExpandWildCardPath(utfPath) + if err == PDH_CSTATUS_NO_OBJECT || err == PDH_CSTATUS_NO_COUNTER { + handle, err := PdhAddEnglishCounter(queryHandle, validQuery, 0) + if err != nil { + t.Fatal(err) + } + defer PdhRemoveCounter(handle) + info, err := PdhGetCounterInfo(handle) + if err != nil { + t.Fatal(err) + } + queryList, err = PdhExpandWildCardPath(info.SzFullPath) + if err != nil { + t.Fatal(err) + } + } + queries := UTF16ToStringArray(queryList) + var counters []PdhCounterHandle + for _, query := range queries { + counterHandle, err := PdhAddCounter(queryHandle, query, 0) + if err != nil && err != PDH_NO_MORE_DATA { + t.Fatal(err) + } + counters = append(counters, counterHandle) + } + //Some counters, such as rate counters, require two counter values in order to compute a displayable value. In this case we must call PdhCollectQueryData twice before calling PdhGetFormattedCounterValue. + // For more information, see Collecting Performance Data (https://docs.microsoft.com/en-us/windows/desktop/PerfCtrs/collecting-performance-data). + err = PdhCollectQueryData(queryHandle) + err = PdhCollectQueryData(queryHandle) + if err != nil { + t.Fatal(err) + } + for _, counter := range counters { + counterType, counterValue, err := PdhGetFormattedCounterValueDouble(counter) + assert.NoError(t, err) + assert.NotZero(t, counterType) + assert.NotNil(t, counterValue) + } + +} diff --git a/helpers/windows/pdh/zpdh_windows.go b/helpers/windows/pdh/zpdh_windows.go new file mode 100644 index 0000000..bd64424 --- /dev/null +++ b/helpers/windows/pdh/zpdh_windows.go @@ -0,0 +1,199 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Code generated by 'go generate'; DO NOT EDIT. + +package pdh + +import ( + "syscall" + "unsafe" + + "golang.org/x/sys/windows" +) + +var _ unsafe.Pointer + +// Do the interface allocations only once for common +// Errno values. +const ( + errnoERROR_IO_PENDING = 997 +) + +var ( + errERROR_IO_PENDING error = syscall.Errno(errnoERROR_IO_PENDING) +) + +// errnoErr returns common boxed Errno values, to prevent +// allocations at runtime. +func errnoErr(e syscall.Errno) error { + switch e { + case 0: + return nil + case errnoERROR_IO_PENDING: + return errERROR_IO_PENDING + } + // TODO: add more here, after collecting data on the common + // error values see on Windows. (perhaps when running + // all.bat?) + return e +} + +var ( + modpdh = windows.NewLazySystemDLL("pdh.dll") + procPdhOpenQueryW = modpdh.NewProc("PdhOpenQueryW") + procPdhAddEnglishCounterW = modpdh.NewProc("PdhAddEnglishCounterW") + procPdhAddCounterW = modpdh.NewProc("PdhAddCounterW") + procPdhRemoveCounter = modpdh.NewProc("PdhRemoveCounter") + procPdhCollectQueryData = modpdh.NewProc("PdhCollectQueryData") + procPdhCollectQueryDataEx = modpdh.NewProc("PdhCollectQueryDataEx") + procPdhGetFormattedCounterValue = modpdh.NewProc("PdhGetFormattedCounterValue") + procPdhCloseQuery = modpdh.NewProc("PdhCloseQuery") + procPdhExpandWildCardPathW = modpdh.NewProc("PdhExpandWildCardPathW") + procPdhExpandCounterPathW = modpdh.NewProc("PdhExpandCounterPathW") + procPdhGetCounterInfoW = modpdh.NewProc("PdhGetCounterInfoW") + procPdhEnumObjectItemsW = modpdh.NewProc("PdhEnumObjectItemsW") +) + +func _PdhOpenQuery(dataSource *uint16, userData uintptr, query *PdhQueryHandle) (errcode error) { + r0, _, _ := syscall.Syscall(procPdhOpenQueryW.Addr(), 3, uintptr(unsafe.Pointer(dataSource)), uintptr(userData), uintptr(unsafe.Pointer(query))) + if r0 != 0 { + errcode = syscall.Errno(r0) + } + return +} + +func _PdhAddEnglishCounter(query PdhQueryHandle, counterPath string, userData uintptr, counter *PdhCounterHandle) (errcode error) { + var _p0 *uint16 + _p0, errcode = syscall.UTF16PtrFromString(counterPath) + if errcode != nil { + return + } + return __PdhAddEnglishCounter(query, _p0, userData, counter) +} + +func __PdhAddEnglishCounter(query PdhQueryHandle, counterPath *uint16, userData uintptr, counter *PdhCounterHandle) (errcode error) { + r0, _, _ := syscall.Syscall6(procPdhAddEnglishCounterW.Addr(), 4, uintptr(query), uintptr(unsafe.Pointer(counterPath)), uintptr(userData), uintptr(unsafe.Pointer(counter)), 0, 0) + if r0 != 0 { + errcode = syscall.Errno(r0) + } + return +} + +func _PdhAddCounter(query PdhQueryHandle, counterPath string, userData uintptr, counter *PdhCounterHandle) (errcode error) { + var _p0 *uint16 + _p0, errcode = syscall.UTF16PtrFromString(counterPath) + if errcode != nil { + return + } + return __PdhAddCounter(query, _p0, userData, counter) +} + +func __PdhAddCounter(query PdhQueryHandle, counterPath *uint16, userData uintptr, counter *PdhCounterHandle) (errcode error) { + r0, _, _ := syscall.Syscall6(procPdhAddCounterW.Addr(), 4, uintptr(query), uintptr(unsafe.Pointer(counterPath)), uintptr(userData), uintptr(unsafe.Pointer(counter)), 0, 0) + if r0 != 0 { + errcode = syscall.Errno(r0) + } + return +} + +func _PdhRemoveCounter(counter PdhCounterHandle) (errcode error) { + r0, _, _ := syscall.Syscall(procPdhRemoveCounter.Addr(), 1, uintptr(counter), 0, 0) + if r0 != 0 { + errcode = syscall.Errno(r0) + } + return +} + +func _PdhCollectQueryData(query PdhQueryHandle) (errcode error) { + r0, _, _ := syscall.Syscall(procPdhCollectQueryData.Addr(), 1, uintptr(query), 0, 0) + if r0 != 0 { + errcode = syscall.Errno(r0) + } + return +} +func _PdhCollectQueryDataEx(query PdhQueryHandle, interval uint32, event windows.Handle) (errcode error) { + r0, _, _ := syscall.Syscall(procPdhCollectQueryDataEx.Addr(), 1, uintptr(query), uintptr(interval), uintptr(event)) + if r0 != 0 { + errcode = syscall.Errno(r0) + } + return +} + +func _PdhGetFormattedCounterValueDouble(counter PdhCounterHandle, format PdhCounterFormat, counterType *uint32, value *PdhCounterValueDouble) (errcode error) { + r0, _, _ := syscall.Syscall6(procPdhGetFormattedCounterValue.Addr(), 4, uintptr(counter), uintptr(format), uintptr(unsafe.Pointer(counterType)), uintptr(unsafe.Pointer(value)), 0, 0) + if r0 != 0 { + errcode = syscall.Errno(r0) + } + return +} + +func _PdhGetFormattedCounterValueLarge(counter PdhCounterHandle, format PdhCounterFormat, counterType *uint32, value *PdhCounterValueLarge) (errcode error) { + r0, _, _ := syscall.Syscall6(procPdhGetFormattedCounterValue.Addr(), 4, uintptr(counter), uintptr(format), uintptr(unsafe.Pointer(counterType)), uintptr(unsafe.Pointer(value)), 0, 0) + if r0 != 0 { + errcode = syscall.Errno(r0) + } + return +} + +func _PdhGetFormattedCounterValueLong(counter PdhCounterHandle, format PdhCounterFormat, counterType *uint32, value *PdhCounterValueLong) (errcode error) { + r0, _, _ := syscall.Syscall6(procPdhGetFormattedCounterValue.Addr(), 4, uintptr(counter), uintptr(format), uintptr(unsafe.Pointer(counterType)), uintptr(unsafe.Pointer(value)), 0, 0) + if r0 != 0 { + errcode = syscall.Errno(r0) + } + return +} + +func _PdhCloseQuery(query PdhQueryHandle) (errcode error) { + r0, _, _ := syscall.Syscall(procPdhCloseQuery.Addr(), 1, uintptr(query), 0, 0) + if r0 != 0 { + errcode = syscall.Errno(r0) + } + return +} + +func _PdhExpandWildCardPath(dataSource *uint16, wildcardPath *uint16, expandedPathList *uint16, pathListLength *uint32) (errcode error) { + r0, _, _ := syscall.Syscall6(procPdhExpandWildCardPathW.Addr(), 4, uintptr(unsafe.Pointer(dataSource)), uintptr(unsafe.Pointer(wildcardPath)), uintptr(unsafe.Pointer(expandedPathList)), uintptr(unsafe.Pointer(pathListLength)), 0, 0) + if r0 != 0 { + errcode = syscall.Errno(r0) + } + return +} + +func _PdhExpandCounterPath(wildcardPath *uint16, expandedPathList *uint16, pathListLength *uint32) (errcode error) { + r0, _, _ := syscall.Syscall(procPdhExpandCounterPathW.Addr(), 3, uintptr(unsafe.Pointer(wildcardPath)), uintptr(unsafe.Pointer(expandedPathList)), uintptr(unsafe.Pointer(pathListLength))) + if r0 != 0 { + errcode = syscall.Errno(r0) + } + return +} + +func _PdhGetCounterInfo(counter PdhCounterHandle, text uint16, size *uint32, lpBuffer *byte) (errcode error) { + r0, _, _ := syscall.Syscall6(procPdhGetCounterInfoW.Addr(), 4, uintptr(counter), uintptr(text), uintptr(unsafe.Pointer(size)), uintptr(unsafe.Pointer(lpBuffer)), 0, 0) + if r0 != 0 { + errcode = syscall.Errno(r0) + } + return +} + +func _PdhEnumObjectItems(dataSource uint16, machineName uint16, objectName *uint16, counterList *uint16, counterListSize *uint32, instanceList *uint16, instanceListSize *uint32, detailLevel uint32, flags uint32) (errcode error) { + r0, _, _ := syscall.Syscall9(procPdhEnumObjectItemsW.Addr(), 9, uintptr(dataSource), uintptr(machineName), uintptr(unsafe.Pointer(objectName)), uintptr(unsafe.Pointer(counterList)), uintptr(unsafe.Pointer(counterListSize)), uintptr(unsafe.Pointer(instanceList)), uintptr(unsafe.Pointer(instanceListSize)), uintptr(detailLevel), uintptr(flags)) + if r0 != 0 { + errcode = syscall.Errno(r0) + } + return +} diff --git a/helpers/windows/run.go b/helpers/windows/run.go new file mode 100644 index 0000000..0a44226 --- /dev/null +++ b/helpers/windows/run.go @@ -0,0 +1,79 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//go:build ignore +// +build ignore + +// This is a simple wrapper to set the GOARCH environment variable, execute the +// given command, and output the stdout to a file. It's the unix equivalent of +// GOARCH=arch command > output. + +package main + +import ( + "flag" + "fmt" + "os" + "os/exec" + "strings" +) + +var ( + goarch = flag.String("goarch", "", "GOARCH value") + output = flag.String("output", "", "output file") + cmd = flag.String("cmd", "", "command to run") +) + +func main() { + flag.Parse() + + if err := run(); err != nil { + fmt.Fprintln(os.Stderr, "error:", err) + os.Exit(1) + } +} + +func run() error { + // Run command. + parts := strings.Fields(*cmd) + cmd := exec.Command(parts[0], parts[1:]...) + cmd.Env = os.Environ() + + if *goarch != "" { + cmd.Env = append(cmd.Env, "GOARCH="+*goarch) + } + + if *output != "" { + outputBytes, err := cmd.Output() + if err != nil { + return err + } + + // Write output. + f, err := os.Create(*output) + if err != nil { + return err + } + defer f.Close() + + _, err = f.Write(outputBytes) + return err + } + + cmd.Stdout = cmd.Stdout + return cmd.Run() +}