#!/usr/bin/perl -w # productname: httpcheck # productrelease: V0.1 # # copyright 2003-2004 by kapper.net # reach kapper.net at http://kapper.net/ or via postal mail to: # kapper & partner communications keg, kapper.net gmbh # loeblichgasse 6, A-1090 Vienna, Austria # development-team: mm@kapper.net, hk@kapper.net # # 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 # http://www.gnu.org/licenses/gpl.txt # ---------------------------------------------------- # project started: 17.09.2003 # ---------------------------------------------------- # synopsis: # script checks the reply of "service httpd status" and tries a start if dead # as well as sends an email to notify its master. # # requirements: Mail::Sendmail perl-class (available from http://alma.ch/perl/mail.html or CPAN) # local sendmail/postfix/whatever mailserver to deliver the sent email if required # alternatively you can edit Sendmail.pm to have your smtp-server globally set # # usage: httpcheck # # possible parameters # ------------------- # # --check-httpdstat | -c # reads output of "service httpd status" # if there is a problems tries start and sends optiona email # --sendto | -s # this is an optional parameter, if used, it requires an email address, # to which a possible problem-report is sent. # # # # History: # -------- # 17.09.2004 hk adopt from raidcheck to httpcheck # 29.07.2004 hk adopt for fedora-core-series of redhat, release it, short params # 30.01.2004 hk from-email-address is now always similar to to-address # 30.01.2004 hk added "die" option for raidstatus-file-open on check # 20.01.2004 mm added "O_CREAT" to sysopen in "--read-mdstat" case # 06.09.2003 mm ready for usage! # 06.09.2003 mm changed sysopen params when writing, added sendmail class calls # 03.09.2003 mm param checking, problem with some chars and their mysterious apperance # 30.09.2003 mm test with file io and mdstat call, usage, ... # use strict; use Fcntl; # for file io use Mail::Sendmail; # for sendmail class use Sys::Syslog; # for notification of syslog-services my $CMD = "service httpd status"; # cmd to get httpd status my $CMD1 = "service httpd restart"; # cmd to start httpd # if needed please change the above systemcall and filepath/-name. # possible params my $PARAM_CHECK = "--check-httpdstat"; my $PARAM_SEND = "--sendto"; my $PARAM_CHECK_SHORT = "-c"; my $PARAM_SEND_SHORT = "-s"; # constants my $PDEFAULT = 0b0000; my $callparam = $PDEFAULT; # don't ask ;-) my $READ = 0b1000; my $CHECK = 0b0100; my $SEND = 0b0010; my $EQUAL = 123443; # here it means "service running" my $NOTEQUAL = 394342; # here it means do a problem-report and restart #my $FROM = ""; # set to get a specific from-address my $SUBJECT = "raidstatus failed: " . `hostname`; # -------------------------------------------- # compares the submitted string on "running" and returns # above specified constants for equal / notequal aka running / notrunning sub compare($) { my $one = $_[0]; my $rc = $NOTEQUAL; # - - - - - - - - if ($one =~m/(.*)(is running)(.*)/) # if it is running... { $rc = $EQUAL; } return ($rc); } # -------------------------------------------------------------------------- # give the user some hints on how to use... sub usage { my $usageinfo = ""; $usageinfo .= "---------------------------------------------------------\n"; $usageinfo .= "usage: $0 \n\n"; $usageinfo .= "possible parameters\n"; $usageinfo .= "-------------------\n"; $usageinfo .= "\t--check-httpdstat | -c\n"; $usageinfo .= "\t checks for a running http-daemon and escalates if necessary\n"; $usageinfo .= "\t if there is a problem it reports to STDOUT\n"; $usageinfo .= "\t --sendto | -s \n"; $usageinfo .= "\t this is optional - if used - it requires an email address,\n"; $usageinfo .= "\t a possible problem is mailed to the given address.\n"; $usageinfo .= "Have fun and may your webserver always run ;)\n"; $usageinfo .= "copyright 2003-2004 by http://kapper.net/\n\n"; $usageinfo .= "---------------------------------------------------------\n"; die "$usageinfo"; } # ---------------------------------------- # main { my $param = shift @ARGV; # shift gets the next parameter and cuts it off ARGV my $infotxt = ""; my $program = "httpdcheck"; my ($mailadr,$cmdreply,$rc,$rc1); my %mail; # sendmail needs this while (defined $param) # as long as there are parameters { if (($param eq $PARAM_CHECK) || ($param eq $PARAM_CHECK_SHORT)) { $callparam |= $CHECK; } elsif (($param eq $PARAM_SEND) || ($param eq $PARAM_SEND_SHORT)) { $callparam |= $SEND; $mailadr = shift @ARGV; # next parameter needs to be an email address if (!($mailadr)) { $callparam = $PDEFAULT; # default value ... means error and needs to show usage } else { if (!($mailadr =~m/.+\@.+\..+/)) { $callparam = $PDEFAULT; # default value ... means error and needs to show usage } } } else # no params { $callparam = $PDEFAULT; # default value ... means error and needs to show usage } $param = shift @ARGV; # get next parameter } if ($callparam == ($SEND)) { $callparam = $PDEFAULT;} # no sendonly option &usage if ($callparam == $PDEFAULT); if (($callparam & $CHECK) == $CHECK) { $cmdreply = qx($CMD); $rc = compare($cmdreply); openlog($program, 'cons,pid', 'user'); syslog('info', 'httpd-checking.'); closelog(); if ($rc == $NOTEQUAL) { $rc1 = qx($CMD1); openlog($program, 'cons,pid', 'user'); syslog('info', 'httpd-check showed a problem - restarting.'); closelog(); } $cmdreply = qx($CMD); $rc = compare($cmdreply); if ($rc == $NOTEQUAL) { openlog($program, 'cons,pid', 'user'); syslog('info', 'httpd-check could not restart httpd!'); closelog(); $infotxt = "----------------------------------------\n"; $infotxt .= "Attention! web-service is DEAD! \n"; $infotxt .= "----------------------------------------\n"; $infotxt .= "system-check returned:\n"; $infotxt .= "----------------------------------------\n"; $infotxt .= "$cmdreply"; $infotxt .= "----------------------------------------\n"; $infotxt .= "service-restart returned:\n"; $infotxt .= "$rc1"; $infotxt .= "----------------------------------------\n"; $infotxt .= "please check your server - I tried to restart already.\n"; $infotxt .= "httpd-status-checker is sponsored by http://kapper.net/\n"; } if (($callparam & $SEND) == $SEND) { if ($infotxt ne "") { # set mail parameters for sending %mail = ( To => $mailadr, From => $mailadr, Subject => $SUBJECT, Message => $infotxt );# sendmail(%mail) or die $Mail::Sendmail::error; #print "OK. Log says:\n", $Mail::Sendmail::log; } } else { if ($infotxt) # if param = --check-mdstat without email { print STDOUT "\n$infotxt\n"; } } } } # end main