mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-10 23:16:36 +00:00
chore: release 0.29.0.rc0 (#1600)
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
package kernel32
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
var (
|
||||
kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||
kernel32 = windows.NewLazySystemDLL("kernel32.dll")
|
||||
|
||||
procGetDynamicTimeZoneInformationSys = kernel32.NewProc("GetDynamicTimeZoneInformation")
|
||||
kernelLocalFileTimeToFileTime = kernel32.NewProc("LocalFileTimeToFileTime")
|
||||
)
|
||||
|
||||
// SYSTEMTIME contains a date and time.
|
||||
@@ -50,3 +52,11 @@ func GetDynamicTimeZoneInformation() (DynamicTimezoneInformation, error) {
|
||||
|
||||
return tzi, nil
|
||||
}
|
||||
|
||||
func LocalFileTimeToFileTime(localFileTime, utcFileTime *windows.Filetime) uint32 {
|
||||
ret, _, _ := kernelLocalFileTimeToFileTime.Call(
|
||||
uintptr(unsafe.Pointer(localFileTime)),
|
||||
uintptr(unsafe.Pointer(utcFileTime)))
|
||||
|
||||
return uint32(ret)
|
||||
}
|
||||
|
||||
@@ -76,6 +76,7 @@ func netApiBufferFree(buffer *wKSTAInfo102) {
|
||||
// https://docs.microsoft.com/en-us/windows/win32/api/lmwksta/nf-lmwksta-netwkstagetinfo
|
||||
func netWkstaGetInfo() (wKSTAInfo102, uint32, error) {
|
||||
var lpwi *wKSTAInfo102
|
||||
|
||||
pLevel := uintptr(102)
|
||||
|
||||
r1, _, _ := procNetWkstaGetInfo.Call(0, pLevel, uintptr(unsafe.Pointer(&lpwi)))
|
||||
@@ -86,6 +87,7 @@ func netWkstaGetInfo() (wKSTAInfo102, uint32, error) {
|
||||
}
|
||||
|
||||
deref := *lpwi
|
||||
|
||||
return deref, 0, nil
|
||||
}
|
||||
|
||||
@@ -95,6 +97,7 @@ func GetWorkstationInfo() (WorkstationInfo, error) {
|
||||
if err != nil {
|
||||
return WorkstationInfo{}, err
|
||||
}
|
||||
|
||||
workstationInfo := WorkstationInfo{
|
||||
PlatformId: info.wki102_platform_id,
|
||||
ComputerName: windows.UTF16PtrToString(info.wki102_computername),
|
||||
@@ -104,5 +107,6 @@ func GetWorkstationInfo() (WorkstationInfo, error) {
|
||||
LanRoot: windows.UTF16PtrToString(info.wki102_lanroot),
|
||||
LoggedOnUsers: info.wki102_logged_on_users,
|
||||
}
|
||||
|
||||
return workstationInfo, nil
|
||||
}
|
||||
|
||||
@@ -132,7 +132,9 @@ func GlobalMemoryStatusEx() (MemoryStatus, error) {
|
||||
// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsysteminfo
|
||||
func GetSystemInfo() SystemInfo {
|
||||
var info lpSystemInfo
|
||||
|
||||
procGetSystemInfo.Call(uintptr(unsafe.Pointer(&info))) //nolint:errcheck
|
||||
|
||||
return SystemInfo{
|
||||
Arch: ProcessorArchitecture(info.Arch.WProcessorArchitecture),
|
||||
PageSize: info.DwPageSize,
|
||||
@@ -153,12 +155,16 @@ func GetComputerName(f WinComputerNameFormat) (string, error) {
|
||||
// 1kb buffer to accept computer name. This should be more than enough as the maximum size
|
||||
// returned is the max length of a DNS name, which this author believes is 253 characters.
|
||||
size := 1024
|
||||
|
||||
var buffer [1024]uint16
|
||||
|
||||
r1, _, err := procGetComputerNameExW.Call(uintptr(f), uintptr(unsafe.Pointer(&buffer)), uintptr(unsafe.Pointer(&size)))
|
||||
if r1 == 0 {
|
||||
return "", err
|
||||
}
|
||||
|
||||
bytes := buffer[0:size]
|
||||
out := utf16.Decode(bytes)
|
||||
|
||||
return string(out), nil
|
||||
}
|
||||
|
||||
@@ -2,11 +2,9 @@ package wtsapi32
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"syscall"
|
||||
"log/slog"
|
||||
"unsafe"
|
||||
|
||||
"github.com/go-kit/log"
|
||||
"github.com/go-kit/log/level"
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
@@ -105,30 +103,30 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
func WTSOpenServer(server string) (syscall.Handle, error) {
|
||||
func WTSOpenServer(server string) (windows.Handle, error) {
|
||||
var (
|
||||
err error
|
||||
serverName *uint16
|
||||
)
|
||||
|
||||
if server != "" {
|
||||
serverName, err = syscall.UTF16PtrFromString(server)
|
||||
serverName, err = windows.UTF16PtrFromString(server)
|
||||
if err != nil {
|
||||
return syscall.InvalidHandle, err
|
||||
return windows.InvalidHandle, err
|
||||
}
|
||||
}
|
||||
|
||||
r1, _, err := procWTSOpenServerEx.Call(uintptr(unsafe.Pointer(serverName)))
|
||||
serverHandle := syscall.Handle(r1)
|
||||
serverHandle := windows.Handle(r1)
|
||||
|
||||
if serverHandle == syscall.InvalidHandle {
|
||||
return syscall.InvalidHandle, err
|
||||
if serverHandle == windows.InvalidHandle {
|
||||
return windows.InvalidHandle, err
|
||||
}
|
||||
|
||||
return serverHandle, nil
|
||||
}
|
||||
|
||||
func WTSCloseServer(server syscall.Handle) error {
|
||||
func WTSCloseServer(server windows.Handle) error {
|
||||
r1, _, err := procWTSCloseServer.Call(uintptr(server))
|
||||
|
||||
if r1 != 1 {
|
||||
@@ -152,8 +150,9 @@ func WTSFreeMemoryEx(class WTSTypeClass, pMemory uintptr, numberOfEntries uint32
|
||||
return nil
|
||||
}
|
||||
|
||||
func WTSEnumerateSessionsEx(server syscall.Handle, logger log.Logger) ([]WTSSession, error) {
|
||||
func WTSEnumerateSessionsEx(server windows.Handle, logger *slog.Logger) ([]WTSSession, error) {
|
||||
var sessionInfoPointer uintptr
|
||||
|
||||
var count uint32
|
||||
|
||||
pLevel := uint32(1)
|
||||
@@ -173,7 +172,7 @@ func WTSEnumerateSessionsEx(server syscall.Handle, logger log.Logger) ([]WTSSess
|
||||
defer func(class WTSTypeClass, pMemory uintptr, NumberOfEntries uint32) {
|
||||
err := WTSFreeMemoryEx(class, pMemory, NumberOfEntries)
|
||||
if err != nil {
|
||||
_ = level.Error(logger).Log("msg", "failed to free memory", "err", fmt.Errorf("WTSEnumerateSessionsEx: %w", err))
|
||||
logger.Warn("failed to free memory", "err", fmt.Errorf("WTSEnumerateSessionsEx: %w", err))
|
||||
}
|
||||
}(WTSTypeSessionInfoLevel1, sessionInfoPointer, count)
|
||||
}
|
||||
@@ -182,6 +181,7 @@ func WTSEnumerateSessionsEx(server syscall.Handle, logger log.Logger) ([]WTSSess
|
||||
sessionSize := unsafe.Sizeof(sizeTest)
|
||||
|
||||
sessions := make([]WTSSession, 0, count)
|
||||
|
||||
for i := range count {
|
||||
curPtr := unsafe.Pointer(sessionInfoPointer + (uintptr(i) * sessionSize))
|
||||
data := (*wtsSessionInfo1)(curPtr)
|
||||
|
||||
Reference in New Issue
Block a user