示例#1
0
        /// <summary>
        /// Execute
        /// </summary>
        /// <returns></returns>
        protected override bool Execute()
        {
            string webconfig = AppDomain.CurrentDomain.BaseDirectory + "Web.config";
            // To get the memcached server data from web.config because not part of appsettings
            XDocument config = XDocument.Load(webconfig);

            var port = config.Descendants("enyim.com")?.Descendants("servers")?.Descendants("add")?.Attributes("port");

            var address = config.Descendants("enyim.com")?.Descendants("servers")?.Descendants("add")?.Attributes("address");

            ServerStats stats = MemCacheD.GetStats();

            if (stats == null)
            {
                return(false);
            }

            Dictionary <string, string> result = new Dictionary <string, string>();

            if (!(port == null || address == null))//if port or address are null return false otherwise get the stats of the first server
            {
                for (int i = 0; i < Enum.GetNames(typeof(StatItem)).Length; i++)
                {
                    // takes first item from each of address and port
                    var    value = stats.GetRaw(new IPEndPoint(IPAddress.Parse(address.First().Value.ToString()), Int32.Parse(port.First().Value)), (StatItem)i);
                    string key   = ((StatItem)i).ToString();
                    result.Add(key, value);
                }
                Response.data = result;
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public InstanceStatistics(System.Net.IPEndPoint serverEndPoint, Server server, Instance instance, ServerStats serverStats, State serviceState)
        {
            string memcachedFileVersion = null;

            if (File.Exists(Configuration.Default.MemcachedBinarySource) == true)
            {
                memcachedFileVersion = FileVersionInfo.GetVersionInfo(Configuration.Default.MemcachedBinarySource).FileVersion;
            }

            _serverName   = server.ServerName;
            _instanceName = instance.DisplayName;
            _serviceState = serviceState;

            Instance = instance;

            _statusIconIndex = StatusIconIndex.CommunicationError;
            _statusTooltip   = Constants.TooltipCommunicationError;

            if (serverStats != null && serverStats.GetRaw(serverEndPoint, StatItem.Version) != null)
            {
                foreach (StatItem statItem in Enum.GetValues(typeof(Enyim.Caching.Memcached.StatItem)))
                {
                    switch (statItem)
                    {
                    case StatItem.BytesRead:
                        _bytesRead = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.BytesWritten:
                        _bytesWritten = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.ConnectionCount:
                        _connectionCount = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.ConnectionStructures:
                        _connectionStructures = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.GetCount:
                        _getCount = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.GetHits:
                        _getHits = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.GetMisses:
                        _getMisses = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.ItemCount:
                        _itemCount = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.MaxBytes:
                        _maxBytes = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.ServerTime:
                        string serverTimeRaw = serverStats.GetRaw(serverEndPoint, statItem);
                        int    serverTimeSeconds;
                        if (serverTimeRaw != null && Int32.TryParse(serverTimeRaw, out serverTimeSeconds) == true)
                        {
                            _serverTime = new DateTime(1970, 1, 1).AddSeconds(serverTimeSeconds).ToLocalTime().ToString();
                        }
                        else
                        {
                            _serverTime = "<unknown>";
                        }
                        break;

                    case StatItem.SetCount:
                        _setCount = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.TotalConnections:
                        _totalConnections = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.TotalItems:
                        _totalItems = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.Uptime:
                        string uptimeRaw = serverStats.GetRaw(serverEndPoint, statItem);
                        int    uptimeSeconds;
                        if (uptimeRaw != null && Int32.TryParse(uptimeRaw, out uptimeSeconds) == true)
                        {
                            _uptime = TimeSpan.FromSeconds(uptimeSeconds).ToString();
                        }
                        else
                        {
                            _uptime = "<unknown>";
                        }
                        break;

                    case StatItem.UsedBytes:
                        _usedBytes = serverStats.GetValue(serverEndPoint, statItem);
                        break;

                    case StatItem.Version:
                        _version = serverStats.GetRaw(serverEndPoint, statItem);
                        break;
                    }
                }
            }

            if (_serviceState != State.Running && _serviceState != State.Unknown)
            {
                _statusIconIndex = StatusIconIndex.ServiceDown;
                _statusTooltip   = Constants.TooltipServiceDown;
            }
            else if (_serviceState == State.Unknown)
            {
                _statusIconIndex = StatusIconIndex.ServiceNonControllable;
                _statusTooltip   = Constants.TooltipServiceNonControllable;
            }
            else if (_version == null || _version == String.Empty)
            {
                _statusIconIndex = StatusIconIndex.CommunicationError;
                _statusTooltip   = Constants.TooltipCommunicationError;
            }
            else if (memcachedFileVersion != null && memcachedFileVersion != _version)
            {
                _statusIconIndex = StatusIconIndex.NeedsUpdate;
                _statusTooltip   = Constants.TooltipNeedsUpdate;
            }
            else
            {
                _statusIconIndex = StatusIconIndex.Up;
                _statusTooltip   = Constants.TooltipUp;
            }
        }