
##########################################################################
# $Id: smartd,v 1.10 2005/08/23 22:19:56 mike Exp $
##########################################################################

#$Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'} || 0;

while (defined($ThisLine = <STDIN>)) {
   chomp($ThisLine);
   if ( ($Device,$Msg) = ($ThisLine =~ /^Device: ([^,]+), No such device or address, open\(\) failed/ )) {
       # ignore
   } elsif ( ($Device,$Msg) = ($ThisLine =~ /^Device: ([^,]+), is SMART capable. Adding to "monitor" list./ )) {
       # ignore
   } elsif ( ($Device,$Msg) = ($ThisLine =~ /^Device: ([^,]+), found in smartd database./ )) {
       # ignore
   } elsif ( ($Device,$Msg) = ($ThisLine =~ /^Device: ([^,]+), opened/)) {
       # ignore
   } elsif ( ($Device,$Msg) = ($ThisLine =~ /^Device: ([^,]+), appears to lack SMART*/ )) {
       # ignore
   } elsif ( ($ThisLine =~ /^smartd version/) || ($ThisLine =~ /^Home page/) || ($ThisLine =~ /configuration file/i) || ($ThisLine =~ /^Monitoring/) || ($ThisLine =~ /smartd has fork/) || ($ThisLine =~ /smartd startup succeeded/)  ) {
       # ignore
   } elsif ( ($ThisLine =~ /^smartd version/)
        || ($ThisLine =~ /^Home page/)
        || ($ThisLine =~ /configuration file/i)
        || ($ThisLine =~ /\[trip Temperature is \d+ Celsius\]/)
        || ($ThisLine =~ /^Monitoring/)
        || ($ThisLine =~ /smartd received signal 15: Terminated/)
        || ($ThisLine =~ /smartd is exiting \(exit status 0\)/)
        || ($ThisLine =~ /smartd has fork/)
        || ($ThisLine =~ /smartd startup succeeded/)  ) {						        # ignore
											
#   } elsif ( ($Device,$Msg) = ($ThisLine =~ /^Device: ([^,]+), (.*)$/)) {
#      $ParamChanges{$Device}{$Msg}++;
   } elsif ( ($Device,$AttribType,$Code,$Name,undef,undef,$NewVal) = ($ThisLine =~ /^Device: ([^,]+), SMART ([A-Za-z]+) Attribute: ([0-9]+) ([A-Za-z_]+) changed from ([0-9]+) (\[Raw [0-9]+\] )?to ([0-9]+)/)) {
      $ParamChanges{$Device}{"$AttribType: $Name ($Code)"}{$NewVal}++;
   # smartd reports temperature changes this way only for SCSI disks
   } elsif ( ($Device,$NewVal) = ($ThisLine =~ /^Device: ([^,]+), Temperature changed -?\d+ Celsius to (\d+) Celsius/)) {
      $TempChanges{$Device}{$NewVal}++;
   } elsif ( ($Device, $Num) = ($ThisLine =~ /^Device: ([^,]+), (\d+) Currently unreadable \(pending\) sectors/) ) {
      $Badsectors{$Device}++;
      $NumBadsectors{$Device} = $Num;
   }
   elsif ( ( $ThisLine =~ /warning/i ) ) {
      $Warnings{$ThisLine}++;
   }
   else {
      # Report any unmatched entries...
      push @OtherList,"$ThisLine\n";
   }

}

if (keys %ParamChanges) {
   foreach $Device (sort keys %ParamChanges) {
      print "\n$Device :\n";
      foreach $Msg (sort keys %{$ParamChanges{$Device}}) {
         print "   $Msg changed to ";
         $vv="";
         foreach $Val (sort keys %{$ParamChanges{$Device}{$Msg}}) {
            if (! $vv eq "") {
               print "$vv, ";
            }
            $vv = "$Val";
            #$vv .= " ($ParamChanges{$Device}{$Msg}{$Val} times)";
         }
         print "$vv\n";
      }
   }
}

if (keys %TempChanges) {
   foreach $Device (sort keys %ParamChanges) {
      print "\n$Device :\n   Temperature changed to ";
      foreach $Val (sort keys %{$TempChanges{$Device}}) {
         if (! $vv eq "") {
            print "$vv, ";
         }
         $vv = "$Val";
      }
      print "$vv\n";
   }
}

if (keys %Badsectors){
   print "\nBad sectors detected:\n";
   foreach $Device (sort keys %Badsectors) {
      print "\t" . $Device . " - " . $Badsectors{$Device} . " Time(s)\n";
      print "\t" . $NumBadsectors{$Device} . " bad sectors detected\n";
   }

}

if ( (keys %Warnings) ) {
   print "\nWarnings:\n";
   foreach $Line (sort {$Warnings{$b} <=> $Warnings{$a}} keys %Warnings) {
      print "\t" . $Line . " - ". $Warnings{$Line} . " Time(s)\n";
   }
}

if (($#OtherList >= 0) and (not $IgnoreUnmatched)){
   print "\n**Unmatched Entries**\n";
   print @OtherList;
}

exit(0);

# vi: shiftwidth=3 tabstop=3 syntax=perl et

