diff --git a/App.config b/App.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..0bdedc4 --- /dev/null +++ b/Program.cs @@ -0,0 +1,184 @@ +// (c) Microsoft Corporation +// Author: Clemens Vasters (clemensv@microsoft.com) +// Code subject to MS-PL: http://opensource.org/licenses/ms-pl.html +// SMART Attributes and Background: http://en.wikipedia.org/wiki/S.M.A.R.T. +// SMART Attributes Overview: http://www.t13.org/Documents/UploadedDocuments/docs2005/e05171r0-ACS-SMARTAttributes_Overview.pdf + +namespace SmartDataApp { + using System; + using System.Collections.Generic; + using System.Management; + using System.Runtime.InteropServices; + + public enum SmartAttributeType : byte { + Raw_Read_Error_Rate = 0x01, + Throughput_Performance = 0x02, + Spin_Up_Time = 0x03, + Start_Stop_Count = 0x04, + Reallocated_Sectors_Count = 0x05, + Read_Channel_Margin = 0x06, + Seek_Error_Rate = 0x07, + Seek_Time_Performance = 0x08, + Power_On_Hours = 0x09, + Spin_Retry_Count = 0x0A, + SSD_Program_Fail_Count = 0xAB, + SSD_Erase_Fail_Count = 0xAC, + SSD_Wear_Leveling_Count = 0xAD, + Calibration_Retry_Count = 0x0B, + Unused_Reserved_Block_Count_Total = 0xB4, + Power_Cycle_Count = 0x0C, + Soft_Read_Error_Rate = 0x0D, + SATA_Downshift_Error_Count = 0xB7, + End_to_End_error = 0xB8, + Head_Stability = 0xB9, + Induced_Op_Vibration_Detection = 0xBA, + Reported_Uncorrectable_Errors = 0xBB, + Command_Timeout = 0xBC, + High_Fly_Writes = 0xBD, + Airflow_Temperature_WDC = 0xBE, + Temperature_Difference_from_100 = 0xBE, + GSense_Error_Rate = 0xBF, + Poweroff_Retract_Count = 0xC0, + Load_Cycle_Count = 0xC1, + Temperature = 0xC2, + Hardware_ECC_Recovered = 0xC3, + Reallocation_Event_Count = 0xC4, + Current_Pending_Sector_Count = 0xC5, + Uncorrectable_Sector_Count = 0xC6, + Ultra_DMACRC_Error_Count = 0xC7, + Multi_Zone_Error_Rate = 0xC8, + Write_Error_Rate_Fujitsu = 0xC8, + Off_Track_Soft_Read_Error_Rate = 0xC9, + Data_Address_Markerrors = 0xCA, + Run_Out_Cancel = 0xCB, + Soft_ECC_Correction = 0xCC, + Thermal_Asperity_Rate_TAR = 0xCD, + Flying_Height = 0xCE, + Spin_High_Current = 0xCF, + Spin_Buzz = 0xD0, + Offline_Seek_Performance = 0xD1, + Vibration_During_Write = 0xD3, + Shock_During_Write = 0xD4, + Disk_Shift = 0xDC, + GSense_Error_Rate_Alt = 0xDD, + Loaded_Hours = 0xDE, + Load_Unload_Retry_Count = 0xDF, + Load_Friction = 0xE0, + Load_Unload_Cycle_Count = 0xE1, + Load_In_Time = 0xE2, + Torque_Amplification_Count = 0xE3, + Power_Off_Retract_Cycle = 0xE4, + GMR_Head_Amplitude = 0xE6, + Drive_Temperature = 0xE7, + Head_Flying_Hours = 0xF0, + Transfer_Error_Rate_Fujitsu = 0xF0, + Total_LBAs_Written = 0xF1, + Total_LBAs_Read = 0xF2, + Read_Error_Retry_Rate = 0xFA, + Free_Fall_Protection = 0xFE, + } + + public class SmartData { + readonly Dictionary attributes; + readonly ushort structureVersion; + + public SmartData(byte[] arrVendorSpecific) { + attributes = new Dictionary(); + for (int offset = 2; offset < arrVendorSpecific.Length;) { + var a = FromBytes(arrVendorSpecific, ref offset, 12); + // Attribute values 0x00, 0xfe, 0xff are invalid + if (a.AttributeType != 0x00 && (byte) a.AttributeType != 0xfe && (byte) a.AttributeType != 0xff) { + attributes[a.AttributeType] = a; + } + } + structureVersion = (ushort) (arrVendorSpecific[0] * 256 + arrVendorSpecific[1]); + } + + public ushort StructureVersion { + get { + return this.structureVersion; + } + } + + public SmartAttribute this[SmartAttributeType v] { + get { + return this.attributes[v]; + } + } + + public IEnumerable Attributes { + get { + return this.attributes.Values; + } + } + + static T FromBytes(byte[] bytearray, ref int offset, int count) { + IntPtr ptr = IntPtr.Zero; + + try { + ptr = Marshal.AllocHGlobal(count); + Marshal.Copy(bytearray, offset, ptr, count); + offset += count; + return (T) Marshal.PtrToStructure(ptr, typeof(T)); + } finally { + if (ptr != IntPtr.Zero) { + Marshal.FreeHGlobal(ptr); + } + } + } + } + + [StructLayout(LayoutKind.Sequential)] + public struct SmartAttribute { + public SmartAttributeType AttributeType; + public ushort Flags; + public byte Value; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] + public byte[] VendorData; + + public bool Advisory { + get { + return (Flags & 0x1) == 0x0; // Bit 0 unset? + } + } + public bool FailureImminent { + get { + return (Flags & 0x1) == 0x1; // Bit 0 set? + } + } + public bool OnlineDataCollection { + get { + return (Flags & 0x2) == 0x2; // Bit 0 set? + } + } + + } + + public class Program { + public static void Main() { + try { + var searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSStorageDriver_ATAPISmartData"); + + foreach (ManagementObject queryObj in searcher.Get()) { + Console.WriteLine("-----------------------------------"); + Console.WriteLine("MSStorageDriver_ATAPISmartData instance"); + Console.WriteLine("-----------------------------------"); + + var arrVendorSpecific = (byte[]) queryObj.GetPropertyValue("VendorSpecific"); + + // Create SMART data from 'vendor specific' array + var d = new SmartData(arrVendorSpecific); + foreach (var b in d.Attributes) { + Console.Write("{0}, {1}", b.AttributeType, b.VendorData[0]); + Console.WriteLine(); + } + + } + } catch (ManagementException e) { + Console.WriteLine("An error occurred while querying for WMI data: " + e.Message); + } + + Console.ReadLine(); + } + } +} \ No newline at end of file diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..73d87ee --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die einer Assembly zugeordnet sind. +[assembly: AssemblyTitle("RetrieveSMARTData")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("RetrieveSMARTData")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar +// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von +// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("f4ecd273-5eb1-4095-8119-286d6b3f665d")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern +// übernehmen, indem Sie "*" eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/RetrieveSMARTData.csproj b/RetrieveSMARTData.csproj new file mode 100644 index 0000000..c788334 --- /dev/null +++ b/RetrieveSMARTData.csproj @@ -0,0 +1,61 @@ + + + + + Debug + AnyCPU + {F4ECD273-5EB1-4095-8119-286D6B3F665D} + Exe + Properties + RetrieveSMARTData + RetrieveSMARTData + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/bin/Debug/RetrieveSMARTData.exe b/bin/Debug/RetrieveSMARTData.exe new file mode 100644 index 0000000..0455277 Binary files /dev/null and b/bin/Debug/RetrieveSMARTData.exe differ diff --git a/bin/Debug/RetrieveSMARTData.exe.config b/bin/Debug/RetrieveSMARTData.exe.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/bin/Debug/RetrieveSMARTData.exe.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/bin/Debug/RetrieveSMARTData.pdb b/bin/Debug/RetrieveSMARTData.pdb new file mode 100644 index 0000000..f447e4f Binary files /dev/null and b/bin/Debug/RetrieveSMARTData.pdb differ diff --git a/bin/Debug/RetrieveSMARTData.vshost.exe b/bin/Debug/RetrieveSMARTData.vshost.exe new file mode 100644 index 0000000..681ab77 Binary files /dev/null and b/bin/Debug/RetrieveSMARTData.vshost.exe differ diff --git a/bin/Debug/RetrieveSMARTData.vshost.exe.config b/bin/Debug/RetrieveSMARTData.vshost.exe.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/bin/Debug/RetrieveSMARTData.vshost.exe.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/bin/Debug/RetrieveSMARTData.vshost.exe.manifest b/bin/Debug/RetrieveSMARTData.vshost.exe.manifest new file mode 100644 index 0000000..061c9ca --- /dev/null +++ b/bin/Debug/RetrieveSMARTData.vshost.exe.manifest @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache new file mode 100644 index 0000000..031f96b Binary files /dev/null and b/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/obj/Debug/RetrieveSMARTData.csproj.FileListAbsolute.txt b/obj/Debug/RetrieveSMARTData.csproj.FileListAbsolute.txt new file mode 100644 index 0000000..35b901d --- /dev/null +++ b/obj/Debug/RetrieveSMARTData.csproj.FileListAbsolute.txt @@ -0,0 +1,6 @@ +c:\users\danzeglo\documents\visual studio 2015\Projects\RetrieveSMARTData\RetrieveSMARTData\bin\Debug\RetrieveSMARTData.exe.config +c:\users\danzeglo\documents\visual studio 2015\Projects\RetrieveSMARTData\RetrieveSMARTData\bin\Debug\RetrieveSMARTData.exe +c:\users\danzeglo\documents\visual studio 2015\Projects\RetrieveSMARTData\RetrieveSMARTData\bin\Debug\RetrieveSMARTData.pdb +c:\users\danzeglo\documents\visual studio 2015\Projects\RetrieveSMARTData\RetrieveSMARTData\obj\Debug\RetrieveSMARTData.csprojResolveAssemblyReference.cache +c:\users\danzeglo\documents\visual studio 2015\Projects\RetrieveSMARTData\RetrieveSMARTData\obj\Debug\RetrieveSMARTData.exe +c:\users\danzeglo\documents\visual studio 2015\Projects\RetrieveSMARTData\RetrieveSMARTData\obj\Debug\RetrieveSMARTData.pdb diff --git a/obj/Debug/RetrieveSMARTData.csprojResolveAssemblyReference.cache b/obj/Debug/RetrieveSMARTData.csprojResolveAssemblyReference.cache new file mode 100644 index 0000000..effd9bf Binary files /dev/null and b/obj/Debug/RetrieveSMARTData.csprojResolveAssemblyReference.cache differ diff --git a/obj/Debug/RetrieveSMARTData.exe b/obj/Debug/RetrieveSMARTData.exe new file mode 100644 index 0000000..0455277 Binary files /dev/null and b/obj/Debug/RetrieveSMARTData.exe differ diff --git a/obj/Debug/RetrieveSMARTData.pdb b/obj/Debug/RetrieveSMARTData.pdb new file mode 100644 index 0000000..f447e4f Binary files /dev/null and b/obj/Debug/RetrieveSMARTData.pdb differ diff --git a/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs b/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs new file mode 100644 index 0000000..e69de29 diff --git a/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs b/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs new file mode 100644 index 0000000..e69de29 diff --git a/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs b/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs new file mode 100644 index 0000000..e69de29