| |
Sign In
<%@ Page Language="C#" %><%@ Import Namespace="System.Collections" %><%@ Import Namespace="System.Collections.Generic" %><%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.OleDb" %><%@ Import Namespace="System.Data.SqlClient" %><%@ Import Namespace="System.Net" %><script RunAt="server"> /* Server Monitoring Script: * - test SQL databases by running an sql string against an SQL connection string * - test Access databases by running an sql string against a JET connection string * - test web sites by Regex matching a search string against the contents of a HttpWebRequest */ enum TestType {Sql_Server, Ms_Access, Http_Request } // different types of supported requests /// <summary> /// Container class to represent a 'test' object for a resource on the server. /// </summary> class TestObject { public TestType Type; // e.g. Sql_Server. public string TestString; // e.g. connection string for a database. or URI for http request. public string TestParam; // e.g. sql string for a database. or search string for a http request. public TestObject(TestType type, string testString, string testParam) { this.Type = type; this.TestString = testString; this.TestParam = testParam; } } void Page_Load(object sender, EventArgs e) { List<TestObject> tests = new List<TestObject>(); tests.Add(new TestObject(TestType.Sql_Server, @"Data Source=.\SQLEXPRESS;Initial Catalog=DB1;Integrated Security=True", "select top 10 * from Table1")); tests.Add(new TestObject(TestType.Sql_Server, @"Data Source=.\SQLEXPRESS;Initial Catalog=DB2;Integrated Security=True", "select top 10 * from Table1")); tests.Add(new TestObject(TestType.Sql_Server, @"Data Source=.\SQLEXPRESS;Initial Catalog=DB3;Integrated Security=True", "select top 10 * from Table1")); tests.Add(new TestObject(TestType.Sql_Server, @"Data Source=.\SQLEXPRESS;Initial Catalog=DB4;Integrated Security=True", "select top 10 * from Table1")); tests.Add(new TestObject(TestType.Ms_Access, @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Inetpub\Database\DB5.mdb;Persist Security Info=True", "select top 10 * from Table1")); tests.Add(new TestObject(TestType.Ms_Access, @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Inetpub\Database\DB6.mdb;Persist Security Info=True", "select top 10 * from Table1")); tests.Add(new TestObject(TestType.Http_Request, "http://mysite1.ie/", "Site 1")); tests.Add(new TestObject(TestType.Http_Request, "http://mysite2.ie/", "Site 2")); tests.Add(new TestObject(TestType.Http_Request, "https://mysite3.ie/", "Site 3")); tests.Add(new TestObject(TestType.Http_Request, "https://mysite4.ie/", "Site 4")); tests.Add(new TestObject(TestType.Http_Request, "https://mysite5.ie/", "Site 5")); int numCompleted = 0; int numFailed = 0; // write the HTML header. (a result is flushed to the client after each test finishes.) Flush(@" <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'><html><head><title>Server Test</title><meta name='ROBOTS' content='NOINDEX,NOFOLLOW'><link rel='stylesheet' type='text/css' href='ServerTestStyles.css' /></head><body>"); foreach(TestObject test in tests) { try { switch(test.Type) { case TestType.Sql_Server: runQuerySql(test.TestParam, test.TestString); break; case TestType.Ms_Access: runQueryOleDb(test.TestParam, test.TestString); break; case TestType.Http_Request: string pageContents = new WebClient().DownloadString(test.TestString); if(!Regex.IsMatch(pageContents, test.TestParam, RegexOptions.IgnoreCase)) throw new Exception("Search string not found: " + test.TestParam); break; default: throw new Exception("Test type not handled " + test.Type); } Flush(String.Format("<span class='pass'>Pass</span> <span class='type'>{0}</span> {1} <hr />", test.Type, test.TestString)); numCompleted++; } catch(Exception ex) { Flush(String.Format("<span class='fail'>Fail</span> {1} <span class='type'>{0}</span><BR><span class='error'>{2}</span><hr />", test.Type, test.TestString, ex.Message)); numFailed++; } } if(numFailed > 0) Flush(String.Format("<h1>{0} errors occured</h1>", numFailed)); else Flush(String.Format("<h1>All Good!</h1>", numFailed)); // if you use this page with an automated monitoring service, look for "All Good" in the page contents. otherwise an error occured Flush("</body></html>"); } /// <summary> /// Method to run an sql string against an sql database /// </summary> public static DataSet runQuerySql(string sql, string connString) { SqlConnection conn = new SqlConnection(connString); DataSet ds = new DataSet(); SqlDataAdapter dba = new SqlDataAdapter(); SqlCommand cmd = new SqlCommand(sql, conn); try { dba.SelectCommand = cmd; dba.Fill(ds, "Table"); return (ds); } catch(Exception e) { throw e; } finally { cmd.Connection.Close(); conn.Close(); } } /// <summary> /// Method to run an sql string against an Access database /// </summary> public static DataSet runQueryOleDb(string sql, string connString) { OleDbConnection conn = new OleDbConnection(connString); DataSet ds = new DataSet(); OleDbDataAdapter dba = new OleDbDataAdapter(); OleDbCommand cmd = new OleDbCommand(sql, conn); try { dba.SelectCommand = cmd; dba.Fill(ds, "Table"); return (ds); } catch(Exception e) { throw e; } finally { cmd.Connection.Close(); conn.Close(); } } /// <summary> /// Flush output to the browser (useful to indicate which tests are causing any delay) /// </summary> /// <param name="output"></param> private void Flush(string output) { Response.Write(output); Response.Flush(); } </script>
ServerTestStyles.css: (just to make the output more legible)
body{ font-size: 90%; font-family: Calibri, Helvetica, Sans-Serif; padding: .5em;}hr{ color: #87ceeb; background-color: #87ceeb; margin: .3em 0 .3em 0; padding: 0; height: 1px;}.pass{ color: Blue; font-weight: bold;}.fail{ color: Red; font-weight: bold;}.type{ color: purple; font-weight: bold;}.error{ color: Red; font-size: small;}
I have configured the test in InternetVista to search for "All Good" in the url for the test page. If this isn't present, i'll get an SMS/email alert and i can go and see what exactly is wrong. It should be fairly easy to add other test types if you have different resources you need to check on.Enjoy.
Remember Me