1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/*
* smart monitoring for scsi and ata
* copyright © 2009 erik quanstrom
*/
#include <u.h>
#include <libc.h>
#include <fis.h>
#include "smart.h"
enum{
Checksec = 600,
Opensec = 60 * 60,
Relogsec = 38400 / 4,
};
static Sdisk *disks;
static Dtype dtab[] = {
Tata, "ata", ataprobe, ataenable, atastatus,
Tscsi, "scsi", scsiprobe, scsienable, scsistatus,
};
static char *logfile = "smart";
static int aflag;
static int tflag;
static int vflag;
void
eprint(Sdisk *d, char *s, ...)
{
char buf[256];
va_list arg;
va_start(arg, s);
vseprint(buf, buf + sizeof buf, s, arg);
va_end(arg);
// syslog(0, logfile, "%s: %s", d->name, buf);
if(vflag)
fprint(2, "%s: %s", d->name, buf);
}
void
smartlog(Sdisk *d, char *s, ...)
{
char buf[256];
va_list arg;
va_start(arg, s);
vseprint(buf, buf + sizeof buf, s, arg);
va_end(arg);
if(!tflag)
syslog(0, logfile, "%s: %s", d->name, buf);
if(tflag || vflag)
fprint(2, "%s: %s\n", d->name, buf);
}
static void
diskclose(Sdisk *d)
{
close(d->fd);
d->fd = -1;
}
static int
diskopen(Sdisk *d)
{
char buf[128];
snprint(buf, sizeof buf, "%s/raw", d->path);
werrstr("");
return d->fd = open(buf, ORDWR);
}
static int
noexist(void)
{
char buf[ERRMAX];
errstr(buf, sizeof buf);
if(strstr(buf, "exist"))
return -1;
return 0;
}
static void
lognew(Sdisk *d)
{
if(aflag && !tflag)
smartlog(d, d->t->tname);
}
static int
newdisk(char *s)
{
char buf[128], *p;
int i;
Sdisk d;
memset(&d, 0, sizeof d);
snprint(d.path, sizeof d.path, "%s", s);
if(p = strrchr(s, '/'))
p++;
else
p = s;
snprint(d.name, sizeof d.name, "%s", p);
snprint(buf, sizeof buf, "%s/raw", s);
if(diskopen(&d) == -1)
return noexist();
for(i = 0; i < nelem(dtab); i++)
if(dtab[i].probe(&d) == 0)
if(dtab[i].enable(&d) == 0){
d.t = dtab + i;
lognew(&d);
break;
}
diskclose(&d);
if(d.t != 0){
d.next = disks;
disks = malloc(sizeof d);
memmove(disks, &d, sizeof d);
}
return 0;
}
static int
probe0(char *s, int l)
{
char *p, *f[3], buf[16];
int i;
s[l] = 0;
for(; p = strchr(s, '\n'); s = p + 1){
if(tokenize(s, f, nelem(f)) < 1)
continue;
for(i = 0; i < 0x10; i++){
snprint(buf, sizeof buf, "/dev/%s%ux", f[0], i);
if(newdisk(buf) == -1 && i > 2)
break;
}
}
return -1;
}
int
probe(void)
{
char *s;
int fd, l, r;
fd = open("/dev/sdctl", OREAD);
if(fd == -1)
return -1;
r = -1;
l = 1024; /* #S/sdctl has 0 size; guess */
if(s = malloc(l + 1))
if((l = read(fd, s, l)) > 0)
r = probe0(s, l);
free(s);
close(fd);
return r;
}
void
run(void)
{
char buf[1024];
int e, s0;
uvlong t, t0;
Sdisk *d;
e = 0;
t = time(0);
for(d = disks; d; d = d->next){
t0 = d->lastcheck;
if(t0 != 0 && t - t0 < Checksec)
continue;
if(diskopen(d) == -1){
if(t - t0 > Opensec)
smartlog(d, "can't open in %ullds\n", t - t0);
continue;
}
s0 = d->status;
d->status = d->t->status(d, buf, sizeof buf);
diskclose(d);
if(d->status == -1)
e++;
if((aflag || d->status != s0 || d->status != 0) && !d->silent){
t0 = d->lastlog;
if(t0 == 0 || t - t0 >= Relogsec){
smartlog(d, buf);
d->lastlog = t;
}
}else
d->lastlog = 0;
d->lastcheck = t;
}
if(tflag)
exits(e? "smart errors": "");
}
void
usage(void)
{
fprint(2, "usage: disk/smart [-aptv] [/dev/sdXX] ...\n");
exits("usage");
}
void
main(int argc, char **argv)
{
int pflag;
pflag = 0;
ARGBEGIN{
case 'a':
aflag = 1;
break;
case 'p':
pflag = 1;
break;
case 't':
tflag = 1;
case 'v':
vflag = 1;
break;
default:
usage();
}ARGEND
for(; *argv; argv++)
newdisk(*argv);
if(argc == 0 || pflag)
probe();
if(disks == nil)
sysfatal("no disks");
for(;; sleep(30*1000))
run();
}
|