dbcheck.in 3.42 KB
Newer Older
1
#!/usr/bin/perl -w
Leigh Stoller's avatar
Leigh Stoller committed
2
#
3
# Copyright (c) 2000-2004, 2016, 2018 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
23
#
24 25
use English;
use Getopt::Std;
Leigh Stoller's avatar
Leigh Stoller committed
26

27
#
28
# Check DB consistency.
29
#
30 31 32 33 34
sub usage() {
    print STDOUT "Usage: dbcheck [-d] [-v]\n".
	"Use the -d option to see debugging output instead of emailing it.\n";
    exit(-1);
}
35 36
sub fatal($);

37 38 39
my $optlist = "vd";
my $debug   = 0;
my $verbose = 0;
40

41
#
42
# Configure variables
43
#
44 45 46 47 48 49
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'};
50

51
#
52
# Turn off line buffering on output
53
#
54 55 56 57
$| = 1; 

# Load the Testbed support stuff.
use lib "@prefix@/lib";
58
use libdb;
59
use libtestbed;
60

61 62 63 64 65 66 67
#
# Only real root can call this.
# 
if ($UID != 0) {
    print STDERR "You must be root to run this script!\n";
    exit(-1);
}
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#
# 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++;
}
86 87

#
88
# Form a temp name.
89
#
90 91 92
my $logname = TBMakeLogname("dbcheck");
my $errors  = 0;
my $query_result;
93

94 95 96 97 98 99 100
#
# 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: $!");
101
}
102 103 104 105 106 107

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

110
while (my ($table) = $query_result->fetchrow()) {
111 112 113
    next
	if ($table eq "log");
    
114 115
    print "Checking $table ...\n";

116
    my $check_result = DBQueryWarn("check table $table medium");
117 118
    if (! $check_result) {
	fatal("Error accessing the database.");
119
    }
120 121 122
    while (my (undef, undef, $msgtype, $msgtext) = $check_result->fetchrow()) {
	if ($verbose) {
	    printf("%8s : $msgtext\n", $msgtype, $msgtext);
123 124
	    next;
	}
125 126 127 128 129 130 131
	if ($msgtype eq "error") {
	    print "*** $msgtext\n";
	    $errors++;
	}
	if ($msgtype eq "status" && $msgtext ne "OK") {
	    print "*** $msgtext\n";
	    $errors++;
132 133 134 135 136
	}
    }
}

#
137
# Send email if anything abnormal happened.
138
#
139 140 141
if (!$debug && $errors) {
    SENDMAIL($TBOPS, "Testbed DBcheck Results", "Testbed DBcheck Results",
	     $TBOPS, undef, ($logname));
142 143
}

144 145 146
unlink("$logname")
    if (-e $logname);
exit($errors);
147

148 149 150 151 152 153 154 155 156 157
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);
158
}