.NET Tip: Are Your Computers Up and Running? Are You Sure?

There are plenty of utilities to help monitor your computer systems, but what if you need to integrate system status information into your own application? I am in the process of building a system-wide dashboard for my employer. I want to display a simple red/yellow/green light for each computer to indicate its current status. A red light indicates that the computer is unreachable.

How do you normally determine if a computer is available? I normally open up a command prompt and fire off a ping to the computer. Ping is a great utility to determine basic system availability. You could write a script that pings the computer and then parses the reply to determine availability. If you are using .NET, then everything you need to do the same thing in code is only a namespace away. The System.Net.NetworkInformation namespace has what you need to ping a computer directly from your application. It is as simple as a single call using the Ping class.

Ping Pinger = new Ping(); 
PingReply Reply = Pinger.Send("127.0.0.1"); 

The above code creates a new instance of the Ping class and then sends a ping to the local machine. It isn't very interesting, but it couldn't be much simpler. Take a look at another example. This time you will start with a list of computers that need to be monitored. The example iterates through each computer in the list, pinging the computer and displaying the results on the console. The list of computers can contain either IP addresses or web sites. For this example I have included a few internal systems as well as a couple web sites you might recognize.

List IPs = new List(); 
IPs.Add("10.1.1.12"); 
IPs.Add("10.1.1.15"); 
IPs.Add("192.168.173.160"); 
IPs.Add("www.google.com"); 
IPs.Add("www.amazon.com"); 
Ping Pinger = new Ping(); 
foreach (string ip in IPs) 
{ 
    PingReply Reply = Pinger.Send(ip); 
    Console.WriteLine("Ping " + ip + ": " + Reply.Status.ToString()); 
}

Here is the console output for this example.

Ping 10.1.1.12: Success 
Ping 10.1.1.15: TimedOut 
Ping 192.168.173.160: TimedOut 
Ping www.google.com: Success 
Ping www.amazon.com: TimedOut  

With only a few lines of code you can now determine the status of your computer systems. Whether it is an email, SMS message, or red/yellow/green light, you can be confident that your systems are up and running. The Ping class provides several options that you can use to control the requests as well as the ability to send asynchronous ping requests. Make sure you check out these options as well as all of the information available in the reply from the ping request.



Comments

  • Cool tip - weird code casing

    Posted by Efitap on 04/03/2009 12:39am

    A tip back to you: 
    
    Use camel casing to name variables and parameters.
    Use Pascal casing to name functions and types. 
    
    List< string > ipNumbers = new  List< string >( );
    ips.Add( "127.0.0.1" );
    ...
    Ping pinger = new Ping( );
    foreach( string currentIp in ipNumbers )
    {
      PingReply reply = pinger.Send( currentIp );
      Console.WriteLine("Ping " + ipNumber + ": " + reply.Status.ToString());
    
    }
    
    
    Code readability is equally important to it's functionality. 
    The days when one could write unreadable code are numbered - the challenge today is to make it as clear as clear can be :)
    
    Other than that, great tip, loved it, didn't know about it!
    
    Pedro,
    Norway

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds