Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Other documents in the Knowlede Base showing how to produce equivalent results have been written for PHP 7 (which produces HTML output), PowerShell and for PythonThe REST API allows users to query the iQSonar results directly using the web client protocol. The results are returned in JSON formatThis article is part of a series on how to make use of the Rest API.

This worked example uses PERL to produce a CSV file containing details of devices directly from the scan results. We look for the host name, the total installed RAM and the CPU type. The device name will be listed in the "device" results. For the CPU Type and the installed RAM we need to then go to the details page for the device.

...

Code Block
languageperl
titleGet the list of devicescollapsetrue
#!/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

...

Code Block
languageperl
titleIterate over devicescollapsetrue
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)" ;
    }
    … and so on …
}

...

Code Block
languageperl
titleGet extra details for devicescollapsetrue
my $urlSelf = $device->{'self'} ;
    $selfResults = $browser->get($urlSelf) ;
    if ( $selfResults ->is_success )
    {
        my $deviceDetails = decode_json( $selfResults->content ) ;
        if ( defined $deviceDetails -> {'cpu_count'} )
        {
            print $deviceDetails -> {'cpu_count'} , ",";
        } else {
            print "(no cpu count), " ;
        }
        if (defined $deviceDetails->{'cpu'}  )
        {
            # CPU info is an ARRAY - let's just report on the first one
            # Also, CPU model can contain a comma, so enclose in quotes
            print '"';
            print $deviceDetails->{'cpu'}->[0]->{'cpu_model'};
            print '"';
        } else {
            print "(No CPU Model)";
        }
        … and so on …
    }

...

Code Block
languageperl
linenumberstruecollapsetrue
#!/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";
}

...