System Usage Information

Problem this snippet solves:

This code sample illustrates the usage of the new System::get_cpu_usage_information() and System::get_disk_usage_information() methods to retrieve the current usage information of the CPUs and disks on the system.

Code :

#!/usr/bin/perl
#----------------------------------------------------------------------------
# The contents of this file are subject to the "END USER LICENSE AGREEMENT FOR F5
# Software Development Kit for iControl"; you may not use this file except in
# compliance with the License. The License is included in the iControl
# Software Development Kit.
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is iControl Code and related documentation
# distributed by F5.
#
# The Initial Developer of the Original Code is F5 Networks,
# Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2004 F5 Networks,
# Inc. All Rights Reserved.  iControl (TM) is a registered trademark of F5 Networks, Inc.
#
# Alternatively, the contents of this file may be used under the terms
# of the GNU General Public License (the "GPL"), in which case the
# provisions of GPL are applicable instead of those above.  If you wish
# to allow use of your version of this file only under the terms of the
# GPL and not to allow others to use your version of this file under the
# License, indicate your decision by deleting the provisions above and
# replace them with the notice and other provisions required by the GPL.
# If you do not delete the provisions above, a recipient may use your
# version of this file under either the License or the GPL.
#----------------------------------------------------------------------------

#use SOAP::Lite + trace => qw(method debug);
use SOAP::Lite;
use MIME::Base64;
use Math::BigInt;

#----------------------------------------------------------------------------
# Validate Arguments
#----------------------------------------------------------------------------
my $sHost = $ARGV[0];
my $sUID = $ARGV[1];
my $sPWD = $ARGV[2];

if ( ($sHost eq "") or ($sUID eq "") or ($sPWD eq "") )
{
die ("Usage: SystemUsage.pl host port uid pwd\n");
}

#----------------------------------------------------------------------------
# Transport Information
#----------------------------------------------------------------------------
sub SOAP::Transport::HTTP::Client::get_basic_credentials
{
return "$sUID" => "$sPWD";
}

sub SOAP::Deserializer::typecast
{
my ($self, $value, $name, $attrs, $children, $type) = @_;
my $retval = undef;
if ( "{urn:iControl}Common.StatisticType" eq $type ) { $retval = $value; }
return $retval;
}

$SystemInfo = SOAP::Lite
-> uri('urn:iControl:System/SystemInfo')
-> proxy("https://$sHost/iControl/iControlPortal.cgi");
eval { $SystemInfo->transport->http_request->header
(
'Authorization' => 
'Basic ' . MIME::Base64::encode("$sUID:$sPWD", '')
); };


print "\n                    *** System Usage Information ***\n\n";
&getCPUUsage();
&getCPUExtendedUsage();
&getDiskUsage();
&getMemoryUsage();


#----------------------------------------------------------------------------
# sub getCPUUsage
#----------------------------------------------------------------------------
sub getCPUUsage()
{
$soapResponse = $SystemInfo->get_cpu_usage_information();
&checkResponse($soapResponse);

$CPUUsageInformation = $soapResponse->result;
@usages = @{$CPUUsageInformation->{"usages"}};
$time_stamp = $CPUUsageInformation->{"time_stamp"};
$ts = &buildTimeStamp($time_stamp);
print "                    System Time: $ts\n";

print "============================================================================\n";
print " CPU Usage Information\n";
print "============================================================================\n";
printf
"%6s %9s %9s %9s %9s %9s %9s %9s\n",
"cpu_id", "user", "niced", "system", "idle", "irq", "softirq", "iowait";
print "----------------------------------------------------------------------------\n";
foreach $CPUUsage (@usages)
{
$cpu_id = $CPUUsage->{"cpu_id"};
$user = &build64($CPUUsage->{"user"});
$niced = &build64($CPUUsage->{"niced"});
$system = &build64($CPUUsage->{"system"});
$idle = &build64($CPUUsage->{"idle"});
$irq = &build64($CPUUsage->{"irq"});
$softirq = &build64($CPUUsage->{"softirq"});
$iowait = &build64($CPUUsage->{"iowait"});

printf
"%6s %9s %9s %9s %9s %9s %9s %9s\n",
$cpu_id, $user, $niced, $system, $idle, $irq, $softirq, $iowait;
}
}

#----------------------------------------------------------------------------
# sub getCPUExtendedUsage
#----------------------------------------------------------------------------
sub getCPUExtendedUsage()
{
  $soapResponse = $SystemInfo->get_all_cpu_usage_extended_information();
  &checkResponse($soapResponse);
  
  print "============================================================================\n";
  print " CPU Usage Extended Information\n";
  print "============================================================================\n";
  $CPUUsageExtendedInformation = $soapResponse->result;
  @hosts = @{$CPUUsageExtendedInformation->{"hosts"}};
  foreach $CPUUsageExtended (@hosts)
  {
    $host_id = $CPUUsageExtended->{"host_id"};
    @statisticsAofA = @{$CPUUsageExtended->{"statistics"}};
    for $i (0 .. $#statisticsAofA)
    {
      print "CPU $i\n";
      @statisticsA = @{$statisticsAofA[$i]};
      foreach $statistic (@statisticsA)
      {
        $type = $statistic->{"type"};
        $value = &build64($statistic->{"value"});
        
        print "  $type => $value\n";
        
      }
    }
  }
}

#----------------------------------------------------------------------------
# sub getDiskUsage
#----------------------------------------------------------------------------
sub getDiskUsage()
{
$soapResponse = $SystemInfo->get_disk_usage_information();
&checkResponse($soapResponse);

$DiskUsageInformation = $soapResponse->result;
@usages = @{$DiskUsageInformation->{"usages"}};
$time_stamp = $DiskUsageInformation->{"time_stamp"};

print "\n============================================================================\n";
print " Disk Information\n";
print "============================================================================\n";
printf "%29s %24s\n",
"block", "nodes";
printf 
"%9s %9s %9s %9s %9s %9s\n",
"partition", "size", "total", "free", "total", "free";
print "----------------------------------------------------------------------------\n";
foreach $DiskUsage (@usages)
{
$partition_name = $DiskUsage->{"partition_name"};
$block_size = &build64($DiskUsage->{"block_size"});
$total_blocks = &build64($DiskUsage->{"total_blocks"});
$free_blocks = &build64($DiskUsage->{"free_blocks"});
$total_nodes = &build64($DiskUsage->{"total_nodes"});
$free_nodes = &build64($DiskUsage->{"free_nodes"});

printf 
"%9s %9s %9s %9s %9s %9s\n",
$partition_name, $block_size, $total_blocks, $free_blocks, $total_nodes, $free_nodes;
}

}

#----------------------------------------------------------------------------
# getMemoryUsage
#----------------------------------------------------------------------------
sub getMemoryUsage()
{
$soapResponse = $SystemInfo->get_memory_usage_information();
&checkResponse($soapResponse);

$MemoryUsageInformation = $soapResponse->result;
$total_memory = &build64($MemoryUsageInformation->{"total_memory"});
$used_memory = &build64($MemoryUsageInformation->{"used_memory"});
@usages = @{$MemoryUsageInformation->{"usages"}};

print "\n============================================================================\n";
print " Memory Information\n";
print "============================================================================\n";
print "Total Memory: $total_memory\n";
print " Used Memory: $used_memory\n";
print "  Subsystems (name, cur, max, size):\n";
foreach $SubsystemMemoryUsage (@usages)
{
$subsystem_name = $SubsystemMemoryUsage->{"subsystem_name"};
$current_allocated = &build64($SubsystemMemoryUsage->{"current_allocated"});
$maximum_allocated = &build64($SubsystemMemoryUsage->{"maximum_allocated"});
$size = &build64($SubsystemMemoryUsage->{"size"});

print "              $subsystem_name ($current_allocated, $maximum_allocated, $size)\n";
}
}

#----------------------------------------------------------------------------
# checkResponse
#----------------------------------------------------------------------------
sub checkResponse()
{
my ($soapResponse) = (@_);
if ( $soapResponse->fault )
{
print $soapResponse->faultcode, " ", $soapResponse->faultstring, "\n";
exit();
}
}

#----------------------------------------------------------------------------
# build64
#----------------------------------------------------------------------------
sub build64()
{
($UInt64) = (@_);
$low = $UInt64->{"low"};
$high = $UInt64->{"high"};

# For some reason this doesn't work...
#$value64 = Math::BigInt->new($high)->blsft(32)->bxor($low);
$value64 = Math::BigInt->new(Math::BigInt->new($high)->blsft(32))->bxor($low);
return $value64;
}

#----------------------------------------------------------------------------
# buildTimeStamp
#----------------------------------------------------------------------------
sub buildTimeStamp()
{
($time_stamp) = (@_);

@monthArray = (
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec',
);

my $sYear = $time_stamp->{"year"};
my $sMonth = $time_stamp->{"month"};
my $sDate = $time_stamp->{"day"};
my $sHour = $time_stamp->{"hour"};
my $sMinute = $time_stamp->{"minute"};
my $sSecond = $time_stamp->{"second"};

$ts = "@monthArray[$sMonth-1] $sDate $sHour:$sMinute:$sSecond $sYear\n";

return $ts;
}
Published Mar 09, 2015
Version 1.0
No CommentsBe the first to comment