Versions Compared

Key

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

The REST API allows users to query the iQSonar results directly using the web client protocol. The results are returned in JSON format. This article is part of a series on how to make use of the Rest API.

This worked example uses Python 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.

Other documents in the Knowlede Base showing how to produce equivalent results have been written for PHP 7 (which produces HTML output), PERL and for PowerShell

A longer worked example in python can be found here.

Pre-requsites

  • NOTE: both Python 2 and Python 3 are actively used in different environments. This example uses Python 3 syntax.
  • You need to know the URL for you iQSonar install - in this example our host is

...

  • iqsonar-

...

  • host
  • You need credentials for the iQSonar install - in this example we use the default login admin /

...

  • password Please update this to the correct values for your site.
  • You need the  "requests" module for Python which handles web requests very nicely. If this is not installed on your system you can install it as follows:

On Linux or macOS:

Code Block
pip install -U pip requests

...

Code Block
languagepy
titleConnect to host
import requests

r = requests.get('http://vmiqsonar-mike-2012bhost/api/v1/devices',auth=('admin','password'))
max = r.headers['X-fetch-count']
data = r.json()
count = len(data)

By default the REST API "/api/v1/devices" page will return 200 devices at a time. We can increase or decrease this using the "fetch_size" parameter. The variable max contains the total number of devices. If this is more than 200 we would need to fetch a second batch - see other worked examples in this series for more details on that. In this example we will print at most 200. data holds the RestAPI data converted into a Python data structure, count is the number of devices returned. Also note that if you specify incorrect credentials, Python will throw an exception when you call the r.headers method.

At this point in the script, we have a variable data which contains the JSON output from the Rest API. 

The data structure is a JSON array of objects, each object contains an array of device attributes:

  • device_id
  • host_name
  • serial_number
  • manufacturer
  • model
  • last_scan
  • self

The “self” attribute is particularly interesting, as it is a URL that gives the link to more details about the device. Also note that if the device in question does not have a value, the key is omitted.

(Note that by default the REST API returns a list of 200 devices at a time. Use the off_set and fetch_size parameters to page through a longer list of devices.

Step Two - print out Hostname, RAM and CPU details for each device

...

info
Code Block
languagepy
titleCompleted Script
collapsetrue
#
# RestAPI duplicate the PowerShell example in Python
#
import requests
# Set this to your own host name/login/password)
r = requests.get('http://vmiqsonar-mike-2012bhost/api/v1/devices',auth=('admin','password'))
max = r.headers['X-fetch-count']
data = r.json()
count = len(data)
i = 0
print ('Host,RAM,CPU Type')
while (i < count):
  row = data[i]
  if ( 'host_name' in row ):
    hostname = row['host_name']
  else:
    hostname = '(no hostname)'
  url2 = row['self']
  r2 = requests.get(url2,auth=('admin','password'))
  device = r2.json()
  if ('total_memory_mb' in device):
    ram = device['total_memory_mb']
  else:
    ram = '(unknown ram)'
  if ('cpu' in device):    
    cpu = device['cpu'][0]['cpu_model']
    # remove commas from the cpu string so as to keep the CSV output valid
    cpu = cpu.replace(',','')
  else:
    cpu = '(unknown cpu)'  
  print (hostname,',',ram,',',cpu,sep='')
  i+=1

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

...