Loading / unloading a cycle using SMART and WMI

In an attempt to develop an analytical tool on the hard drive, I am trying to get the value of the number of load / unload cycles from the data of my SMART hard drive, I wonder if anyone knows how to do this. What am I trying:

  • I am looking for data from the WMI class MSStorageDriver_ATAPISmartData , where attribute number 193 is what I need (an attribute representing the load / unload cycle counter)
  • The data I receive looks like

enter image description here

I think I'm close, the data in red is the same as the version of Everest Home shows when I launch it, ideally I would like the last part to be (an attribute called data)

enter image description here

A way to collect this data:

 static void doStuff() { try { byte TEMPERATURE_ATTRIBUTE = 193; ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"\root\WMI", "SELECT * FROM MSStorageDriver_ATAPISmartData"); //loop through all the hard disks foreach (ManagementObject queryObj in searcher.Get()) { byte[] arrVendorSpecific = (byte[])queryObj.GetPropertyValue("VendorSpecific"); int tempIndex = Array.IndexOf(arrVendorSpecific, TEMPERATURE_ATTRIBUTE); Console.WriteLine("HDD TEMP: " + arrVendorSpecific[tempIndex + 5].ToString()); foreach (byte dat in arrVendorSpecific) { Console.Write(dat.ToString() + " "); } } } catch (Exception err) { Console.WriteLine(err.Message); } } 

PS this method works to collect the temperature of the hard drive (which means the Console.WriteLine("HDD TEMP: " + arrVendorSpecific[tempIndex + 5].ToString()); line Console.WriteLine("HDD TEMP: " + arrVendorSpecific[tempIndex + 5].ToString()); but I'm not sure why its tempIndex + 5

+4
source share
1 answer

The code you use is incorrect because you use a network search (Array.IndexOf) to find the SMART Attribute ID (you may have false positives because this value may match another in the array), the identifier of these attributes has a fixed position within a documented structure ( SMART Attribute Overview ).

SMART attribute table

 Offset Length Description (bytes) 0 2 SMART structure version (this is vendor-specific) 2 12 Attribute entry 1 2+(12) 12 Attribute entry 2 . . . 2+(12*29) 12 Attribute entry 30 

Attribute table entry

enter image description here

here you can write code to search for the location of each attribute and get the values ​​you are looking for

 using System; using System.Collections.Generic; using System.Management; using System.Text; using System.Runtime.InteropServices; namespace GetWMI_Info { class Program { [StructLayout(LayoutKind.Sequential)] public struct Attribute { public byte AttributeID; public ushort Flags; public byte Value; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public byte[] VendorData; } static void Main(string[] args) { try { Attribute AtributeInfo; ManagementScope Scope = new ManagementScope(String.Format("\\\\{0}\\root\\WMI", "localhost"), null); Scope.Connect(); ObjectQuery Query = new ObjectQuery("SELECT VendorSpecific FROM MSStorageDriver_ATAPISmartData"); ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query); byte LoadCycleCount = 0xC1; int Delta = 12; foreach (ManagementObject WmiObject in Searcher.Get()) { byte[] VendorSpecific = (byte[])WmiObject["VendorSpecific"]; for (int offset = 2; offset < VendorSpecific.Length; ) { if (VendorSpecific[offset] == LoadCycleCount) { IntPtr buffer = IntPtr.Zero; try { buffer = Marshal.AllocHGlobal(Delta); Marshal.Copy(VendorSpecific, offset, buffer, Delta); AtributeInfo = (Attribute)Marshal.PtrToStructure(buffer, typeof(Attribute)); Console.WriteLine("AttributeID {0}", AtributeInfo.AttributeID); Console.WriteLine("Flags {0}", AtributeInfo.Flags); Console.WriteLine("Value {0}", AtributeInfo.Value); Console.WriteLine("Value {0}", BitConverter.ToString(AtributeInfo.VendorData)); } finally { if (buffer != IntPtr.Zero) { Marshal.FreeHGlobal(buffer); } } } offset += Delta; } } } catch (Exception e) { Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace)); } Console.WriteLine("Press Enter to exit"); Console.Read(); } } } 
+8
source

Source: https://habr.com/ru/post/1403232/


All Articles