Index: /trunk/Nebulous-Server/Build.PL
===================================================================
--- /trunk/Nebulous-Server/Build.PL	(revision 16263)
+++ /trunk/Nebulous-Server/Build.PL	(revision 16264)
@@ -111,13 +111,14 @@
         bin/neb-cull
         bin/neb-df
+        bin/neb-fsck
         bin/neb-initdb
         bin/neb-locate
         bin/neb-ls
         bin/neb-mv
+        bin/neb-replicate
         bin/neb-rm
         bin/neb-stat
         bin/neb-touch
         bin/nebdiskd
-        bin/neb-replicate
     )],
 )->create_build_script;
Index: /trunk/Nebulous-Server/MANIFEST
===================================================================
--- /trunk/Nebulous-Server/MANIFEST	(revision 16263)
+++ /trunk/Nebulous-Server/MANIFEST	(revision 16264)
@@ -13,4 +13,5 @@
 bin/neb-cull
 bin/neb-df
+bin/neb-fsck
 bin/neb-initdb
 bin/neb-locate
Index: /trunk/Nebulous-Server/bin/neb-fsck
===================================================================
--- /trunk/Nebulous-Server/bin/neb-fsck	(revision 16264)
+++ /trunk/Nebulous-Server/bin/neb-fsck	(revision 16264)
@@ -0,0 +1,205 @@
+#!/usr/bin/env perl
+
+# Copyright (C) 2005-2008  Joshua Hoblitt
+#
+# $Id: neb-fsck,v 1.1 2008-01-30 00:19:34 jhoblitt Exp $
+
+use strict;
+use warnings FATAL => qw( all );
+
+use vars qw( $VERSION );
+$VERSION = '0.01';
+
+use DBI;
+use Nebulous::Server::SQL;
+use Nebulous::Client;
+use URI;
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version );
+use Pod::Usage qw( pod2usage );
+
+my ($db, $dbuser, $dbpass, $server);
+
+$db     = $ENV{'NEB_DB'} unless $db;
+$dbuser = $ENV{'NEB_USER'} unless $dbuser;
+$dbpass = $ENV{'NEB_PASS'} unless $dbpass;
+$server = $ENV{'NEB_SERVER'} unless $server;
+
+GetOptions(
+    'db=s'          => \$db,
+    'user=s'        => \$dbuser,
+    'pass=s'        => \$dbpass,
+    'server|s=s'    => \$server,
+) || pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage( -msg => "Required options: --server", -exitval => 2 )
+    unless $server;
+pod2usage( -msg => "Required options: --db --user --pass", -exitval => 2 )
+    unless $db && $dbuser && $dbpass;
+
+my $neb = Nebulous::Client->new(
+    proxy => "$server",
+);
+
+die "can't connected to Nebulous Server: $server"
+    unless defined $neb;
+
+my $dbh = DBI->connect(
+    "DBI:mysql:database=$db:host=localhost",
+    $dbuser,
+    $dbpass,
+    {
+        RaiseError => 1,
+        PrintError => 0,
+        AutoCommit => 1,
+    },
+);
+
+my $sql = Nebulous::Server::SQL->new();
+
+# so_id, ext_id, instances, available_instances, need_recovery, recoverable
+my $query = $dbh->prepare( $sql->find_objects_with_unavailable_instances );
+$query->execute;
+
+my @rows;
+while (my $row = $query->fetchrow_hashref) {
+    push @rows, $row;        
+}
+$query->finish;
+
+foreach my $obj (@rows) {
+    my $so_id = $obj->{so_id};
+    my $key = $obj->{ext_id};
+    my $has = $obj->{instances};
+    my $available = $obj->{instances_available};
+    my $need = $obj->{need_recovery};
+    my $recoverable = $obj->{recoverable};
+
+    # objects with only a single instance that are offline are unrecoverable so
+    # we don't need to handle that special case
+    next unless $need;
+    unless ($recoverable) {
+        warn "so_id: $so_id key: \'$key\' ",
+            "can not be recovered - no available instances\n";
+        next;
+    }
+
+    $available ||= 0;
+    next unless $available < 2;
+
+    warn "so_id: $so_id key: \'$key\' ",
+            " has only 1 instance avaiable -- replicating";
+
+    $neb->replicate($obj->{ext_id})
+        or warn "so_id: $so_id key: \'$key\' failed to replicate";
+}
+
+__END__
+
+=pod
+
+=head1 NAME
+
+neb-fsck - check and repair Nebulous volumes
+
+=head1 SYNOPSIS
+
+    neb-fsck [--db <database>] [--user <username>] [--pass <password>]
+        [--server <server>]
+
+=head1 DESCRIPTION
+
+=head1 OPTIONS
+
+=over 4
+
+=item * --server|-s <URL>
+
+URL of the Nebulous server to connect to.
+
+Optional if the appropriate environment variable is set.
+
+=item * --db|-d <database>
+
+Name of database (C<namespace>) to create tables in.
+
+Optional if the appropriate environment variable is set.
+
+=item * --user|-u <username>
+
+Username to authenticate with.
+
+Optional if the appropriate environment variable is set.
+
+=item * --pass|-p <password>
+
+Password to authenticate with.
+
+Optional if the appropriate environment variable is set.
+
+=back
+
+=head1 ENVIRONMENT
+
+These environment variables may be used in place of the specified command line
+options.  All command line option will override the corresponding environment
+value.
+
+=over 4
+
+=item * C<NEB_SERVER>
+
+Equivalent to --server|-s
+
+=item * C<NEB_DB>
+
+Equivalent to --db|-d
+
+=item * C<NEB_USER>
+
+Equivalent to --user|-u
+
+=item * C<NEB_PASS>
+
+Equivalent to --pass|-p 
+
+=back
+
+=head1 CREDITS
+
+Just me, myself, and I.
+
+=head1 SUPPORT
+
+Please contact the author directly via e-mail.
+
+=head1 AUTHOR
+
+Joshua Hoblitt <jhoblitt@cpan.org>
+
+=head1 COPYRIGHT
+
+Copyright (C) 2005-2008  Joshua Hoblitt.  All rights reserved.
+
+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 of the License, or (at your option) any later
+version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along with
+this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+Place - Suite 330, Boston, MA  02111-1307, USA.
+
+The full text of the license can be found in the L<perlgpl> Pod as supplied
+with Perl 5.8.1 and later.
+
+=head1 SEE ALSO
+
+L<Nebulous::Server>, L<neb-initdb>, L<neb-df>, L<nebdiskd>
+
+=cut
Index: /trunk/Nebulous-Server/lib/Nebulous/Server/SQL.pm
===================================================================
--- /trunk/Nebulous-Server/lib/Nebulous/Server/SQL.pm	(revision 16263)
+++ /trunk/Nebulous-Server/lib/Nebulous/Server/SQL.pm	(revision 16264)
@@ -1,5 +1,5 @@
 # Copyright (c) 2004  Joshua Hoblitt
 #
-# $Id: SQL.pm,v 1.45 2008-01-28 19:48:08 jhoblitt Exp $
+# $Id: SQL.pm,v 1.46 2008-01-30 00:19:06 jhoblitt Exp $
 
 package Nebulous::Server::SQL;
@@ -172,5 +172,6 @@
             storage_object.so_id,
             uri,
-            assigned_vol_id
+            assigned_vol_id,
+            available
         FROM storage_object
         JOIN instance
@@ -242,4 +243,20 @@
         SET ext_id = ?
         WHERE ext_id = ?
+    },
+    find_objects_with_unavailable_instances => qq{
+        SELECT
+            so_id,
+            ext_id,
+            count(ins_id) as instances,
+            count(mountedvol.vol_id) as available_instances,
+            count(mountedvol.vol_id) < count(ins_id) as need_recovery,
+            count(mountedvol.vol_id) > 0 as recoverable
+        FROM storage_object
+        JOIN instance
+            USING(so_id)
+        LEFT JOIN mountedvol
+            USING(vol_id)
+        GROUP BY so_id
+        HAVING need_recovery = 1
     },
 );
Index: /trunk/Nebulous/Build.PL
===================================================================
--- /trunk/Nebulous/Build.PL	(revision 16263)
+++ /trunk/Nebulous/Build.PL	(revision 16264)
@@ -111,13 +111,14 @@
         bin/neb-cull
         bin/neb-df
+        bin/neb-fsck
         bin/neb-initdb
         bin/neb-locate
         bin/neb-ls
         bin/neb-mv
+        bin/neb-replicate
         bin/neb-rm
         bin/neb-stat
         bin/neb-touch
         bin/nebdiskd
-        bin/neb-replicate
     )],
 )->create_build_script;
Index: /trunk/Nebulous/MANIFEST
===================================================================
--- /trunk/Nebulous/MANIFEST	(revision 16263)
+++ /trunk/Nebulous/MANIFEST	(revision 16264)
@@ -13,4 +13,5 @@
 bin/neb-cull
 bin/neb-df
+bin/neb-fsck
 bin/neb-initdb
 bin/neb-locate
Index: /trunk/Nebulous/bin/neb-fsck
===================================================================
--- /trunk/Nebulous/bin/neb-fsck	(revision 16264)
+++ /trunk/Nebulous/bin/neb-fsck	(revision 16264)
@@ -0,0 +1,205 @@
+#!/usr/bin/env perl
+
+# Copyright (C) 2005-2008  Joshua Hoblitt
+#
+# $Id: neb-fsck,v 1.1 2008-01-30 00:19:34 jhoblitt Exp $
+
+use strict;
+use warnings FATAL => qw( all );
+
+use vars qw( $VERSION );
+$VERSION = '0.01';
+
+use DBI;
+use Nebulous::Server::SQL;
+use Nebulous::Client;
+use URI;
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version );
+use Pod::Usage qw( pod2usage );
+
+my ($db, $dbuser, $dbpass, $server);
+
+$db     = $ENV{'NEB_DB'} unless $db;
+$dbuser = $ENV{'NEB_USER'} unless $dbuser;
+$dbpass = $ENV{'NEB_PASS'} unless $dbpass;
+$server = $ENV{'NEB_SERVER'} unless $server;
+
+GetOptions(
+    'db=s'          => \$db,
+    'user=s'        => \$dbuser,
+    'pass=s'        => \$dbpass,
+    'server|s=s'    => \$server,
+) || pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage( -msg => "Required options: --server", -exitval => 2 )
+    unless $server;
+pod2usage( -msg => "Required options: --db --user --pass", -exitval => 2 )
+    unless $db && $dbuser && $dbpass;
+
+my $neb = Nebulous::Client->new(
+    proxy => "$server",
+);
+
+die "can't connected to Nebulous Server: $server"
+    unless defined $neb;
+
+my $dbh = DBI->connect(
+    "DBI:mysql:database=$db:host=localhost",
+    $dbuser,
+    $dbpass,
+    {
+        RaiseError => 1,
+        PrintError => 0,
+        AutoCommit => 1,
+    },
+);
+
+my $sql = Nebulous::Server::SQL->new();
+
+# so_id, ext_id, instances, available_instances, need_recovery, recoverable
+my $query = $dbh->prepare( $sql->find_objects_with_unavailable_instances );
+$query->execute;
+
+my @rows;
+while (my $row = $query->fetchrow_hashref) {
+    push @rows, $row;        
+}
+$query->finish;
+
+foreach my $obj (@rows) {
+    my $so_id = $obj->{so_id};
+    my $key = $obj->{ext_id};
+    my $has = $obj->{instances};
+    my $available = $obj->{instances_available};
+    my $need = $obj->{need_recovery};
+    my $recoverable = $obj->{recoverable};
+
+    # objects with only a single instance that are offline are unrecoverable so
+    # we don't need to handle that special case
+    next unless $need;
+    unless ($recoverable) {
+        warn "so_id: $so_id key: \'$key\' ",
+            "can not be recovered - no available instances\n";
+        next;
+    }
+
+    $available ||= 0;
+    next unless $available < 2;
+
+    warn "so_id: $so_id key: \'$key\' ",
+            " has only 1 instance avaiable -- replicating";
+
+    $neb->replicate($obj->{ext_id})
+        or warn "so_id: $so_id key: \'$key\' failed to replicate";
+}
+
+__END__
+
+=pod
+
+=head1 NAME
+
+neb-fsck - check and repair Nebulous volumes
+
+=head1 SYNOPSIS
+
+    neb-fsck [--db <database>] [--user <username>] [--pass <password>]
+        [--server <server>]
+
+=head1 DESCRIPTION
+
+=head1 OPTIONS
+
+=over 4
+
+=item * --server|-s <URL>
+
+URL of the Nebulous server to connect to.
+
+Optional if the appropriate environment variable is set.
+
+=item * --db|-d <database>
+
+Name of database (C<namespace>) to create tables in.
+
+Optional if the appropriate environment variable is set.
+
+=item * --user|-u <username>
+
+Username to authenticate with.
+
+Optional if the appropriate environment variable is set.
+
+=item * --pass|-p <password>
+
+Password to authenticate with.
+
+Optional if the appropriate environment variable is set.
+
+=back
+
+=head1 ENVIRONMENT
+
+These environment variables may be used in place of the specified command line
+options.  All command line option will override the corresponding environment
+value.
+
+=over 4
+
+=item * C<NEB_SERVER>
+
+Equivalent to --server|-s
+
+=item * C<NEB_DB>
+
+Equivalent to --db|-d
+
+=item * C<NEB_USER>
+
+Equivalent to --user|-u
+
+=item * C<NEB_PASS>
+
+Equivalent to --pass|-p 
+
+=back
+
+=head1 CREDITS
+
+Just me, myself, and I.
+
+=head1 SUPPORT
+
+Please contact the author directly via e-mail.
+
+=head1 AUTHOR
+
+Joshua Hoblitt <jhoblitt@cpan.org>
+
+=head1 COPYRIGHT
+
+Copyright (C) 2005-2008  Joshua Hoblitt.  All rights reserved.
+
+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 of the License, or (at your option) any later
+version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along with
+this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+Place - Suite 330, Boston, MA  02111-1307, USA.
+
+The full text of the license can be found in the L<perlgpl> Pod as supplied
+with Perl 5.8.1 and later.
+
+=head1 SEE ALSO
+
+L<Nebulous::Server>, L<neb-initdb>, L<neb-df>, L<nebdiskd>
+
+=cut
Index: /trunk/Nebulous/lib/Nebulous/Server/SQL.pm
===================================================================
--- /trunk/Nebulous/lib/Nebulous/Server/SQL.pm	(revision 16263)
+++ /trunk/Nebulous/lib/Nebulous/Server/SQL.pm	(revision 16264)
@@ -1,5 +1,5 @@
 # Copyright (c) 2004  Joshua Hoblitt
 #
-# $Id: SQL.pm,v 1.45 2008-01-28 19:48:08 jhoblitt Exp $
+# $Id: SQL.pm,v 1.46 2008-01-30 00:19:06 jhoblitt Exp $
 
 package Nebulous::Server::SQL;
@@ -172,5 +172,6 @@
             storage_object.so_id,
             uri,
-            assigned_vol_id
+            assigned_vol_id,
+            available
         FROM storage_object
         JOIN instance
@@ -242,4 +243,20 @@
         SET ext_id = ?
         WHERE ext_id = ?
+    },
+    find_objects_with_unavailable_instances => qq{
+        SELECT
+            so_id,
+            ext_id,
+            count(ins_id) as instances,
+            count(mountedvol.vol_id) as available_instances,
+            count(mountedvol.vol_id) < count(ins_id) as need_recovery,
+            count(mountedvol.vol_id) > 0 as recoverable
+        FROM storage_object
+        JOIN instance
+            USING(so_id)
+        LEFT JOIN mountedvol
+            USING(vol_id)
+        GROUP BY so_id
+        HAVING need_recovery = 1
     },
 );
