# Register routine with plugin registery
$DATAFUNCS{'rrd_disks'} = \&data_rrd_disks;

use Data::Dumper;
use File::Basename;

# Take the disk status messages and create rrd databases for each of 
# mount point. Skipping the @DFIGNORE mount points.

# RRA to be created for each RRD
# "daily" 5min avg last 48hr
# "weekly" 30min avg last 12days
# "monthly 2hr avg last 48days
# "yearly" 24hr avg last 576days
$RRAS = "RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:576 RRA:AVERAGE:0.5:24:576 " . 
        "RRA:AVERAGE:0.5:288:576";

$RRDTOOL = "/usr/local/rrdtool/bin/rrdtool";
$RRDDIR  = "/usr/local/spong/var/rrd";

sub data_rrd_disks {
   my( $host, $service, $color, $start, $time, $sum, $message ) = @_;

   if ($service ne 'disk' ) { return; }
   if ($color eq 'purple' ) { return; }

   my( $line, $rawfs, $used, $pct, $name, %namemap, $target );

   &main::save_data('>', "$RRDDIR/$host/.rrd-disk", $message);

   foreach $line ( split(/\n/,$message) ) {
      chomp($line);
      $goodline = 0;
      if ( $line =~ m!^(\S+)\s+\S*\s+(\d+)\s+\d+\s+(\d+)%\s+[^/]*(/.*)$! ) {
         # Parsed as coming from a Unix-style system
         ( $rawfs, $used, $pct, $name ) = ( $1, $2, $3, $4 );
         $goodline = 1;
      }
      if ( $line =~ m!^Drive: (.):\\, Size: (\d+)[^,]*, Free: (\d+)[^,]*, (\d+)% full! ) {
         # Parsed as coming from an NT-style system
         ( $rawfs, $used, $pct, $name ) = ( "/$1:", ($2-$3)/1024, $4, "/$1" );
         $goodline = 1;
      }

      if ($goodline == 1) {
         foreach my $check (@main::DFIGNORE) { # FS's to skip
            next if $rawfs =~ /$check/;
            next if $name  =~ /$check/;
         }

         # Reformat the mount point
         if ( $name eq "/" ) {
            $target = "root";
         } else {
            $target = substr($name,1);  # strip of leading '/'
            $cnt = $target =~ tr!/!-!; # Convert '/' to '-' and count 'em
            # If path if too long, just use the basename of mount pont
            if ($cnt > 1) {
               $target = basename($name);
            }
         }

         # If .rrd file not found, built it
         if ( ! -f "$RRDDIR/$host/disk-$target.rrd" ) {
            &debug("$RRDDIR/$host/disk-$target.rrd not found creating it",4);
            { local $SIG{'PIPE'} = 'IGNORE';
              local $SIG{'CHLD'} = 'IGNORE';

              eval {
                 system "$RRDTOOL create $RRDDIR/$host/disk-$target.rrd " . 
                        "DS:pct:GAUGE:600:0:100 DS:used:GAUGE:600:0:U $RRAS";
              };
            }
            if (@?) { &error("Error: rrdtool create: $@"); }
         }

        # Update the .rrd file
        &debug("Updating $host $name rrd file",4);
        { local $SIG{'PIPE'} = 'IGNORE';
          local $SIG{'CHLD'} = 'IGNORE';

          system "$RRDTOOL update $RRDDIR/$host/disk-$target.rrd " .
                 "$time:$pct:$used";
           if ($@) { &error("Error: rrdtool update: $@"); }
        }

        $namemap{$target} = $name;
      } else {
         &debug("Couldn't parse line: '$line'",8);
      }
   }

   $line = "";
   while(($k,$v) = (each %namemap)) {
      $line .= "$k:$v\n";
   }

   # Write the file name to mount map to 
   &main::save_data('>', "$RRDDIR/$host/disk-name-map", $line );

}

# I'm include perl code, I need this line.
1;

