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
{
}
}
}
}
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
{
}
}
}
}
No comments:
Post a Comment