#!/usr/bin/perl
#
# Copyright (c) 2000-2002 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/>.
# 
# }}}
#

# iddata_clean - script to purge the aged slothd data from the emulab
# database.

use lib '/usr/testbed/lib';

use libdb;
use English;
use Getopt::Long;
use strict;

my %opts = ();

GetOptions(\%opts,'h','m=i');

sub usage() {
    print "Usage:\n$0 -h               This message.\n".
        "$0 -m <maxage>      Maximum idle data age to leave (in seconds).\n";
}

if ($opts{'h'}) {
    exit &usage;
}

# security check.

my @whgrp = getgrnam("wheel");
my $uname = getpwuid($UID);
my $maxage = $opts{'m'} ? $opts{'m'} : 129600; # default is 36 hours

if (!TBAdmin($UID) && !($whgrp[3] =~ /$uname/)) {
    print "You must either be a testbed admin, or in the wheel group\n".
        "to run this script.\n";
    exit 1;
}

print "Idlestats cleaner script started..\n\n";

clean_node_idlestats_table();
clean_iface_counters_table();    

exit 0;

sub clean_node_idlestats_table() {
    my $qres;
    if ($qres = DBQueryFatal("delete from node_idlestats ".
                             "where unix_timestamp(now()) - ".
                             "unix_timestamp(tstamp) > $maxage")) {
        print "Number of rows deleted from node_idlestats: ". 
            $qres->numrows() ."\n";
        return 1;
    }
    return 0;    
}

sub clean_iface_counters_table() {
    my $qres;
    if ($qres = DBQueryFatal("delete from iface_counters ".
                             "where unix_timestamp(now()) ". 
                             "- unix_timestamp(tstamp) > $maxage")) {
        print "Number of rows deleted from iface_counters: ". 
            $qres->numrows() ."\n";
        return 1;
    }
    return 0;
}
