Versions Compared

Key

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

...

So, the PERL code to connect to the server and get the devices is as follows:


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

...

Now we’re producing a CSV file from the device list… so for each key/value pair we test it is defined, and print out either a result or a placeholder showing there is no result.

Code Block
languageperl
titleIterate over devices
collapsetrue
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 …

...


}


Step Three: For each device, get the extra information we’re interested in

Let’s say that we want to know the CPU details – CPU Count, CPU Type, and No of Cores. (Useful for licencing queries)

...