Wednesday, September 12, 2012

asp.net - asp:scriptmanager is not a known element

You will get this error if the below assembly reference is missing in the web.config file when you add an asp.net script manager to the page.Add this to the assembly section of the web.config file.

<add assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

Thursday, September 6, 2012

c# - Ping production servers and send email notifications

Recently I had posted code to check whether production database SQL server service is running or not and send email notification if any of the SQL Server database server is down.I wanted a similar email alert for my production servers.I maintain about 21 servers which are used as web servers,file servers,database servers and so on.

The below code pings a list of servers and if any of the server is down then it will send an email notification.I have created a scheduled task out of this exe and this scheduled task runs every 10 minutes and  sends email alerts if any server is unreachable to the ping command.


using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Configuration;
using System.Net.Mail;

namespace PingServer

{
    public class PingExample
    {
        
        public static void Main(string[] args)
        {
            try
            {
                Ping ping = new Ping();
                string sServers = ConfigurationManager.AppSettings["SERVER_LIST"];
                string [] sServerList = sServers.Split(',');
                foreach (string server in sServerList)
                {
                    try
                    {
                        PingReply reply = ping.Send(server);
                        if (reply.Status == IPStatus.Success)
                            Console.WriteLine("Server " + server + " running");
                        else
                        {
                            Console.WriteLine("Server " + server + " down");
                            SendMail("Server " + server + " down", "SERVER_STATUS", new string[] { "me@mycompany.com", "mymanager@mycompany.com" });
                        }
                    }
                    catch
                    {
                        Console.WriteLine(server + " is not reachable.Exception raised by ping.Send()");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
        public static void SendMail(String message, String subject, String[] notificationList)
        {
            try
            {
                MailMessage MyMessage = new MailMessage();
                MyMessage.From = new MailAddress("me@mycompany.com");
                foreach (String email in notificationList)
                    MyMessage.To.Add(new MailAddress(email));

                MyMessage.Subject = subject;

                MyMessage.Body = message;

                SmtpClient emailClient = new SmtpClient("smtp.me.mycompany.com");

                emailClient.Send(MyMessage);
            }
            catch
            {
            }
        }
    }
}