#servicenow #0365 #azure #microsoft
O365 Service Alerts & the Service Status widget, and outages table etc - IT Service Management - Question - ServiceNow Communityvar O365OutageSync = Class.create(); O365OutageSync.prototype = { initialize: function() { }, refreshO365OutageRecords: function(token){ var date_now = gs.nowDateTime(); var parser = new JSONParser(); var json_responseBody = "failed to load"; try { var r = new sn_ws.RESTMessageV2('Microsoft Azure', 'Get Service Status'); r.setStringParameterNoEscape('TenantID', gs.getProperty('AzureAccessToken.TenantID')); r.setStringParameterNoEscape('Token', token); var response = r.execute(); var httpStatus = response.getStatusCode(); if(httpStatus == '200'){ //Success var str_responseBody = response.getBody(); json_responseBody = parser.parse(str_responseBody); } else { gs.log("Error requesting O365 Service Status. httpStatus:" + httpStatus); } } catch(ex) { var message = ex.message; gs.log("Error requesting O365 Service Status. Error Message:" + message); } if(json_responseBody != "failed to load"){ for (s = 0; s < json_responseBody.value.length; s++) { //gs.log(json_responseBody.value[s].WorkloadDisplayName + " : " + json_responseBody.value[s].StatusDisplayName); var str_bs_sys_id = ""; //***Create Business Service Record if it does not already exist*** var gr_bs = new GlideRecord('cmdb_ci_service'); gr_bs.addQuery('name', 'O365 - ' + json_responseBody.value[s].WorkloadDisplayName); gr_bs.query(); if(gr_bs.next()){ str_bs_sys_id = gr_bs.sys_id + ""; } else if (json_responseBody.value[s].IncidentIds.length > 0){ //Doesnt exist, create it var gr_new_bs = new GlideRecord('cmdb_ci_service'); gr_new_bs.initialize(); gr_new_bs.setValue('name', 'O365 - ' + json_responseBody.value[s].WorkloadDisplayName); gr_new_bs.setValue('used_for', 'Production'); str_bs_sys_id = gr_new_bs.insert(); } //***Create Outages for open incidents*** var arr_openOutages = []; for (i = 0; i < json_responseBody.value[s].IncidentIds.length; i++) { arr_openOutages.push(json_responseBody.value[s].IncidentIds[i]); var gr_existing_outage = new GlideRecord('cmdb_ci_outage'); gr_existing_outage.addQuery('u_number',json_responseBody.value[s].IncidentIds[i]); gr_existing_outage.query(); if(gr_existing_outage.next()){ //Set Outage Status for existing Outage Record switch(json_responseBody.value[s].Status){ case 'ServiceDegradation': gr_existing_outage.setValue('type', 'degradation'); break; case 'ServiceInterruption': gr_existing_outage.setValue('type', 'outage'); break; } gr_existing_outage.update(); } else { var gr_new_outage = new GlideRecord('cmdb_ci_outage'); gr_new_outage.initialize(); gr_new_outage.setValue('u_number', json_responseBody.value[s].IncidentIds[i]); gr_new_outage.setValue('cmdb_ci', str_bs_sys_id); gr_new_outage.begin = date_now; //Set Outage Status for existing Outage Record switch(json_responseBody.value[s].Status){ case 'Investigating': gr_new_outage.setValue('type', 'degradation'); break; case 'ServiceDegradation': gr_new_outage.setValue('type', 'degradation'); break; case 'ServiceInterruption': gr_new_outage.setValue('type', 'outage'); break; case 'RestoringService': gr_new_outage.setValue('type', 'degradation'); break; case 'ExtendedRecovery': gr_new_outage.setValue('type', 'degradation'); break; case 'VerifyingService': gr_new_outage.setValue('type', 'degradation'); break; } gr_new_outage.insert(); } } //***End Outages for resolved incidents*** //gs.log("Closing incidents for :" + json_responseBody.value[s].WorkloadDisplayName +", cisysid:" + str_bs_sys_id + "Status:" + json_responseBody.value[s].Status + ", open outages:" + arr_openOutages.join(',')); if(str_bs_sys_id.length > 0){ var arr_closedcodes = ['ServiceRestored', 'PostIncidentReportPublished', 'ServiceOperational', 'FalsePositive']; var arrUtil = new ArrayUtil(); var gr_active_outage = new GlideRecord('cmdb_ci_outage'); gr_active_outage.addQuery('cmdb_ci', str_bs_sys_id); gr_active_outage.addNullQuery('end'); gr_active_outage.query(); while(gr_active_outage.next()){ if((arrUtil.indexOf(arr_closedcodes, json_responseBody.value[s].Status) >= 0)||(arrUtil.indexOf(arr_openOutages, gr_active_outage.u_number) < 0)){ //Service is fully operational. //Close out all active outages gr_active_outage.end = date_now; gr_active_outage.update(); } else { } } } } } }, refreshO365OutageMessages: function(token){ //***Pull Down Outage Messages*** var parser = new JSONParser(); var json_responseBody = "failed to load"; try { var r = new sn_ws.RESTMessageV2('Microsoft Azure', 'Get All Messages'); r.setStringParameterNoEscape('TenantID', gs.getProperty('AzureAccessToken.TenantID')); r.setStringParameterNoEscape('Token', token); var response = r.execute(); var httpStatus = response.getStatusCode(); if(httpStatus == '200'){ //Success var str_responseBody = response.getBody(); json_responseBody = parser.parse(str_responseBody); } else { gs.log("Error requesting O365 Service Messages. httpStatus:" + httpStatus); } } catch(ex) { var message = ex.message; gs.log("Error requesting O365 Service Messages. Error Message:" + message); } if(json_responseBody != "failed to load"){ for (i = 0; i < json_responseBody.value.length; i++) { if(json_responseBody.value[i].Messages.length > 0){ this.updateOutage(json_responseBody.value[i].Id, json_responseBody.value[i].Messages[json_responseBody.value[i].Messages.length-1].MessageText + "<BR><BR><u>Last Updated: " + json_responseBody.value[i].Messages[json_responseBody.value[i].Messages.length-1].PublishedTime + "</u>"); } else { this.updateOutage(json_responseBody.value[i].Id, json_responseBody.value[i].ImpactDescription); } } } }, updateOutage: function(str_number, str_details){ var gr_outage = new GlideRecord('cmdb_ci_outage'); gr_outage.addQuery('u_number', str_number); gr_outage.query(); if(gr_outage.next()){ gr_outage.setValue('details', str_details); gr_outage.update(); } }, getAzureAPIAccessToken: function(){ var token = ""; try { var r = new sn_ws.RESTMessageV2('Microsoft Azure', 'Get Access Token'); r.setStringParameterNoEscape('ClientSecret', gs.getProperty('AzureAccessToken.ClientSecret')); r.setStringParameterNoEscape('ClientID', gs.getProperty('AzureAccessToken.ClientID')); r.setStringParameterNoEscape('TenantID', gs.getProperty('AzureAccessToken.TenantID')); var response = r.execute(); var httpStatus = response.getStatusCode(); if(httpStatus == '200'){ //Success var str_responseBody = response.getBody(); var parser = new JSONParser(); var json_responseBody = parser.parse(str_responseBody); token = json_responseBody.access_token; } else { gs.log("Error requesting Azure API Token. httpStatus:" + httpStatus); } } catch(ex) { var message = ex.message; gs.log("Error requesting Azure API Token. Error Message:" + message); } return token; }, type: ‘O365OutageSync’ };
star
photo_camera
Fri Jan 29 2021 18:34:35 GMT+0000 (Coordinated Universal Time) https://community.servicenow.com/community?id
#servicenow #0365 #azure #microsoft