Versions Compared

Key

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

...

Code Block
languageperl
titleGet the list of devices
collapsetrue
#!/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.com: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.com/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

...

We take the value from the “self” key and look up another result from the server

...

Code Block
languageperl
titleGet extra details for devices
collapsetrue
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 …

...


    }


Source code for the completed script

...

Code Block
languageperl
linenumberstrue
collapsetrue
#!/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.com: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.com/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";
}


Related articles

Filter by label (Content by label)
showLabelsfalse
max5
spacescom.atlassian.confluence.content.render.xhtml.model.resource.identifiers.SpaceResourceIdentifier@1fb5c7
showSpacefalse
sortmodified
reversetrue
typepage
cqllabel = "restapi" and type = "page" and space = "CSKB"
labelsRestAPI

...