# # NWPE.pm # # Nagios Web Plugin Executor # ATonns Wed Jul 9 17:42:04 EDT 2003 # # $Id: NWPE.pm,v 1.1 2003/07/09 21:45:40 atonns Exp atonns $ # # NWPE.pm - perl module for nagios service check CGI # Copyright (C) 2003 - iVillage.com, Anthony Tonns # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # package NWPE; # create instance sub new { my $this = shift; # get program info my $PROGNAME = $_[0] ? $_[0] : "unknown program"; shift; my $version = $_[0] ? $_[0] : "some version"; shift; # space for collecting output my $stdout_str; # redirect STDOUT to $stdout_str my $stdout_new = IO::String->new($stdout_str); my $stdout_old = select($stdout_new); # parse the input my $query = new CGI; bless my $self = { "PROGNAME" => $PROGNAME, "version" => $version, "stdout_str" => \$stdout_str, "stdout_old" => \$stdout_old, "query" => \$query, } => ( ref $this || $this ); return $self; } sub nwpe_version { my $self = shift; my $nwpe_version = '$Revision: 1.1 $'; return $nwpe_version; } # access data sub PROGNAME { my $self = shift; return $self->{"PROGNAME"}; } sub version { my $self = shift; return $self->{"version"}; } sub query { my $self = shift; return $self->{"query"}; } # auto-parse args into an array sub get_args { my $self = shift; my $queryref = $self->{"query"}; # whoops, no args if (! $$queryref->param('args') ) { return; } my $args = $$queryref->param('args'); return split(/\s/,$args); } # clean up sub quit { my($self,$state)=@_; alarm(0); # select correct exit function use subs qw(exit); *exit = $ENV{MOD_PERL} ? \&Apache::exit : sub { CORE::exit }; my $PROGNAME = $self->{"PROGNAME"}; my $str = $self->{"stdout_str"}; my $out = $self->{"stdout_old"}; my $query = $self->{"query"}; # reset printing to real STDOUT select($$out); # if we don't have a query string if ( ! $$str ) { # it's very VERY bad (ie: no args were present) $state = $utils::ERRORS{UNKNOWN}; # return minimal print "Content-Type: text/plain;\n"; print "Return-value: $state;\n"; print "\n"; print "$PROGNAME: Invalid query\n"; exit($state); } # set the HTTP headers # "Return-value:" being the key header print ${$query}->header( -type=>"text/plain", -Return_value=>"$state", ); # output check messages print $$str; exit($state); } 1;