Feature: Collect additional hardware audit information

When devices enrol the client collects processor, memory, disk and network information which is persisted in the database
This commit is contained in:
Gary Sharp
2020-11-29 16:43:44 +11:00
parent 28e5901929
commit f1d27732c7
9 changed files with 409 additions and 82 deletions
+273 -35
View File
@@ -1,6 +1,7 @@
using Disco.Models.ClientServices;
using Disco.Models.ClientServices.EnrolmentInformation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
@@ -40,14 +41,200 @@ namespace Disco.Client.Interop
throw new Exception($"The serial number reported by this device is over 60 characters long:\r\n{audit.SerialNumber}");
}
#warning TODO: Processors, PhysicalMemory, DiskDrives, etc
audit.ApplyProcessorInformation();
audit.ApplyPhysicalMemoryInformation();
audit.ApplyDiskDriveInformation();
audit.NetworkAdapters = Network.GetNetworkAdapters();
return audit;
}
private static void ApplyBIOSInformation(this DeviceHardware DeviceHardware)
private static void ApplyProcessorInformation(this DeviceHardware deviceHardware)
{
try
{
using (var mSearcher = new ManagementObjectSearcher("SELECT DeviceID, Manufacturer, Name, Description, Architecture, Family, MaxClockSpeed, NumberOfCores, NumberOfLogicalProcessors FROM Win32_Processor"))
{
using (var mResults = mSearcher.Get())
{
if (mResults.Count > 0)
{
var processors = new List<Processor>(mResults.Count);
foreach (var mItem in mResults.Cast<ManagementObject>())
{
if (mItem != null)
{
var processor = new Processor();
processor.DeviceID = (string)mItem.GetPropertyValue("DeviceID");
processor.Manufacturer = (string)mItem.GetPropertyValue("Manufacturer");
processor.Name = (string)mItem.GetPropertyValue("Name");
processor.Description = (string)mItem.GetPropertyValue("Description");
processor.Family = (ushort)mItem.GetPropertyValue("Family");
processor.MaxClockSpeed = (uint)mItem.GetPropertyValue("MaxClockSpeed");
processor.NumberOfCores = (uint)mItem.GetPropertyValue("NumberOfCores");
processor.NumberOfLogicalProcessors = (uint)mItem.GetPropertyValue("NumberOfLogicalProcessors");
var architectureCode = (ushort)mItem.GetPropertyValue("Architecture");
processor.Architecture = Enum.GetName(typeof(ProcessorArchitectures), architectureCode) ?? $"Unknown {architectureCode}";
processors.Add(processor);
}
}
deviceHardware.Processors = processors;
}
}
}
}
catch (Exception)
{
// ignore errors to ensure backwards compatibility
}
}
private static void ApplyPhysicalMemoryInformation(this DeviceHardware deviceHardware)
{
try
{
using (var mSearcher = new ManagementObjectSearcher("SELECT Tag, SerialNumber, Manufacturer, PartNumber, Capacity, ConfiguredClockSpeed, Speed, DeviceLocator FROM Win32_PhysicalMemory"))
{
using (var mResults = mSearcher.Get())
{
if (mResults.Count > 0)
{
var physicalMemories = new List<PhysicalMemory>(mResults.Count);
foreach (var mItem in mResults.Cast<ManagementObject>())
{
if (mItem != null)
{
var physicalMemory = new PhysicalMemory();
physicalMemory.Tag = (string)mItem.GetPropertyValue("Tag");
physicalMemory.SerialNumber = (string)mItem.GetPropertyValue("SerialNumber");
physicalMemory.Manufacturer = (string)mItem.GetPropertyValue("Manufacturer");
physicalMemory.PartNumber = (string)mItem.GetPropertyValue("PartNumber");
physicalMemory.Capacity = (ulong)mItem.GetPropertyValue("Capacity");
physicalMemory.ConfiguredClockSpeed = (uint)mItem.GetPropertyValue("ConfiguredClockSpeed");
physicalMemory.Speed = (uint)mItem.GetPropertyValue("Speed");
physicalMemory.DeviceLocator = (string)mItem.GetPropertyValue("DeviceLocator");
physicalMemories.Add(physicalMemory);
}
}
deviceHardware.PhysicalMemory = physicalMemories;
}
}
}
}
catch (Exception)
{
// ignore errors to ensure backwards compatibility
}
}
private static void ApplyDiskDriveInformation(this DeviceHardware deviceHardware)
{
try
{
using (var diskSearcher = new ManagementObjectSearcher("SELECT DeviceID, Manufacturer, Model, MediaType, InterfaceType, SerialNumber, FirmwareRevision, Size FROM Win32_DiskDrive"))
{
using (var diskResults = diskSearcher.Get())
{
if (diskResults.Count > 0)
{
var diskDrives = new List<DiskDrive>(diskResults.Count);
foreach (var diskResult in diskResults.Cast<ManagementObject>())
{
if (diskResult != null)
{
var diskDrive = new DiskDrive();
diskDrive.DeviceID = (string)diskResult.GetPropertyValue("DeviceID");
diskDrive.Manufacturer = (string)diskResult.GetPropertyValue("Manufacturer");
diskDrive.Model = (string)diskResult.GetPropertyValue("Model");
diskDrive.MediaType = (string)diskResult.GetPropertyValue("MediaType");
diskDrive.InterfaceType = (string)diskResult.GetPropertyValue("InterfaceType");
diskDrive.SerialNumber = (string)diskResult.GetPropertyValue("SerialNumber");
diskDrive.FirmwareRevision = (string)diskResult.GetPropertyValue("FirmwareRevision");
diskDrive.Size = (ulong)diskResult.GetPropertyValue("Size");
using (var partitionSearcher = new ManagementObjectSearcher($"ASSOCIATORS OF {{Win32_DiskDrive.DeviceID=\"{diskDrive.DeviceID.Replace(@"\", @"\\")}\"}} WHERE AssocClass = Win32_DiskDriveToDiskPartition"))
{
using (var partitionResults = partitionSearcher.Get())
{
if (partitionResults.Count > 0)
{
var partitions = new List<DiskDrivePartition>(partitionResults.Count);
foreach (var partitionResult in partitionResults.Cast<ManagementObject>())
{
if (partitionResult != null)
{
var partition = new DiskDrivePartition();
partition.DeviceID = (string)partitionResult.GetPropertyValue("DeviceID");
partition.Bootable = (bool)partitionResult.GetPropertyValue("Bootable");
partition.BootPartition = (bool)partitionResult.GetPropertyValue("BootPartition");
partition.PrimaryParition = (bool)partitionResult.GetPropertyValue("PrimaryPartition");
partition.Size = (ulong)partitionResult.GetPropertyValue("Size");
partition.StartingOffset = (ulong)partitionResult.GetPropertyValue("StartingOffset");
partition.Type = (string)partitionResult.GetPropertyValue("Type");
using (var logicalSearcher = new ManagementObjectSearcher($"ASSOCIATORS OF {{Win32_DiskPartition.DeviceID=\"{partition.DeviceID}\"}} WHERE AssocClass = Win32_LogicalDiskToPartition"))
{
using (var logicalResults = logicalSearcher.Get())
{
if (logicalResults.Count > 0)
{
foreach (var logicalResult in logicalResults.Cast<ManagementObject>().Take(1))
{
if (logicalResult != null)
{
var logical = new DiskLogical();
logical.DeviceID = (string)logicalResult.GetPropertyValue("DeviceID");
logical.Description = (string)logicalResult.GetPropertyValue("Description");
var driveType = (uint)logicalResult.GetPropertyValue("DriveType");
logical.DriveType = Enum.GetName(typeof(DiskLogicalDriveTypes), driveType) ?? $"Unknown {driveType}";
var mediaType = (uint)logicalResult.GetPropertyValue("MediaType");
logical.MediaType = Enum.GetName(typeof(DiskLogicalMediaTypes), mediaType) ?? $"Unknown {mediaType}";
logical.FileSystem = (string)logicalResult.GetPropertyValue("FileSystem");
logical.Size = (ulong)logicalResult.GetPropertyValue("Size");
logical.FreeSpace = (ulong)logicalResult.GetPropertyValue("FreeSpace");
logical.VolumeName = (string)logicalResult.GetPropertyValue("VolumeName");
logical.VolumeSerialNumber = (string)logicalResult.GetPropertyValue("VolumeSerialNumber");
partition.LogicalDisk = logical;
}
}
}
}
}
partitions.Add(partition);
}
}
diskDrive.Partitions = partitions;
}
}
}
diskDrives.Add(diskDrive);
}
}
deviceHardware.DiskDrives = diskDrives;
}
}
}
}
catch (Exception)
{
// ignore errors to ensure backwards compatibility
}
}
private static void ApplyBIOSInformation(this DeviceHardware deviceHardware)
{
try
{
@@ -62,16 +249,16 @@ namespace Disco.Client.Interop
var serialNumber = (string)mItem.GetPropertyValue("SerialNumber");
if (!string.IsNullOrWhiteSpace(serialNumber))
{
DeviceHardware.SerialNumber = serialNumber.Trim();
deviceHardware.SerialNumber = serialNumber.Trim();
}
var manufacturer = (string)mItem.GetPropertyValue("Manufacturer");
if (DeviceHardware.Manufacturer == null && !string.IsNullOrWhiteSpace(manufacturer))
if (deviceHardware.Manufacturer == null && !string.IsNullOrWhiteSpace(manufacturer))
{
DeviceHardware.Manufacturer = manufacturer.Trim();
deviceHardware.Manufacturer = manufacturer.Trim();
}
ErrorReporting.DeviceIdentifier = DeviceHardware.SerialNumber;
ErrorReporting.DeviceIdentifier = deviceHardware.SerialNumber;
}
else
{
@@ -87,7 +274,7 @@ namespace Disco.Client.Interop
}
}
private static void ApplySystemInformation(this DeviceHardware DeviceHardware)
private static void ApplySystemInformation(this DeviceHardware deviceHardware)
{
try
{
@@ -102,16 +289,16 @@ namespace Disco.Client.Interop
var manufacturer = (string)mItem.GetPropertyValue("Manufacturer");
if (!string.IsNullOrWhiteSpace(manufacturer))
{
DeviceHardware.Manufacturer = manufacturer.Trim();
deviceHardware.Manufacturer = manufacturer.Trim();
}
var model = (string)mItem.GetPropertyValue("Model");
if (!string.IsNullOrWhiteSpace(model))
{
DeviceHardware.Model = model.ToString();
deviceHardware.Model = model.ToString();
}
DeviceHardware.ModelType = ((PCSystemTypes)mItem.GetPropertyValue("PCSystemType")).Description();
deviceHardware.ModelType = ((PCSystemTypes)mItem.GetPropertyValue("PCSystemType")).Description();
}
else
{
@@ -127,7 +314,7 @@ namespace Disco.Client.Interop
}
}
public static void ApplySystemInformation(this Enrol Enrol)
public static void ApplySystemInformation(this Enrol enrol)
{
try
{
@@ -139,11 +326,11 @@ namespace Disco.Client.Interop
{
if (mItem != null)
{
Enrol.IsPartOfDomain = (bool)mItem.GetPropertyValue("PartOfDomain");
enrol.IsPartOfDomain = (bool)mItem.GetPropertyValue("PartOfDomain");
if (Enrol.IsPartOfDomain)
if (enrol.IsPartOfDomain)
{
Enrol.DNSDomainName = (string)mItem.GetPropertyValue("Domain");
enrol.DNSDomainName = (string)mItem.GetPropertyValue("Domain");
}
}
else
@@ -160,7 +347,7 @@ namespace Disco.Client.Interop
}
}
private static void ApplyBaseBoardInformation(this DeviceHardware DeviceHardware)
private static void ApplyBaseBoardInformation(this DeviceHardware deviceHardware)
{
try
{
@@ -174,34 +361,34 @@ namespace Disco.Client.Interop
{
// Apply Manufacturer/Model information only if not previously found in Win32_ComputerSystem
var manufacturer = (string)mItem.GetPropertyValue("Manufacturer");
if (DeviceHardware.Manufacturer == null && !string.IsNullOrWhiteSpace(manufacturer))
if (deviceHardware.Manufacturer == null && !string.IsNullOrWhiteSpace(manufacturer))
{
DeviceHardware.Manufacturer = manufacturer.Trim();
deviceHardware.Manufacturer = manufacturer.Trim();
}
var model = (string)mItem.GetPropertyValue("Model");
if (DeviceHardware.Model == null && !string.IsNullOrWhiteSpace(model))
if (deviceHardware.Model == null && !string.IsNullOrWhiteSpace(model))
{
DeviceHardware.Model = model.ToString();
deviceHardware.Model = model.ToString();
}
var product = (string)mItem.GetPropertyValue("Product");
if (DeviceHardware.Model == null && !string.IsNullOrWhiteSpace(product))
if (deviceHardware.Model == null && !string.IsNullOrWhiteSpace(product))
{
DeviceHardware.Model = product.ToString();
deviceHardware.Model = product.ToString();
}
// Added 2012-11-22 G# - Lenovo IdeaPad Serial SHIM
// http://www.discoict.com.au/forum/feature-requests/2012/11/serial-number-detection-on-ideapads.aspx
var serialNumber = (string)mItem.GetPropertyValue("SerialNumber");
if (!string.IsNullOrWhiteSpace(serialNumber) &&
(DeviceHardware.SerialNumber == null ||
((DeviceHardware.Manufacturer?.Equals("LENOVO", StringComparison.OrdinalIgnoreCase) ?? false) &&
((DeviceHardware.Model?.Equals("S10-3", StringComparison.OrdinalIgnoreCase) ?? false) // S10-3
|| (DeviceHardware.Model?.Equals("2957", StringComparison.OrdinalIgnoreCase) ?? false)))))
(deviceHardware.SerialNumber == null ||
((deviceHardware.Manufacturer?.Equals("LENOVO", StringComparison.OrdinalIgnoreCase) ?? false) &&
((deviceHardware.Model?.Equals("S10-3", StringComparison.OrdinalIgnoreCase) ?? false) // S10-3
|| (deviceHardware.Model?.Equals("2957", StringComparison.OrdinalIgnoreCase) ?? false)))))
{
DeviceHardware.SerialNumber = serialNumber.Trim();
ErrorReporting.DeviceIdentifier = DeviceHardware.SerialNumber;
deviceHardware.SerialNumber = serialNumber.Trim();
ErrorReporting.DeviceIdentifier = deviceHardware.SerialNumber;
}
}
else
@@ -218,7 +405,7 @@ namespace Disco.Client.Interop
}
}
private static void ApplySystemProductInformation(this DeviceHardware DeviceHardware)
private static void ApplySystemProductInformation(this DeviceHardware deviceHardware)
{
try
{
@@ -230,28 +417,28 @@ namespace Disco.Client.Interop
{
if (mItem != null)
{
if (DeviceHardware.SerialNumber == null)
if (deviceHardware.SerialNumber == null)
{
var serialNumber = mItem.GetPropertyValue("IdentifyingNumber") as string;
if (!string.IsNullOrWhiteSpace(serialNumber))
{
DeviceHardware.SerialNumber = serialNumber.Trim();
ErrorReporting.DeviceIdentifier = DeviceHardware.SerialNumber;
deviceHardware.SerialNumber = serialNumber.Trim();
ErrorReporting.DeviceIdentifier = deviceHardware.SerialNumber;
}
}
var uUID = (string)mItem.GetPropertyValue("UUID");
if (!string.IsNullOrWhiteSpace(uUID))
{
DeviceHardware.UUID = uUID.Trim();
deviceHardware.UUID = uUID.Trim();
// if serial number is absent attempt using UUID if valid
if (string.IsNullOrWhiteSpace(DeviceHardware.SerialNumber))
if (string.IsNullOrWhiteSpace(deviceHardware.SerialNumber))
{
Guid uuidGuid;
if (Guid.TryParse(DeviceHardware.UUID, out uuidGuid) && uuidGuid != Guid.Empty)
if (Guid.TryParse(deviceHardware.UUID, out uuidGuid) && uuidGuid != Guid.Empty)
{
DeviceHardware.SerialNumber = $"UUID{uuidGuid:N}";
deviceHardware.SerialNumber = $"UUID{uuidGuid:N}";
}
}
}
@@ -308,5 +495,56 @@ namespace Disco.Client.Interop
PerformanceServer,
Maximum
}
private enum ProcessorArchitectures : ushort
{
x86 = 0,
MIPS = 1,
Alpha = 2,
PowerPC = 3,
ia64 = 6,
x64 = 9,
}
private enum DiskLogicalDriveTypes : uint
{
Unknown = 0,
NoRootDirectory,
Removable,
Fixed,
Remote,
CDRom,
RAMDisk,
}
private enum DiskLogicalMediaTypes : uint
{
Unknown = 0,
F5_1Pt2_512,
F3_1Pt44_512,
F3_2Pt88_512,
F3_20Pt8_512,
F3_720_512,
F5_360_512,
F5_320_512,
F5_320_1024,
F5_180_512,
F5_160_512,
RemovableMedia,
FixedMedia,
F3_120M_512,
F3_640_512,
F5_640_512,
F5_720_512,
F3_1Pt2_512,
F3_1Pt23_1024,
F5_1Pt23_1024,
F3_128Mb_512,
F3_230Mb_512,
F8_256_128,
F3_200Mb_512,
F3_240M_512,
F3_32M_512,
}
}
}