Merge pull request #4 from ckaczor/master

Associate different data sources using properties rather than by index
This commit is contained in:
Jaap-Willem Dooge
2018-10-22 16:53:47 +02:00
committed by GitHub

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Management; using System.Management;
/* /*
@@ -44,6 +45,8 @@ namespace SmartHDD
{ {
public int Index { get; set; } public int Index { get; set; }
public string DeviceId { get; set; }
public string InstanceName { get; set; }
public bool IsOK { get; set; } public bool IsOK { get; set; }
public string Model { get; set; } public string Model { get; set; }
public string Type { get; set; } public string Type { get; set; }
@@ -222,34 +225,32 @@ namespace SmartHDD
{ {
// retrieve list of drives on computer (this will return both HDD's and CDROM's and Virtual CDROM's) // retrieve list of drives on computer (this will return both HDD's and CDROM's and Virtual CDROM's)
var dicDrives = new Dictionary<int, HDD>(); var dicDrives = new List<HDD>();
var wdSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive"); var wdSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
// extract model and interface information // extract model and interface information
int iDriveIndex = 0;
foreach (ManagementObject drive in wdSearcher.Get()) foreach (ManagementObject drive in wdSearcher.Get())
{ {
var hdd = new HDD(); var hdd = new HDD();
hdd.Model = drive["Model"].ToString().Trim(); hdd.Model = drive["Model"].ToString().Trim();
hdd.Type = drive["InterfaceType"].ToString().Trim(); hdd.Type = drive["InterfaceType"].ToString().Trim();
dicDrives.Add(iDriveIndex, hdd); hdd.DeviceId = drive["DeviceID"].ToString().Trim();
iDriveIndex++; hdd.InstanceName = drive["PNPDeviceID"].ToString().Trim();
dicDrives.Add(hdd);
} }
var pmsearcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia"); var pmsearcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
// retrieve hdd serial number // retrieve hdd serial number
iDriveIndex = 0;
foreach (ManagementObject drive in pmsearcher.Get()) foreach (ManagementObject drive in pmsearcher.Get())
{ {
// because all physical media will be returned we need to exit var tag = drive["Tag"].ToString().Trim();
// after the hard drives serial info is extracted
if (iDriveIndex >= dicDrives.Count)
break;
dicDrives[iDriveIndex].Serial = drive["SerialNumber"] == null ? "None" : drive["SerialNumber"].ToString().Trim(); var dicDrive = dicDrives.FirstOrDefault(d => d.DeviceId.Equals(tag, StringComparison.InvariantCultureIgnoreCase));
iDriveIndex++;
if (dicDrive != null)
dicDrive.Serial = drive["SerialNumber"] == null ? "None" : drive["SerialNumber"].ToString().Trim();
} }
// get wmi access to hdd // get wmi access to hdd
@@ -258,18 +259,27 @@ namespace SmartHDD
// check if SMART reports the drive is failing // check if SMART reports the drive is failing
searcher.Query = new ObjectQuery("Select * from MSStorageDriver_FailurePredictStatus"); searcher.Query = new ObjectQuery("Select * from MSStorageDriver_FailurePredictStatus");
iDriveIndex = 0;
foreach (ManagementObject drive in searcher.Get()) foreach (ManagementObject drive in searcher.Get())
{ {
dicDrives[iDriveIndex].IsOK = (bool)drive.Properties["PredictFailure"].Value == false; var instanceName = drive["InstanceName"].ToString().Trim();
iDriveIndex++;
var dicDrive = dicDrives.FirstOrDefault(d => instanceName.StartsWith(d.InstanceName, StringComparison.InvariantCultureIgnoreCase));
if (dicDrive != null)
dicDrive.IsOK = (bool)drive.Properties["PredictFailure"].Value == false;
} }
// retrive attribute flags, value worste and vendor data information // retrive attribute flags, value worste and vendor data information
searcher.Query = new ObjectQuery("Select * from MSStorageDriver_FailurePredictData"); searcher.Query = new ObjectQuery("Select * from MSStorageDriver_FailurePredictData");
iDriveIndex = 0;
foreach (ManagementObject data in searcher.Get()) foreach (ManagementObject data in searcher.Get())
{ {
var instanceName = data["InstanceName"].ToString().Trim();
var dicDrive = dicDrives.FirstOrDefault(d => instanceName.StartsWith(d.InstanceName, StringComparison.InvariantCultureIgnoreCase));
if (dicDrive == null)
continue;
Byte[] bytes = (Byte[])data.Properties["VendorSpecific"].Value; Byte[] bytes = (Byte[])data.Properties["VendorSpecific"].Value;
for (int i = 0; i < 30; ++i) for (int i = 0; i < 30; ++i)
{ {
@@ -287,7 +297,7 @@ namespace SmartHDD
int vendordata = BitConverter.ToInt32(bytes, i * 12 + 7); int vendordata = BitConverter.ToInt32(bytes, i * 12 + 7);
if (id == 0) continue; if (id == 0) continue;
var attr = dicDrives[iDriveIndex].Attributes[id]; var attr = dicDrive.Attributes[id];
attr.Current = value; attr.Current = value;
attr.Worst = worst; attr.Worst = worst;
attr.Data = vendordata; attr.Data = vendordata;
@@ -298,15 +308,20 @@ namespace SmartHDD
// given key does not exist in attribute collection (attribute not in the dictionary of attributes) // given key does not exist in attribute collection (attribute not in the dictionary of attributes)
} }
} }
iDriveIndex++;
} }
// retreive threshold values foreach attribute // retreive threshold values foreach attribute
searcher.Query = new ObjectQuery("Select * from MSStorageDriver_FailurePredictThresholds"); searcher.Query = new ObjectQuery("Select * from MSStorageDriver_FailurePredictThresholds");
iDriveIndex = 0;
foreach (ManagementObject data in searcher.Get()) foreach (ManagementObject data in searcher.Get())
{ {
var instanceName = data["InstanceName"].ToString().Trim();
var dicDrive = dicDrives.FirstOrDefault(d => instanceName.StartsWith(d.InstanceName, StringComparison.InvariantCultureIgnoreCase));
if (dicDrive == null)
continue;
Byte[] bytes = (Byte[])data.Properties["VendorSpecific"].Value; Byte[] bytes = (Byte[])data.Properties["VendorSpecific"].Value;
for (int i = 0; i < 30; ++i) for (int i = 0; i < 30; ++i)
{ {
@@ -317,7 +332,7 @@ namespace SmartHDD
int thresh = bytes[i * 12 + 3]; int thresh = bytes[i * 12 + 3];
if (id == 0) continue; if (id == 0) continue;
var attr = dicDrives[iDriveIndex].Attributes[id]; var attr = dicDrive.Attributes[id];
attr.Threshold = thresh; attr.Threshold = thresh;
} }
catch catch
@@ -325,8 +340,6 @@ namespace SmartHDD
// given key does not exist in attribute collection (attribute not in the dictionary of attributes) // given key does not exist in attribute collection (attribute not in the dictionary of attributes)
} }
} }
iDriveIndex++;
} }
WriteFullLine("┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐"); WriteFullLine("┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐");
@@ -338,14 +351,14 @@ namespace SmartHDD
{ {
WriteFullLine("┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐"); WriteFullLine("┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐");
WriteFullLine(string.Format("{0, -155}", string.Format("│ DRIVE ({0}): " + drive.Value.Model + " (S/N: " + drive.Value.Serial + ") - " + drive.Value.Type, ((drive.Value.IsOK) ? "OK" : "BAD"))) + "│"); WriteFullLine(string.Format("{0, -155}", string.Format("│ DRIVE ({0}): " + drive.Model + " (S/N: " + drive.Serial + ") - " + drive.Type, ((drive.IsOK) ? "OK" : "BAD"))) + "│");
WriteFullLine("├──────────────────────────────────────────────────────────────────────────────────────────────┬───────────┬───────────┬───────────┬──────────────┬────────┤"); WriteFullLine("├──────────────────────────────────────────────────────────────────────────────────────────────┬───────────┬───────────┬───────────┬──────────────┬────────┤");
WriteFullLine("│ ID │ Current │ Worst │ Threshold │ Data │ Status │"); WriteFullLine("│ ID │ Current │ Worst │ Threshold │ Data │ Status │");
bool oddLine = false; bool oddLine = false;
ConsoleColor rowColor = ConsoleColor.DarkBlue; ConsoleColor rowColor = ConsoleColor.DarkBlue;
foreach (var attr in drive.Value.Attributes) foreach (var attr in drive.Attributes)
{ {
if (attr.Value.HasData) if (attr.Value.HasData)