Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Low-Cost High Availability: Simple Database Monitoring in a Windows Environment : Page 2

The .NET Framework 2.0 and Visual Studio 2005 make it easy to create a basic but extensible database-monitoring solution without a lot of complexity or coding.


advertisement

Getting Started with E-mail Notifications

Windows Services are harder to develop and debug because you can't just step into them like you would with a Windows Form or Console Application. A good solution is putting the entire database-monitoring code into a class library and then adding the class library to a Console Application that you can easily manipulate. Once you've got all your class library code working in a Console Application, you can add the class library to your Windows Service.

Your first task should be getting the e-mail notifications to work. This will probably be the most difficult part of the application to debug if it doesn't work. You should find e-mail problems early because resolving them might require the assistance of others, such as network administrators or Exchange staff. Depending on how your organization configures security, you may run into security issues as well.

Take these steps to enable e-mail notifications:

  1. Create a Console Application named TestMonitor.
  2. Delete the Program.cs (or .vb) file and add a new class library file called TestMonitor.cs (or .vb).
  3. Add an application configuration file.
  4. Add the entries for the e-mail parameters to the application configuration file as follows:


    <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="IntervalMsec" value="2000" /> <add key="RepeatNotificationMinutes" value="1" /> <add key="SMTPServer" value="127.0.0.1" /> <add key="SMTPPort" value="25" /> <add key="MailFrom" value="yourAddress@yourAddress.com" /> <add key="MailTo" value="yourAddress@yourAddress.com" /> </appSettings> </configuration>

    • IntervalMsec is the number of milliseconds the application waits before testing the database connection again.
    • RepeatNotificationMinutes is the number of minutes the application waits before sending another e-mail about the same database connection failing.
    • SMTPServer is the local machine running the database monitoring application.
    • SMTPPort is the port number SMTP is using on the local machine.
    • MailFrom is the e-mail address that appears as the From field of the e-mail message.
    • MailTo is the e-mail address that appears as the To field of the e-mail message.
  5. Create a class library project named DbMonitorWrapper.
  6. Delete the existing Class1.cs (or .vb) file. Add a new class library file and name it DbMonitor. Within the file, you must add public to the class declaration.
  7. Add a reference to System.Configuration. Add a C# using statement or VB Imports statement for System.Net.Mail.
  8. Add a C# using statement or VB Imports statement for System.Configuration.
  9. Add the code to read the AppSettings from the app.config file, and create a function to send e-mail. The following is the completed code:

    using System; using System.Collections.Generic; using System.Text; using System.Configuration; using System.Net.Mail; namespace DbMonitorWrapper { public class DbMonitor { static string SMTPServer = ConfigurationManager.AppSettings["SMTPServer"]; static int SMTPPort = Convert.ToInt32( ConfigurationManager.AppSettings["SMTPPort"]); static string MailFrom = ConfigurationManager.AppSettings["MailFrom"]; static string MailTo = ConfigurationManager.AppSettings["MailTo"]; public static int IntervalMsec = Convert.ToInt32( ConfigurationManager.AppSettings["IntervalMsec"]); static int RepeatNotificationMinutes = Convert.ToInt32( ConfigurationManager.AppSettings["RepeatNotificationMinutes"]); public static void SendMail(string FriendlyName) { SmtpClient client = new SmtpClient(SMTPServer, SMTPPort); MailMessage message = new MailMessage(); message.From = new MailAddress(MailFrom); if (MailTo.IndexOf("@") > 0) message.To.Add(new MailAddress(MailTo)); message.Subject = FriendlyName + " connection error"; message.Body = "Could not connect to database " + FriendlyName; try { client.Send(message); } catch (Exception MailException) { string Error = MailException.Message; } finally { } } } }

  10. Build the DbMonitorWrapper class library project and resolve any errors.
  11. Go back to the TestMonitor project. Add a reference to the class library by using the Browse tab and select DbMonitorWrapper.dll. Add a C# using statement or VB Imports statement for DbMonitorWrapper.
  12. Add a C# using statement or VB Imports statement for System.Timers.
  13. Use a System.Timers.Timer control to execute the code at an interval defined in IntervalMsec in the app.config file.
  14. The completed code is listed below:

    using System; using System.Collections.Generic; using System.Text; using DbMonitorWrapper; using System.Timers; namespace TestMonitor { class TestMonitor { Timer monitorTimer = new Timer(); static void Main(string[] args) { DbMonitor.InitializeList(); TestMonitor test = new TestMonitor (); test.monitorTimer.Enabled = true; test.monitorTimer.Interval = DbMonitor.IntervalMsec; test.monitorTimer.Elapsed += new System.Timers.ElapsedEventHandler(test.monitorTimer_Elapsed); Console.ReadLine(); } private void monitorTimer_Elapsed(object sender, ElapsedEventArgs e) { DbMonitor.SendMail("test"); } } }

    The Console.ReadLine is necessary to allow the program to continue execution and have the Timer control's Elapsed event fire. Without it, the program would quickly execute once and end without giving the elapsed event time to fire even once.

  15. Build and run the TestMonitor application. If all your monitored servers are working, you won't receive any notification. You can force an error during testing by shutting down a monitored server or altering a connection string to make it invalid.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap