Memcached monitoring tool for ASP. NET

Source: Internet
Author: User
Tags php memcached

In the previous article "using Memcached to Improve the Performance of. NET Applications", Zhou Gong described how to use Memcached in. NET to Improve the Performance of. NET applications. In actual use, Memcached may be suspended for some unpredictable reasons. Once such a situation occurs, it will again put a huge pressure on the database, therefore, you must monitor the running status of Memcached. Zhou Gong searched for the Memcached monitoring tool for PHP on the Internet. On the PHP page, he can see the running status of each Memcached. Once he cannot obtain the data, it indicates that Memcached is inaccessible because of network failure or Memcached fails. Although the cause is different, the result is the same. According to the implementation of the Enyim Memcached and PHP Memcached monitoring tools, Zhou Gong implemented a. NET monitoring tool.
Implementation
In the previous article, use Memcached to improve performance. in "NET application performance", Zhou Gong told us that Memcached can be obtained through Telnet and Memcached data can be obtained through the "stats" command, if no data is available, Memcached cannot be accessed.
The data obtained by sending the "stats" command to Memcached is as follows:
Pid: 32u, server process ID.
Uptime: 32u, server running time, in seconds.
Time: 32u, the current UNIX time of the server.
Version: string, the server version number.
Curr_items: 32u, number of contents currently stored by the server Current number of items stored by the server
Total_items: 32u, total number of contents stored since the server was started.
Bytes: 64u, the number of bytes occupied by the server's current storage content.
Curr_connections: 32u, number of connections.
Total_connections: 32u, total number of connections received since the server was running.
Connection_structures: 32u, number of connection structures allocated by the server.
Cmd_get: 32u, total number of retrieved requests.
Pai_set: 32u, total number of stored requests.
Get_hits: 32u, the total number of successful requests.
Get_misses: 32u, the total number of failed requests.
Bytes_read: 64u, the total number of bytes that the server reads from the network.
Bytes_written: 64u, the total number of bytes sent by the server to the network.
Limit_maxbytes: 32u, total number of bytes that can be used by the server during storage.
In the above description, 32u and 64u indicate 32-bit and 64-bit unsigned integers, and string indicates string type data.
In this article, we connect to Memcached through Socket instead of Telnet and then parse the returned data.
Program code
For ease of management and maintenance, the single page mode is used in this example, that is, all the code is in An ASPX page and there is no corresponding aspx. cs page.
The program code is as follows:
<% @ Page Language = "C #" %>
<% @ Import Namespace = "System" %>
<% @ Import Namespace = "System. IO" %>
<% @ Import Namespace = "System. Net" %>
<% @ Import Namespace = "System. Net. Sockets" %>
<% @ Import Namespace = "System. Collections. Generic" %>
<% @ Import Namespace = "System. Threading" %>
<% @ Import Namespace = "System. Security" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Mce: script runat = "server"> <! --
/*
* Author: Zhou Gong
* Date: 2011-03-27
* Source: http://blog.csdn.net/zhoufoxcn or http://zhoufoxcn.blog.51cto.com
* Copyright Description: This article may be used for non-commercial purposes if the source of the original document is retained. Zhou Gong does not provide any warranty or commitment to this document.
**/

/// <Summary>
/// Memcached server monitoring class
/// </Summary>
Public class MemcachedMonitor
{
/// <Summary>
/// Timeout for connecting to Memcached
/// </Summary>
Public TimeSpan ConnectionTimeout {get; set ;}
/// <Summary>
/// Timeout for receiving data returned by Memcached
/// </Summary>
Public TimeSpan ReceiveTimeout {get; set ;}
Private List <IPEndPoint> serverList;
Public MemcachedMonitor (ICollection <IPEndPoint> list)
{
ConnectionTimeout = TimeSpan. FromSeconds (10 );
ReceiveTimeout = TimeSpan. FromSeconds (20 );
ServerList = new List <IPEndPoint> ();
ServerList. AddRange (list );
}

Public List <MemcachedServerStats> GetAllServerStats ()
{
List <MemcachedServerStats> resultList = new List <MemcachedServerStats> ();
Foreach (IPEndPoint endPoint in serverList)
{
ResultList. Add (GetServerStats (endPoint, ConnectionTimeout, ReceiveTimeout ));
}
Return resultList;
}

Public static MemcachedServerStats GetServerStats (IPEndPoint ip, TimeSpan connectionTimeout, TimeSpan receiveTimeout)
{
MemcachedSocket socket = new MemcachedSocket (ip, connectionTimeout, receiveTimeout );
MemcachedServerStats stats = socket. GetStats ();
Return stats;
}

Public static IPEndPoint Parse (string hostName, int port)
{
IPHostEntry host = Dns. GetHostEntry (hostName );
IPEndPoint endPoint = null;
Foreach (IPAddress ip in host. AddressList)
{
If (ip. AddressFamily = AddressFamily. InterNetwork)
{
EndPoint = new IPEndPoint (ip, port );
Break;
}
}
Return endPoint;
}
}
/// <Summary>
/// Memcached server running status data class. The data obtained only when the IsReachable is true is meaningful. Otherwise, the data cannot be accessed or Memcached is suspended.
/// </Summary>
Public class MemcachedServerStats
{
Private Dictionary <string, string> results;
/// <Summary>
& Nb

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.