logfile.in 7.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 103 104 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 176 177 178 179 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
#!/usr/bin/perl -w
#
# Copyright (c) 2003-2017 University of Utah and the Flux Group.
# 
# {{{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/>.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;
use Data::Dumper;
use Date::Parse;
use Time::Local;

#
# Show some logfile table stuff.
#
sub usage()
{
    print STDERR "Usage: logfile <logid | logidx | filename>";
    print STDERR "Options:\n";
    print STDERR " -d       - Turn on debugging\n";
    print STDERR " -o       - List all open logfiles.\n";
    print STDERR " -n       - Impotent mode; just say what will happen.\n";
    print STDERR " -C       - Clean up (close/store) stale logfiles.\n";
    print STDERR " -P       - Prune some logs (sliverstatus,deletesliver).\n";
    exit(-1);
}
my $optlist   = "oCndP";
my $debug     = 0;
my $impotent  = 0;
my $open      = 0;
my $clean     = 0;
my $prune     = 0;

# Protos
sub ShowOpen();
sub CleanOpenLogfiles(@);
sub ShowLogfiles(@);
sub PruneLogfiles();
sub fatal($);

#
# Configure variables
#
my $TB          = "@prefix@";
my $SUDO        = "/usr/local/bin/sudo";
my $FSTAT       = "/usr/bin/fstat";
my $CHMOD       = "/bin/chmod";

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use emdb;
use emutil;
use Logfile;
use GeniHRN;
use Group;

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

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:/usr/local/bin";

#
# Parse command arguments. 
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{'h'})) {
    usage();
}
if (defined($options{'d'})) {
    $debug = 1;
}
if (defined($options{'n'})) {
    $impotent = 1;
}
if (defined($options{'o'})) {
    $open = 1;
}
if (defined($options{'C'})) {
    $clean = 1;
}
if (defined($options{'P'})) {
    $prune = 1;
}

if ($open) {
    exit(ShowOpen());
}
elsif ($clean) {
    exit(CleanOpenLogfiles(@ARGV));
}
elsif ($prune) {
    exit(PruneLogfiles());
}
elsif (@ARGV) {
    exit(ShowLogfiles(@ARGV));
}
exit(0);

#
# List all open files.
#
sub ShowOpen()
{
    my $query_result =
	DBQueryWarn("select logid from logfiles where isopen!=0 ".
		    "order by date_created desc");
    while (my ($logid) = $query_result->fetchrow_array()) {
	my $logfile = Logfile->Lookup($logid);
	next
	    if (!defined($logfile));
	next
	    if (! -e $logfile->filename());
	
	my $filename = $logfile->filename();
	print "$logid: $filename\n";
    }
    return 0;
}

#
# Clean up open logfiles.
#
sub CleanOpenLogfile($)
{
    my ($logid) = @_;

    Logfile->LockTables();
	
    my $logfile = Logfile->Lookup($logid);
    if (!defined($logfile)) {
	Logfile->UnlockTables();
	return 0;
    }
    # Closed before we locked.
    if (! $logfile->isopen()) {
	Logfile->UnlockTables();
	return 0;
    }
    # Anything younger then 48 hours, leave for debugging.
    my $created = str2time($logfile->date_created());
    if (defined($created) && $created > (time() - (3600 * 48))) {
	print "Skipping newish $logfile\n" if ($debug);
	Logfile->UnlockTables();
	return 0;
    }
    #
    # No actual file, just mark it closed and clear the filename.
    #
    if (! -e $logfile->filename()) {
	if ($impotent) {
	    print "Would clean $logfile - " .
		$logfile->date_created() . "\n";
	}
	else {
	    $logfile->Clean();
	}
	Logfile->UnlockTables();
	return 0;
    }
    my $filename = $logfile->filename();
    my $fstat = emutil::ExecQuiet("$SUDO $FSTAT $filename");
    if ($?) {
	print $fstat;
	Logfile->UnlockTables();
	return 0;
    }
    my $skip = 0;
    foreach my $line (split(/\n/, $fstat)) {
	# Some kind of warning from fstat, skip to be safe.
	if ($line =~ /^fstat:/) {
	    $skip = 1;
	    last;
	}
	# In use.
	if ($line =~ /${filename}$/) {
	    print "$line\n" if ($debug);
	    $skip = 1;
	    last;
	}
    }
    if ($skip) {
	Logfile->UnlockTables();
	return 0;
    }
    #
    # Zero size file, just clean it up.
    #
    print "Stale: $logfile, " . $logfile->date_created() . ", $filename\n";	
    if (! -s $filename) {
	if ($impotent) {
	    print "Would clean zero length logfile\n";
	}
	else {
	    $logfile->Clean();
	}
    }
    else {
	#
	# Store it. Safest thing to do. But need to make sure we
	# can read the file.
	#
	if ($impotent) {
	    print "Would close/store logfile\n";
	}
	else {
	    if (system("$SUDO $CHMOD 666 $filename") == 0) {
		if ($logfile->Store() == 0) {
		    # We probably did not have access to delete original
		    # file, so do that with super powers
		    system("$SUDO /bin/rm -f $filename");
		}
		else {
		    print STDERR "*** Could not store $logfile\n";
		}
	    }
	}
    }
    Logfile->UnlockTables();
    return 0;
}

sub CleanOpenLogfiles(@)
{
    if (@_) {
	my @logids = @_;
	
	while (@logids) {
	    my $logid = pop(@logids);
	    CleanOpenLogfile($logid);
	}

    }
    else {
	my $query_result =
	    DBQueryWarn("select logid from logfiles where isopen!=0 ".
			"order by date_created desc");
	while (my ($logid) = $query_result->fetchrow_array()) {
	    CleanOpenLogfile($logid);
	}
    }
}

#
# Display one or more logfiles.
#
sub ShowLogfiles(@)
{
    my @ids = @_;

    while (@ids) {
	my $id = pop(@ids);
	my $logfile = Logfile->Lookup($id);
	if (!defined($logfile)) {
	    print STDERR "No such logfile $id\n";
	    next;
	}
	my $group = Group->Lookup($logfile->gid_idx());
	my $pid   = $group->pid();
	my $gid   = $group->gid();
	print "ID:       " . $logfile->logid() . "\n";
	print "IDX:      " . $logfile->logidx() . "\n";
	print "Created:  " . $logfile->date_created() . "\n";
	print "Filename: " . $logfile->filename() . "\n";
	print "Open?:    " . $logfile->isopen() . "\n";
	print "Pid/Gid:  " . "$pid/$gid" . "\n";
	print "Stored?:  " . $logfile->stored() . "\n";
	print "Zipped?:  " . $logfile->compressed() . "\n";
	if ($logfile->Metadata()) {
	    print "Metadata:\n";
	    foreach my $el (@{$logfile->MetadataList()}) {
		my ($key,$val) = @{$el};
		print "$key => $val\n";
	    }
	}
    }
}

#
# Prune logfiles we do not care about; for now these are deletesliver
# logs where the slice was busy, and sliverstatus calls.
#
sub PruneLogfiles()
{
    my $query_result =
	DBQueryFatal("select m.logidx,l.logid from logfile_metadata as m ".
		     "left join logfiles as l on m.logidx=l.logidx ".
		     "where metakey='Method' and metaval='SliverStatus' and ".
		     "      l.filename is null");
    while (my ($logidx,$logid) = $query_result->fetchrow_array()) {
	my $logfile = Logfile->Lookup($logid);
	next
	    if (!defined($logfile));
	if ($impotent) {
	    print "Would delete $logfile\n";
	}
	else {
	    $logfile->Delete();
	}
    }
    $query_result =
	DBQueryFatal("select m.logidx,l.logid from logfile_metadata as m ".
		     "left join logfiles as l on m.logidx=l.logidx ".
		     "where metakey='Method' and metaval='DeleteSliver' and ".
		     "      l.filename is null");
    while (my ($logidx,$logid) = $query_result->fetchrow_array()) {
	my $logfile = Logfile->Lookup($logid);
	next
	    if (!defined($logfile));
	# Only kill busy and no such slice logs.
	if (!exists($logfile->Metadata()->{"Error"}) ||
	    ($logfile->Metadata()->{"Error"} ne "14" &&
	     $logfile->Metadata()->{"Error"} ne "12")) {
	    print "Skipping $logfile\n";
	    next
	}
	if ($impotent) {
	    print "Would delete $logfile\n";
	}
	else {
	    $logfile->Delete();
	}
    }
}

sub fatal($)
{
    my ($mesg) = $_[0];

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