audit.in 11.2 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2008 University of Utah and the Flux Group.
5 6 7
# All rights reserved.
#
use English;
8
use Getopt::Std;
9
use POSIX qw(strftime);
10 11 12

#
# Audit the DB, looking for things we want to catch. Run once a day from cron.
13 14 15 16 17 18
#
sub usage() {
    print STDOUT "Usage: audit [-d]\n".
	"Use the -d option to see debugging output instead of emailing it.\n";
    exit(-1);
}
19
sub fatal($);
20 21
my $optlist = "d";
my $debug   = 0;
22 23 24 25 26 27

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
28
my $ISOLATEADMINS = @ISOLATEADMINS@;
29 30 31 32 33 34 35 36 37 38 39 40 41 42

# 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;
43
use Project;
44 45 46 47 48 49 50 51 52

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

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#
# 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++;
}

68 69 70 71 72 73 74 75 76
#
# Form a temp name.
#
my $logname = TBMakeLogname("audit");
my $query_result;

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

#
# Look for experiments running longer than 1 day, using real nodes.
#
if (! ($query_result = 
       DBQueryWarn("select e.pid,e.eid,e.expt_head_uid,expt_swapped, ".
88 89 90 91
		   "       count(r.node_id) as ncount, ".
		   "       UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(expt_swapped)".
		   "         as swapsec ".
		   "  from experiments as e ".
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
		   "left join reserved as r on e.pid=r.pid and e.eid=r.eid ".
		   "left join nodes as n on n.node_id=r.node_id ".
		   "left join node_types as nt on nt.type=n.type ".
		   "where e.state='active' and ".
		   "      e.expt_swapped < date_sub(now(), interval 1 day) and ".
		   "      nt.isvirtnode=0 and nt.isremotenode=0 ".
		   "group by e.pid,e.eid ".
		   "having ncount>0 ".
		   "order by expt_swapped desc"))) {
    fatal("Error accessing the database.");
}

if ($query_result->numrows) {
    print "\n";
    print "Experiments swapped in longer than 1 day.\n";
107 108 109 110
    printf("%-12s %-8s %-12s %-12s %-22s %s\n",
           "PID", "Hours", "EID", "Creator", "Swapped", "PCs");
    print "------------ -------- ------------ ------------ ".
	  "---------------------- ----\n";
111
    
112 113
    while (my ($pid,$eid,$creator,$swapped,$count,$seconds) =
	   $query_result->fetchrow()) {
114 115
	next
	    if ($count == 0);
116
	my $hours = int($seconds / (60 * 60));
117

118 119
	printf("%-12s %-8s %-12s %-12s %-22s %s\n",
	       $pid, $hours, $eid, $creator, $swapped, $count);
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
#
# Look for experiments stuck in transition
#
if (! ($query_result = 
       DBQueryWarn("select e.pid,e.eid,e.expt_head_uid,expt_locked, ".
		   "       count(r.node_id) as ncount, ".
		   "      UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(expt_locked)".
		   "         as locksec ".
		   "  from experiments as e ".
		   "left join reserved as r on e.pid=r.pid and e.eid=r.eid ".
		   "left join nodes as n on n.node_id=r.node_id ".
		   "left join node_types as nt on nt.type=n.type ".
		   "where e.expt_locked is not NULL and ".
		   "      e.expt_locked < date_sub(now(), interval 1 day) ".
#		   "      and nt.isvirtnode=0 and nt.isremotenode=0 ".
		   "group by e.pid,e.eid ".
		   "order by expt_locked desc"))) {
    fatal("Error accessing the database.");
}

if ($query_result->numrows) {
    print "\n";
    print "----------------------------------------------------------------\n";
    print "\n";
    print "Experiments stuck in transition for more than 1 day\n";
    printf("%-12s %-8s %-12s %-12s %-22s %s\n",
           "PID", "Hours", "EID", "Creator", "Started", "PCs");
    print "------------ -------- ------------ ------------ ".
	  "---------------------- ----\n";
    
    while (my ($pid,$eid,$creator,$locked,$count,$seconds) =
	   $query_result->fetchrow()) {
	next
	    if ($count == 0);
	my $hours = int($seconds / (60 * 60));

	printf("%-12s %-8s %-12s %-12s %-22s %s\n",
	       $pid, $hours, $eid, $creator, $locked, $count);
    }
}

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
#
# Once a week (Sunday) look for anyone that has used the wireless nodes.
#
my $today = POSIX::strftime("%u", localtime());
if ($today == 7) {
    my $query_result =
	DBQueryWarn("select e.exptidx,pid,eid,swapin_last,wirelesslans ".
		    "   from experiment_resources as r ".
		    "left join experiment_stats as e on e.exptidx=r.exptidx ".
		    "where wirelesslans!=0 and swapin_last is not null and ".
		    "UNIX_TIMESTAMP(swapin_last) > ".
		    "  UNIX_TIMESTAMP(now()) - (7 * 60 * 60 * 24) ".
		    "order by e.exptidx");
    fatal("Could not get wireless experiment stats")
	if (!$query_result);

    if ($query_result->numrows) {
	print "\n";
	print "Experiments that have used wireless nodes in the last week\n";
	printf("%-10s %-12s %-14s %-22s %-5s\n",
	       "ExptIDX", "PID", "EID", "Swapin Last", "Wlans");
	print "---------- ------------ -------------- ".
	    "---------------------- -----\n";
    
	while (my ($exptidx,$pid,$eid,$swapin_last,$wlans) =
	       $query_result->fetchrow()) {

	    printf("%-10s %-12s %-14s %-22s %-5s\n",
		   $exptidx, $pid, $eid, $swapin_last, $wlans);
	}
    }
}

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
#
# Look for free nodes in odd states.
#

if (! ($query_result = 
       DBQueryWarn("select n.node_id from nodes as n ".
		   "left join reserved as r on r.node_id=n.node_id ".
		   "where (n.eventstate!='". TBDB_NODESTATE_ISUP ."' and ".
		   "       n.eventstate!='". TBDB_NODESTATE_PXEWAIT ."' and ".
		   "       n.eventstate!='". TBDB_NODESTATE_ALWAYSUP ."' and ".
		   "       n.eventstate!='". TBDB_NODESTATE_POWEROFF ."') ".
		   " and r.pid is null and n.role='testnode'"))) {
    fatal("Error accessing the database.");
}

if ($query_result->numrows) {
    print "\n";
    print "----------------------------------------------------------------\n";
    print "\n";
    print "'Free' nodes in an unknown state:\n";
    
    while (my ($node_id) = $query_result->fetchrow()) {
	print "$node_id ";
    }
    print "\n";
}

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
#
# Look for real nodes in hwdown.
#
$DPID = NODEDEAD_PID();
$DEID = NODEDEAD_EID();

if (! ($query_result = 
       DBQueryWarn("select r.node_id,rsrv_time from reserved as r ".
		   "left join nodes as n on n.node_id=r.node_id ".
		   "left join node_types as nt on nt.type=n.type ".
		   "where r.pid='$DPID' and r.eid='$DEID' and ".
		   "      nt.isremotenode=0 ".
		   "order by rsrv_time desc"))) {
    fatal("Error accessing the database.");
}

if ($query_result->numrows) {
    print "\n";
    print "----------------------------------------------------------------\n";
    print "\n";
244
    print "Local nodes stuck in $DPID/$DEID:\n";
245 246 247 248 249 250 251
    
    while (my ($node_id) = $query_result->fetchrow()) {
	print "$node_id ";
    }
    print "\n";
}

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
#
# Look for pre-reserved nodes.
#

if (! ($query_result = 
       DBQueryWarn("select reserved_pid,type,count(*) from nodes ".
		   "where reserved_pid is not null ".
		   "group by reserved_pid,type"))) {
    fatal("Error accessing the database.");
}

if ($query_result->numrows) {
    print "\n";
    print "----------------------------------------------------------------\n";
    print "\n";
    print "Pre-reserved nodes:\n";
    
    printf("%-16s %-16s %-16s\n", "PID", "Type", "Count");
    print "---------------- ---------------- ---------------- \n";
    while (my ($pid,$type,$count) = $query_result->fetchrow()) {
	printf("%-16s %-16s %-16s\n", $pid, $type, $count);
    }
    print "\n";
}

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
#
# Look for stuck checkups.
#

if (! ($query_result = 
       DBQueryWarn("select object,type,state,start from active_checkups ".
		   "where start < DATE_SUB(NOW(), INTERVAL 1 DAY)"))) {
    fatal("Error accessing the database.");
}

if ($query_result->numrows) {
    print "\n";
    print "----------------------------------------------------------------\n";
    print "\n";
    print "Checkups running for more than a day:\n";
    
    printf("%-16s %-16s %-16s %-22s\n", "Object", "Type", "State", "Start");
    print "---------------- ---------------- ---------------- ".
	"--------------------\n";
    while (my ($object,$type,$state,$start) = $query_result->fetchrow()) {
	printf("%-16s %-16s %-16s %-20s\n", $object, $type, $state, $start);
    }
    print "\n";
}

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
#
# Look for inconsistent resource records.
#
if (! ($query_result =
       DBQueryWarn("select e.idx,r.idx,e.pid,e.eid,".
		   "    e.expt_head_uid,UNIX_TIMESTAMP(r.swapin_time) ".
		   " from experiment_resources as r ".
		   "left join experiments as e on e.idx=r.exptidx ".
		   "  where e.state='swapped' and swapin_time!=0 and ".
		   "        swapout_time=0 and swapmod_time=0 and pnodes>0"))){
    fatal("Error accessing the database.");
}

if ($query_result->numrows) {
    print "\n";
    print "----------------------------------------------------------------\n";
    print "\n";
    print "Experiments with inconsistent experiment_resource records:\n";
    printf("%-12s %-12s %-8s %-12s %-22s %s\n",
           "PID", "EID", "IDX", "Creator", "Started", "RIDX");
    print "------------ ------------ -------- ------------ ------------ ".
	  "---------------------- -------\n";
    
    while (my ($exptidx,$idx,$pid,$eid,$creator,$swapin_time) =
	   $query_result->fetchrow()) {

	printf("%-12s %-12s %-8s %-12s %-22s %s\n",
	       $pid, $eid, $exptidx, $creator, $swapin_time, $idx);
    }
}

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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
#
# Looks for admin/nonadmin mix users in projects.
#
if ($ISOLATEADMINS) {
    $query_result =
	DBQueryWarn("select pid_idx from projects where approved=1");

    fatal("Error accessing the database.")
	if (! $query_result);

    my @badlist = ();

    while (my ($pid_idx) = $query_result->fetchrow()) {
	my $project = Project->Lookup($pid_idx);
	fatal("Could not look up project $pid_idx in DB")
	    if (!defined($project));
	my @members;

	if ($project->GetProjectGroup()->MemberList(\@members,
				    $Group::MEMBERLIST_FLAGS_ALLUSERS)) {
	    fatal("Could not look up members for project $pid_idx")
	}
	next
	    if (@members == 0);
	
	$adminflag = $members[0]->admin();
	foreach my $user (@members) {
	    if ($user->admin() != $adminflag) {
		#
		# This gets special email.
		#
		if ($debug) {
		    print "Bad mix of admin AND non-admin users in $project\n";
		}
		else  {
		    push(@badlist, $project);
		}
		last;
	    }
	}
    }
    if (@badlist && !$debug) {
	my $msg = "The following projects have an unsafe mix of admin and ".
	    "non-admin users:\n\n@badlist\n";

	SENDMAIL($TBOPS, "Testbed Admin Users Check", $msg, $TBOPS);
    }
}

382 383 384 385 386 387
#
# Age any login entries that have timed out.
#
DBQueryWarn("delete from login ".
	    "where (unix_timestamp(now()) - timeout) > (12 * 60 * 60)");

388 389 390
#
# Send email if anything was reported.
#
391
if (!$debug && -s $logname) {
392 393 394 395
    SENDMAIL($TBOPS, "Testbed Audit Results", "Testbed Audit Results",
	     $TBOPS, undef, ($logname));
}

396 397
unlink("$logname")
    if (-e $logname);
398 399 400 401 402 403 404
exit 0;

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

    print STDERR "$msg\n";
    SENDMAIL($TBOPS, "Testbed Audit Failed", $msg, undef, undef, ($logname));
405 406
    unlink("$logname")
	if (-e $logname);
407 408
    exit(1);
}