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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
/*
* smartd.c
*
* Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* You should have received a copy of the GNU General Public License
* (for example COPYING); if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <linux/hdreg.h>
#include <syslog.h>
#include "atacmds.h"
#include "scsicmds.h"
#include "smartd.h"
int daemon_init(void)
{
pid_t pid;
if ( (pid = fork()) < 0)
return -1;
else if (pid != 0)
exit (0);
setsid ();
chdir("/");
umask(0);
return(0);
}
/* void Usage (void)
prints help information for command syntax */
void Usage ( void)
{
printf( "smartd version %i.%i - S.M.A.R.T. Daemon\n",
VERSION_MAJOR, VERSION_MINOR);
printf( "useage: smartd -[opts] \n");
printf( "Read Only Commands:\n");
printf( "\t\t%c\t\tStart smartd in debug Mode\n",DEBUGMODE);
}
/* void ParseOpts ( chars *opts)
Takes command options and sets features to be run */
void ParseOpts ( char *opts)
{
int i = 0;
char options[255];
strcpy ( (char *) &options, opts);
if ( options[0] != '-' )
{
Usage();
exit (-1);
}
for (i = 1; options[i] != '\0'; i++ )
{
switch (options[i])
{
case DEBUGMODE :
debugmode = TRUE;
break;
case EMAILNOTIFICATION:
emailnotification = TRUE;
break;
default:
Usage();
exit (-1);
}
}
}
void failurenotify ( char *message)
{
}
void atadevicescan ( atadevices_t *devices)
{
int i;
int fd;
struct hd_driveid drive;
char device[] = "/dev/hda";
for(i = 0; i < MAXATADEVICES ; i++,device[7]++ )
{
fd = 0;
if (debugmode)
printf("Reading Device %s\n", device);
fd = open ( device , O_RDWR );
if ( fd >= 0)
{
if (ataReadHDIdentity ( fd, &drive) == 0 )
{
if (ataSmartSupport (drive) )
{
if (ataEnableSmart)
{
devices[numatadevices].fd = fd;
strcpy((char *) &devices[numatadevices].devicename,
(char*) &device);
devices[numatadevices].drive = drive;
if ( ataReadSmartValues ( fd,
&devices[numatadevices].smartval) != 0 )
{
syslog (LOG_INFO,
"Device: %s, Read Smart Values Failed\n",
device);
}
if ( ataReadSmartThresholds ( fd,
&devices[numatadevices].smartthres) != 0 )
{
syslog (LOG_INFO,
"Device: %s, Read Smart Thresholds Failed\n",
device);
}
if (debugmode)
printf("%s Found and is SMART capable\n",
devices[numatadevices].devicename);
syslog (LOG_INFO,
"Device: %s, Found and is SMART capable\n",
device);
devices[numatadevices].selftest =
isSupportSelfTest(devices[numatadevices].smartval);
numatadevices++;
}
}
}
}
}
}
void scsidevicescan ( scsidevices_t *devices)
{
int i, fd, smartsupport;
unsigned char tBuf[4096];
char device[] = "/dev/sda";
for(i = 0; i < MAXSCSIDEVICES ; i++,device[7]++ )
{
fd = 0;
if (debugmode)
printf("Reading Device %s\n", device);
fd = open ( device , O_RDWR );
if(debugmode)
printf ("Device: %s, File Descriptor: %d \n", device, fd);
if ( fd >= 0)
{
if ( testunitready (fd) == 0 )
{
if (modesense ( fd, 0x1c, (UINT8 *) &tBuf) != 0)
{
if(debugmode)
printf ("Device: %s, Failed read of ModePage 1C \n", device);
}
else
{
if ( scsiSmartSupport( fd, (UINT8 *) &smartsupport) == 0)
{
if (!(smartsupport & DEXCPT_ENABLE))
{
devices[numscsidevices].fd = fd;
strcpy((char *) &devices[numscsidevices].devicename,
(char*) &device);
if (debugmode)
printf("%s Found and is SMART capable\n",
devices[numscsidevices].devicename);
syslog (LOG_INFO, "Device: %s, Found and is SMART capable\n",
device);
if (logsense ( fd , SUPPORT_LOG_PAGES, (UINT8 *) &tBuf) == 0)
{
for ( i = 4; i < tBuf[3] + LOGPAGEHDRSIZE ; i++)
{
switch ( tBuf[i])
{
case TEMPERATURE_PAGE:
devices[numscsidevices].TempPageSupported = 1;
break;
case SMART_PAGE:
devices[numscsidevices].SmartPageSupported = 1;
break;
default:
break;
}
}
}
numscsidevices++;
}
}
}
}
}
}
}
void ataCompareSmartValues ( atadevices_t *device, struct ata_smart_values b )
{
int i;
for ( i =0; i < NUMBER_ATA_SMART_ATTRIBUTES; i++)
{
if( device->smartval.vendor_attributes[i].id & b.vendor_attributes[i].id
& (device->smartval.vendor_attributes[i].current
!= b.vendor_attributes[i].current))
{
syslog (LOG_INFO, "Device: %s, S.M.A.R.T. Attribute: %i Changed %i\n",
device->devicename,
device->smartval.vendor_attributes[i].id,
(device->smartval.vendor_attributes[i].current
- b.vendor_attributes[i].current));
}
}
}
int ataCheckDevice( atadevices_t *drive)
{
struct ata_smart_values tempsmartval;
struct ata_smart_thresholds tempsmartthres;
if ( ataReadSmartValues ( drive->fd, &tempsmartval) != 0 )
{
syslog (LOG_INFO, "%s:Failed to read smart values\n", drive->devicename);
}
if ( ataReadSmartThresholds ( drive->fd, &tempsmartthres) != 0 )
{
syslog (LOG_INFO, "%s:Failed to read smart thresholds\n",
drive->devicename);
}
if (ataCheckSmart( tempsmartval, tempsmartthres))
{
if(debugmode)
printf ("Device: %s, Failed attribute: %i\n", drive->devicename,
ataCheckSmart( tempsmartval, tempsmartthres));
else
syslog (LOG_CRIT, "Device: %s, Failed attribute: %i\n", drive->devicename,
ataCheckSmart( tempsmartval, tempsmartthres));
}
if(debugmode)
printf ("Device: %s, S.M.A.R.T. Failure attribute: %i\n", drive->devicename,
ataCheckSmart( tempsmartval, tempsmartthres));
ataCompareSmartValues (drive , tempsmartval);
/* Copy the values back to the structure */
drive->smartval = tempsmartval;
drive->smartthres = tempsmartthres;
return 0;
}
int scsiCheckDevice( scsidevices_t *drive)
{
UINT8 returnvalue;
UINT8 currenttemp;
UINT8 triptemp;
currenttemp = triptemp = 0;
if ( scsiCheckSmart( drive->fd, drive->SmartPageSupported, &returnvalue, ¤ttemp, &triptemp ) != 0)
{
syslog (LOG_INFO, "%s:Failed to read smart values\n", drive->devicename);
}
if ( returnvalue )
{
if(debugmode)
printf ("Device: %s, S.M.A.R.T. Failure: (%02x) %s\n", drive->devicename,
returnvalue, scsiSmartGetSenseCode( returnvalue) );
else
syslog (LOG_CRIT, "Device: %s, S.M.A.R.T. Failure: (%02x) %s\n", drive->devicename,
returnvalue, scsiSmartGetSenseCode( returnvalue) );
}
else
{
if(debugmode)
printf ("Device: %s, Okay attribute: %d\n", drive->devicename, returnvalue);
}
if ( currenttemp )
{
if ( (currenttemp != drive->Temperature) && ( drive->Temperature) )
syslog (LOG_INFO, "Device: %s, Temperature changed %d degrees to %d degrees since last reading\n",
drive->devicename, (int) (currenttemp - drive->Temperature), (unsigned int) currenttemp );
drive->Temperature = currenttemp;
}
return 0;
}
void CheckDevices ( atadevices_t *atadevices, scsidevices_t *scsidevices)
{
int i;
while (1)
{
for (i = 0; i < numatadevices;i++)
{
if ( ataCheckDevice ( &atadevices[i]) != 0 )
{
exit (-1); /* never reached */
}
}
for (i = 0; i < numscsidevices;i++)
{
if ( scsiCheckDevice ( &scsidevices[i]) != 0 )
{
exit (-1); /* never reached */
}
}
sleep ( checktime );
}
}
/* Main Program */
int main (int argv, char **argc)
{
atadevices_t atadevices[MAXATADEVICES], *atadevicesptr;
scsidevices_t scsidevices[MAXSCSIDEVICES], *scsidevicesptr;
numatadevices=0;
numscsidevices=0;
scsidevicesptr = scsidevices;
atadevicesptr = atadevices;
if ( *(argc+1) != NULL)
ParseOpts ( *(argc+1));
syslog (LOG_INFO, "smartd started\n");
if (!debugmode)
{
daemon_init();
}
/* fork into indepentant process */
atadevicescan (atadevicesptr);
scsidevicescan (scsidevicesptr);
CheckDevices ( atadevicesptr, scsidevicesptr);
return 0;
}
|