public async Task <string[]> GetIMGVersionsAsync(FiOSServer Server) { if (string.IsNullOrEmpty(this.beIMGAppConnectionStr)) { throw new Exception("No connection string for the backend IMG application database could be found."); } StringBuilder sb = new StringBuilder(); sb.AppendLine("SELECT DISTINCT c.strSlicerVersionId FROM tFiOSVHOServerMap a (NOLOCK)"); sb.AppendLine("INNER JOIN tFIOSRegion b (NOLOCK) ON b.strVHOId = a.strVHOId"); sb.AppendLine("INNER JOIN tFIOSSliceFileVersion c (NOLOCK) ON c.strFiosRegionID = b.strFIOSRegionId"); sb.AppendLine("INNER JOIN tFIOSServers d (NOLOCK) ON d.strServerId = a.strServerId"); sb.AppendLine("INNER JOIN tFIOSDataSlicerRegionsProcess e (NOLOCK) ON e.strFIOSRegionId = b.strFIOSRegionId AND e.strSlicerVersionID = c.strSlicerVersionId"); sb.AppendFormat("WHERE strServerTypeId = 'LOADBAL' AND a.strServerId = {0}", Server.HostName); List <string> versions = new List <string>(); await DBFactory.SQL_ExecuteReaderAsync(this.beIMGAppConnectionStr, sb.ToString(), System.Data.CommandType.Text, null, (dr) => { while (dr.Read()) { versions.Add(dr.GetString(0)); } }); return(await Task.FromResult <string[]>(versions.ToArray())); }
private static void processIncludeExclude(ref HCWinService winService, XElement elem, bool isInclude) { foreach (var ele in elem.Elements()) { switch (ele.Name.LocalName.ToUpper()) { case "FUNCTION": { if (!ele.HasAttributes) { throw new FormatException("XML format error. Function element for {0} must contain attributes"); } if (ele.Attributes().Any(x => x.Name.LocalName.ToUpper().Equals("ALL"))) { foreach (var allAttr in ele.Attributes().Where(x => x.Name.LocalName.ToUpper().Equals("ALL"))) { winService.Function = getServerFunction(allAttr.Value); } } break; } case "ROLES": { winService.Roles.AddRange(processWinServiceRoles(isInclude, ele)); break; } case "SERVERS": { foreach (var svrEle in ele.Descendants(_ns + "Server")) { var fs = new FiOSServer(); fs.HostName = svrEle.Attribute("Name").Value; fs.IPAddress = svrEle.Attribute("IP").Value; fs.HostLocation = svrEle.Attribute("Domain").Value.ToUpper().Contains("VHE") ? ServerLocation.VHE : svrEle.Attribute("Domain").Value.ToUpper().Contains("VHO") ? ServerLocation.VHO : ServerLocation.Unknown; //if (svrEle.Ancestors().Any(x => x.Name.LocalName.ToUpper().Equals("EXCLUDE"))) // winService.Servers.Add(new Tuple<bool, FiOSServer>(false, fs)); //else winService.Servers.Add(new Tuple <bool, FiOSServer>(isInclude, fs)); } break; } } //End include child element switch } //End foreach include element }
/// <summary> /// Returns whether the server is currently online or not /// </summary> /// <param name="serverFullName">FQDN of the server</param> /// <returns>True if server is online, False otherwise</returns> public static bool getIsOnline(FiOSServer server) { PingReply pingReply; using (var ping = new Ping()) { try { pingReply = ping.Send(server.HostFullName); } catch { return(false); } } return(pingReply.Status == IPStatus.Success); }
public static bool IsExempt(FiOSServer Server, ExemptionType ExemptType, params string[] Args) { var exemptionsEle = _config.Root.Descendants(_ns + "Exemptions"); var exemptTypeEle = exemptionsEle.Elements().Where(x => x.Name.LocalName.ToUpper() == ExemptType.ToString().ToUpper()); if (exemptTypeEle.Count() == 0) { return(false); } if (exemptTypeEle.Descendants().Any(x => x.Name.LocalName.ToUpper().Equals("SERVER")) && !exemptTypeEle.Descendants(_ns + "Server").Attributes("Name").Select(x => x.Value).Any(x => x.Equals(Server.HostName))) { return(false); } if (ExemptType == ExemptionType.HardDrive) { var driveLetters = exemptTypeEle.Elements(_ns + "DriveLetter").Select(x => x.Value); bool containsAll = true; foreach (var dl in driveLetters) { if (!(Args.Contains(dl) || Args.Contains(dl + ":")) && containsAll) { containsAll = false; } } return(containsAll); } else if (ExemptType == ExemptionType.IIS) { return(true); } return(false); }
/// <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)); }
public static async Task <IEnumerable <HealthRollup> > CheckWindowsServices(FiOSServer Server, HCWinService[] ServicesToCheck) { var hruList = new List <HealthRollup>(); var hcErrors = new List <HealthCheckError>(); if (!Server.IsOnline || !Server.IsActive) { var svcFuncts = ServicesToCheck.GroupBy(x => x.Function) .Select(x => new { x.Key, RoleFunctions = x.Where(y => y.Function.Equals(x.Key)).SelectMany(y => y.Roles.Select(z => z.Item3)).ToList(), ServerCount = x.Where(y => y.Function.Equals(x.Key)).SelectMany(y => y.Servers).Count(), }).ToList(); svcFuncts.ForEach(x => { var hcErr = new HealthCheckError(); if (Server.IsActive) { hcErr.Result = StatusResult.Critical; } else { hcErr.Result = StatusResult.Skipped; } if (x.Key == ServerFunction.Database || (x.RoleFunctions.Count > 0 && x.RoleFunctions.Any(y => y.Equals(ServerFunction.Database)))) { hcErr.HCType = HealthCheckType.Database; } else if (x.Key == ServerFunction.Web || (x.RoleFunctions.Count > 0 && x.RoleFunctions.Any(y => y.Equals(ServerFunction.Web)))) { hcErr.HCType = HealthCheckType.IIS; } if (!(hcErr.HCType == HealthCheckType.GeneralServer) && Server.IsActive) { hcErr.Error.Add(string.Format("Cannot check windows services because the server is offline.")); } hcErrors.Add(hcErr); }); var hru = new HealthRollup() { Server = Server, Errors = hcErrors }; hruList.Add(hru); return(await Task.FromResult <IEnumerable <HealthRollup> >(hruList)); } hcErrors = await checkWindowsServices(Server.HostFullName, ServicesToCheck.Where(x => !x.OnePerGroup).ToArray()); foreach (var result in hcErrors.GroupBy(x => x.HCType).Select(x => new HealthCheckError() { HCType = x.Key, Result = x.Where(y => y.HCType.Equals(x.Key)).Select(y => y.Result).Max(), Error = x.Where(y => y.HCType.Equals(x.Key)).SelectMany(y => y.Error).ToList() })) { var hru = new HealthRollup(); hru.Server = Server; hru.Errors.Add(result); hruList.Add(hru); } return(await Task.FromResult <IEnumerable <HealthRollup> >(hruList)); }
public static IEnumerable <FiOSServer> GetServers() { var serverElems = _config.Root.Descendants(_ns + "Server"); foreach (var serverElem in serverElems) { if (serverElem.HasAttributes) { if (serverElem.Attributes().Any(x => x.Name.LocalName.ToUpper().Equals("FUNCTION"))) { if (serverElem.Attribute("Function").Value.ToUpper().Equals("DATABASE")) { string vhoName = null; var dbServer = new FiOSDbServer(); dbServer.HostFunction = ServerFunction.Database; dbServer.HostName = getName(serverElem); dbServer.HostFullName = getFullName(dbServer.HostName); dbServer.HostLocation = getLocation(serverElem, out vhoName); dbServer.HostLocationName = vhoName; dbServer.HostRole = getRole(serverElem, dbServer.HostLocation); dbServer.IPAddress = getIP(serverElem); dbServer.IsActive = getIsActive(serverElem); yield return(dbServer); } else if (serverElem.Attribute("Function").Value.ToUpper().Equals("WEB")) { string vhoName = null; var webServer = new FiOSWebServer(); webServer.HostFunction = ServerFunction.Web; webServer.HostName = getName(serverElem); webServer.HostFullName = getFullName(webServer.HostName); webServer.HostLocation = getLocation(serverElem, out vhoName); webServer.HostLocationName = vhoName; webServer.HostRole = getRole(serverElem, webServer.HostLocation); webServer.IPAddress = getIP(serverElem); webServer.IsActive = getIsActive(serverElem); yield return(webServer); } } else { string vhoName = null; var server = new FiOSServer(); server.HostName = getName(serverElem); server.HostFullName = getFullName(server.HostName); server.HostLocation = getLocation(serverElem, out vhoName); server.HostLocationName = vhoName; server.HostRole = getRole(serverElem, server.HostLocation); server.HostFunction = serverElem.HasAttributes && serverElem.FirstAttribute.Value.ToUpper().Equals("APP") ? ServerFunction.Application : serverElem.Ancestors().Any(x => x.Name.LocalName.ToUpper().Equals("INFRASTRUCTURE")) || serverElem.Ancestors().Any(x => x.Name.LocalName.ToUpper().Equals("DOMAINCONTROLLERS")) ? ServerFunction.Infrastructure : ServerFunction.Unknown; server.IPAddress = getIP(serverElem); server.IsActive = getIsActive(serverElem); yield return(server); } } else { throw new FormatException("XML Format error. Server element has no attributes."); } } }