from decouple import config import yaml import requests import json import pandas as pd from datetime import datetime def make_request(url, headers,verify): try: response = requests.get(url, headers=headers,verify=verify) response.raise_for_status() except requests.exceptions.HTTPError as errh: return "An Http Error occurred:" + repr(errh) except requests.exceptions.ConnectionError as errc: return "An Error Connecting to the API occurred:" + repr(errc) except requests.exceptions.Timeout as errt: return "A Timeout Error occurred:" + repr(errt) except requests.exceptions.RequestException as err: return "An Unknown Error occurred" + repr(err) return response def GatherReportingInfo(DTAPIToken, DTENV,friendlyName): env = DTENV DTAPIToken = DTAPIToken if (DTENV.find('dyna-synth-cn') != -1): verify=False else: verify=True DTAPIURL= env + "/api/config/v1/dashboards" headers = { 'Content-Type': 'application/json', 'Authorization': 'Api-Token ' + DTAPIToken } #r = requests.get(DTAPIURL,headers=headers) r = make_request(DTAPIURL,headers,verify) df = pd.DataFrame() mgmt = pd.DataFrame() now=datetime.now() strnow = now.strftime("%Y%m%d_%H%M") try: dashboards = r.json() count = 0 for dashboard in dashboards['dashboards']: row={'id':dashboard['id'],'name':dashboard['name'],'owner':dashboard['owner']} DTAPIURL= env + "/api/config/v1/dashboards/" + dashboard['id'] r = make_request(DTAPIURL,headers,verify) response = r.text if ("wirkkette" in response.lower()): print("Text Wirkkette found in dashboard json" + dashboard['name']) mgmt = mgmt.append(row,ignore_index=True) #mgmt= pd.concat((mgmt,row), axis=0) count=count+1 print (friendlyName + "done: " + str(count) + " dashboards checked.") # df.to_csv(friendlyName + '_Restart_' + strnow +'.csv') mgmt.to_csv(friendlyName + '_DashboardsWithWirkkette_' + strnow +'.csv',encoding='utf-8') except: print(r) with open('./environment.yaml') as file: doc = yaml.safe_load(file) for item, doc in doc.items(): token = dict(doc[2]) url = dict(doc[1]) print("Crawling through: " + item) print("Check if token exists in environment...") if(config(token.get('env-token-name')) != ""): print("Gather data, hold on a minute") DTTOKEN = config(token.get('env-token-name')) DTURL = url.get('env-url') GatherReportingInfo(DTTOKEN,DTURL,item) else: print("token not found, skipping " + item)