Index: trunk/Nebulous-Server/Build.PL
===================================================================
--- trunk/Nebulous-Server/Build.PL	(revision 19790)
+++ trunk/Nebulous-Server/Build.PL	(revision 19791)
@@ -16,4 +16,5 @@
         'Digest::SHA1'          => 0,
         'File::ExtAttr'         => '1.03',
+        'File::Mountpoint'      => '0.01',
         'File::Path'            => '1.08',
         'File::Spec'            => 0,
Index: trunk/Nebulous-Server/Changes
===================================================================
--- trunk/Nebulous-Server/Changes	(revision 19790)
+++ trunk/Nebulous-Server/Changes	(revision 19791)
@@ -5,4 +5,7 @@
     - rename neb-addvol -> neb-voladd
     - add a pid file to neb-admin so only one instance can be run at a time
+    - rework nebdiskd to only record data on volumes it is supposed to be
+      monitoring and then only for paths that actually have volumes mounted on
+      them
 
 0.15 Thu Sep 11 13:00:59 HST 2008
Index: trunk/Nebulous-Server/bin/nebdiskd
===================================================================
--- trunk/Nebulous-Server/bin/nebdiskd	(revision 19790)
+++ trunk/Nebulous-Server/bin/nebdiskd	(revision 19791)
@@ -3,5 +3,5 @@
 # Copyright (C) 2007  Joshua Hoblitt
 # 
-# $Id: nebdiskd,v 1.6 2008-07-03 22:01:03 jhoblitt Exp $
+# $Id: nebdiskd,v 1.7 2008-10-01 00:30:53 jhoblitt Exp $
 
 use strict;
@@ -9,8 +9,9 @@
 
 use vars qw( $VERSION );
-$VERSION = '0.02';
+$VERSION = '0.03';
 
 use Config::YAML;
 use DBI;
+use File::Mountpoint qw( is_mountpoint );
 use File::Spec;
 use Nebulous::Server::SQL;
@@ -74,16 +75,13 @@
 $SIG{HUP}  = sub { $c = read_rcfile($rcfile) }; 
 
-my $dbh = setup_db(
-        db      => $db,
-        dbhost  => $dbhost,
-        dbuser  => $dbuser,
-        dbpass  => $dbpass,
-    );
 
 poll_mounts(
-        dbh             => $dbh,
+        db              => $db,
+        dbhost          => $dbhost,
+        dbuser          => $dbuser,
+        dbpass          => $dbpass,
         poll_interval   => $poll_interval,
         debug           => $debug,
-    ) or die "poll_mounts() should not have returned";
+) or die "poll_mounts() should not have returned";
 
 sub poll_mounts
@@ -91,5 +89,8 @@
     my %p = @_;
 
-    my $dbh             = $p{dbh} or return;
+    my $db              = $p{db} or return;
+    my $dbhost          = $p{dbhost} or return;
+    my $dbuser          = $p{dbuser} or return;
+    my $dbpass          = $p{dbpass} or return;
     my $poll_interval   = $p{poll_interval} || 60;
     my $debug           = $p{debug} || 0;
@@ -98,20 +99,54 @@
 
     while (1) {
-        # list of mount points to stat so that the automounter will mount these
-        # volumes if they have been unmounted.
-        stat_dirs($mounts);
+        # setup the db on every pass incase the database died on us
+        my $dbh = setup_db(
+            db      => $db,
+            dbhost  => $dbhost,
+            dbuser  => $dbuser,
+            dbpass  => $dbpass,
+        );
 
         eval {
+            my @valid_mounts;
+
+            # determine valid mountpoints
+            foreach my $mnt (@$mounts) {
+                print "checking $mnt\n" if $debug;
+                # this /SHOULD/ fail if the mount point is handled by the
+                # automounter and it fails to mount
+                eval {
+                    unless (is_mountpoint($mnt)) {
+                        print "$mnt is not a valid mountpoint\n" if $debug;
+                        next;
+                    }
+                };
+                if ($@) {
+                    print "$mnt is not a valid mountpoint\n" if $debug;
+                    next;
+                }
+                push @valid_mounts, $mnt;
+            }
+            # empty the mount table
+            $dbh->do("DELETE FROM mount");
+            print "flushed mount table\n" if $debug;
+
+            # repopulate the mount table with all valid mounts that we are
+            # supposed to be watching
             my $query = $dbh->prepare_cached("INSERT INTO mount VALUES(?, ?, ?)");
-            $dbh->do("DELETE FROM mount");
-
-            print "flushed mount table\n" if $debug;
-
+            # get stats on all currently mounted volumes
+            # this has to be done AFTER we determine what the valid mountpoints
+            # are incase is_mountpoint() invokes the automounter
             my $stats = $lxs->get;
-            foreach my $dev (keys %$stats) {
-                my $mnt = $stats->{$dev};
-                my %mount;
-                $query->execute(@$mnt{qw( mountpoint total usage )});
-                print "adding $mnt->{mountpoint} to db\n" if $debug;
+            $stats = fix_disk_stats($stats);
+
+            foreach my $mnt (@valid_mounts) {
+                my $dev_info = $stats->{$mnt};
+                unless (defined $dev_info) {
+                    print "can't find device info for $mnt\n"
+                        if $debug;
+                    next;
+                }
+                $query->execute(@$dev_info{qw( mountpoint total usage )});
+                print "adding $dev_info->{mountpoint} to db\n" if $debug;
             }
 
@@ -119,7 +154,9 @@
 
             $dbh->commit;
+            print "commited to database\n" if $debug;
         };
         if ($@) {
-            $db->rollback;
+            $dbh->rollback;
+            print "rolledback transaction\n" if $debug;
             warn $@;
         }
@@ -158,5 +195,5 @@
     my $sql = Nebulous::Server::SQL->new;
 
-    my $dbh = DBI->connect(
+    my $dbh = DBI->connect_cached(
         "DBI:mysql:database=$p{db}:host=$p{dbhost}",
         $p{dbuser},
@@ -247,4 +284,20 @@
         }
     }
+}
+
+
+sub fix_disk_stats
+{
+    my $stats = shift;
+
+    # take the hash returned by Sys::Statistics::Linux::DiskUsage->new->get and
+    # replace the keynames with those of the mountpoint for each device
+    my %new_stats;
+    foreach my $key (keys %$stats) {
+        my $disk = $stats->{$key};
+        $new_stats{$disk->{mountpoint}} = $disk;
+    }
+
+    return \%new_stats;
 }
 
