Thursday, 31 March 2022

[OCI][Python] Collecting All NetApp OCI Queries Across Multiple OCI Servers and Export to CSV

A Python script to connect to multiple NetApp OnCommand Insight Servers in turn, and collect all the Queries (queries you see in the WebUI) and relating information. Perhaps useful if you're doing a consolidation exercise (like  many OCI to one CI). And in the next post, we'll collect the Annotation Rules (which will use some of these queries.)

My Python skills are pretty poor, which is a good reason for me to have a go at improving them!

Save the script as say GetQueries.py and run from your prompt as python GetQueries.py.

The Script

## IMPORT THE ESSENTIALS ##
import requests
from base64 import b64encode
import csv
import json
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

## SETUP CREDENTIALS FOR THE OCI SERVERS (ASSUMING ALL THE SAME) ##
userAndPass = b64encode(b"USERNAME:PASSWORD").decode("ascii")
headers = {'Authorization':'Basic %s' % userAndPass}

## CREATE A LIST OF OCI SERVERS ##
OCIS=[]
OCIS.append("OCI_SERVER1_FQDN")
OCIS.append("OCI_SERVER2_FQDN")
OCIS.append("OCI_SERVER3_FQDN")

## INITIALIZE A FILE FOR THE CSV OUTPUT ##
output = open('queries.csv',mode='w',newline='')
writer = csv.writer(output, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['OCI Server','Query Name','Object Type','Sort Order','Fields','filters'])

## CYCLE THROUGH THE OCIS COLLECTING THE QUERIES ##
for OCI in OCIS:
  baseurl = "https://" + OCI + "/rest/v1/"
  api = "queries"
  response = requests.request("GET",baseurl + api, headers = headers, verify = False)
  json_data = json.loads(response.text)

  for query in json_data:
    writer.writerow([OCI,query["name"], query["objectType"], query["sort"], query["fields"], query["filters"]])

## CLOSE THE CSV ##
output.close()

No comments:

Post a Comment

Note: only a member of this blog may post a comment.