Disk Drive Dangers - SMART and WMI
Written by Mike James   
Monday, 01 July 2024
Article Index
Disk Drive Dangers - SMART and WMI
WMI Query Language
Get Smart
Decoding WMI data
Processing the data
Listing

The Complete program with an additional textbox to show the processed data is:

namespace WinFormsApp1
{
    using System.Management;
    using System.Windows.Forms;
    public partial class Form1 : Form
    {
        public struct SMARTAttribute
        {
            public int status;
            public int value;
            public int rawvalue;
            public SMARTAttribute()
            {
                status = 0;
                value = 0;
                rawvalue = 0;
            }
        }
        public struct SMART
        {
         public SMARTAttribute RawReadErrorRate;
         public SMARTAttribute ReallocatedSectorCount;
         public SMARTAttribute ReallocationEventCount;
         public SMARTAttribute CurrentPendingSectorCount;
         public SMARTAttribute
OfflineScanUncorrectableSectorCount;
        }
        public Form1()
        {
            InitializeComponent();
            richTextBox2.Text = "Unknw\tUnknw\tAttribute
\tStatus\tUnknw\tValue\tWorst\t
Raw\t\tUnknw\n";
        }
        private void button1_Click(object sender, 
EventArgs e)
        {
            ManagementObjectSearcher WMISearch =
new ManagementObjectSearcher(
                      "Select * from Win32_DiskDrive");
            ManagementObjectCollection Drives =
WMISearch.Get();
            richTextBox1.Text = "Drive\t\t\tStatus\n";
            foreach (ManagementObject Drive in Drives)
            {
                richTextBox1.Text = richTextBox1.Text +
Drive.Properties["DeviceId"].
Value.ToString() + "\t";
                richTextBox1.Text = richTextBox1.Text +
Drive.Properties["Status"].Value.
ToString() + "\n";
            }
            WMISearch.Scope = new ManagementScope(
@"\root\wmi");
            WMISearch.Query = new ObjectQuery(
"Select * from
MSStorageDriver_FailurePredictData");
            ManagementObjectCollection FailDataSet =
WMISearch.Get();
            foreach (ManagementObject FailData
in FailDataSet)
            {
                Byte[] data = (Byte[])FailData.
Properties["VendorSpecific"].Value;
                for (int i = 0; i < data[0] - 1; i++)
                {
                    for (int j = 0; j < 12; j++)
                    {
                        richTextBox2.Text = richTextBox2.
Text + data[i * 12 + j] + "\t";
                    }
                    richTextBox2.Text = richTextBox2.
Text + "\n";
                }
            }
            SMART smartdata =new SMART();
            foreach (ManagementObject FailData2
in FailDataSet)
            {
                Byte[] data2 = (Byte[])FailData2.
Properties["VendorSpecific"].Value;
                for (int i = 0; i < data2[0] - 1; i++)
                {
                 int start = i * 12;
                 switch (data2[start + 2])
                 {
                  case 1:
                    smartdata.RawReadErrorRate.
value = data2[start + 5];
                    smartdata.RawReadErrorRate.rawvalue =
data2[start + 7] +
256 * data2[start + 8];
                            break;
                  case 5:
                    smartdata.ReallocatedSectorCount.
value = data2[start + 5];
                    smartdata.ReallocatedSectorCount.
rawvalue = data2[start + 7] +
256 * data2[start + 8];
                            break;
                  case 196:
                    smartdata.ReallocationEventCount.
value = data2[start + 5];
                    smartdata.ReallocationEventCount.
rawvalue = data2[start + 7] +
256 * data2[start + 8];
                            break;
                  case 197:
                    smartdata.CurrentPendingSectorCount.
value = data2[start + 5];
                    smartdata.CurrentPendingSectorCount.
rawvalue = data2[start + 7] +
256 * data2[start + 8];
                            break;
                 case 198:
                    smartdata.
OfflineScanUncorrectableSectorCount.
value = data2[start + 5];
                    smartdata.
OfflineScanUncorrectableSectorCount.
rawvalue = data2[start + 7] +
256 * data2[start + 8];
                            break;
                    }
                   
                }
                richTextBox3.Text +=
"Raw Read Error Rate " +
smartdata.RawReadErrorRate.value + "\n";
                richTextBox3.Text +=
"Reallocated Sector Count " +
smartdata.ReallocatedSectorCount.value + "\n";
                richTextBox3.Text +=
"Reallocation Event Count " +
smartdata.ReallocationEventCount.value + "\n";
                richTextBox3.Text +=
"Current Pending Sector Count " +
smartdata.CurrentPendingSectorCount.value + "\n";
                richTextBox3.Text +=
"Offline Scan Uncorrectable Sector Count "
+ smartdata.OfflineScanUncorrectableSectorCount.
value + "\n";
            }
        }
    }
}

More Information

USENIX study - Disk failures in the real world

Related Articles

Getting remote DCOM (WMI) to work

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Mico - A Personality For Copilot
24/10/2025

Microsoft has designed an animated face for Copilot, its ubiquitous AI Assistant. Is Mico, whose name rhymes with "pico", going be a worthy successor to Clippy, the highly annoying paperclip that trie [ ... ]



JavaScript Survey Opens Ahead of JavaScript Day
29/09/2025

JetBrains is hosting JavaScript Day on October 2nd with all presentations being streamed live on You Tube, where they will remain available after the event. And anyone interested in the JavaScrip [ ... ]


More News

pico book

 

Comments




or email your comment to: comments@i-programmer.info

 

<ASIN:0672321440>
<ASIN:1555582990>



Last Updated ( Monday, 01 July 2024 )