Archive

Archive for January, 2012

Site Checker in C#–A Website health monitoring system

January 19th, 2012 1 comment

Below code checks the list of websites and sends a notification mail if found if any of the website is down. This I wrote for a special purpose but thought to share as this might be a helpful code snippet for small to medium companies.

You can see many places in the code are hard coded and exceptions not handled properly since I wrote this for a special purpose and I am not sharing the complete code here. So please feel free to modify.

Store all the domains in a text file (I used sites.txt) and keep in the same folder as .exe. Put one domain per line. “http://” not required.

 public bool ErrorFlag = false;
        public StringBuilder SiteString = new StringBuilder(string.Empty);
 
 
        private void btnCheck_Click(object sender, EventArgs e)
        {
            CheckSites();
        }
 
 
        private void CheckSites()
        {
            string sitestatus = "Ok";
            foreach (ListViewItem lvi in listView1.Items)
            {
                lvi.UseItemStyleForSubItems = false;   
                lvi.SubItems.Add("...");
 
		// Use some threading methods instead of DoEvents()
                Application.DoEvents(); 
 
                WebRequest request = WebRequest.Create(lvi.Text);
                HttpWebResponse response;
 
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch
                {
                    response = null;
                }
                if (response == null || 
			response.StatusCode != HttpStatusCode.OK)
                {
                    SiteString.Append(" - " + lvi.Text + "\n");
 
                    lvi.ForeColor = lvi.SubItems[1].ForeColor = Color.Red; 
                    sitestatus = "Error";
                    ErrorFlag = true;
                }
                else
                {
                    response.Close();
                    lvi.SubItems[1].ForeColor = Color.Green;    
                    sitestatus = "Ok";
                }
 
                lvi.SubItems[1].Text = sitestatus;
 
 
            }
 
            if (ErrorFlag)
            {
                StringBuilder message = new StringBuilder();
 
 
                if (SiteString.Length > 0)
                {
                    message.Append("Below website(s) may need your/IT 
			attention:\n\n");
                    message.Append(SiteString.ToString());
                }
                else
                {
                    message.Append("SiteChecker application failed to 
			load the sites.txt file. Please inform IT person");
                }
                message.Append("\n\n\nThis is an automatic 
			notification from SiteChecker applicaiton.\n");
                message.Append("[Application created by PraVeeN 
				under C# WinForms on VS Express 2010]");
 
                SendMail(message.ToString());
 
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            IEnumerable<string> sites;
            try
            {
                sites = File.ReadLines("sites.txt");
                foreach (var site in sites)
                {
                    listView1.Items.Add("http://" + site + "/");
                }
            }
            catch
            {
                ErrorFlag = true;
            }
 
	    //Application.DoEvents();
	    //CheckSites();
	    //Application.Exit();
        }
 
        private void SendMail(string content)
        {
		// Mail sending procedure
	}
VN:F [1.9.18_1163]
Rating: 5.0/5 (2 votes cast)
Categories: C#, Code Snippets

Visual Studio 11 and .NET Framework 4.5 previews

January 9th, 2012 No comments

Don’t be late. Check out what’s special in new versions.

URL: http://msdn.microsoft.com/en-US/vstudio/hh127353

Here is a what’s new link for .net framework 4.5.

VN:F [1.9.18_1163]
Rating: 5.0/5 (2 votes cast)
Categories: DOTNET, News

Programmability Enhancements in SQL Server 2012

January 6th, 2012 No comments

Programmability enhancements in the Database Engine include FileTables, statistical semantic search, property-scoped full-text search and customizable proximity search, ad-hoc query paging, circular arc segment support for spatial types, support for sequence objects, default support for 15,000 partitions, and numerous improvements and additions to Transact-SQL.

MSDN page here – http://msdn.microsoft.com/en-us/library/cc645577(v=SQL.110).aspx

VN:F [1.9.18_1163]
Rating: 5.0/5 (1 vote cast)
Categories: SQL

My new year resolutions for 2012

January 1st, 2012 No comments
  • ITIL Certification – Information Technology Infrastructure Library
  • Be active in Toastmasters Club
  • Learn Arabic, if I continue in UAE
  • Learn new technologies – as usual
  • Continue with next papers of MBA
  • More MCP certifications
VN:F [1.9.18_1163]
Rating: 5.0/5 (2 votes cast)
Categories: Personal