Re: quick and dirty Netsaint plugins with Perl question
"Roger Flemming/SYD/CEtv" <[email protected]>
| Newsgroups | gmane.network.netsaint.user |
|---|---|
| Message-ID | <[email protected]> |
Hello Robert,
Robert Threet wrote:
> I've never written a plugin...
> I have a service I want to check that uses a POST via HTTPS. Can I
simply use Perl's LWP
> to submit the request and grep the response for the correct response?
Like
> if ($response =~ m/okay/) {
> print "0"; # for return 0
> } else {
> print "-1"; # for return -1
> }
not quite.
It's the script's exit code that Netsaint interprets as a status, viz (from
utils.pm):
'DEPENDENT'=>4,
'UNKNOWN'=>3,
'OK'=>0,
'WARNING'=>1,
'CRITICAL'=>2
Anything you send to STDOUT (e.g. prints) gets added as explanatory message
text when run from Netsaint (thus everything except help/usage should be
short).
If you will ever share your plugin (or just as good coding style, anyway),
there are a few other conventions to follow though. There is a FAQ about
this on the website somewhere, but the easiest way is probably to take an
existing plugin and just modify the "business logic" (and the help text!).
There is a also a little module, utils.pm, which has a few convenience
functions but is mainly useful for saying:
exit $ERRORS{'CRITICAL'};
instead of having to memorise the status codes. (Makes for much more
readable code!)
I use the following template (largely taken from sourceforge) which can be
easily used to make nice plugins at quick-and-dirty speed. Anywhere with
??? needs changing:
#!/usr/bin/perl -w
# Checks the ??? for ???
#
# Copyright 2002, same licence as Perl itself
#
# $Log: ???.pl,v $
# Revision 0.0.0 01/01/2002 00:00 RF ???
# boilerplate created from sourceforge example code
#
use vars qw($PROGNAME $runtimedir);
BEGIN {
if ($0 =~ m/^(.*?)[\/\\]([^\/\\]+)$/) {
$runtimedir = $1;
$PROGNAME = $2;
}
}
use strict;
use lib $runtimedir;
use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
use Getopt::Long;
# ??? modules required for "business logic":
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
my ($opt_p,$opt_q); # ??? modify according to actual params (so 'use
strict' doesn't moan)
Getopt::Long::Configure('bundling', 'no_ignore_case');
GetOptions
("V|version" => \&version,
"h|help" => \&help,
"p|param1=s" => \$opt_p, # ??? Put real params here.
"q|param2=i" => \$opt_q);
# ??? Maybe do some bounds checking on params here, with
# exit $ERRORS{'UNKNOWN'} if they don't make sense.
$opt_q = 2 unless defined $opt_q; # ??? Set a default
help() unless $opt_p; # ??? Required param
#Just in case of problems, let's not hang NetSaint
$SIG{'ALRM'} = sub {
print "No Answer from Client\n";
exit $ERRORS{"UNKNOWN"};
};
alarm 60;
do_check($opt_p, $opt_q);
######################################################################
############## This subroutine does the actual checking ##############
######################################################################
sub do_check {
# ??? All this needs to be changed for your actual business logic
# ??? but I left some stuff in as an example
my $url = shift;
my $max_count = shift;
my $ua = new LWP::UserAgent;
$ua->agent('NetSaint');
my $request = new HTTP::Request('GET', $url);
my $response = $ua->request($request);
unless ($response->is_success()) {
print "Couldn't read $url\n";
exit $ERRORS{'UNKNOWN'};
}
# Counts how many times '.Net' is found in the response:
my $string = $response->as_string();
my $count = ( $string =~ s/\.Net//gos );
if ($count > $max_count) {
print "Uh-oh, the page had $count .Net references in it!!\n";
exit $ERRORS{'CRITICAL'};
} elsif ($count) {
print "Hmm, the page had a few .Net references in it!\n";
exit $ERRORS{'WARNING'};
}
print "Phew, didn't mention .Net at all.\n";
exit $ERRORS{'OK'};
}
######################################################################
############## Boilerplate subs for Netsaint conventions #############
######################################################################
sub print_help() {
print_revision($PROGNAME,'$Revision: #1 $ ');
print "Copyright (c) 2002 Austar United Communications\n\tPerl ???
plugin for NetSaint\n";
print_usage();
# ??? Modify help text as required:
print <<END_HELP;
-h, --help
Display this message.
-V, --version
Print version numbers and license information
-p, --param1=STRING
URL of the page to check, include the http://
-q, --param2=INTEGER
Maximum number of times .Net should be on one page. (Default = 2)
END_HELP
}
sub print_usage {
# ??? modify next line with actual params
print "$PROGNAME -p URL [-q max_count]\n";
print "$PROGNAME [-h | --help]\n";
print "$PROGNAME [-V | --version]\n";
}
sub version {
print_revision($PROGNAME,'$Revision: #1 $ ');
exit $ERRORS{'OK'};
}
sub help {
print_help();
# The example had exit OK, but that hides mangled parameters
exit $ERRORS{'UNKNOWN'};
}
(Perl/Netsaint gurus, please feel free to critique my template!)
Cheers,
Roger
***************************************************************************************************************************************************************************
This email and any files transmitted with it, are confidential and is intended solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error, please notify the system manager.
This footnote also confirms that this email message has been scanned by AUSTAR Communications content and virus scanning applications
for the presence of computer viruses.
***************************************************************************************************************************************************************************
-------------------------------------------------------
This sf.net email is sponsored by: To learn the basics of securing
your web site with SSL, click here to get a FREE TRIAL of a Thawte
Server Certificate: http://www.gothawte.com/rd524.html