I recently started using mon and wrote a script to monitor a HylaFAX server. Thought I'd share it with the list. Comments/Suggestions welcome.

Thanks for a great software package,
Dave
#!/usr/local/bin/perl -w
#
# verify that a HylaFAX server is operating.
#
# usage: hylafax.monitor [options] [host[:port] ...]
#
# options: [-m] [-v] [-P port] [-t timeout] [-d debug] [-u username] [-p password]
# 
# -m       exclude modem checks.
# -v       script debugging
# -P       PORT
# -t       time in seconds before conection time out, default 30.
# -d       debug level [0-9], default 0 (server communication debugging);
# -u       username to login to the server.
# -p       password
#
# Uses Net::FTP (from http://www.cpan.org/)
# to send a HylaFAx status message to each of the servers
# listed on the command line, and then waits for a response. It checks that
# the scheduler is up and running and, optionally, that modems are reporting
# a known state or test for a specific state.
#
# Requires client access to the server being monitored.
#     see HylaFAX (hosts.hfaxd,faxadduser,faxdeluser) manpages for details.
#
#  NOTE on USERNAME/PASSWORD:  There are a few way to use username/password
#      pairs for logins:
#      1)  The most secure way (used loosely) is to define the username/password
#          within this script and set the file permission accordingly then add
#          that username/password to each HylaFAX server hosts.hfaxd file
#           (hosts.hfaxd example:  [EMAIL PROTECTED]:encrypted_password).
#      2)  use the -u/-p option and that username/password will be passed to
#          all hosts.
# 
# HylaFAX is a trademark of Silicon Graphics Corporation.
# http://www.hylafax.org/
#
# Adapted from telnet.monitor by:
# Jim Trocki, [EMAIL PROTECTED]
#
# Copyright (C) 2003  David Spreitzer.
#
#    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
#
#------------------------------------------------------------------------------

use strict;
use Net::FTP;
use Getopt::Std;

sub verbose($);

my %opt;
getopts ("d:mp:t:u:v", \%opt) || die "unknow option";

my $TIMEOUT = $opt{"t"} || 30;
my $DEBUG = $opt{"d"} || 0;
my $NO_MODEM = $opt{"m"} || 0;
my $VERBOSE = $opt{"v"} || 0;
my $PORT = $opt{"P"} || 4559;

# rather than sending the username/password from mon (which can be seen on the
# command line), define them here and set this file's permission accordingly.
my $USER = $opt{"u"} || "monserver";
my $PASSWORD = $opt{"p"} || "monserverpassword";
#my $USER = $opt{"u"} || "";
#my $USER = $opt{"u"} || $ENV{LOGNAME};
#my $PASSWORD = $opt{"p"} || "";

my @failures = ();
my @l = ();

my @modem_test =  ();
my @modem_ignore =
    ("Waiting for modem to come ready",
     "Initializing server",
     "Running and idle",
     "Sending job",
    );

HOST: foreach my $HOST (@ARGV) {
  verbose("-----") if ($VERBOSE);
  verbose("HOST: $HOST") if ($VERBOSE);
    
  verbose("CONNECT: $HOST, Port=>$PORT, Timeout=>$TIMEOUT, Debug=>$DEBUG") if 
($VERBOSE);
  my $hfax = new Net::FTP($HOST, Port=>$PORT, Timeout=>$TIMEOUT, Debug=>$DEBUG);
  if (!defined $hfax) {
     verbose("   FAILED! $@") if ($VERBOSE);
     push @failures, [$HOST,"Connection Failed, $@"];
     next;
  }

  verbose("LOGIN: USER=>$USER, PASSWORD=>$PASSWORD") if ($VERBOSE);
  if (! $hfax->login("$USER","$PASSWORD")) {
     my $errmsg = $hfax->message;
     verbose("   FAILED! $errmsg") if ($VERBOSE);
     push @failures, [$HOST, "Login failed, host=$HOST user=$USER, $errmsg"];
     next;
  }
  
  verbose("STATUS CHECK:") if ($VERBOSE);
  my $output = $hfax->list("status");
  if (! $output) {
     my $errmsg = $hfax->message;
     verbose("   FAILED! $errmsg") if ($VERBOSE);
     push @failures, [$HOST, "Status check failed, $errmsg"];
     next;
  }

  LINE: while(<$output>)
  {
    my ($item,$status) = split(':',$_);
    $status =~ s/^\s*(.*?)\s*$/$1/;

    verbose("STATUS: VAR=>$item, VAL=>$status") if ($VERBOSE);

    if ($item =~ /^HylaFAX scheduler/) {
        verbose("SCHEDULER: VAR=>$item, VAL=>$status") if ($VERBOSE);
        if  ($status !~ /^Running$/) {
           verbose("   FAILED!") if ($VERBOSE);
           push @failures, [$HOST, "$_"];
        }
        next LINE;
    }
    next HOST if ($NO_MODEM);
    if ($item =~ /^Modem/) {
       verbose("MODEM: VAR=>$item, VAL=>$status") if ($VERBOSE);
       foreach my $mtest (@modem_ignore) {
           if ($status =~ /$mtest/) {
              next LINE;
           }
       }
       if (@modem_test) {
           foreach my $mtest (@modem_test) {
               if ($status =~ /$mtest/) {
                  verbose("   FAILED!") if ($VERBOSE);
                  push @failures, [$HOST, "$_"];
                  next LINE;
               }
           }
       }
       else {
           verbose("   FAILED!") if ($VERBOSE);
           push @failures, [$HOST, "$_"];
       }
    }
    else {
       verbose("UNKNOWN") if ($VERBOSE);
       push @failures, [$HOST, "$_"];
    }
  }
  $hfax->quit;
}

if (@failures == 0) {
    exit 0;
}

for (@failures) {
    push @l, $_->[0];
}

print join (" ", sort @l), "\n";

for (@failures) {
    print "$_->[0]: $_->[1]\n";
}

exit 1;

sub verbose($) {
 my ($line) = @_;
 print ("hylafax.monitor: $line\n");
}

Reply via email to