public static string GetDisplayString(ObjectState state) { Type type = typeof(ObjectState); FieldInfo fieldInfo = type.GetField(state.ToString()); Attribute attr = fieldInfo.GetCustomAttribute(typeof(EnumDisplayStringAttribute)); var dispAttr = (EnumDisplayStringAttribute)attr; if (dispAttr == null) { return state.ToString(); } return dispAttr.DisplayString; }
public static string GetString(this ObjectState state) { try { return(state.ToString()); } catch (Exception) { } return(null); }
/// <summary> /// 获取应用程序池和站点的状态 /// </summary> /// <param name="serverIP">服务器IP</param> /// <param name="webName">站点名称</param> /// <returns></returns> public static string GetWebState(string serverIP, string webName) { ObjectState poolState = ObjectState.Unknown; ObjectState siteState = ObjectState.Unknown; using (ServerManager sm = ServerManager.OpenRemote(serverIP)) { //应用程序池 ApplicationPool appPool = sm.ApplicationPools.FirstOrDefault(x => x.Name.ToUpper() == webName.ToUpper()); if (appPool != null) { poolState = appPool.State; } //Site Site site = sm.Sites.FirstOrDefault(x => x.Name.ToUpper() == webName.ToUpper()); if (site != null) { siteState = site.State; } } return($"{poolState.ToString()} | {siteState.ToString()}"); }
/// <summary> /// Notifies the observers. /// </summary> public static void NotifyObservers(ObjectState state) { try { LogManager.WriteLog("|##> Object State : " + state.ToString(), LogManager.enumLogLevel.Info); foreach (ObjectStateObserver observer in GetObservers()) { try { observer.NotifyState(state); } catch (Exception ex) { ExceptionManager.Publish(ex); } } } catch (Exception ex) { ExceptionManager.Publish(ex); } }
private static Site ParseSite(Microsoft.Web.Administration.Site site, bool excludeAppPools = false) { if (site == null) { return(null); } var servantSite = new Site { IisId = (int)site.Id, Name = site.Name, ApplicationPool = site.Applications[0].ApplicationPoolName, SitePath = site.Applications[0].VirtualDirectories[0].PhysicalPath, SiteState = (InstanceState)Enum.Parse(typeof(InstanceState), site.State.ToString()), LogFileDirectory = site.LogFile.Directory, Bindings = GetBindings(site).ToList(), }; if (!excludeAppPools) { using (var manager = new ServerManager()) { ObjectState applicationPoolState = manager.ApplicationPools[site.Applications[0].ApplicationPoolName].State; servantSite.ApplicationPoolState = (InstanceState)Enum.Parse(typeof(InstanceState), applicationPoolState.ToString()); } } if (site.Applications.Count > 1) { foreach (var application in site.Applications.Skip(1)) { servantSite.Applications.Add(new SiteApplication { ApplicationPool = application.ApplicationPoolName, Path = application.Path, DiskPath = application.VirtualDirectories[0].PhysicalPath, }); } } return(servantSite); }
public override async Task <State> Poll() { ServerManager server = null; ObjectState stateResult = ObjectState.Unknown; State state = default(State); try { server = ServerManager.OpenRemote(serverInstance.Name); if (server != null && this.Exist()) { state = new State(); state.Url = string.Concat(this.Name, "@", serverInstance.Name); stateResult = server.ApplicationPools[Name].State; if (stateResult == ObjectState.Started) { if (server.ApplicationPools[Name].WorkerProcesses.Count > 0) { WorkerProcess wp = server.ApplicationPools[Name].WorkerProcesses .Where(p => p .AppPoolName.ToLowerInvariant() == Name.ToLowerInvariant()) .FirstOrDefault(); PerformanceCounter cpuCounter = new PerformanceCounter("Process" , "% Processor Time" , Process.GetProcessById(wp.ProcessId, serverInstance.Name).ProcessName , serverInstance.Name); cpuPercent = cpuCounter.NextValue(); await Task.Delay(2000); cpuPercent = cpuCounter.NextValue(); await Task.Delay(30000); //the requests interval time is now 30000 int numOfRequests = wp.GetRequests(30000).Count; float cpuPercentAfterThirthySeconds = cpuCounter.NextValue(); if (cpuPercent >= _cpuAlertTreshold && cpuPercentAfterThirthySeconds >= _cpuAlertTreshold) { state.Status = "High CPU Load"; state.Description = string.Format("The CPU for app pool {0} on {1} has reach {2}% and may cause HTTP 503 errors.\r Current Number of Requests: {3}", Name, serverInstance.Name, cpuPercent, numOfRequests); state = GetCommonState(server, state, numOfRequests); } else { state.Status = _positiveStatusCode; } if (!string.IsNullOrEmpty(state.Status)) { server.Dispose(); cpuCounter.Close(); cpuCounter.Dispose(); } } else { state.Status = _positiveStatusCode; } return(state); } else { state.Description = stateResult == ObjectState.Started ? string.Format("The app pool on server {0} is in the following in {1} state, the number of worker processes is {2}", serverInstance.Name, stateResult.ToString(), server.ApplicationPools[Name].WorkerProcesses.Count) : "Unavailable"; state.Status = stateResult == ObjectState.Started ? _positiveStatusCode: "App Pool Not Started"; state = GetCommonState(server, state, 0); if (stateResult == ObjectState.Stopped) { server.ApplicationPools[Name].Start(); state.Description += server.ApplicationPools[Name].State == ObjectState.Started ? "\rResolution: The appPool was started\r" : state.Description; } } } } catch (Exception ex) { state.Status = ex.GetType().Name; state.Description = "'polling failed with" + ex.Message; state.Url = string.Concat(this.Name, "@", serverInstance.Name); return(state); } return(state); }
/// <summary> /// Get any differences between the old and new property values. /// </summary> /// <param name="prevValue">Prevous property value.</param> /// <param name="newValue">New property value.</param> /// <param name="ObjectState">The state of the object.</param> /// <param name="ObjectType">The type of the object.</param> /// <param name="propertyInfo">The property being evaluated.</param> /// <returns></returns> private ChangeLogItem GetChangeLogItem(Object prevValue, Object newValue, ObjectState ObjectState, Type ObjectType, PropertyInfo propertyInfo) { ChangeLogItem lItem = new ChangeLogItem(); lItem.OwnerID = this.OwnerID; lItem.OwnerName = ObjectType.Name; lItem.PropertyName = propertyInfo.Name; lItem.Type = ObjectState.ToString().Replace("ToBe", String.Empty); // Get rid of future tense from state name. if (Object.ReferenceEquals(propertyInfo.PropertyType, typeof(String))) { // COMPARE STRINGS String logPrevious = HString.SafeTrim(prevValue); String logNew = HString.SafeTrim(newValue); if (!logNew.Equals(logPrevious)) { lItem.PreviousValue = logPrevious; lItem.NewValue = logNew; return(lItem); } } else if (Object.ReferenceEquals(propertyInfo.PropertyType, typeof(Int32))) { // COMPARE INTEGERS Int32 logPrevious = HNumeric.GetSafeInteger(prevValue); Int32 logNew = HNumeric.GetSafeInteger(newValue); if (!logNew.Equals(logPrevious)) { lItem.PreviousValue = logPrevious.ToString(); lItem.NewValue = logNew.ToString(); return(lItem); } } else if (Object.ReferenceEquals(propertyInfo.PropertyType, typeof(Decimal))) { // COMPARE DECIMALS Decimal logPrevious = HNumeric.GetSafeDecimal(prevValue); Decimal logNew = HNumeric.GetSafeDecimal(newValue); if (!logNew.Equals(logPrevious)) { lItem.PreviousValue = logPrevious.ToString(); lItem.NewValue = logNew.ToString(); return(lItem); } } else if (Object.ReferenceEquals(propertyInfo.PropertyType, typeof(DateTime))) { // COMPARE DATETIMES DateTime logPrevious = HDateTime.GetDateTime(prevValue); DateTime logNew = HDateTime.GetDateTime(newValue); if (!logNew.Equals(logPrevious)) { lItem.PreviousValue = logPrevious.ToString(); lItem.NewValue = logNew.ToString(); return(lItem); } } else if (propertyInfo.PropertyType.IsEnum) { // COMPARE ENUMS Int32 logPrevious = Convert.ToInt32(prevValue); Int32 logNew = Convert.ToInt32(newValue); if (!logNew.Equals(logPrevious)) { lItem.PreviousValue = logPrevious.ToString(); lItem.NewValue = logNew.ToString(); return(lItem); } } return(null); }
private static IisSite ParseSite(Microsoft.Web.Administration.Site site, bool excludeAppPools = false, List <Microsoft.Web.Administration.ApplicationPool> applicationPools = null) { if (site == null) { return(null); } var servantSite = new IisSite { IisId = (int)site.Id, Name = site.Name, ApplicationPool = site.Applications[0].ApplicationPoolName, SitePath = site.Applications[0].VirtualDirectories[0].PhysicalPath, SiteState = (InstanceState)Enum.Parse(typeof(Shared.Objects.Enums.InstanceState), site.State.ToString()), LogFileDirectory = site.LogFile.Directory, Bindings = GetBindings(site).ToList(), }; if (!excludeAppPools) { if (applicationPools == null) { using (var manager = new ServerManager()) { applicationPools = manager.ApplicationPools.ToList(); } } ObjectState applicationPoolState = applicationPools.Single(x => x.Name == site.Applications[0].ApplicationPoolName).State; servantSite.ApplicationPoolState = (InstanceState)Enum.Parse(typeof(InstanceState), applicationPoolState.ToString()); } foreach (var directory in site.Applications[0].VirtualDirectories.Skip(1)) { servantSite.Applications.Add(new SiteApplication { ApplicationPool = "", Path = directory.Path, DiskPath = directory.PhysicalPath, IsApplication = false }); } if (site.Applications.Count > 1) { foreach (var application in site.Applications.Skip(1)) { servantSite.Applications.Add(new SiteApplication { ApplicationPool = application.ApplicationPoolName, Path = application.Path, DiskPath = application.VirtualDirectories[0].PhysicalPath, IsApplication = true }); } } return(servantSite); }
public static EntityState ConvertState(this ObjectState state) { return((EntityState)Enum.Parse(typeof(EntityState), state.ToString())); }