首页 > 编程知识 正文

硬盘smart信息抓取,硬盘smart信息解读

时间:2023-05-06 17:50:00 阅读:163543 作者:2691

我知道在Windows环境下读取硬盘SMART信息:有两种方法。 一个是名为DeviceIoControl的Win API,另一个是通过wql语句进行查询。 据说这两种方法已经在网上腐烂了,本文主要要记录一个死也找不到的东西。 在上面的Api和wql中调查也会返回两个512字节的数据。 一个是记录属性。 另一个是智能信息中的THRESHOLDS。 那么,这两个512字节的数据结构到底怎么样了? (我在smartctl的源代码中找到了它们)。

首先是THRESHOLDS的512字节的数据结构。 强烈建议使用#pragmapack(push,1 )字节对齐。 )

#pragmapack(push,1 )/* vendorattributeofsmartthreshold ) compare toata _ smart _ attribute above/struct ATA _ smata unsigned char reserved[10]; (;/* formatofreadsmartthresholdcommand */struct ATA _ smart _ thresholds _ PVT { unsignedshortintrevnumber; struct ATA _ smart _ threshold _ entrythres _ entries [ number _ ATA _ smart _ attributes ]; unsigned char reserved[149]; unsigned char chksum; (; #pragmapack(pop )后面紧跟着其他Attribute的512字节数据。

#pragmapack(push,1 ) struct ATA _ smart _ attribute { unsignedcharid; unsigned short flags; unsigned char current; unsigned char worst; unsigned char raw[7]; (; struct ATA _ smart _ values { unsignedshortintrevnumber; struct ATA _ smart _ attribute vendor _ attributes [ number _ ATA _ smart _ attributes ]; unsignedcharoffline _ data _ collection _ status; unsignedcharself _ test _ exec _ status; //IBM # segmentsforofflinecollectionunsignedshortinttotal _ time _ to _ complete _ off _ line; //ibmdifferentunsignedcharvendor _ specific _ 366; //maxtoribmcurentsegmentpointerunsignedcharoffline _ data _ collection _ capability; unsignedshortintsmart _ capability; unsignedcharerrorlog _ capability; unsignedcharvendor _ specific _ 371; //Maxtor,IBM :自助- testfailurecheckpointseebelow! unsignedcharshort _ test _ completion _ time; unsignedcharextend _ test _ completion _ time _ b; //If0xff,use 16-bitvaluebelowunsignedcharconveyance _ test _ completion _ time; unsignedshortextend _ test _ completion _ time _ w; //e04130r2,added to T13/1699-D Revision 1c,April 2005 unsignedcharreserved _ 377 _ 385 [9]; unsignedcharvendor _ specific _ 386 _ 510 [ 125 ]; //maxtor bytes 508-509 attribute/threshold revision # unsignedcharchksum; (; #pragmapack(pop )两种方法查询硬盘SMART信息(都要管理员权限)

1. WQL(WMI Query Language)

wmi的存在避免了win api的调用,以便不用C的孩子可以用喜欢的语言读写系统的底层内容。 当然,效率会慢一点。 因此,wmi的查询语言称为wql。 语法和名字都类似于sql。 详细的语法请问谷歌,但真的值得学习。

使用wql时,管理员权限查询rootwmi命名空间下的msstoragedriver _ failurepredictthresholds和msstoragedriver _ failurepredictdata这两个表。 两者都有vendorstoragedriver _ failurepredictdata

Update:发现在项目中通过wmi获取的硬盘Interface Type是IDE,但是通过STORAGE_PROPERTY_QUERY获取的类型是SATA。 wim不正确,因为实际类型是SATA。 不太推荐wmi。 建议使用win api。

2. DeviceIoControl

说实话这个代码让人厌烦,所以我直接上了核心代码。

//这里的光盘id大概是这样的。 '\\.\PHYSICALDRIVE0','\ ". " physical drive1' boolreadsmartattributes (chysicaldrive1' boolreads ) const wchar _ t * physical _ prefix=l '\". " physical drive '; if (磁盘id!=nullwcslen(diskid ) wcslen ) physical_prefix ) _wcsnicmp ) diskid,physical_prefix,wcslen ) physical_prefix intucdriveindex=WC stol (diskidwcslen ) physical_prefix ),NULL,10 ); handlehdevice=create file w (磁盘id,GENERIC_READ | GENERIC_WRITE,file _ share _ delete|file _ share _ read | byteszattributes [ sizeof (sendcmdoutparams ) READ_ATTRIBUTE_BUFFER_SIZE - 1]; SENDCMDINPARAMS stCIP={ 0 }; STC IP.cbuffersize=read _ attribute _ buffer _ size; STC IP.bdrivenumber=ucdriveindex; STC IP.irdriveregs.bfeaturesreg=read _ attributes; STC IP.irdriveregs.bsectorcountreg=1; STC IP.irdriveregs.bsectornumberreg=1; STC IP.irdriveregs.bcyllowreg=smart _ cyl _ low; STC IP.irdriveregs.bcylhighreg=smart _ cyl _ hi; STC IP.irdriveregs.bdriveheadreg=0xa 0; STC IP.irdriveregs.bcommandreg=smart _ cmd; bret=deviceiocontrol(hdevice,SMART_RCV_DRIVE_DATA,stCIP,sizeof(STCIP ),szAttributes,sizeof ) sendcmdoof if(bret ) smart _ value=* (ATA _ smart _ values * ) ) (SENDCMDOUTPARAMS* ) szAttributes )-bBuffer; } STC IP.irdriveregs.bfeaturesreg=read _ thresholds; STC IP.cbuffersize=read _ threshold _ buffer _ size; bret=deviceiocontrol(hdevice,SMART_RCV_DRIVE_DATA,stCIP,sizeof(STCIP ),szAttributes,sizeof ) sendcmdoof if(bret ) ) threshold _ value=* (ATA _ smart _ thresholds _ PVT * ) ) (SENDCMDOUTPARAMS* ) SZ attributes-b buffft } return! 布雷特; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。