dumperrorlog.in 5.12 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2005 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use Getopt::Std;

#
# Check DB consistency.
#
sub usage() {
    print STDOUT "Usage: dumperrorlog [-d]\n";
    exit(-1);
}
my $optlist  = "d";
18
my $debug    = 0;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
my $tempfile = "/var/tmp/testbed_dumperrorlog_tempfile";

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $ISMAINSITE  = @TBMAINSITE@;
my $USERNODE	= "@USERNODE@";
my $PROXY       = "$TB/sbin/dumperrorlog.proxy";
my $SCP         = "/usr/bin/scp";
my $SSHTB	= "$TB/bin/sshtb";

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

#
# Turn off line buffering on output
#
$| = 1; 

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

# Protos
sub fatal($);

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

#
# Form a temp name.
#
my $logname = TBMakeLogname("dumperrorlog");

#
# 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++;
}

#
# 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: $!");
}

#
# Get the last index we archived out.
#
my $query_result =
    DBQueryFatal("select idx from emulab_indicies ".
		 "where name='lastlog_seqnum'");
my ($lastseq) = $query_result->fetchrow_array();
if (!defined($lastseq)) {
    DBQueryFatal("insert into emulab_indicies (name, idx) ".
		 "values ('lastlog_seqnum', 0)");
    $lastseq = 0;
}

#
# Grab the index number for the NOTICE priority.
#
$query_result =
103
104
    DBQueryFatal("select priority from priorities ".
		 "where priority_name='NOTICE'");
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
my ($NOTICE) = $query_result->fetchrow_array();
if (!defined($NOTICE)) {
    fatal("No priority named 'NOTICE' in the priorities table!");
}

#
# Only the main site does this archiving stuff.
#
if ($ISMAINSITE) {
    #
    # Must lock the table of course
    #
    DBQueryFatal("lock tables log write");

    my $maxseq = $lastseq + 10;

    #
    # Grab all of the new entries and store to the file
    #
    DBQueryFatal("select * from log ".
		 "where seq > $lastseq " .
		 "into outfile '${tempfile}.log' ");

    #
    # Now we need to find out what the last index really was. 
    # 
    $query_result =
	DBQueryFatal("select seq from log ".
		     "where seq > $lastseq " .
		     "order by seq desc limit 1");
    # Save for later.
    $lastseq = $query_result->fetchrow_array();

    DBQueryFatal("unlock tables");

    #
    # Might not be any new entries ...
    # 
    if (defined($lastseq)) {
	system("echo \"load data infile '${tempfile}.log' into table log\" ".
	       "     | mysql errorlog");
	fatal("Could not load into archive DB!")
	    if ($?);

	# Okay, now that we have really archived them away, update the
	# seq number.
	DBQueryFatal("update emulab_indicies set idx='$lastseq' ".
		     "where name='lastlog_seqnum'");

	#
	# XXX This awful hack will go away when Kevin is happy with
	# the error logging code.
	#

	#
	# Copy the file over to ops and load it into the DB over there.
	#
	system("$SCP -p ${tempfile}.log ".
	       "     ${USERNODE}:/var/db/mysql/errorlog") == 0
	    or fatal("Could not copy $tempfile to $USERNODE!");

	system("$SSHTB -host $USERNODE $PROXY log") == 0
	    or fatal("Could not load log entries into ops DB!");
    }
    unlink("${tempfile}.log");

    #
    # Now the other tables Kevin needs
    #
    foreach my $tablename ("scripts", "priorities") {
	DBQueryFatal("select * from $tablename ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
176
		     "into outfile '${tempfile}.${tablename}' ");
177
178

	system("echo \"load data infile '${tempfile}.${tablename}' ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
179
	       "     replace into table $tablename\" | mysql errorlog");
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
	fatal("Could not load $tablename into archive DB!")
	    if ($?);

	#
	# Copy the file over to ops and load it into the DB over there.
	#
	system("$SCP -p ${tempfile}.${tablename} ".
	       "     ${USERNODE}:/var/db/mysql/errorlog") == 0
	    or fatal("Could not copy ${tempfile}.${tablename} to $USERNODE!");

	system("$SSHTB -host $USERNODE $PROXY $tablename") == 0
	    or fatal("Could not load $tablename entries into ops DB!");
	
	unlink("${tempfile}.${tablename}");
    }
}

#
# And then age out entries older then 14 days.
#
DBQueryFatal("delete from log ".
	     "where stamp < (UNIX_TIMESTAMP(now()) - (14 * 24 * 60 * 60))");

#
# And age out non critical data older then 7 days.
#
DBQueryFatal("delete from log ".
	     "where priority > $NOTICE and ".
	     "      stamp < (UNIX_TIMESTAMP(now()) - (7 * 24 * 60 * 60))");

210
211
212
unlink("$logname")
    if (-e $logname);

213
214
215
216
217
218
219
220
221
222
223
224
225
226
exit(0);

sub fatal($)
{
    my ($msg) = @_;

    SENDMAIL($TBOPS, "DumpErrorLog Failed", $msg, undef, undef, ($logname));
   
    unlink("$logname")
	if (-e $logname);

    die("*** $0:\n".
	"    $msg\n");
}