Home » .NET 4.0 » Website Uptime Monitor

Website Uptime Monitor

Default Page

<markup code>
< %@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" 
AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<asp:content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:content>
<asp:content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<!--page title-->
<h2> Current website status!
<div style="min-height: 20px;">
<!-- iframe loading content -->
<iframe src="real_time_site_monitor.aspx" frameborder="0" style="width: 900px; 
height: 400px;"></iframe>
</asp:content>
</asp:content>

<code behind>
Imports System.Net
Imports System.Net.Sockets
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
                            Handles Me.Load
        If Not Page.IsPostBack Then
            'page will be refereshed at a interval of 10 sec
            Response.AddHeader("Refresh", "10")
        End If
    End Sub
End Class

Site Monitor Page

<markup code>
<%@ Page Language="VB" AutoEventWireup="false" 
CodeFile="real_time_site_monitor.aspx.vb" 
Inherits="intranet_monitor" %>
<!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>Intranet Uptime Monitor
    <link href="Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
<body style="background-color: #FFF;">
    <form id="form1" runat="server">
    <p><h1>My Website</h1></p>
    <div> 
        <asp:Label ID="LblVer" runat="server"></asp:Label>
        <asp:Label ID="LbltVer" runat="server"></asp:Label> 
     </div>
    </form>
</body>
</html>


<code behind>
Imports System.Net
Imports System.Net.Sockets
Imports System.Net.Mail

Partial Class intranet_monitor
 Inherits System.Web.UI.Page
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
                           Handles Me.Load
  '-----------------'
  '     MY SERVER   '
  '-----------------'
  'define http request and url
  Dim httpReqIn As HttpWebRequest = _
      DirectCast(WebRequest.Create("http://www.mysite.com"), HttpWebRequest)
  httpReqIn.AllowAutoRedirect = False

  Try
    Dim httpResIn As HttpWebResponse = CType(httpReqIn.GetResponse(), HttpWebResponse)

    If httpResIn.StatusCode = HttpStatusCode.NotFound Then
    ' Code for moved resources goes here.
    Else
    ' the site is up
     LblInt.Text = "The site is up!<br />"
     LblInt.Attributes.Add("class", "site_status_up")
     LbltInt.Text = "Last load time: <b>" + DateTime.Now.ToString + ""
    End If

    httpResIn.Close()

  Catch ex As WebException
    If Not ex.Message.IndexOf("could not be resolved") > -1 Then
    'Could not resolve hostname.
    'This means the internet connection is down, DNS server doesn't have entry,
    'and/or the website does not exist.
      LblInt.Text = "Site is down!<br />"
      LblInt.Attributes.Add("class", "site_status_down")
      LbltInt.Text = "Last load time: <b>" + DateTime.Now.ToString + ""
    
    'send automated email notification
     Dim SmtpServer As New SmtpClient
     Dim mail As New MailMessage
     SmtpServer.Port = 25
     SmtpServer.Host = "smpt.mywebsite.com" ' - IP address (x.x.x.x)
     mail = New MailMessage()
     mail.From = New MailAddress("downtime@mysite.com")
     mail.To.Add("myemail@vmywebsite.com")
     mail.Subject = "Web server is down"
     mail.Body = "<div>The server appears to be down"
     mail.IsBodyHtml = True
     SmtpServer.Send(mail)
    Else
    End If

   End Try
  End Sub
End Class