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 list of hosts (devices) which have enterprise database applications. If the device has a database, we print off the host name, the total installed RAM and the CPU type; we then list the number and edition of the databases. The device name will be taken from the "device" results. For the CPU Type and the installed RAM we need to then go to the details page for the device; For the database details we then go into the applications page. Thus for each device we find, we make at least two and sometimes three REST API calls.

...

Code Block
languagepy
titleCompleted Script
#!/usr/bin/python3
# RestAPI example in Python
#
import requests
# Set this to your own host name/login/password)
r = requests.get('http://iqsonar-host/api/v1/devices',auth=('admin','password'))

# set some counters
noapps=0
nodb = 0
gotone=0

max = r.headers['X-fetch-count']
data = r.json()


count = len(data)
i = 0
while (i < count):
    row = data[i]
    gotone=0
        
    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)' 
        
    status = hostname + ': ' + str(ram) + 'MB RAM, ' + str(cpu)
    # Having got Hostname, Ram and CPU, lets look for databases?
    if ('applications' in device):        
        url3 = device['applications']
        r3 = requests.get(url3,auth=('admin','password'))
        # the URL might not be valid. If no applications, server will return a 500 error
        try:
            apps = r3.json()            
        except:
            noapps += 1
        else:
            numapps = len(apps)
            j=0
            while (j < numapps):
                if ('product' in apps[j]):
                        if (('SQL' in apps[j]['product']['name']) or
                            ('Oracle Database' in apps[j]['product']['name']) or
                            ('Informix' in apps[j]['product']['name']) ):
                            status = status + '\n  ' + str(apps[j]['product']['name']) 
                            if ('edition' in apps[j]): status = status + ' '+ str(apps[j]['edition'])
                            else: status = status + ' (no edition info)'
                            gotone+=1
                        if ('DB2 Database' in apps[j]['product']['description']):
                            status = status + '\n  ' + str(apps[j]['product']['name']) 
                            if ('edition' in apps[j]): status = status + ' '+ str(apps[j]['edition'])
                            else: status = status + ' (no edition info)'
                            gotone+=1

                j+=1        
    if (gotone > 0):
        print (status)
    i+=1
if (int(max) > count):
    print( "There is another page of results in the REST-API if you want to get it" )
else:
    print("That's all folks")

...