Example #1
0
        public void CheckIncidentsStatus(ILogger log)
        {
            log.LogInformation("Starting incident status check with service management system...");
            List <AlertIncidentEntity> alertIncidents = GetAllAlertIncidents();

            log.LogInformation($"Found {alertIncidents.Count} open incidents, checking status if necessary..");

            ServiceManagementStatusResponseDto incidentsStatus = serviceManagemement.GetIncidentsStatus(alertIncidents, log);

            foreach (string incidentId in incidentsStatus.result.Keys)
            {
                ServiceManagementStatus status = incidentsStatus.result[incidentId];
                log.LogInformation($"Got new service management status for incident {incidentId}: {status.state}");

                if (status.state == "Resolved" || status.state == "Closed")
                {
                    AlertIncidentEntity alertIncident = GetAlertIncident("SNOW", incidentId);
                    if (alertIncident != null)
                    {
                        AlertEntity alert = GetAlert(alertIncident.AlertPartitionId, alertIncident.AlertRowId);
                        if (alert != null)
                        {
                            DeleteAlert(alert);
                        }
                        DeleteAlertIncident(alertIncident);
                    }
                }
            }
        }
        public ServiceManagementStatusResponseDto GetIncidentStatus(AlertIncidentEntity incident, ILogger log)
        {
            ServiceManagementStatusResponseDto result = new ServiceManagementStatusResponseDto();

            result.result.Add(incident.IncidentId, new ServiceManagementStatus()
            {
                state = "Closed", short_description = "Nothing to say.."
            });
            return(result);
        }
        public ServiceManagementStatusResponseDto GetIncidentsStatus(List <AlertIncidentEntity> incidents, ILogger log)
        {
            ServiceManagementStatusResponseDto response = null;

            using (HttpClient client = new HttpClient())
            {
                string serviceManagementCredentials = System.Environment.GetEnvironmentVariable("ServiceManagementCredentials");
                if (!String.IsNullOrEmpty(serviceManagementCredentials))
                {
                    var byteArray = Encoding.ASCII.GetBytes(serviceManagementCredentials);
                    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                }

                string serviceManagementUserAgent = System.Environment.GetEnvironmentVariable("ServiceManagementUserAgent");
                if (!String.IsNullOrEmpty(serviceManagementUserAgent))
                {
                    client.DefaultRequestHeaders.Add("User-Agent", serviceManagementUserAgent);
                }

                string snowUrl = System.Environment.GetEnvironmentVariable("ServiceManagementUrl") + "/state?";
                foreach (AlertIncidentEntity incident in incidents)
                {
                    snowUrl += "number=" + incident.IncidentId + "&";
                }

                // Remove last & symbol
                snowUrl = snowUrl.Substring(0, snowUrl.Length - 1);

                log.LogInformation($"Calling SNOW incident status REST API {snowUrl}");

                HttpResponseMessage msg = client.GetAsync(snowUrl).Result;
                if (msg.IsSuccessStatusCode)
                {
                    var JsonDataResponse = msg.Content.ReadAsStringAsync().Result;
                    log.LogInformation($"Received SNOW incident status response {JsonDataResponse}");
                    response = Newtonsoft.Json.JsonConvert.DeserializeObject <ServiceManagementStatusResponseDto>(JsonDataResponse);
                }
                else
                {
                    var    JsonDataResponse = msg.Content.ReadAsStringAsync().Result;
                    string responseMessage  = "";
                    if (JsonDataResponse != null)
                    {
                        responseMessage = JsonDataResponse.ToString();
                    }
                    log.LogError($"Could not reach SNOW server: {msg.ToString()} - {responseMessage}");
                }
            }

            return(response);
        }