/// <summary>
        /// Checks the application gateway back end health metrics and deletes App Payload Failed nodes
        /// </summary>
        /// <returns><c>true</c>, if we deleted nodes, <c>false</c> otherwise.</returns>
        /// <param name="appGw">App gateway</param>
        /// <param name="scaleSet">Scale set.</param>
        /// <param name="minHealthyServers">Minimum healthy servers.</param>
        /// <param name="log">Log.</param>
        public static bool CheckApplicationGatewayBEHealthAndDeleteBadNodes(ApplicationGatewayBackendHealthInner appGw, IVirtualMachineScaleSet scaleSet, int minHealthyServers, ILogger log)
        {
            try
            {
                log.LogInformation("Enumerating Application Gateway Backend Servers");
                var healthy   = new List <ApplicationGatewayBackendHealthServer>();
                var unhealthy = new List <ApplicationGatewayBackendHealthServer>();
                foreach (var server in appGw.BackendAddressPools[0].BackendHttpSettingsCollection[0].Servers)
                {
                    if (server.Health.Value == "Healthy")
                    {
                        healthy.Add(server);
                    }
                    else
                    {
                        unhealthy.Add(server);
                    }
                }

                List <string> appGwBadIps = new List <string>();

                // If we have unhealthy nodes, then delete them
                if (unhealthy.Count > 0)
                {
                    log.LogInformation("App Payload Failed node count = {0}, removing nodes", unhealthy.Count);

                    if (healthy.Count <= 3)
                    {
                        var nodeCount = healthy.Count() + unhealthy.Count() + 3;
                        log.LogInformation("Healthy Node Count <=3, mandatory scale event to Current Healthly Count + UnHealthy Count +  3 nodes. ScaleTarget Set to {0}", nodeCount);
                        vmScaleSetOperations.ScaleToTargetSize(scaleSet, nodeCount, 10, 100, false, false, log);
                    }

                    return(vmScaleSetOperations.RemoveVMSSInstancesByIP(scaleSet, unhealthy.Select(s => s.Address).ToList(), log));
                }


                return(false);
            }
            catch (Exception e)
            {
                log.LogInformation("Error Message: " + e.Message);
                log.LogInformation("HResult: " + e.HResult);
                log.LogInformation("InnerException:" + e.InnerException);
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the healthy and unhealthy node counts.
        /// </summary>
        /// <returns>The healthy and unhealthy node counts.</returns>
        /// <param name="appGw">App gw.</param>
        /// <param name="log">Log.</param>
        public static Tuple <int, int> GetHealthyAndUnhealthyNodeCounts(ApplicationGatewayBackendHealthInner appGw, ILogger log)
        {
            var healthy   = 0;
            var unhealthy = 0;

            foreach (var h in appGw.BackendAddressPools[0].BackendHttpSettingsCollection[0].Servers.Select(s => s.Health.Value))
            {
                switch (h.ToLower())
                {
                case "healthy":
                    healthy++;
                    break;

                default:
                    unhealthy++;
                    break;
                }
            }
            return(new Tuple <int, int>(healthy, unhealthy));
        }
        /// <summary>
        /// Checks the application gateway back end health metrics and deletes App Payload Failed nodes
        /// </summary>
        /// <returns><c>true</c>, if we deleted nodes, <c>false</c> otherwise.</returns>
        /// <param name="appGw">App gateway</param>
        /// <param name="scaleSet">Scale set.</param>
        /// <param name="minHealthyServers">Minimum healthy servers.</param>
        /// <param name="log">Log.</param>
        public static bool CheckApplicationGatewayBEHealthAndReimageBadNodes(ApplicationGatewayBackendHealthInner appGw, IVirtualMachineScaleSet scaleSet, IVirtualMachineScaleSets allScaleSets, int minHealthyServers, ILogger log)
        {
            try
            {
                log.LogInformation("Enumerating Application Gateway Backend Servers");
                var healthy   = new List <ApplicationGatewayBackendHealthServer>();
                var unhealthy = new List <ApplicationGatewayBackendHealthServer>();
                foreach (var server in appGw.BackendAddressPools[0].BackendHttpSettingsCollection[0].Servers)
                {
                    if (server.Health.Value == "Healthy")
                    {
                        healthy.Add(server);
                    }
                    else
                    {
                        unhealthy.Add(server);
                    }
                }

                List <string> appGwBadIps = new List <string>();

                // If we have unhealthy nodes, then delete them
                if (unhealthy.Count > 0)
                {
                    log.LogInformation("App Payload Failed node count = {0}, reimaging nodes", unhealthy.Count);
                    return(VmScaleSetOperations.ReplaceVMSSInstancesByIP(scaleSet, allScaleSets, unhealthy.Select(s => s.Address).ToList(), log));
                }
                return(false);
            }
            catch (Exception e)
            {
                log.LogInformation("Error Message: " + e.Message);
                log.LogInformation("HResult: " + e.HResult);
                log.LogInformation("InnerException:" + e.InnerException);
                return(false);
            }
        }