check-shared-bw.in 3.87 KB
Newer Older
1
#!/usr/bin/perl -w                                                              
2 3
#
# Copyright (c) 2008-2011 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 23 24 25 26 27 28
# 
# {{{GENIPUBLIC-LICENSE
# 
# GENI Public License
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
# 
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
# 
# }}}
29 30 31 32
#
use strict;
use English;
use Getopt::Std;
33

34
#
35 36
# Checks to see if the bandwidth remaining on shared interfaces
# matches the maximum capacity - used capacity for those interfaces
37
#
38 39 40
sub usage()
{
    print "Usage: check-shared-bw [-d] [-f]\n";
41 42
    exit(1);
}
43 44 45 46
my $optlist    = "df";
my $debug      = 0;
my $shouldFix  = 0;

47 48

our $TB = "@prefix@";
49

50
use lib "@prefix@/lib";
51
use libdb;
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
use libaudit;

#
# Check args.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"f"})) {
    $shouldFix = 1;
}
usage()
    if (@ARGV);

# Record output in case of error.
LogStart(0, undef, LIBAUDIT_LOGTBOPS());
72 73 74 75

# Get max bandwidth for interface types on all matching nodes

# Key is node:iface
76
my %iface_bw = ();
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
my $dbresult = DBQueryFatal("select i.node_id, i.iface, t.max_speed " .
			    "from interfaces as i " .
			    "left join interface_types as t " .
			    "  on i.interface_type=t.type ".
			    " left join reserved as r on i.node_id=r.node_id ".
			    "where r.erole='sharedhost'");
while (my ($node, $iface, $max_speed) = $dbresult->fetchrow_array) {
    $iface_bw{"$node:$iface"} = $max_speed;
}

# Find all vinterface usage
$dbresult = DBQueryFatal("select v.node_id, v.iface, v.bandwidth ".
			 "from vinterfaces as v ".
			 "left join reserved as r on v.node_id=r.node_id ".
			 "where r.erole='sharedhost'");
while (my ($node, $iface, $bw) = $dbresult->fetchrow_array) {
93 94 95 96 97
    if (defined($iface)) {
	my $key = "$node:$iface";
	if (exists($iface_bw{$key})) {
	    $iface_bw{$key} -= $bw;
	}
98 99 100 101
    }
}

# Find remaining bandwidth for interfaces
102
my %iface_remaining = ();
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
$dbresult = DBQueryFatal("select i.node_id, i.iface, i.remaining_bandwidth ".
			 "from interface_state as i ".
			 "left join reserved as r on i.node_id=r.node_id ".
			 "where r.erole='sharedhost'");
while (my ($node, $iface, $bw) = $dbresult->fetchrow_array) {
    $iface_remaining{"$node:$iface"} = $bw;
}

foreach my $key (sort(keys(%iface_remaining))) {
    if (! exists($iface_bw{$key})) {
	print STDERR "$key is in interface_state, but not " .
	    "interfaces.\n";
    } elsif ($iface_bw{$key} != $iface_remaining{$key}) {
	print STDERR "$key\thas\t" . $iface_remaining{$key} .
	    "\tshould have\t" . $iface_bw{$key} . "\n";
118
	if ($shouldFix) {
119
	    print STDERR "Fixing interface ...\n";
120 121 122 123 124 125 126
	    my ($node, $iface) = split(":", $key);
	    my $query = "update interface_state " .
		"set remaining_bandwidth=" . $iface_bw{$key} . " " .
		"where node_id='$node' and iface='$iface'";
	    print "Running: " . $query . "\n";
	    DBQueryFatal($query);
	}
127 128 129
    }
}

130 131 132 133 134
print STDERR scalar(keys(%iface_remaining)) . " interfaces analyzed\n"
    if ($debug);

exit(0);