freenodes.in 3.36 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
#!/usr/bin/perl 
#
# Copyright (c) 2009-2015 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 English;
use Getopt::Std;
use Data::Dumper;
use Carp;
use POSIX;
use strict;
use warnings;

our @free_types;
require "@prefix@/etc/node_usage.conf";

# Load the Testbed support stuff.
use lib "@prefix@/lib";
use emdb;
use EmulabConstants;

chdir "@prefix@/data/node_usage";

my $date = POSIX::strftime("%m/%d/20%y %H:%M:%S %Z", localtime(time()));

#
# Initialize the counts by type.
#
my %total = ();
my %free  = ();

foreach my $type (@free_types) {
    my $count = 0;
    
    my $query_result =
	DBQueryWarn("select count(*) from nodes where type='$type'");
    if ($query_result) {
	($count) = $query_result->fetchrow_array();
    }
    $total{$type} = $count;

    $count = 0;
    $query_result =
	DBQueryFatal("select count(a.node_id) from nodes as a ".
		     "left join reserved as b on a.node_id=b.node_id ".
		     "where b.node_id is null and a.type='$type' and ".
                     "    a.reserved_pid is null and ".
                     "    (a.eventstate='" . TBDB_NODESTATE_ISUP . "' or ".
                     "     a.eventstate='" . TBDB_NODESTATE_POWEROFF . "' or ".
                     "     a.eventstate='" . TBDB_NODESTATE_PXEWAIT . "')");
    if ($query_result) {
	($count) = $query_result->fetchrow_array();
    }
    $free{$type} = $count;
}
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

#
# Create a csv file.
#
open(DAT, ">freenodes.csv")
    or die("Could open freenodes.csv for writing");
print DAT "Type,Inuse,Free\n";
foreach my $type (@free_types) {
    my $free  = $free{$type};
    my $inuse = $total{$type} - $free{$type};
    
    print DAT "$type,$inuse,$free\n";
}
close(DAT);

system("cp -fp freenodes.csv @prefix@/www/node_usage/");

if (! -x "/usr/local/bin/gnuplot") {
    print STDERR "gnuplot is not installed, bailing without creating plots.\n";
    exit(1);
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
open(DAT, ">freenodes.dat")
    or die("Could open freenodes.dat for writing");
print DAT "Type\tInuse\tFree\n";
foreach my $type (@free_types) {
    my $free  = $free{$type};
    my $inuse = $total{$type} - $free{$type};
    
    print DAT "$type\t$inuse\t$free\n";
}
close(DAT);

open(PLOT, "|/usr/local/bin/gnuplot")
    or die("Could not start gnuplot");

print PLOT "set terminal svg\n";
print PLOT "set output 'freenodes.svg'\n";
print PLOT "set title '$date'\n";
113
print PLOT "set boxwidth 0.5 absolute\n";
114 115 116 117 118 119
print PLOT "set style fill solid 1 border lt -1\n";
print PLOT "set style data histogram\n";
print PLOT "set style histogram rowstacked\n";
print PLOT "plot 'freenodes.dat' using 2 ti col, '' using 3:xtic(1) ti col\n";
close(PLOT)
    or die("Error running gnuplot");
120

121
system("cp -fp freenodes.svg @prefix@/www/node_usage/");