Ticket #20: hfh.cpp

File hfh.cpp, 1.7 KB (added by stlman, 15 years ago)

Decode Head_Flying_Hours from ST31000528AS Seagate drive

Line 
1// This programme reads SMART values and decodes
2// Head_Flying_Hours from my ST31000528AS.
3// Put this file in the smartmontools tree.
4#include <stdio.h>
5
6#include <unistd.h>
7#include <sys/ioctl.h>
8#include <fcntl.h>
9#include <string.h>
10#include <stdint.h>
11#include <time.h>
12
13#include "atacmds.h"
14
15#define HDIO_GET_IDENTITY 0x030d /* get IDE identification info */
16#define HDIO_DRIVE_TASK 0x031e /* execute task and special drive command */
17#define HDIO_DRIVE_CMD 0x031f /* execute a special drive command */
18
19#define BUFFER_LENGTH (4+512)
20
21using namespace std;
22int main(int ac, char* av[]) {
23 time_t t;
24 int i;
25 int fd;
26 int rc;
27 unsigned short deviceid[256];
28 unsigned char buf[BUFFER_LENGTH];
29
30 struct ata_smart_attribute *attrs;
31 struct ata_smart_values *values;
32
33 fd=open("/dev/sda", O_RDONLY|O_NONBLOCK);
34 if(fd < 0) {
35 perror("open");
36 }
37
38 memset(deviceid, 0, sizeof(deviceid));
39 rc = ioctl(fd, HDIO_GET_IDENTITY, deviceid);
40 if( -1 == rc) {
41 perror("ioctl");
42 }
43
44 while(1) {
45 memset(buf, 0, sizeof(buf));
46 buf[0]=ATA_SMART_CMD;
47 buf[2]=ATA_SMART_READ_VALUES;
48 buf[3]=1;
49 t=time(NULL);
50 rc = ioctl(fd, HDIO_DRIVE_CMD, buf);
51 if( -1 == rc) {
52 perror("ioctl");
53 }
54 values = (struct ata_smart_values*)(buf+4);
55 for (i = 0; i < NUMBER_ATA_SMART_ATTRIBUTES; ++i) {
56 if(240 != values->vendor_attributes[i].id) continue;
57 printf("%d\t", t);
58 printf("0x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x\n",
59 values->vendor_attributes[i].raw[3],
60 values->vendor_attributes[i].raw[2],
61 values->vendor_attributes[i].raw[1],
62 values->vendor_attributes[i].raw[0],
63 values->vendor_attributes[i].reserv,
64 values->vendor_attributes[i].raw[5],
65 values->vendor_attributes[i].raw[4]
66 );
67 }
68 sleep(10);
69 }
70 close(fd);
71}