#!/bin/perl -w
# Perl script to access JSON data from iQSonar REST API
use LWP; # Module to get web page content
use JSON::XS ; # Module to parse JSON
my $iQSonar = 'vm-mike-2k12a.iqdomain.comiqsonar-host:80'; # URL of the REST API for the iQSonar instance, for credentials
my $realm = 'iQSonar RestAPI Realm';
my $browser = LWP::UserAgent->new; # need to get a web page
$browser -> credentials($iQSonar,$realm,'admin'=>'password');
my $url = 'http://vm-mike-2k12a.iqdomain.comiqsonar-host/api/v1/devices';
my $DeviceList = $browser->get($url);
die "Error: unable to get REST API results"
unless $DeviceList->is_success;
# The variable $DeviceList now contains the JSON output list of known devices
my @ParsedDeviceList = @{decode_json( $DeviceList->content ) }; # Parse into a PERL Array of objects
print "HostName,Serial No,Manufacturer,Model,VM,CPU Count,Core Count,Total Ram,CPU Model \n";
foreach $device ( @ParsedDeviceList )
{
if ( defined $device->{'host_name'} )
{
print $device->{'host_name'} ;
} else {
print "(no hostname)";
} print ",";
if ( defined $device->{'serial_number'})
{
print $device->{'serial_number'};
} else {
print "(no s/n)" ;
}
print ",";
if (defined $device->{'manufacturer'})
{
# The VMWare manufacturer string contains a comma, so output this in double quotes to keep this as valid CSV
print '"', $device->{'manufacturer'}, '"';
} else {
print "(no mfg)" ;
} print ",";
if ( defined $device->{'model'} )
{
# SUN models can contain a comma
print '"', $device->{'model'} , '"';
} else {
print "(no model)" ;
}
print ",";
my $urlSelf = $device->{'self'} ;
$selfResults = $browser->get($urlSelf) ;
if ( $selfResults ->is_success )
{
my $deviceDetails = decode_json( $selfResults->content ) ;
if ( $deviceDetails -> {'is_virtual'} )
{
print "True,";
} else {
print "False," ;
} # VM or physical host?
if ( defined $deviceDetails -> {'cpu_count'} )
{
print $deviceDetails -> {'cpu_count'} , ",";
}
else {
print "(no cpu count), " ;
}
if ( defined $deviceDetails -> {'core_count'} )
{
print $deviceDetails -> {'core_count'} , ",";
}
else {
print "(no core count), " ;
}
if (defined $deviceDetails->{'total_memory_mb'})
{
print $deviceDetails->{'total_memory_mb'} , ",";
}
else {
print "(No RAM), ";
}
if (defined $deviceDetails->{'cpu'} )
{
# CPU info is an ARRAY - but let's just report on the first one
# Also, CPU model can contain a comma
print '"', $deviceDetails->{'cpu'}->[0]->{'cpu_model'} , '"';
}
else {
print "(No CPU Model)";
}
}
else {
print " ** Unable to query device details ** ";
}
print "\n";
} |