While there are many full-featured network monitoring solutions available, from commercial software to open source packages, these programs don’t always do exactly what you need them to do. Plus, with cost considerations and installation requirements, bigger is not always better when it comes to network monitoring packages. In certain circumstances, the use of Perl scripts to perform monitoring or even to extend the capabilities of a program currently installed can be very beneficial. Perl scripts can be used to simply handle basic tasks that may otherwise require a bloated program.

The good news is that you don’t have to be a Perl expert to accomplish this. While some knowledge of Perl is helpful, it is certainly not a requirement to see how this little programming language can be used to help with several monitoring tasks. I’ll show you how Perl can perform monitoring capabilities and provide you with some code snippets to get you started.

Perl basics
Perl is a powerful general-purpose programming language. With its large variety of built-in and modular features, Perl is capable of handling a variety of tasks, such as connecting to a remote host via ssh or speaking directly to databases. This provides incredible flexibility when designing a Perl program to accomplish a specific purpose.

On a large scale, the use of Perl to monitor servers, hosts, and network clients can become overly cumbersome. But when circumstances dictate a small or open source solution, you might be surprised by what you can accomplish with Perl. Most Linux distributions come with Perl installed by default, but you can also download it from Perl.org, where you’ll find versions of Perl for nearly every operating system you can imagine. For the purposes of this article, we’ll be working with Perl from a Linux system, but nearly everything we cover will apply to other versions of Perl as well.

Learning more on Perl

If you would like to learn more about Perl, then I would recommend checking out some of the O’Reilly books on Perl and taking a look at these sites:

Installing modules
Perl “modules” can extend the built-in features of the language. In this case, modules can help you execute a number of valuable network monitoring tasks. You can search for available modules when you have a task that you want to perform, or you can browse to find potentially helpful modules. For example, later on, we’ll look at the Net::SSH::Perl module. This module allows you to log in and execute commands on remote servers with straightforward, easy-to-understand commands. But, we’re getting a little ahead of ourselves; let’s see how to get modules installed first.

The best place to search for Perl modules is CPAN (Comprehensive Perl Archive Network), which stores a wide array of Perl modules and breaks them down into categories for easy navigation. Once you’ve located a module you want to try, then you’ll need to download it to your local system. Most of the Linux modules listed are compressed tarballs so you’ll need to unpack them before doing the installation. For example, perform the following steps (as the root user):
tar xpfz sample-module.tar.gz
cd sample-module
perl Makefile.PL
make test
make install

There is also a way to automate the installation of Perl modules. With the use of the CPANPLUS module, you can install additional modules from the command line like this:
perl -MCPANPLUS -e ‘install Net::SNMP’

Keep in mind that the module name is case sensitive. In the above example, the appropriate module will be downloaded, extracted, and installed. This module will also check the version on currently installed modules and will provide an update if necessary.

Monitoring with Perl
The first things you will most likely want to monitor are basic system availability and whether common services are alive and kicking. The first one is akin to running a ping command from the prompt. In fact, there just happens to be a module called Net::Ping that provides you the ability to test hosts within Perl. Here is a quick snippet of code illustrating how the module is used.

In this example, we start by saying that we want to use the Net::Ping module. We will now be able to use its functions in our script. An array of hosts that we want monitored is then created. IP addresses or resolvable hostnames are acceptable here. The next line creates a ping object for our use. Here, you can specify general ping options such as the protocol or packet size in bytes. Net::Ping allows you to generate ICMP, UDP, or TCP ping packets. ICMP is what is generated from a UNIX ping and the others hit the remote host’s echo port. With “$p->ping($host,2),” we’re actually doing the pinging and, in this case, setting a timeout of 2 seconds. This is basically a true or false; if the host responds, a 1 is returned; otherwise, a 0. Finally, the network connection is closed when you issue the “$p->close();” command. From here, you could get more advanced, looping the ICMP tests and logging to a text file. You could also easily use such a Perl program to generate an HTML Web page, which generates a user-friendly interface that indicates what is up and what is down.

This type of system availability check is pretty much the most basic type of monitoring. This confirms that a host is responding to ping requests, but not that it is online in any other way. For example, you may be able to ping the system, but Apache may have died or the box may no longer be accepting SSH connections. We’ll now take a look at how to monitor some common Linux services.

First, we’ll take a crack at HTTP. Once again, this process is facilitated with a readily available module that’s called LWP::UserAgent, which is able to do HTTP requests and can be used to determine if a Web server is responding. Here’s the code we’ll use:
use LWP::UserAgent;
$url = “http://magrathea.example.net/”;
$ua = LWP::UserAgent->new;
$response = $ua->get($url);
if ($response->is_success) { print “URL $url is alive.\n”; }
else { print “HTTP on $host is down.\n”; }

In this example, we load the module and then specify the URL we would like to monitor. A new object is then created with the third line, allowing us to specify additional options, if necessary, such as a timeout. This lets the request run for 5 seconds before dying. Like the ping example, we’re getting back a true or false output, and we can do whatever we want based on that response (such as creating an HTML page, as mentioned above). In this case, we’re “printing” a message (showing the output on screen) showing whether the URL is up or down. The LWP::UserAgent is fairly robust and allows you to specify much more advanced options. A search on “lwpcook” at CPAN will provide you with a number of options and an explanation on exactly what they do.

Other services, such as FTP and SSH, can easily be monitored by similar means. Modules like Net::FTP and Net:SSH:Perl provide everything you need to test their availability.

Running remote commands
While it can be beneficial to monitor devices from a central location, there is often plenty to be learned from remote devices, such as the current load on a server, space available on a remote file system, or packets inbound on a Cisco router’s serial interface. To run a remote “df” command, which displays disk free space, you would need something like the following:
use Net::SSH::Perl;
$ssh = Net::SSH::Perl->new(frogstar);
$ssh->login(“user”, “password”);
($stdout, $stderr) = $ssh->cmd(‘df -k’);
print $stdout;

The above code snippet demonstrates how to connect to a remote host, run a command, and display the result. We open a SSH connection to the host frogstar, log in with the appropriate user/password combination, and then run the command “df –k.” The result is saved to the variable $stdout unless there is an error condition. The results are then displayed by printing the contents of $stdout. We then send a remote command to exit, ending the session.

This is a simple example and performs no decision-making logic to determine whether the results are good or not. You may want to drill down on the specific number indicating bytes available and generate a warning if it is below a certain threshold. You could also configure the program to generate an e-mail, send a page, or perform other types of communication. You could also insert this information into a database to create a historical record using one of the many database modules for Perl.

A Cisco router example
Accessing Cisco equipment can be accomplished with the Net::Telnet::Cisco module. This allows you to connect to a remote router or switch and run commands. You can use this to check on current statistics, view the configuration, or make changes. Here’s the code example:
use Net::Telnet::Cisco;
my $session = Net::Telnet::Cisco->new(Host => ‘’);
$session->login(‘login’, ‘password’);
my @output = $session->cmd(‘show hardware’);
print @output;

This is how a basic connection and command are performed. Rather than running a large program to harvest configs, you could easily create a Perl script to go out and get them, then store them in a centralized location. Run it from cron job and always have a recent backup copy of any equipment. There is much more to the Net::Telnet::Cisco module than demonstrated here. If you are in charge of monitoring or administering Cisco equipment, it’s definitely worth taking a closer look at this valuable module.

As we’ve seen, Perl can be used to configure customer monitoring for a large number of purposes and systems. Whether it’s simply checking to see if a host is alive or not, listening to HTTP requests, or interacting with Cisco routers, Perl is extremely versatile and can be a powerful tool for monitoring and automating administrative tasks. Plus, not only is Perl free, it has a very large base of users that are constantly adding new modules to extend its functionality. When you’re operating on a shoestring budget or just need to quickly set up some basic monitoring, Perl can be the perfect choice.