Skip to content

Commit

Permalink
[windows] handle nil strings in WMI output
Browse files Browse the repository at this point in the history
The github.com/StackExchange/wmi package is great, but handles nil
results from the WMI/WQL call *only* if the type in the receiving struct
is a pointer type:

https://github.com/StackExchange/wmi/blob/cbe66965904dbe8a6cd589e2298e5d8b986bd7dd/wmi.go#L420-L426

So, for various fields (all strings that I can gather so far) that on
*some* systems return nil type from WMI, I've made the fields in the
receiver structs *string instead of string. This should allow nil-types
to be properly handled and not run into the `unsupported type (<nil>)`
issue faced in Issue #189

Closes Issue #189
  • Loading branch information
jaypipes committed May 21, 2020
1 parent 211284c commit 591c46d
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 110 deletions.
16 changes: 8 additions & 8 deletions baseboard_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import "github.com/StackExchange/wmi"
const wqlBaseboard = "SELECT Manufacturer, SerialNumber, Tag, Version FROM Win32_BaseBoard"

type win32Baseboard struct {
Manufacturer string
SerialNumber string
Tag string
Version string
Manufacturer *string
SerialNumber *string
Tag *string
Version *string
}

func (ctx *context) baseboardFillInfo(info *BaseboardInfo) error {
Expand All @@ -23,10 +23,10 @@ func (ctx *context) baseboardFillInfo(info *BaseboardInfo) error {
return err
}
if len(win32BaseboardDescriptions) > 0 {
info.AssetTag = win32BaseboardDescriptions[0].Tag
info.SerialNumber = win32BaseboardDescriptions[0].SerialNumber
info.Vendor = win32BaseboardDescriptions[0].Manufacturer
info.Version = win32BaseboardDescriptions[0].Version
info.AssetTag = *win32BaseboardDescriptions[0].Tag
info.SerialNumber = *win32BaseboardDescriptions[0].SerialNumber
info.Vendor = *win32BaseboardDescriptions[0].Manufacturer
info.Version = *win32BaseboardDescriptions[0].Version
}

return nil
Expand Down
12 changes: 6 additions & 6 deletions bios_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import "github.com/StackExchange/wmi"
const wqlBIOS = "SELECT InstallDate, Manufacturer, Version FROM CIM_BIOSElement"

type win32BIOS struct {
InstallDate string
Manufacturer string
Version string
InstallDate *string
Manufacturer *string
Version *string
}

func (ctx *context) biosFillInfo(info *BIOSInfo) error {
Expand All @@ -22,9 +22,9 @@ func (ctx *context) biosFillInfo(info *BIOSInfo) error {
return err
}
if len(win32BIOSDescriptions) > 0 {
info.Vendor = win32BIOSDescriptions[0].Manufacturer
info.Version = win32BIOSDescriptions[0].Version
info.Date = win32BIOSDescriptions[0].InstallDate
info.Vendor = *win32BIOSDescriptions[0].Manufacturer
info.Version = *win32BIOSDescriptions[0].Version
info.Date = *win32BIOSDescriptions[0].InstallDate
}
return nil
}
80 changes: 40 additions & 40 deletions block_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ import (
const wqlDiskDrive = "SELECT Caption, CreationClassName, DefaultBlockSize, Description, DeviceID, Index, InterfaceType, Manufacturer, MediaType, Model, Name, Partitions, SerialNumber, Size, TotalCylinders, TotalHeads, TotalSectors, TotalTracks, TracksPerCylinder FROM Win32_DiskDrive"

type win32DiskDrive struct {
Caption string
CreationClassName string
Caption *string
CreationClassName *string
DefaultBlockSize uint64
Description string
DeviceID string
Description *string
DeviceID *string
Index uint32 // Used to link with partition
InterfaceType string
Manufacturer string
MediaType string
Model string
Name string
InterfaceType *string
Manufacturer *string
MediaType *string
Model *string
Name *string
Partitions int32
SerialNumber string
SerialNumber *string
Size uint64
TotalCylinders int64
TotalHeads int32
Expand All @@ -40,37 +40,37 @@ const wqlDiskPartition = "SELECT Access, BlockSize, Caption, CreationClassName,
type win32DiskPartition struct {
Access uint16
BlockSize uint64
Caption string
CreationClassName string
Description string
DeviceID string
Caption *string
CreationClassName *string
Description *string
DeviceID *string
DiskIndex uint32 // Used to link with Disk Drive
Index uint32
Name string
Name *string
Size int64
SystemName string
Type string
SystemName *string
Type *string
}

const wqlLogicalDiskToPartition = "SELECT Antecedent, Dependent FROM Win32_LogicalDiskToPartition"

type win32LogicalDiskToPartition struct {
Antecedent string
Dependent string
Antecedent *string
Dependent *string
}

const wqlLogicalDisk = "SELECT Caption, CreationClassName, Description, DeviceID, FileSystem, FreeSpace, Name, Size, SystemName FROM Win32_LogicalDisk"

type win32LogicalDisk struct {
Caption string
CreationClassName string
Description string
DeviceID string
FileSystem string
Caption *string
CreationClassName *string
Description *string
DeviceID *string
FileSystem *string
FreeSpace uint64
Name string
Name *string
Size uint64
SystemName string
SystemName *string
}

func (ctx *context) blockFillInfo(info *BlockInfo) error {
Expand Down Expand Up @@ -98,17 +98,17 @@ func (ctx *context) blockFillInfo(info *BlockInfo) error {
disks := make([]*Disk, 0)
for _, diskdrive := range win32DiskDriveDescriptions {
disk := &Disk{
Name: strings.TrimSpace(diskdrive.DeviceID),
Name: strings.TrimSpace(*diskdrive.DeviceID),
SizeBytes: diskdrive.Size,
PhysicalBlockSizeBytes: diskdrive.DefaultBlockSize,
DriveType: toDriveType(diskdrive.MediaType, diskdrive.Caption),
StorageController: toStorageController(diskdrive.InterfaceType),
BusType: toBusType(diskdrive.InterfaceType),
DriveType: toDriveType(*diskdrive.MediaType, *diskdrive.Caption),
StorageController: toStorageController(*diskdrive.InterfaceType),
BusType: toBusType(*diskdrive.InterfaceType),
BusPath: UNKNOWN, // TODO: add information
NUMANodeID: -1,
Vendor: strings.TrimSpace(diskdrive.Manufacturer),
Model: strings.TrimSpace(diskdrive.Caption),
SerialNumber: strings.TrimSpace(diskdrive.SerialNumber),
Vendor: strings.TrimSpace(*diskdrive.Manufacturer),
Model: strings.TrimSpace(*diskdrive.Caption),
SerialNumber: strings.TrimSpace(*diskdrive.SerialNumber),
WWN: UNKNOWN, // TODO: add information
Partitions: make([]*Partition, 0),
}
Expand All @@ -119,16 +119,16 @@ func (ctx *context) blockFillInfo(info *BlockInfo) error {
// Finding logical partition linked to current disk partition
for _, logicaldisk := range win32LogicalDiskDescriptions {
for _, logicaldisktodiskpartition := range win32LogicalDiskToPartitionDescriptions {
var desiredAntecedent = "\\\\" + diskpartition.SystemName + "\\root\\cimv2:" + diskpartition.CreationClassName + ".DeviceID=\"" + diskpartition.DeviceID + "\""
var desiredDependent = "\\\\" + logicaldisk.SystemName + "\\root\\cimv2:" + logicaldisk.CreationClassName + ".DeviceID=\"" + logicaldisk.DeviceID + "\""
if logicaldisktodiskpartition.Antecedent == desiredAntecedent && logicaldisktodiskpartition.Dependent == desiredDependent {
var desiredAntecedent = "\\\\" + *diskpartition.SystemName + "\\root\\cimv2:" + *diskpartition.CreationClassName + ".DeviceID=\"" + *diskpartition.DeviceID + "\""
var desiredDependent = "\\\\" + *logicaldisk.SystemName + "\\root\\cimv2:" + *logicaldisk.CreationClassName + ".DeviceID=\"" + *logicaldisk.DeviceID + "\""
if *logicaldisktodiskpartition.Antecedent == desiredAntecedent && *logicaldisktodiskpartition.Dependent == desiredDependent {
// Appending Partition
p := &Partition{
Name: strings.TrimSpace(logicaldisk.Caption),
Label: strings.TrimSpace(logicaldisk.Caption),
Name: strings.TrimSpace(*logicaldisk.Caption),
Label: strings.TrimSpace(*logicaldisk.Caption),
SizeBytes: logicaldisk.Size,
MountPoint: logicaldisk.DeviceID,
Type: diskpartition.Type,
MountPoint: *logicaldisk.DeviceID,
Type: *diskpartition.Type,
IsReadOnly: toReadOnly(diskpartition.Access),
}
disk.Partitions = append(disk.Partitions, p)
Expand Down
26 changes: 13 additions & 13 deletions chassis_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
const wqlChassis = "SELECT Caption, Description, Name, Manufacturer, Model, SerialNumber, Tag, TypeDescriptions, Version FROM CIM_Chassis"

type win32Chassis struct {
Caption string
Description string
Name string
Manufacturer string
Model string
SerialNumber string
Tag string
Caption *string
Description *string
Name *string
Manufacturer *string
Model *string
SerialNumber *string
Tag *string
TypeDescriptions []string
Version string
Version *string
}

func (ctx *context) chassisFillInfo(info *ChassisInfo) error {
Expand All @@ -30,12 +30,12 @@ func (ctx *context) chassisFillInfo(info *ChassisInfo) error {
return err
}
if len(win32ChassisDescriptions) > 0 {
info.AssetTag = win32ChassisDescriptions[0].Tag
info.SerialNumber = win32ChassisDescriptions[0].SerialNumber
info.AssetTag = *win32ChassisDescriptions[0].Tag
info.SerialNumber = *win32ChassisDescriptions[0].SerialNumber
info.Type = UNKNOWN // TODO:
info.TypeDescription = win32ChassisDescriptions[0].Model
info.Vendor = win32ChassisDescriptions[0].Manufacturer
info.Version = win32ChassisDescriptions[0].Version
info.TypeDescription = *win32ChassisDescriptions[0].Model
info.Vendor = *win32ChassisDescriptions[0].Manufacturer
info.Version = *win32ChassisDescriptions[0].Version
}
return nil
}
8 changes: 4 additions & 4 deletions cpu_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
const wmqlProcessor = "SELECT Manufacturer, Name, NumberOfLogicalProcessors, NumberOfCores FROM Win32_Processor"

type win32Processor struct {
Manufacturer string
Name string
Manufacturer *string
Name *string
NumberOfLogicalProcessors uint32
NumberOfCores uint32
}
Expand Down Expand Up @@ -44,8 +44,8 @@ func (ctx *context) processorsGet(win32descriptions []win32Processor) []*Process
for index, description := range win32descriptions {
p := &Processor{
Id: index, // TODO: how to get a decent "Physical ID" to use ?
Model: description.Name,
Vendor: description.Manufacturer,
Model: *description.Name,
Vendor: *description.Manufacturer,
NumCores: description.NumberOfCores,
NumThreads: description.NumberOfLogicalProcessors,
}
Expand Down
26 changes: 13 additions & 13 deletions memory_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ type win32OperatingSystem struct {
const wqlPhysicalMemory = "SELECT BankLabel, Capacity, DataWidth, Description, DeviceLocator, Manufacturer, Model, Name, PartNumber, PositionInRow, SerialNumber, Speed, Tag, TotalWidth FROM Win32_PhysicalMemory"

type win32PhysicalMemory struct {
BankLabel string
BankLabel *string
Capacity uint64
DataWidth uint16
Description string
DeviceLocator string
Manufacturer string
Model string
Name string
PartNumber string
Description *string
DeviceLocator *string
Manufacturer *string
Model *string
Name *string
PartNumber *string
PositionInRow uint32
SerialNumber string
SerialNumber *string
Speed uint32
Tag string
Tag *string
TotalWidth uint16
}

Expand All @@ -51,11 +51,11 @@ func (ctx *context) memFillInfo(info *MemoryInfo) error {
for _, description := range win32MemDescriptions {
totalPhysicalBytes += description.Capacity
info.Modules = append(info.Modules, &MemoryModule{
Label: description.BankLabel,
Location: description.DeviceLocator,
SerialNumber: description.SerialNumber,
Label: *description.BankLabel,
Location: *description.DeviceLocator,
SerialNumber: *description.SerialNumber,
SizeBytes: int64(description.Capacity),
Vendor: description.Manufacturer,
Vendor: *description.Manufacturer,
})
}
var totalUsableBytes uint64
Expand Down
24 changes: 12 additions & 12 deletions net_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ import (
const wqlNetworkAdapter = "SELECT Description, DeviceID, Index, InterfaceIndex, MACAddress, Manufacturer, Name, NetConnectionID, ProductName, ServiceName FROM Win32_NetworkAdapter"

type win32NetworkAdapter struct {
Description string
DeviceID string
Description *string
DeviceID *string
Index uint32
InterfaceIndex uint32
MACAddress string
Manufacturer string
Name string
NetConnectionID string
ProductName string
ServiceName string
MACAddress *string
Manufacturer *string
Name *string
NetConnectionID *string
ProductName *string
ServiceName *string
}

func (ctx *context) netFillInfo(info *NetworkInfo) error {
Expand All @@ -43,7 +43,7 @@ func (ctx *context) nics(win32NetDescriptions []win32NetworkAdapter) []*NIC {
for _, nicDescription := range win32NetDescriptions {
nic := &NIC{
Name: ctx.netDeviceName(nicDescription),
MacAddress: nicDescription.MACAddress,
MacAddress: *nicDescription.MACAddress,
IsVirtual: false,
Capabilities: []*NICCapability{},
}
Expand All @@ -56,10 +56,10 @@ func (ctx *context) nics(win32NetDescriptions []win32NetworkAdapter) []*NIC {

func (ctx *context) netDeviceName(description win32NetworkAdapter) string {
var name string
if strings.TrimSpace(description.NetConnectionID) != "" {
name = description.NetConnectionID + " - " + description.Description
if strings.TrimSpace(*description.NetConnectionID) != "" {
name = *description.NetConnectionID + " - " + *description.Description
} else {
name = description.Description
name = *description.Description
}
return name
}
28 changes: 14 additions & 14 deletions product_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
const wqlProduct = "SELECT Caption, Description, IdentifyingNumber, Name, SKUNumber, Vendor, Version, UUID FROM Win32_ComputerSystemProduct"

type win32Product struct {
Caption string
Description string
IdentifyingNumber string
Name string
SKUNumber string
Vendor string
Version string
UUID string
Caption *string
Description *string
IdentifyingNumber *string
Name *string
SKUNumber *string
Vendor *string
Version *string
UUID *string
}

func (ctx *context) productFillInfo(info *ProductInfo) error {
Expand All @@ -31,12 +31,12 @@ func (ctx *context) productFillInfo(info *ProductInfo) error {
}
if len(win32ProductDescriptions) > 0 {
info.Family = UNKNOWN
info.Name = win32ProductDescriptions[0].Name
info.Vendor = win32ProductDescriptions[0].Vendor
info.SerialNumber = win32ProductDescriptions[0].IdentifyingNumber
info.UUID = win32ProductDescriptions[0].UUID
info.SKU = win32ProductDescriptions[0].SKUNumber
info.Version = win32ProductDescriptions[0].Version
info.Name = *win32ProductDescriptions[0].Name
info.Vendor = *win32ProductDescriptions[0].Vendor
info.SerialNumber = *win32ProductDescriptions[0].IdentifyingNumber
info.UUID = *win32ProductDescriptions[0].UUID
info.SKU = *win32ProductDescriptions[0].SKUNumber
info.Version = *win32ProductDescriptions[0].Version
}

return nil
Expand Down

0 comments on commit 591c46d

Please sign in to comment.