From 78f4266ba46a4419249308bf98f1d8dfe9f9e860 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Sat, 16 Nov 2024 21:53:18 +0100 Subject: [PATCH] mssql: expose server version info MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- docs/collector.mssql.md | 2 +- go.mod | 2 +- internal/collector/mssql/mssql.go | 2 +- internal/headers/iphlpapi/iphlpapi_test.go | 2 + internal/headers/iphlpapi/types.go | 5 +++ test/main.go | 52 ---------------------- 6 files changed, 10 insertions(+), 55 deletions(-) delete mode 100644 test/main.go diff --git a/docs/collector.mssql.md b/docs/collector.mssql.md index d77d61e42..61fc47b32 100644 --- a/docs/collector.mssql.md +++ b/docs/collector.mssql.md @@ -26,7 +26,7 @@ Port of MSSQL server used for `windows_mssql_info` metric. Default is `1433`. | Name | Description | Type | Labels | |--------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|-------------------------------| -| `windows_mssql_info` | Returns infomation about the MSSQL server running on port 1433 | gauge | `version` | +| `windows_mssql_info` | Returns information about the MSSQL server running on port 1433 | gauge | `version` | | `windows_mssql_collector_duration_seconds` | The time taken for each sub-collector to return | gauge | `collector`, `mssql_instance` | | `windows_mssql_collector_success` | 1 if sub-collector succeeded, 0 otherwise | gauge | `collector`, `mssql_instance` | | `windows_mssql_accessmethods_au_batch_cleanups` | The total number of batches that were completed successfully by the background task that cleans up deferred dropped allocation units | counter | `mssql_instance` | diff --git a/go.mod b/go.mod index 46f82c16e..f6c7c7398 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/prometheus-community/windows_exporter go 1.23 require ( + github.com/Microsoft/go-winio v0.6.2 github.com/Microsoft/hcsshim v0.12.9 github.com/alecthomas/kingpin/v2 v2.4.0 github.com/bmatcuk/doublestar/v4 v4.7.1 @@ -19,7 +20,6 @@ require ( ) require ( - github.com/Microsoft/go-winio v0.6.2 // indirect github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect diff --git a/internal/collector/mssql/mssql.go b/internal/collector/mssql/mssql.go index f8d44f738..51b2bf359 100644 --- a/internal/collector/mssql/mssql.go +++ b/internal/collector/mssql/mssql.go @@ -430,7 +430,7 @@ func (c *Collector) getMSSQLServerVersion(port uint16) (string, string, error) { return "", "", fmt.Errorf("failed to open the process with PID %d: %w", pid, err) } - defer windows.CloseHandle(hProcess) + defer windows.CloseHandle(hProcess) //nolint:errcheck processFilePath, err := process.QueryFullProcessImageName(hProcess, process.ImageNameFormatWin32Path) if err != nil { diff --git a/internal/headers/iphlpapi/iphlpapi_test.go b/internal/headers/iphlpapi/iphlpapi_test.go index 6ad1c48fb..5867a0d36 100644 --- a/internal/headers/iphlpapi/iphlpapi_test.go +++ b/internal/headers/iphlpapi/iphlpapi_test.go @@ -22,6 +22,8 @@ func TestGetOwnerPIDOfTCPPort(t *testing.T) { t.Parallel() lister, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, lister.Close()) }) diff --git a/internal/headers/iphlpapi/types.go b/internal/headers/iphlpapi/types.go index 448e00335..d3931cb90 100644 --- a/internal/headers/iphlpapi/types.go +++ b/internal/headers/iphlpapi/types.go @@ -9,6 +9,8 @@ import ( // MIB_TCPROW_OWNER_PID structure for IPv4. // https://learn.microsoft.com/en-us/windows/win32/api/tcpmib/ns-tcpmib-mib_tcprow_owner_pid +// +//nolint:unused type MIB_TCPROW_OWNER_PID struct { dwState MIB_TCP_STATE dwLocalAddr BigEndianUint32 @@ -20,6 +22,8 @@ type MIB_TCPROW_OWNER_PID struct { // MIB_TCP6ROW_OWNER_PID structure for IPv6. // https://learn.microsoft.com/en-us/windows/win32/api/tcpmib/ns-tcpmib-mib_tcp6row_owner_pid +// +//nolint:unused type MIB_TCP6ROW_OWNER_PID struct { ucLocalAddr [16]byte dwLocalScopeId uint32 @@ -85,5 +89,6 @@ type BigEndianUint32 uint32 func (b BigEndianUint32) uint16() uint16 { data := make([]byte, 2) binary.BigEndian.PutUint16(data, uint16(b)) + return binary.LittleEndian.Uint16(data) } diff --git a/test/main.go b/test/main.go deleted file mode 100644 index 10e27d326..000000000 --- a/test/main.go +++ /dev/null @@ -1,52 +0,0 @@ -package main - -import ( - "fmt" - "syscall" - "unsafe" - - "github.com/Microsoft/go-winio/pkg/process" - "github.com/prometheus-community/windows_exporter/internal/headers/iphlpapi" - "golang.org/x/sys/windows" -) - -func main() { - pid, err := iphlpapi.GetOwnerPIDOfTCPPort(windows.AF_INET, 1433) - if err != nil { - panic(err) - } - - hProcess, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, pid) - if err != nil { - panic(err) - } - - cmdLine, err := process.QueryFullProcessImageName(hProcess, process.ImageNameFormatWin32Path) - if err != nil { - panic(err) - } - fmt.Println(cmdLine) - - // Load the file version information - size, err := windows.GetFileVersionInfoSize(cmdLine, nil) - if err != nil { - panic(err) - } - - data := make([]byte, size) - err = windows.GetFileVersionInfo(cmdLine, 0, uint32(len(data)), unsafe.Pointer(&data[0])) - if err != nil { - panic(err) - } - - var verSize uint32 - var verData *byte - - err = windows.VerQueryValue(unsafe.Pointer(&data[0]), `\StringFileInfo\040904b0\ProductVersion`, unsafe.Pointer(&verData), &verSize) - if err != nil { - panic(err) - } - - version := syscall.UTF16ToString((*[1 << 16]uint16)(unsafe.Pointer(verData))[:verSize]) - fmt.Println(version) -}