1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of libatasmart.
6 Copyright 2008 Lennart Poettering
8 libatasmart is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation, either version 2.1 of the
11 License, or (at your option) any later version.
13 libatasmart is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with libatasmart. If not, If not, see
20 <http://www.gnu.org/licenses/>.
50 int main(int argc, char *argv[]) {
52 const char *device = NULL, *argv0, *p, *file = NULL;
55 SkBool from_blob = FALSE;
57 static const struct option long_options[] = {
58 {"overall", no_argument, &mode, MODE_OVERALL},
59 {"power-on", no_argument, &mode, MODE_POWER_ON},
60 {"bad", no_argument, &mode, MODE_BAD},
61 {"temperature", no_argument, &mode, MODE_TEMPERATURE},
62 {"save", optional_argument, NULL, ARG_SAVE},
63 {"load", optional_argument, NULL, ARG_LOAD},
64 {"help", no_argument, NULL, 'h' },
69 if ((p = strrchr(argv0, '/')))
75 if ((opt = getopt_long(argc, argv, "h", long_options, NULL)) < 0)
84 "Usage: %s [PARAMETERS] DEVICE\n"
85 "Reads ATA SMART data from a device and parses it.\n"
87 "\t--overall \tShow overall status\n"
88 "\t--power-on \tPrint power on time in ms\n"
89 "\t--bad \tPrint bad sector count\n"
90 "\t--temperature \tPrint drive temperature in mKelvin\n"
91 "\t--save[=FILENAME]\tSave raw data to file/STDOUT\n"
92 "\t--load[=FILENAME]\tRead data from a file/STDIN instead of device\n"
93 "\t-h | --help \tShow this help\n", argv0);
103 device = optarg ? optarg : "-";
111 fprintf(stderr, "Invalid arguments.\n");
117 if (optind != argc-1) {
118 fprintf(stderr, "No or more than one device specified.\n");
122 device = argv[optind];
124 if (optind != argc) {
125 fprintf(stderr, "Too many arguments.\n");
135 if ((ret = sk_disk_open(NULL, &d)) < 0) {
136 fprintf(stderr, "Failed to open disk: %s\n", strerror(errno));
140 if (strcmp(device, "-")) {
141 if (!(f = fopen(device, "r"))) {
142 fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
147 size = fread(blob, 1, sizeof(blob), f);
152 if (size >= sizeof(blob)) {
153 fprintf(stderr, "File too large for buffer.\n");
157 if ((ret = sk_disk_set_blob(d, blob, size)) < 0) {
158 fprintf(stderr, "Failed to set blob: %s\n", strerror(errno));
163 if ((ret = sk_disk_open(device, &d)) < 0) {
164 fprintf(stderr, "Failed to open disk %s: %s\n", device, strerror(errno));
171 if ((ret = sk_disk_dump(d)) < 0) {
172 fprintf(stderr, "Failed to dump disk data: %s\n", strerror(errno));
179 SkSmartOverall overall;
181 if ((ret = sk_disk_smart_read_data(d)) < 0) {
182 fprintf(stderr, "Failed to read SMART data: %s\n", strerror(errno));
186 if ((ret = sk_disk_smart_get_overall(d, &overall)) < 0) {
187 fprintf(stderr, "Failed to get overall status: %s\n", strerror(errno));
191 printf("%s\n", sk_smart_overall_to_string(overall));
192 q = overall == SK_SMART_OVERALL_GOOD ? 0 : 1;
196 case MODE_POWER_ON: {
199 if ((ret = sk_disk_smart_read_data(d)) < 0) {
200 fprintf(stderr, "Failed to read SMART data: %s\n", strerror(errno));
204 if ((ret = sk_disk_smart_get_power_on(d, &ms)) < 0) {
205 fprintf(stderr, "Failed to get power on time: %s\n", strerror(errno));
209 printf("%llu\n", (unsigned long long) ms);
217 if ((ret = sk_disk_smart_read_data(d)) < 0) {
218 fprintf(stderr, "Failed to read SMART data: %s\n", strerror(errno));
222 if ((ret = sk_disk_smart_get_bad(d, &bad)) < 0) {
223 fprintf(stderr, "Failed to get bad sectors: %s\n", strerror(errno));
227 printf("%llu\n", (unsigned long long) bad);
232 case MODE_TEMPERATURE: {
235 if ((ret = sk_disk_smart_read_data(d)) < 0) {
236 fprintf(stderr, "Failed to read SMART data: %s\n", strerror(errno));
240 if ((ret = sk_disk_smart_get_temperature(d, &mkelvin)) < 0) {
241 fprintf(stderr, "Failed to get temperature: %s\n", strerror(errno));
245 printf("%llu\n", (unsigned long long) mkelvin);
255 if ((ret = sk_disk_smart_read_data(d)) < 0) {
256 fprintf(stderr, "Failed to read SMART data: %s\n", strerror(errno));
260 if ((ret = sk_disk_get_blob(d, &blob, &size)) < 0) {
261 fprintf(stderr, "Failed to get blob: %s\n", strerror(errno));
265 if (file && strcmp(file, "-")) {
266 if (!(f = fopen(file, "w"))) {
267 fprintf(stderr, "Failed to open '%s': %s\n", file, strerror(errno));
272 n = fwrite(blob, 1, size, f);
278 fprintf(stderr, "Failed to write to disk: %s\n", strerror(errno));