From 3aa602d7fc758538a80116ca6ab8e8efc407626e Mon Sep 17 00:00:00 2001 From: Patryk Gudalewicz Date: Mon, 11 Jul 2022 11:55:20 +0200 Subject: [PATCH] Initial commit --- Jenkinsfile | 8 ++++---- createReport.py | 51 ++++++++++++++++++------------------------------- 2 files changed, 23 insertions(+), 36 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index f3acdcb..cc06f6d 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -28,13 +28,13 @@ //here comes the trigger according to crontabs - jenkins is in UTC triggers { //every 1st of every month at 00:00 - cron('0 0 1 * *') + //cron('0 0 1 * *') //every day at 08:00 //cron('0 8 * * *') //every monday at 08:00 - //cron('0 8 * * MON') + cron('0 8 * * MON') } @@ -85,8 +85,8 @@ try { emailext subject: env.JOB_NAME, body: 'Please find the output of your reports attached', - to: 'rene.forstner@nttdata.com', - replyTo: 'coco-apm@bmw.de', + to: 'patryk.gudalewicz.bp@nttdata.com', + replyTo: 'patryk.gudalewicz.bp@nttdata.com', attachmentsPattern: '*.csv' } diff --git a/createReport.py b/createReport.py index df1390a..43710b2 100644 --- a/createReport.py +++ b/createReport.py @@ -1,3 +1,4 @@ +from html import entities from decouple import config import yaml import requests @@ -30,7 +31,7 @@ def GatherReportingInfo(DTAPIToken, DTENV,friendlyName): else: verify=True - DTAPIURL= env + "/api/v2/metrics?metricSelector=calc:service.*&fields=created,lastWritten&pageSize=499" + DTAPIURL= env + "/api/v2/entities?entitySelector=type(\"SERVICE\"),fromRelationships.runsOn(type(\"PROCESS_GROUP\"),tag(\"compass-id\"))&fields=tags&pageSize=499" headers = { 'Content-Type': 'application/json', 'Authorization': 'Api-Token ' + DTAPIToken @@ -41,39 +42,25 @@ def GatherReportingInfo(DTAPIToken, DTENV,friendlyName): df = pd.DataFrame() mgmt = pd.DataFrame() - + entityResponse = r.json() + entityList = entityResponse["entities"] + print("Got {} results from API: ".format(len(entityList))) + while "nextPageKey" in entityResponse: + print("Getting next page: " + entityResponse["nextPageKey"]) + nextKey = entityResponse["nextPageKey"] + DTAPIURL= env + "/api/v2/entities?nextPageKey={}".format(nextKey) + print("Got {} results from API: ".format(len(entityList))) + r = make_request(DTAPIURL,headers,verify) + entityResponse = r.json() + entityList.extend(entityResponse["entities"]) now=datetime.now() strnow = now.strftime("%Y%m%d_%H%M") - metrics = r.json() - for metric in metrics['metrics']: - row={'metricID':metric['metricId'],'created':metric['created'],'lastWritten':metric['lastWritten']} - print(metric['metricId']) - cleanedMetric = metric['metricId'].replace('\"','') - cleanedMetric = cleanedMetric.replace('"','') - DTAPIURL= env + "/api/config/v1/calculatedMetrics/service/" + cleanedMetric - - response = make_request(DTAPIURL,headers,verify) - - row.update({'name':response.json()['name']}) - for condition in response.json()['conditions']: - i = 0 - if(condition['attribute']=='SERVICE_TAG'): - try: - row.update({'SERVICE_TAG_KEY_'+str(i):condition['comparisonInfo']['value']['key'],'SERVICE_TAG_VALUE_'+str(i):condition['comparisonInfo']['value']['value']}) - except: - row.update({'SERVICE_TAG_KEY_'+str(i):'NO DATA','SERVICE_TAG_VALUE_'+str(i):'NO DATA'}) - - if(condition['attribute']=='PROCESS_GROUP_TAG'): - try: - row.update({'PG_TAG_KEY_'+str(i):condition['comparisonInfo']['value']['key'],'PG_TAG_VALUE_'+str(i):condition['comparisonInfo']['value']['value']}) - except: - row.update({'PG_TAG_KEY_'+str(i):'NO DATA','SERVICE_TAG_VALUE_'+str(i):'NO DATA'}) - i=i+1 - - mgmt = mgmt.append(row,ignore_index=True) - -# df.to_csv(reportingFolder + friendlyName + '_Restart_' + strnow +'.csv') - mgmt.to_csv(friendlyName + '_CalculatedMetrics_' + strnow +'.csv',encoding='utf-8') + for entity in entityList: + if any(tag["stringRepresentation"].startswith('[Kubernetes]app.bmwgroup.com/') for tag in entity["tags"]): + tagval = next((tag for tag in entity["tags"] if tag["stringRepresentation"].startswith('[Kubernetes]app.bmwgroup.com/')), None) + row={'Tag':tagval["stringRepresentation"],'ServiceName':entity['displayName'],'ServiceId':entity['entityId']} + mgmt = mgmt.append(row,ignore_index=True) + mgmt.to_csv("C:/Users/patry/OneDrive/Dokumenty/NTT/" + friendlyName + '_TaggedEntities_' + strnow +'.csv') with open('./environment.yaml') as file: doc = yaml.safe_load(file)