dbcheck.in 2.8 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2004 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5 6
# All rights reserved.
#
7 8
use English;
use Getopt::Std;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
9

10
#
11
# Check DB consistency.
12
#
13 14 15 16 17
sub usage() {
    print STDOUT "Usage: dbcheck [-d] [-v]\n".
	"Use the -d option to see debugging output instead of emailing it.\n";
    exit(-1);
}
18 19
sub fatal($);

20 21 22
my $optlist = "vd";
my $debug   = 0;
my $verbose = 0;
23

24
#
25
# Configure variables
26
#
27 28 29 30 31 32
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
33

34
#
35
# Turn off line buffering on output
36
#
37 38 39 40
$| = 1; 

# Load the Testbed support stuff.
use lib "@prefix@/lib";
41
use libdb;
42
use libtestbed;
43

44 45 46 47 48 49 50
#
# Only real root can call this.
# 
if ($UID != 0) {
    print STDERR "You must be root to run this script!\n";
    exit(-1);
}
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV) {
    usage();
}
if (defined($options{"d"})) {
    $debug++;
}
if (defined($options{"v"})) {
    $verbose++;
}
69 70

#
71
# Form a temp name.
72
#
73 74 75
my $logname = TBMakeLogname("dbcheck");
my $errors  = 0;
my $query_result;
76

77 78 79 80 81 82 83
#
# Reopen both stdout and stderr so that we can record all the output for
# later mailing.
#
if (! $debug) {
    open(STDERR, ">> $logname") or die("opening $logname for STDERR: $!");
    open(STDOUT, ">> $logname") or die("opening $logname for STDOUT: $!");
84
}
85 86 87 88 89 90

#
# Check all tables to make sure none are corrupted.
#
if (! ($query_result = DBQueryWarn("show tables"))) {
    fatal("Error accessing the database.");
91
}
92

93 94 95 96 97
while (my ($table) = $query_result->fetchrow()) {
    print "Checking $table ...\n";

    # virt_routes is too big for this.
    my $checktype = ($table eq "virt_routes" ? "medium" : "extended");
98

99 100 101
    my $check_result = DBQueryWarn("check table $table $checktype");
    if (! $check_result) {
	fatal("Error accessing the database.");
102
    }
103 104 105
    while (my (undef, undef, $msgtype, $msgtext) = $check_result->fetchrow()) {
	if ($verbose) {
	    printf("%8s : $msgtext\n", $msgtype, $msgtext);
106 107
	    next;
	}
108 109 110 111 112 113 114
	if ($msgtype eq "error") {
	    print "*** $msgtext\n";
	    $errors++;
	}
	if ($msgtype eq "status" && $msgtext ne "OK") {
	    print "*** $msgtext\n";
	    $errors++;
115 116 117 118 119
	}
    }
}

#
120
# Send email if anything abnormal happened.
121
#
122 123 124
if (!$debug && $errors) {
    SENDMAIL($TBOPS, "Testbed DBcheck Results", "Testbed DBcheck Results",
	     $TBOPS, undef, ($logname));
125 126
}

127 128 129
unlink("$logname")
    if (-e $logname);
exit($errors);
130

131 132 133 134 135 136 137 138 139 140
sub fatal($) {
    my ($msg) = @_;

    print STDERR "*** $0:\n".
	         "    $msg\n";
    
    SENDMAIL($TBOPS, "Testbed DBcheck Failed", $msg, undef, undef, ($logname));
    unlink("$logname")
	if (-e $logname);
    exit(1);
141
}