Softpanorama

May the source be with you, but remember the KISS principle ;-)
Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and  bastardization of classic Unix

Programming Monitors in Perl

News See also Recommended Links Perl Humor Etc

 

         

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.

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;
$ua->timeout(5);
$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;
$ssh->cmd("exit");
 

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 => '192.168.2.1');
$session->login('login', 'password');
my @output = $session->cmd('show hardware');
print @output;
$session->close;
 

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.

Summary

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.

 

NEWS CONTENTS

Old News

Checkservice

Checkservice is a Perl script that monitors services on remote hosts. It uses plugins to provide a more thorough check than just a socket check, and can be configured to check multiple services on multiple hosts using two different methods (simple and extended). It is able to write logs and when running in the background enables warnings. It features a beep-, mail-, and SMS-warning system.

simplemon

simplemon is a Perl script for monitoring processes (and their UID/GID) and free disk space. It generates carefully formatted mail, which is suitable for sending via SMS. It currently supports Solaris, FreeBSD, and Linux.

http://opensmart.sourceforge.net/ by Ulrich Herbst

OpenSMART is a monitoring (and reporting) environment for servers and applications in a network. Its main features are a nice Web front end, monitored servers requiring only a Perl installation, XML configuration, and good documentation. It is easy to write more checks. Supported platforms are Linux, HP/UX, Solaris, AIX, *BSD, and Windows (only as a client).

Tar/GZ:
http://sourceforge.net/[..]iles.php?group_id=89342&package_id=93602
Demo site:
http://opensmart.sourceforge.net/cgi-bin/monitorgui.cgi

Trove categories: [change]

[Development Status] 5 - Production/Stable
[License] OSI Approved :: GNU General Public License (GPL)
[Operating System] POSIX :: AIX, POSIX :: HP-UX, POSIX :: Linux, POSIX :: SunOS/Solaris, Unix
[Programming Language] Perl
[Topic] System :: Monitoring, System :: Systems Administration
[Translations] English, German

Dependencies: [change]
No dependencies filed


nPULSE

a Web-based network monitoring package for Unix-like operating systems. It can quickly monitor up to thousands of sites/devices at a time on multiple ports. nPULSE is written in Perl and comes with its own (SSL optional) Web server for extra security.

Author:
horsburgh [contact developer]

Homepage:
http://www.horsburgh.com/h_npulse.html
Tar/GZ:
http://www.horsburgh.com/download/npulse-0.54.tar.gz
Changelog:
http://www.horsburgh.com/h_npulse.html#changelog

SolutionBase: Set up customized network monitoring with Perl by Thomas "Nooning CCNA, CCDA"

Feb 3, 2004
2 comment(s)

Takeaway: There is no short supply of network monitoring solutions, from expensive enterprise packages to open source programs. However, for a quick, precise monitoring solution on a limited budget, your best bet may be to use a Perl script.


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
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.

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.



Etc

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to to buy a cup of coffee for authors of this site

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: March 12, 2019