def createIncident(incident): payload = {} payload['name'] = incident['name'] payload['message'] = incident['message'] payload['status'] = incident['status'] payload['component_id'] = incident['component_id'] payload['component_status'] = incident['component_status'] payload['visible'] = 1 payload['notify'] = 1 try: response = requests.post("{}/incidents".format(dtadCachetAPI), data=json.dumps(payload), headers={ 'X-Cachet-Token': APIKey, 'Content-Type': "application/json" }) response.raise_for_status() except requests.HTTPError as e: log("Error", "Couldn't create incident {}".format(incident['name'])) log("Error", "Unsuccessful HTTP POST Request! Error Code {}".format(str(e))) log("Error", str(response.text)) else: log( "Success", "Created the incident \"{name}\" at the provider {providerName}". format(name=incident['name'], providerName=incident['provider'])) incidentID = response.json()['data']['id'] numberOfNewUpdates = len(incident['updates']) updateIncident(incidentID, incident, numberOfNewUpdates)
def createComponent(component): payload = {} payload['name'] = component['name'] payload['description'] = component['description'] payload['status'] = component['status'] payload['group_id'] = component['group_id'] payload['enabled'] = 1 try: response = requests.post("{}/components".format(dtadCachetAPI), data=json.dumps(payload), headers={'X-Cachet-Token': APIKey, 'Content-Type': "application/json"}) response.raise_for_status() except requests.HTTPError as e: log("Error", "Couldn't create component {name}".format(name=component['name'])) log("Error", "Unsuccessful HTTP POST Request! Error Code {}".format(str(e))) log("Error", str(response.text)) else: log("Success", "Created the component \"{name}\" at the provider {providerName}".format( name=component['name'], providerName=component['provider'] ) )
def deleteGroup(groupID): try: response = requests.delete("{dtadCachetAPI}/components/groups/{id}".format(dtadCachetAPI=dtadCachetAPI, id=groupID), headers={'X-Cachet-Token': APIKey}) response.raise_for_status() except requests.HTTPError as e: log("Error", "Coulden't delete an object from the endpoint 'components/groups/' !!!") log("Error", "Unsuccessful HTTP DELETE Request! Error Code {}".format(str(e))) log("Error", str(response.text)) else: log("Success", "Deleted the group with the id {id}".format(id=groupID))
def wrapper(): try: results = func() except Exception as e: exceptionType, exception, traceback = exc_info() log( "Error", "Gathering maintenances of \"{providerName}\" failed! Exception {name}: {description}" .format(providerName=providerName, name=exceptionType.__name__, description=exception)) with open(logFile, "a") as file: print_tb(traceback, file=file) print("\n") raise e if results: log( "Success", "Maintenances found at \"{providerName}\"! {maintenances}". format(providerName=providerName, maintenances=results)) else: log( "Info", "No maintenances at \"{providerName}\"".format( providerName=providerName)) return results
def createMaintenance(maintenance): payload = {} payload['name'] = maintenance['name'] payload['message'] = maintenance['message'] payload['scheduled_at'] = maintenance['scheduled_at'] payload['completed_at'] = maintenance['completed_at'] payload['status'] = 1 try: response = requests.post("{}/schedules".format(dtadCachetAPI), data=json.dumps(payload), headers={'X-Cachet-Token': APIKey, 'Content-Type': "application/json"}) response.raise_for_status() except requests.HTTPError as e: log("Error", "Unsuccessful HTTP Request! Error Code {}".format(str(e))) log("Error", str(response.text)) else: log("Success", "Created the maintenance {name} at the provider {providerName}".format( name=maintenance['name'], providerName=maintenance['provider'] ) )
def CRUDIncidents(): incidentIDs = getCachetIncident("incidentHash: id") # Check which incidents and update them if already there or create them otherwise. for providerName, getIncidents in incidentFunctions: for incident in getIncidents(): componentID = incident['component_id'] componentStatus = incident['component_status'] currentComponentStatus = latestComponentStatuses.get( componentID, 0) latestComponentStatuses[componentID] = max(componentStatus, currentComponentStatus) incidentHash = hashIncident(incident) if incidentHash in incidentIDs: numberOfNewUpdates = len(incident['updates']) - len( getIncidentUpdates(incidentIDs[incidentHash])) if numberOfNewUpdates > 0: updateIncident(incidentIDs[incidentHash], incident, numberOfNewUpdates) else: log( "Info", "Incident {name} with the id {id} is up-to-date". format(name=incident['name'], id=incidentIDs[incidentHash])) else: createIncident(incident) for componentID, latestComponentStatus in latestComponentStatuses.items(): cachetComponentStatuses = getCachetComponents("id: status") if latestComponentStatus != cachetComponentStatuses[componentID]: updateComponentStatus(componentID, latestComponentStatus) if debug: print("Incidents in Cachet before they where updated:") pprint(incidentIDs) print("\nEnabled Incidents:") pprint(incidentFunctions)
def updateComponentStatus(componentID, componentStatus): payload = {} payload['status'] = componentStatus try: response = requests.put("{dtadCachetAPI}/components/{componentID}".format( dtadCachetAPI = dtadCachetAPI, componentID = componentID ), data = json.dumps(payload), headers = {'X-Cachet-Token': APIKey, 'Content-Type': "application/json"} ) response.raise_for_status() except requests.HTTPError as e: log("Error", "Couldn't update the status of the component with the id {id}".format(id=componentID)) log("Error", "Unsuccessful HTTP PUT Request! Error Code {}".format(str(e))) log("Error", str(response.text)) else: log("Success", "Updated the component with the id {id}".format(id=componentID))
def createGroup(groupName): payload = {} payload['name'] = groupName payload['visible'] = 1 # True payload['collapsed'] = 1 # True # 1: always collapsed # 2: collapsed as long as there are no problems try: response = requests.post("{}/components/groups".format(dtadCachetAPI), data=json.dumps(payload), headers={'X-Cachet-Token': APIKey, 'Content-Type': "application/json"}) response.raise_for_status() return response except requests.HTTPError as e: log("Error", "Coulden't create the group {}".format(groupName)) log("Error", "Unsuccessful HTTP POST Request! Error Code {}".format(str(e))) log("Error", str(response.text)) else: log("Success", "Created the group {name}".format(name=groupName))
def getIncidentUpdates(incidentID): try: response = requests.get( "{dtadCachetAPI}/incidents/{incidentID}/updates".format( dtadCachetAPI=dtadCachetAPI, incidentID=incidentID)) response.raise_for_status() except requests.HTTPError as e: log( "Error", "Couldn't get the updates of the incident with the id {}".format( incidentID)) log("Error", "Unsuccessful HTTP GET Request! Error Code {}".format(str(e))) log("Error", str(response.text)) return response.json()['data']
def updateIncident(incidentID, incident, numberOfNewUpdates): payload = {} payload['message'] = incident['message'] try: response = requests.put( "{dtadCachetAPI}/incidents/{incidentID}".format( dtadCachetAPI=dtadCachetAPI, incidentID=incidentID), data=json.dumps(payload), headers={ 'X-Cachet-Token': APIKey, 'Content-Type': "application/json" }) response.raise_for_status() except requests.HTTPError as e: log("Error", "Couldn't update incident {}".format(incident['name'])) log("Error", "Unsuccessful HTTP PUT Request! Error Code {}".format(str(e))) log("Error", str(response.text)) else: log("Success", "Updated the main body of incident \"{name}\" with the id {id}" \ " at the provider {providerName}".format( name=incident['name'], id=incidentID, providerName=incident['provider'] ) ) payload = {} latestUpdate = incident['updates'][-numberOfNewUpdates] payload['message'] = latestUpdate['message'] payload['status'] = latestUpdate['status'] try: response = requests.post( "{dtadCachetAPI}/incidents/{incidentID}/updates".format( dtadCachetAPI=dtadCachetAPI, incidentID=incidentID), data=json.dumps(payload), headers={ 'X-Cachet-Token': APIKey, 'Content-Type': "application/json" }) response.raise_for_status() except requests.HTTPError as e: log( "Error", "Couldn't create an update for incident {}".format( incident['name'])) log("Error", "Unsuccessful HTTP POST Request! Error Code {}".format(str(e))) log("Error", str(response.text)) else: log("Success", "Posted an updated for the incident \"{name}\" with the id {id}" \ " at the provider {providerName}".format( name=incident['name'], id=incidentID, providerName=incident['provider'] ) )