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 |
---|
language | py |
---|
title | Connect 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.
...
Code Block |
---|
language | py |
---|
title | Completed Script |
---|
collapse | true |
---|
|
#
# 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 |
info |
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 |
---|
language | bash |
---|
title | Sample 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
|
Related articles
Filter by label (Content by label) |
---|
showLabels | false |
---|
max | 5 |
---|
spaces | com.atlassian.confluence.content.render.xhtml.model.resource.identifiers.SpaceResourceIdentifier@1fb5c7 |
---|
showSpace | false |
---|
sort | modified |
---|
reverse | true |
---|
type | page |
---|
cql | label = "restapi" and type = "page" and space = "CSKB" |
---|
labels | RestAPI |
---|
|
...