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.

...

Code Block
languagepy
titleConnect to host
import requests

r = requests.get('http://iqsonar-host/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.

...

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://iqsonar-host/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

Sample Output

Below is a small excerpt of the output of this script from our test environment, showing four different hosts; two hosting SQL server instances, one hosting an Informix database and one hosting an Oracle database:

Code Block
languagebash
titleSample Output
mike@ubuntu:~/python$ python3 v1.python
VM-SQL16-2K12: 2047MB RAM, Intel(R) Xeon(R) CPU E5-2667 v3 @ 3.20GHz
  SQL Server Express Edition (64-bit)
  SQL Server Enterprise Edition (64-bit)
vm-pegasus-f10: 2048MB RAM, Intel(R) Xeon(R) CPU E5-2667 v3 @ 3.20GHz
  Informix Developer Edition
ORA-DB-WL-2K3: 766MB RAM, Intel(R) Pentium(R) 4 CPU 2.80GHz
  Oracle Database Server Standard
VM-SQL2K-2K: 1023MB RAM, Intel(R) Xeon(R) CPU E5-2667 v3 @ 3.20GHz
  SQL Server Enterprise Edition
  SQL Server Standard Edition
  SQL Server Standard Edition


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

...