Versions Compared

Key

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

...

info
Code Block
languagepy
titleCompleted Script
collapsetrue
#!/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 ('name' in apps[j]['product']):
                        if ('SQL' in apps[j]['product']['name']):
                            status = status + '\n  ' + str(apps[j]['product']['name']) + ' '+ str(apps[j]['edition'])
                            gotone+=1
                        if ('Oracle' in apps[j]['product']['name']):
                            status = status + '\n  ' + str(apps[j]['product']['name']) + ' '+ str(apps[j]['edition'])
                            gotone+=1
                        if ('Informix' in apps[j]['product']['name']):
                            status = status + '\n  ' + str(apps[j]['product']['name']) + ' '+ str(apps[j]['edition'])
                            gotone+=1

                j+=1        
    if (gotone > 0):
        print (status)
    i+=1
if (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")

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@226f31
showSpacefalse
sortmodified
reversetrue
typepage
cqllabel in ("python","restapi") and type = "page" and space = "ISDK"
labelsRestAPI python

...