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> /// Check if a windows service meets expected parameters on at least one of an array of servers /// </summary> /// <param name="ServerNames">The group of server names to check services</param> /// <param name="ServiceToCheck">The services that only one of the servers in the group must meet the parameters</param> /// <returns></returns> public static async Task <List <HealthCheckError> > CheckWindowsServices(string[] ServerNames, HCWinService ServiceToCheck) { List <HealthCheckError> lstHce = new List <HealthCheckError>(); if (!ServiceToCheck.OnePerGroup) { return(lstHce); } for (int i = 0; i < ServerNames.Length; i++) { foreach (var result in await checkWindowsServices(ServerNames[i], new HCWinService[1] { ServiceToCheck })) { if (result.Error.Count < 1) { lstHce.Clear(); lstHce.Add(result); return(lstHce); } else { var errs = new List <string>(); result.Error.ForEach(x => errs.Add(string.Format("{0}. At least one server within the same \"cluster\" as {1} requires this to be corrected.", x, ServerNames[i]))); result.Error.Clear(); result.Error.AddRange(errs); lstHce.Add(result); } } } return(lstHce); }
public static IEnumerable <HCWinService> GetWindowsServicesToCheck() { var serviceElems = _config.Root.Descendants(_ns + "Services"); foreach (var serviceElem in serviceElems.Elements()) { HCWinService winService = new HCWinService(); winService.Function = ServerFunction.Unknown; if (serviceElem.HasAttributes) { winService.Name = serviceElem.Attribute("Name").Value; winService.DisplayName = serviceElem.Attribute("DisplayName").Value; } else { throw new FormatException("XML Format error. Attributes missing on WindowsService element."); } //Check for defaults if (serviceElem.HasElements && serviceElem.Elements().Any(x => x.Name.LocalName.ToUpper().Equals("DEFAULTS"))) { var defaultsEle = serviceElem.Element(_ns + "Defaults"); foreach (var def in defaultsEle.Elements()) { switch (def.Name.LocalName.ToUpper()) { case "STATUS": { switch (def.Value.ToUpper()) { case "RUNNING": winService.CheckStatus.Add(ServiceControllerStatus.Running); break; case "STOPPED": winService.CheckStatus.Add(ServiceControllerStatus.Stopped); break; case "PAUSED": winService.CheckStatus.Add(ServiceControllerStatus.Paused); break; default: throw new FormatException(string.Format("XML format error. {0} is not a valid windows service status for {1}", def.Value, winService.Name)); } break; } case "STARTUPTYPE": { switch (def.Value.ToUpper()) { case "AUTOMATIC": winService.CheckStartupType.Add(ServiceStartMode.Automatic); break; case "MANUAL": winService.CheckStartupType.Add(ServiceStartMode.Manual); break; case "DISABLED": winService.CheckStartupType.Add(ServiceStartMode.Disabled); break; default: throw new FormatException(string.Format("XML format error. {0} is not a valid windows service start mode for {1}", def.Value, winService.Name)); } break; } case "LOGONAS": { winService.CheckLogonAs.Add(Toolset.RemoveWhitespace(def.Value).ToUpper()); } break; default: throw new FormatException(string.Format("XML format error. {0} is not a valid child element for defaults.", def.Name.LocalName)); } //End Switch - Default Element Name } //End Foreach - Default Element try { if (defaultsEle.HasAttributes && defaultsEle.FirstAttribute.Name.LocalName.ToUpper().Equals("ONEPERGROUP")) { winService.OnePerGroup = bool.Parse(defaultsEle.FirstAttribute.Value); } } catch { throw new FormatException(string.Format("XML format error. Invalid value for defaults attribute ONEPERGROUP for windows service {0}", winService.Name)); } } //End If - Defaults Check var svcElems = serviceElem.Elements(); //Check for Includes if (serviceElem.HasElements && serviceElem.Elements().Any(x => x.Name.LocalName.ToUpper().Equals("INCLUDE"))) { processIncludeExclude(ref winService, serviceElem.Element(_ns + "Include"), true); } //Check for Excludes if (serviceElem.HasElements && serviceElem.Elements().Any(x => x.Name.LocalName.ToUpper().Equals("EXCLUDE"))) { processIncludeExclude(ref winService, serviceElem.Element(_ns + "Exclude"), false); } yield return(winService); } //End Foreach - Service Element }