From: Stephen L Johnson Date: Wed, 28 Jun 2000 04:02:07 +0000 (+0000) Subject: added support for GetOpts::Long and detailed message field of status message X-Git-Tag: spong-2_7-alpha7~16 X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4c6a4a49629f298108189ad5df97490f5dea80f4;p=spong.git added support for GetOpts::Long and detailed message field of status message --- diff --git a/src/spong-message.pl b/src/spong-message.pl index 9d5f746..b14b638 100755 --- a/src/spong-message.pl +++ b/src/spong-message.pl @@ -35,20 +35,76 @@ being send within a short amount of time. use lib "@@LIBDIR@@"; -use Data::Dumper; - use Spong::AckList; use Sys::Hostname; +use Getopt::Long; +use FileHandle; use English; -if( $ARGV[0] eq "--debug" ) { $debug = 1; shift @ARGV; } -if( $ARGV[0] eq "--test" ) { $test = 1; shift @ARGV; } +my %opt; +@options = ( "help", "host=s", "service=s", "time=i", "color|status=s", "summary=s", + "duration=i", "message=s", "file=s", "debug", "test" ); +Getopt::Long::Configure("pass_through"); +if( ! GetOptions( \%opt, @options ) ) { &error("Incorect usage"); exit 1; } + +# Check usage, and read in what we are supposed to do. + +&help if defined $opt{'help'}; + +my($color, $host, $service, $time, $summary, $duration) = @ARGV; + +if ( ! $color && defined $opt{'color'} ) { $color = $opt{'color'}; } +if ( $color !~ /red|yellow|green/ ) { + &error( "Invalid status color. Must be green, yellow, or red."); exit 1; +} +if ( ! $host && defined $opt{'host'} ) { $host = $opt{'host'}; } +if ( ! $host ) { &error("Host name is required"); exit 1; } +if ( ! $service && defined $opt{'service'} ) { $service = $opt{'service'}; } +if ( ! $service ) { &error("Service name is required"); exit -1; } +if ( ! $time && defined $opt{'time'} ) { $time = $opt{'time'}; } +if ( ! $time ) { &error("Time is required"); exit 1; } +if ( ! $summary && defined $opt{'summary'} ) { $summary = $opt{'summary'}; } +if ( ! $summary ) { warn "Summary text is required\n\n"; exit 1; } +#if ( ! defined $opt{'message'} && ! defined $opt{'file'} ) { +# &error("--message or --file is required"); exit 1; } +if ( defined $opt{'message'} && defined $opt{'file'} ) { + &error("Only one of --message or --file can be specified"); exit 1; } +if ( ! defined $duration ) { + if ( defined $opt{'duration'} ) { $duration = $opt{'duration'}; } else { $duration = 0; } } +$debug = $opt{'debug'}; +$test = $opt{'test'}; + + +# Handle message file processing if necessary +my ( $message ) = ""; +if ( $opt{'message'} ) { $message = $opt{'message'}; } +elsif ( ! defined $opt{'file'} ) { } +elsif ( $opt{'file'} eq '-' ) { while () { $message .= $_; } } +else { + my($fh) = File::Handle->new($opt{'file'}); + if (! defined $fh ) { + &error("Could not open file " . $opt{'file'} ); + exit -1; + } + while (<$fh>) { $message .= $_; } + undef $fh; +} + +#if( $#ARGV == 4 || $#ARGV == 5) { +# if (! defined $duration || $duration eq "") { $duration = 0; } +#} else { +# print STDERR "Error: Incorrect usage!\n"; +# print STDERR +# "Usage: spong-message [--debug] color host service time message [duration]\n"; +# exit(-1); +#} + $conf_file = "@@ETCDIR@@/spong.conf"; $hosts_file = "@@ETCDIR@@/spong.hosts"; $groups_file = "@@ETCDIR@@/spong.groups"; -$message_file = "@@ETCDIR@@/spong.message"; +$summary_file = "@@ETCDIR@@/spong.message"; $msgfunc_path = "@@LIBDIR@@/Spong/Message/plugins"; ($HOST) = gethostbyname(&Sys::Hostname::hostname()); $HOST =~ tr/A-Z/a-z/; @@ -79,7 +135,7 @@ require $hosts_file || die "Can't load $hosts_file: $!"; # Read in the spong.message file -require $message_file || die "Can't load $message_file: $!"; +require $summary_file || die "Can't load $summary_file: $!"; &debug( "spong.message file loaded" ); # Read in the spong.hosts file @@ -97,18 +153,6 @@ while( ) { chomp; push( @history, $_ ); } close( HISTORY ); &debug( "message history database loaded" ); -# Check usage, and read in what we are supposed to do. - -if( $#ARGV == 4 || $#ARGV == 5) { - ($color, $host, $service, $time, $message, $duration) = @ARGV; - if (! defined $duration || $duration eq "") { $duration = 0; } -} else { - print STDERR "Error: Incorrect usage!\n"; - print STDERR - "Usage: spong-message [--debug] color host service time message [duration]\n"; - exit(-1); -} - # Load any acknowledgements for the host and check the message against then $acks = new Spong::AckList($host); @@ -215,7 +259,7 @@ if ($RULES_MATCH eq 'OLD') { print MAIL "To: $email\n"; print MAIL "Subject: spong - $color $host $service\n\n"; print MAIL scalar localtime($time), "\n"; - print MAIL "$message\n"; + print MAIL "$summary\n"; close( MAIL ); &debug( "mail message sent to $email" ); } @@ -228,7 +272,7 @@ if ($RULES_MATCH eq 'OLD') { print MAIL "To: $skytel\@skymail.com\n"; print MAIL "Subject: spong - $color $host $service\n\n"; print MAIL scalar localtime($time), "\n"; - print MAIL "$message\n"; + print MAIL "$summary\n"; close( MAIL ); &debug( "skytel page sent to $skytel" ); @@ -623,7 +667,7 @@ sub email_status { print MAIL "\n"; # Header/Body boundry print MAIL scalar localtime($time), "\n"; print MAIL "$color $host $service\n"; - print MAIL "$message\n"; + print MAIL "$summary\n"; close( MAIL ); }