
use strict;
use warnings;
use DBI;
use Sys::Hostname;
use Data::Dumper;
use POSIX qw{floor ceil};


# set to 2 for more debug information
my $DEBUG = 0;
#hostname for system
my $host = 'rsysnbu';
# port number that database daemon/service is listening on
my $port = "3306";

my $key = '';

my $user = "jimmy";     # username for database
my $password = "superman";      #password for database

#**********************************************
# This query is similiar to the query that
# generates the Full Backups->Job Summary
# report in the client  UI.
#
# By default it shows data from the last 30
# days
#
#**********************************************

my $query = q{
select
classname, max(classid) as classid,  max(created) as created, max(active) as active
from nbclasses
group by classname
order by classname, created
};


my $dbh = DBI->connect
                (
                "dbi:mysql:nbar:$host:$port",
                "$user",
                "$password",
                ) || die "Cannot connect to NetBackup Advanced Reporter database,\n arexport requires NetBackup Advanced Reporter to be installed\n and running on $host\n please restart NetBackup Advanced Reporter: init()\n" ;
my $comma = "\t";
my $sth = $dbh->prepare ($query);
my $rv  = $sth->execute();
my $c = 0; # counter
# gets all data from query and returns it into a reference to a hash or arrays
my $table = $sth->fetchall_hashref('classid') or die "Query failure $sth->errstr\n";
my ($i,$j,%output);
#
# loop through the hash
# and print out the data
#
# <=> forces numeric sort

print "Content-type: text/html\n\n";

print "<HTML>\n";
print "<BODY >\n";
print "<PRE>\n";
print "Inactive NetBackup Classes\n\n";

foreach $key ( sort  { $table->{$a} cmp $table->{$b} } keys %$table ) {
        if ( $table->{$key}{active} > 0 ) {
                #print "class name: $table->{$key}{classname} created: $table->{$key}{created} active: $table->{$key}{active}\n";
                printf("%-30s  as of: %-10s\n",$table->{$key}{classname}, $table->{$key}{created} );
        }
}

print "<PRE>\n";
print "<BODY >\n";
print "<HTML>\n";
