ASP.net monitor server

  In this article we will see how to monitor server up or down status                                                                                                                               kick it on DotNetKicks.com

  We will monitor HTTP, HTTPs servers, POP , SMTP servers, and FTP server.

Below is sample code which explain the rest. Each request send to monitor the server is online 5-6 line of code.


Add following code in to design view of your Default.aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:TextBox ID="TextBox1" runat="server" Width="184px"></asp:TextBox>

&nbsp;&nbsp;<asp:DropDownList ID="DropDownList1" runat="server">

<asp:ListItem Value="110">POP</asp:ListItem>

<asp:ListItem Value="25">SMTP</asp:ListItem>

<asp:ListItem Value="80">HTTP</asp:ListItem>

<asp:ListItem Value="443">HTTPS</asp:ListItem>

<asp:ListItem Value="21">FTP</asp:ListItem>

<asp:ListItem Value="995">SSLPOP</asp:ListItem>

<asp:ListItem Value="465">SSLSMTP</asp:ListItem>

</asp:DropDownList><br />

<br />

<br />

<br />

&nbsp;<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><br />


</div>

</form>

</body>

</html>

 

Add below code into code behind file of default.aspx page


*********************************************************************************************************

Default.aspx.cs

 

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Button1_Click(object sender, EventArgs e)

{


switch(DropDownList1.SelectedItem.Text)

{

case "POP" :

Monitor.ServerName = TextBox1.Text;

Monitor.Port = int.Parse(DropDownList1.SelectedValue);

Response.Write(Monitor.GetStreamResponse());

break;

case "HTTP" :

Response.Write(Monitor.GethttpResponse("http://" + TextBox1.Text));

break;

case "HTTPS" :

Response.Write(Monitor.GethttpResponse("https://" + TextBox1.Text));

break;

case "SMTP" :

Monitor.ServerName = TextBox1.Text;

Monitor.Port = int.Parse(DropDownList1.SelectedValue);

Response.Write(Monitor.GetStreamResponse());

break;

case "SSLPOP":

Monitor.ServerName = TextBox1.Text;

Monitor.Port = int.Parse(DropDownList1.SelectedValue);

Response.Write(Monitor.GetSecureStreamResponse());

break;

case "SSLSMTP":

Monitor.ServerName = TextBox1.Text;

Monitor.Port = int.Parse(DropDownList1.SelectedValue);

Response.Write(Monitor.GetSecureStreamResponse());

break;

}

}

}


Add monitor.cs class in to your app_Code folder

 

***************************************************************************************************

Monitor.cs


using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.IO;

using System.Net;

using System.Net.Sockets;

using System.Net.NetworkInformation;

using System.Net.Security;

/// <summary>

/// Summary description for Monitor

/// </summary>

public class Monitor

{

private static string _serverName = string.Empty;

private static int _port = 0; // int.MinValue;

static TcpClient tcpclient = null;

static NetworkStream ns = null;

static SslStream sslstream = null;

static StreamReader reader ;

static StreamWriter writer ;

public static string ServerName

{

get { return _serverName; }

set { _serverName = value; }

}

public static Int32 Port

{

get { return _port; }

set { _port = value; }

}


public static string GetStreamResponse()

{

try

{

tcpclient = new TcpClient();

tcpclient.SendTimeout = 10000;

//DateTime start = DateTime.Now.TimeOfDay;

tcpclient.Connect(ServerName, Port);

ns = tcpclient.GetStream();

reader = new StreamReader(ns);

string response = reader.ReadLine();

//DateTime end = DateTime.Now.TimeOfDay;

return response;

}

catch (Exception ex)

{

reader.Close();

ns.Close();

tcpclient.Close();

return ex.Message;

}

finally

{

ns.Close();

tcpclient.Close();

}

}

public static string GetSecureStreamResponse()

{

try

{

tcpclient = new TcpClient();

tcpclient.SendTimeout = 10000;

tcpclient.Connect(ServerName, Port);

sslstream = new SslStream(tcpclient.GetStream());

sslstream.AuthenticateAsClient(ServerName);

reader = new StreamReader(sslstream);

string str = reader.ReadLine();

return str;

}

catch (Exception ex)

{

reader.Close();

return ex.Message;

}

finally

{


sslstream.Close();

tcpclient.Close();

}

}

public static string GethttpResponse(string domainName)

{

try

{

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(domainName);

request.Timeout = 10000;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

string str = response.StatusCode.ToString();

response.Close();

return str;

}

catch (Exception ex)

{

return ex.Message;

}

}

}


-Satalaj

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.