#!/usr/bin/perl
#This unfinished script will poll a satellite/spacewalk server for each registered host and return the uptime of each hosts
##Start of Sample output
###########
#Host Name = centos64small01.localdomain
#last_boot = 20140426T04:42:41
#last_checkin = 20140426T08:53:03
#lastboot_epoc :1398487320
#time now :1398502383
# seconds 15063
# minutes 251.05
# hours 4.18416666666667
#
###########
#Host Name = centos64small02.localdomain
#last_boot = 20130808T01:34:21
#last_checkin = 20130808T10:18:03
#lastboot_epoc :1375925640
#time now :1398502383
# seconds 22576743
# minutes 376279.05
# hours 6271.3175
##End of Sample output
use strict;
use warnings;
use Frontier::Client;
use boolean;
use Time::Local;
#use Data::Dumper;
#use DateTime::Format::ISO8601;
#use POSIX;
my $HOST = 'spacewalk';
my $user = 'admin';
my $pass = 'password';
my $client = new Frontier::Client(url => "http://$HOST/rpc/api");
my $session = $client->call('auth.login',$user, $pass) or die;
my $systems = $client->call('system.listSystems', $session);
foreach my $system (@$systems) {
my %client = %{$system};
my $lastcheckin = ${$client{'last_checkin'}};
my $systemid = $system->{'id'};
my $hostname = $system->{'name'};
my $Detail = $client->call('system.getDetails', $session, $systemid);
my %clientdetails = %{$Detail};
my $lastboot = ${$clientdetails{'last_boot'}};
print "\n###########\n";
print "Host Name = $hostname\n";
print "last_boot = $lastboot\n";
print "last_checkin = $lastcheckin\n";
## convert the non standard ISO8601 date format to a series of variables the to epoc.
my $year = substr("$lastboot", 0,4);
my $month = substr("$lastboot", 4,2);
$month--; ## timegm counts months from 0 ie... Jan = 0 Dec =11
my $day = substr("$lastboot", 6,2);
my $hour = substr("$lastboot", 9,2);
my $min = substr("$lastboot", 12,2);
## print " year = $year\n month = $month -1\n day = $day\n hour = $hour\n min = $min\n";
my $lastboot_epoc = timegm(00,$min,$hour,$day,$month,$year);
print "lastboot_epoc :$lastboot_epoc\n";
##calculating the difference between lastboot_epoc and now
my $timenow = time;
print "time now :$timenow\n";
my $timediff = $timenow - $lastboot_epoc ;
print " seconds $timediff\n";
my $minutes = $timediff / 60 ;
my $hours = $minutes / 60 ;
print " minutes $minutes\n";
print " hours $hours\n";
}