static void Main(string[] args)
        {
            var server = ServerConfigMgr.GetServers().Where(x => x.HostName.ToUpper().Equals("CTXV01PIMGD01")).FirstOrDefault();

            bool isExempt = ServerHealthCheckConfigMgr.IsExempt(server, ExemptionType.HardDrive, "O");


            var winServicesToCheck = ServerHealthCheckConfigMgr.GetWindowsServicesToCheck()
                                     .Where
                                     (
                x => x.Servers.Where(y => y.Item1 && y.Item2.HostName == server.HostName).Count() > 0 && x.Roles.Where(y => !y.Item1 && y.Item2 == server.HostRole && y.Item3 == server.HostFunction).Count() == 0 ||
                x.Roles.Where(y => y.Item1 && y.Item2 == server.HostRole && y.Item3 == server.HostFunction).Count() > 0 && x.Servers.Where(y => !y.Item1 && y.Item2.HostName == server.HostName).Count() == 0 ||
                !x.Function.Equals(ServerFunction.Unknown) && (x.Function.Equals(server.HostFunction) && x.Servers.Where(y => !y.Item1 && y.Item2.HostName == server.HostName).Count() + x.Roles.Where(y => !y.Item1 && y.Item2 == server.HostRole && y.Item3 == server.HostFunction).Count() == 0) ||
                ((x.Roles.Count + x.Servers.Count == 0) && (x.Function.Equals(ServerFunction.Unknown)))
                                     ).ToArray();


            //hcWServices = ServerHealthCheckConfigMgr.GetWindowsServicesToCheck().Where(x => x.OnePerGroup).ToArray();

            foreach (var svc in ServerHealthCheckConfigMgr.GetWindowsServicesToCheck().Where(x => x.Name == "WAS"))
            {
                var count  = svc.Servers.Where(y => !y.Item1 && y.Item2.HostName == server.HostName).Count();
                var count2 = svc.Roles.Where(y => !y.Item1 && y.Item2 == server.HostRole && y.Item3 == server.HostFunction).Count();

                //if (svc.Roles.Count >= 1)
                //svc.Roles.ForEach(x => Console.WriteLine("{3}  - {0} | {1} | {2}", x.Item1, x.Item2, x.Item3, svc.Name));

                bool bol1 = svc.Servers.Where(y => y.Item1 && y.Item2.HostName == server.HostName).Count() > 0 && svc.Roles.Where(y => !y.Item1 && y.Item2 == server.HostRole && y.Item3 == server.HostFunction).Count() == 0;
                bool bol2 = svc.Roles.Where(y => y.Item1 && y.Item2 == server.HostRole && y.Item3 == server.HostFunction).Count() > 0 && svc.Servers.Where(y => !y.Item1 && y.Item2.HostName == server.HostName).Count() == 0;
                bool bol3 = (svc.Function.Equals(server.HostFunction) && svc.Servers.Where(y => !y.Item1 && y.Item2.HostName == server.HostName).Count() + svc.Roles.Where(y => !y.Item1 && y.Item2 == server.HostRole && y.Item3 == server.HostFunction).Count() == 0);
                bool bol4 = ((svc.Roles.Count + svc.Servers.Count == 0) && (svc.Function.Equals(ServerFunction.Unknown)));
                bool bol5 = svc.Servers.Where(y => y.Item1 && y.Item2.HostName == server.HostName).Count() > 0 && svc.Roles.Where(y => !y.Item1 && y.Item2 == server.HostRole && y.Item3 == server.HostFunction).Count() == 0 ||
                            svc.Roles.Where(y => y.Item1 && y.Item2 == server.HostRole && y.Item3 == server.HostFunction).Count() > 0 && svc.Servers.Where(y => !y.Item1 && y.Item2.HostName == server.HostName).Count() == 0 ||
                            (svc.Function.Equals(server.HostFunction) && svc.Servers.Where(y => !y.Item1 && y.Item2.HostName == server.HostName).Count() + svc.Roles.Where(y => !y.Item1 && y.Item2 == server.HostRole && y.Item3 == server.HostFunction).Count() == 0) ||
                            ((svc.Roles.Count + svc.Servers.Count == 0) && (svc.Function.Equals(ServerFunction.Unknown)));

                Console.WriteLine("{0} | {1} | {2} | {3} | {4} | {5} | {6} | {7} | {8}", svc.Name, count, server.HostFunction, server.HostRole, count2, bol1, bol2, bol3, bol4, bol5);
            }

            //var roleCheck = ServerHealthCheckConfigMgr.GetWindowsServicesToCheck().Where(x => x.Roles.Where(y => y.Item1 && y.Item2 == svr.HostRole).Count() > 0);

            foreach (var service in winServicesToCheck)
            {
                Console.WriteLine("Name:\t\t{0}", service.Name);
                Console.WriteLine("Display:\t{0}", service.DisplayName);
                if (service.CheckStatus.Count > 0)
                {
                    service.CheckStatus.ForEach(x => Console.WriteLine("Status:\t\t{0}", x.ToString()));
                }
                if (service.CheckStartupType.Count > 0)
                {
                    service.CheckStartupType.ForEach(x => Console.WriteLine("StartupType:\t{0}", x.ToString()));
                }
                if (service.CheckLogonAs.Count > 0)
                {
                    service.CheckLogonAs.ForEach(x => Console.WriteLine("LogonAs:\t{0} - {1}", x, GenericChecks.GetWindowsServiceLogonAccount("B1ZHN32", service.Name)));
                }
                if (service.Roles.Count > 0)
                {
                    service.Roles.ForEach((x) =>
                    {
                        if (x.Item1)
                        {
                            Console.WriteLine("INCLUDE ROLES");
                        }
                        else
                        {
                            Console.WriteLine("EXCLUDE ROLES");
                        }
                        Console.WriteLine("Role:\t\t{0}", x.Item2.ToString());
                        Console.WriteLine("Function:\t{0}", x.Item3.ToString());
                    });
                }
                if (service.Servers.Count > 0)
                {
                    service.Servers.ForEach((x) =>
                    {
                        if (x.Item1)
                        {
                            Console.WriteLine("INCLUDE SERVERS");
                        }
                        else
                        {
                            Console.WriteLine("EXCLUDE SERVERS");
                        }

                        Console.WriteLine("Server Name:\t{0}", x.Item2.HostName);
                        Console.WriteLine("Server IP:\t{0}", x.Item2.IPAddress);
                        Console.WriteLine("Server Location:\t{0}", x.Item2.HostLocation.ToString());
                    });
                }
                Console.WriteLine("OnePerGroup:\t{0}", service.OnePerGroup);
                Console.WriteLine();
            }
            Console.WriteLine("Press Enter to Continue...");
            Console.ReadLine();

            HTMLFormatter formatter = new HTMLFormatter();

            formatter.SetRole(ServerRole.NSP.ToString());

            formatter.BeginTable("NSPTXCAWAPV01");
            formatter.AddStatusRow("Server Status", StatusResult.Ok);
            formatter.AddStatusRow("Web Services", StatusResult.Error);
            List <string> errors = new List <string>();

            errors.Add("Config file mismatch with NSPTXCAWAPV02");
            errors.Add("Web API returned error");
            formatter.AddErrorDescriptionRows(errors);

            formatter.AddStatusRow("Database", StatusResult.Error);
            errors.Clear();
            errors.Add("Missing data in table");
            formatter.AddErrorDescriptionRows(errors);
            formatter.EndTable();

            formatter.BeginTable("NSPTXCAWAPV02");
            formatter.AddStatusRow("Server Status", StatusResult.Ok);
            formatter.AddStatusRow("Web Services", StatusResult.Error);
            errors.Clear();
            errors.Add("Config file mismatch with NSPTXCAWAPV01");
            formatter.AddErrorDescriptionRows(errors);
            formatter.AddStatusRow("Database", StatusResult.Ok);
            formatter.EndTable();

            formatter.BeginTable("NSPTXCAWAPV03");
            formatter.AddStatusRow("Server Status", StatusResult.Critical);
            errors.Clear();
            errors.Add("Server is unreachable.");
            formatter.AddErrorDescriptionRows(errors);
            formatter.AddStatusRow("Web Services", StatusResult.Ok);
            formatter.AddStatusRow("Database", StatusResult.Ok);
            formatter.EndTable();


            Toolset.SendEmail("mailrelay.corp.pvt", null, 25, false, "HealthCheck Test Email", formatter.ToString(), "*****@*****.**", new string[1] {
                "*****@*****.**"
            }, null);

            var servers = ServerConfigMgr.GetServers().ToList();

            foreach (var svr in servers.Where(x => x.HostRole == ServerRole.IMG))
            {
                if (svr is FiOSDbServer)
                {
                    Console.WriteLine("\nDATABASE:\n");
                    Console.WriteLine((svr as FiOSDbServer).DatabaseType);
                }
                else if (svr is FiOSWebServer)
                {
                    Console.WriteLine("\nIIS SERVER:\n");
                }
                else
                {
                    Console.Write("\nFiOS Server:\n");
                }

                Console.WriteLine(svr.HostName);
                Console.WriteLine(svr.HostFullName);
                Console.WriteLine(svr.HostFunction);
                Console.WriteLine(svr.HostLocation);
                Console.WriteLine(svr.HostLocationName);
                Console.WriteLine(svr.HostName);
                Console.WriteLine(svr.HostRole);
                Console.WriteLine(svr.IsOnline);

                try
                {
                    if (svr is FiOSWebServer)
                    {
                        using (var iisMgr = new IISServerMgr(svr))
                        {
                            try
                            {
                                var Sites = iisMgr.GetSiteCollection();
                                foreach (var site in Sites)
                                {
                                    Console.WriteLine("Site: {0} - {1} - {2}", site.Name, site.ServerAutoStart, site.State);

                                    foreach (var app in site.Applications)
                                    {
                                        Console.WriteLine("App Pool: {0} - {1}", app.ApplicationPoolName, app.Path);

                                        foreach (var vd in app.VirtualDirectories)
                                        {
                                            Console.WriteLine("Virtual Dir: {0} - {1}", vd.Path, vd.PhysicalPath);
                                            foreach (var attr in vd.Attributes)
                                            {
                                                Console.WriteLine("Attribute: {0} - {1}", attr.Name, attr.Value);
                                            }

                                            foreach (var ce in vd.ChildElements)
                                            {
                                                foreach (var attr in ce.Attributes)
                                                {
                                                    Console.WriteLine("Child Attribute: {0} - {1}", attr.Name, attr.Value);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("Failed to get Sites for {0}. {1}", svr.HostName, ex.Message);
                                Console.ResetColor();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("General Error: {0}", ex.Message);
                    Console.ResetColor();
                }
            }

            Console.WriteLine("\n\nPress any key to exit...");
            Console.ReadLine();
        }
Example #2
0
        /// <summary>
        /// Performs all server checks on the server
        /// </summary>
        /// <param name="Server">FiOS Server</param>
        /// <returns>A health rollup containing all general server checks</returns>
        public async Task <HealthRollup> PerformServerCheck(FiOSServer Server)
        {
            var hru = new HealthRollup();
            var hce = new HealthCheckError();

            hru.Server = Server;

            if (!Server.IsActive)
            {
                hce.Result = StatusResult.Skipped;
                hce.HCType = HealthCheckType.GeneralServer;
                if (Server.IsOnline)
                {
                    hce.Error.Add("Server is marked as inactive but is currently online.");
                }
                else
                {
                    hce.Error.Add("Server is offline");
                }

                hru.Errors.Add(hce);
                return(await Task.FromResult <HealthRollup>(hru));
            }

            if (!Server.IsOnline)
            {
                hce.Error.Add(string.Format("{0} - Server is offline or unreachable via it's FQDN.", StatusResult.Critical));
                hce.Result = StatusResult.Critical;
                hru.Errors.Add(hce);
                return(await Task.FromResult <HealthRollup>(hru));
            }

            #region CheckHardDrives
            try
            {
                var hdds = await HardDrive.GetHardDriveAsync(Server.HostFullName);

                for (int i = 0; i < hdds.Length; i++)
                {
                    var result = StatusResult.Ok;

                    //If server is exempt, skip
                    if (ServerHealthCheckConfigMgr.IsExempt(Server, ExemptionType.HardDrive, hdds[i].DriveLetter))
                    {
                        hce.Error.Add(string.Format("Skipped hard drive checks for drive letter {0}. {1}GB Remaining.", hdds[i].DriveLetter, ((decimal)hdds[i].FreeSpace / 1024M / 1024M / 1024M).ToString("N1")));
                        continue;
                    }

                    //Check Hard Drive Space
                    try
                    {
                        result = await checkHDDSpaceAsync(hdds[i]);

                        if (result != StatusResult.Ok)
                        {
                            hce.Error.Add(string.Format("{0} - Disk {1} currently only has {2}GB of {3}GB of space remaining. SN: {4}.",
                                                        result,
                                                        hdds[i].Name,
                                                        ((decimal)hdds[i].FreeSpace / 1024M / 1024M / 1024M).ToString("N1"),
                                                        ((decimal)hdds[i].Capacity / 1024M / 1024M / 1024M).ToString("N1"),
                                                        hdds[i].SerialNumber));

                            hce.Result = getCorrectStatusResult(hce.Result, result);
                        }
                    }
                    catch (Exception ex)
                    {
                        hce.Error.Add(string.Format("{0} - Failed to get available disk space for {2}. Exception: {1}", StatusResult.Error, ex.Message, hdds[i].DriveLetter));
                        hce.Result = getCorrectStatusResult(hce.Result, StatusResult.Error);
                    }

                    //Check Hard Drive Status
                    try
                    {
                        result = await checkHDDStatusAsync(hdds[i]);

                        hce.Result = getCorrectStatusResult(hce.Result, result);

                        if (result != StatusResult.Ok)
                        {
                            hce.Error.Add(string.Format("{0} - Disk {1} is currently in a {2} state. Type: {3} - SN: {4}", result, hdds[i].Name, hdds[i].Status, hdds[i].DriveType, hdds[i].SerialNumber));
                        }

                        hce.Result = getCorrectStatusResult(hce.Result, result);
                    }
                    catch (Exception ex)
                    {
                        hce.Error.Add(string.Format("{0} - Failed to get hard drive status. Exception: {1}", StatusResult.Error, ex.Message));
                        hce.Result = getCorrectStatusResult(hce.Result, StatusResult.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                hce.Error.Add(string.Format("{0} - Failed to perform hard drive check. {1}", StatusResult.Error, ex.Message));
                hce.Result = getCorrectStatusResult(hce.Result, StatusResult.Error);
            }
            #endregion CheckHardDrives

            hru.Errors.Add(hce);

            return(await Task.FromResult <HealthRollup>(hru));
        }
Example #3
0
        public async Task <HealthRollup> CheckWebServer(FiOSWebServer Server)
        {
            HealthRollup     hru = new HealthRollup();
            HealthCheckError hce = new HealthCheckError();

            hru.Server = Server;
            hce.Result = StatusResult.Ok;
            hce.HCType = HealthCheckType.IIS;

            if (ServerHealthCheckConfigMgr.IsExempt(Server, ExemptionType.IIS))
            {
                hce.Result = StatusResult.Skipped;
                hru.Errors.Add(hce);
                return(await Task.FromResult <HealthRollup>(hru));
            }

            if (!Server.IsOnline)
            {
                hce.Result = StatusResult.Critical;
                hce.Error.Add(string.Format("{0} - Cannot communicate with web services due to the server being unreachable.", StatusResult.Critical));
                hru.Errors.Add(hce);
                return(await Task.FromResult <HealthRollup>(hru));
            }

            try
            {
                using (var iisManager = new IISServerMgr(Server))
                {
                    foreach (var site in iisManager.GetSiteCollection())
                    {
                        switch (site.State)
                        {
                        case ObjectState.Started:
                            break;

                        case ObjectState.Stopped:
                            hce.Result = StatusResult.Critical;
                            hce.Error.Add(string.Format("{0} - IIS Site {1} is in a stopped state.", StatusResult.Critical, site.Name));
                            break;

                        case ObjectState.Stopping:
                            hce.Result = StatusResult.Critical;
                            hce.Error.Add(string.Format("{0} - IIS Site {1} is currently in a stopping state.", StatusResult.Error, site.Name));
                            break;

                        case ObjectState.Starting:
                            hce.Result = StatusResult.Critical;
                            hce.Error.Add(string.Format("{0} - IIS Site {1} is currently starting.", StatusResult.Warning, site.Name));
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                hce.Result = StatusResult.Error;
                hce.Error.Add(string.Format("{0} - Error connecting to IIS. {1}", StatusResult.Error, ex.Message));
            }

            hru.Errors.Add(hce);
            return(await Task.FromResult <HealthRollup>(hru));
        }