Title: [logdaemon] [25] Refacter code
Revision
25
Author
dferguson
Date
2010-02-19 10:35:54 +0000 (Fri, 19 Feb 2010)

Log Message

Refacter code

Set up options once and reused for all commands
Amend debug level of output from logdaemonreader to level 2 to tidy up debug
Add in -L <num and -M <num> to pass through to logdaemonreader
Debug output command being run before running for all methods
Debug note on logdaemonreader output and where it is going
Tidy up help output

Modified Paths

Modified: trunk/check_logdaemon
===================================================================
--- trunk/check_logdaemon	2010-02-19 09:49:53 UTC (rev 24)
+++ trunk/check_logdaemon	2010-02-19 10:35:54 UTC (rev 25)
@@ -26,10 +26,10 @@
 my $nagios_check_nrpe   = "/usr/local/nagios/libexec/check_nrpe";
 my $nagios_check_by_ssh = "";                                       # redundant for now
 
-my $ssh_bin        = "/usr/bin/ssh";
-my $ssh_user       = 'nagios';                                      # Probably should change to nagios
-my $remote_host    = "";
-my $remote_command = "";
+my $ssh_bin         = "/usr/bin/ssh";
+my $ssh_user        = 'nagios';                                     # Probably should change to nagios
+my $remote_host     = "";
+my $command_options = "";
 
 my $logdaemon_host   = "";                                          # From Nagios/OpsView
 my $logdaemon_descr  = "";                                          # From Nagios/OpsView
@@ -41,8 +41,8 @@
 # Autoflush = Great
 $| = 1;
 
-our ( $opt_v, $opt_h, $opt_H, $opt_l, $opt_c, $opt_m, $opt_r, $opt_t );
-getopts('hv:H:l:c:m:r:t:');
+our ( $opt_v, $opt_h, $opt_H, $opt_l, $opt_c, $opt_m, $opt_r, $opt_t, $opt_L, $opt_M );
+getopts('hv:H:l:c:m:r:t:L:M:');
 
 if ( defined $opt_t and $opt_t ) {
     $logdaemon_label = $opt_t;
@@ -117,40 +117,45 @@
 
 # Check if we are using the logfile or label
 if ( defined $opt_l ) {
-    $remote_command = "$logdaemon_reader -c $logdaemon_conf -l $logdaemon_log";
+    $command_options = "-c $logdaemon_conf -l $logdaemon_log";
 }
 elsif ( defined $opt_t ) {
-    $remote_command = "$logdaemon_reader -c $logdaemon_conf -t $logdaemon_label";
+    $command_options = "-c $logdaemon_conf -t $logdaemon_label";
 }
-debug( 1, "About to run [$remote_command]" );
+else {
+    print "WARNING: neither -t label or -l logfile defined\n";
+    exit 1;
+}
 
+if ($opt_M) {
+    $command_options .= " -M $opt_M";
+}
+if ($opt_L) {
+    $command_options .= " -L $opt_L";
+}
+
+debug( 1, "About to run with options [$command_options]" );
+
 # Connect using the -m mode
 if ( $opt_m eq 'local' ) {
-    debug( 1, "Running command locally" );
-    open( LOGDAEMONREADER, " $remote_command|" )
+    my $cmd = "$logdaemon_reader $command_options";
+    debug( 1, "Running command '$cmd' locally" );
+    open( LOGDAEMONREADER, "$cmd|" )
         or print "Couldn't run command: $!\n" && exit 2;
 }
 elsif ( $opt_m eq 'ssh' ) {
-    debug( 1, "Running command via ssh" );
+    my $cmd = "$ssh_bin -q -n -o PreferredAuthentications=publickey $ssh_us...@$remote_host \'$logdaemon_reader $command_options\'";
+    debug( 1, "Running command '$cmd' via ssh" );
     open( STDERR, '>&STDOUT' );
-    open( LOGDAEMONREADER, " $ssh_bin -q -n -o PreferredAuthentications=publickey $ssh_us...@$remote_host \'$remote_command\' | " ) or print "Couldn't run command: $!\n" && exit 2;
+    open( LOGDAEMONREADER, "$cmd| " ) or print "Couldn't run command: $!\n" && exit 2;
     debug( 1, "Ran SSH command.  Err: [$!] / [$?]" );
 }
 elsif ( $opt_m eq 'nrpe' ) {
     debug( 1, "Running command via nrpe" );
     open( STDERR, '>&STDOUT' );
-    if ( defined $opt_l ) {
-        debug( 1, "About to run $nagios_check_nrpe -t 30 -H $remote_host -c check_logdaemon -a \'-c $logdaemon_conf -l $logdaemon_log\'" );
-        open( LOGDAEMONREADER, " $nagios_check_nrpe -t 30 -H $remote_host -c check_logdaemon -a \'-c $logdaemon_conf -l $logdaemon_log\' | " ) or print "Couldn't run command: $!\n" && exit 2;
-    }
-    elsif ( defined $opt_t ) {
-        debug( 1, "About to run $nagios_check_nrpe -t 30 -H $remote_host -c check_logdaemon -a \'-c $logdaemon_conf -t $logdaemon_label\'" );
-        open( LOGDAEMONREADER, " $nagios_check_nrpe -t 30 -H $remote_host -c check_logdaemon -a \'-c $logdaemon_conf -t $logdaemon_label\' | " ) or print "Couldn't run command: $!\n" && exit 2;
-    }
-    else {
-        print "WARNING: neither -t label or -l logfile defined\n";
-        exit 1;
-    }
+    my $cmd = "$nagios_check_nrpe -t 30 -H $remote_host -c check_logdaemon -a \'$command_options\'";
+    debug( 1, "About to run $cmd" );
+    open( LOGDAEMONREADER, "$cmd | " ) or print "Couldn't run command: $!\n" && exit 2;
 }
 else {
     debug( 1, "Yep others are coming soon" );
@@ -201,11 +206,11 @@
 
 foreach $line (<LOGDAEMONREADER>) {
     chomp($line);
-    debug( 1, "Line: $line" );
+    debug( 2, "Line: $line" );
 
     ( $log_err, $log_msg ) = split( ',', $line, 2 );
 
-    debug( 1, "Err: $log_err Msg: $log_msg" );
+    debug( 2, "Err: $log_err Msg: $log_msg" );
 
     if ( not defined $log_err ) {
         print "WARNING: Fields missing from alert! $line\n";
@@ -240,6 +245,7 @@
     my $lastmsg = pop(@results);
 
     # Submit any other messages as passive results
+    debuf( 1, 'Summitting all results via nagios command pipe' );
     open( CMD, "> $nagios_cmd" )
         or print("Can't open cmd $nagios_cmd: $!\n") && exit 2;
 
@@ -250,6 +256,7 @@
     close(CMD)
         or print "UNKNOWN: Couldn't close handle to Nagios CMD: $!/$?\n" && exit 3;
 
+    debug( 1, 'Printing last message received ' );
     print $lastmsg->{'active'};
     exit $lastmsg->{'rc'};
 }
@@ -281,23 +288,22 @@
 ##########################################################################
 # Help / Aide / Assistance / Helfen
 sub help {
-
     print <<_END_;
-
 $0
-	Options with * are mandatory
+    Options with * are mandatory
 
-	-h  		This screen
+    -h          This screen
+    -v N        Verbosity level, 0 off, 1 informational, 4 full
+*   -H host     Hostname to check, FQDN
+*   -c file     Path to Config file: default $logdaemon_conf
+*?  -l file     Path to logfile to check for outfile messages
+*?  -t label    Label of logfile
+*   -m [mode]   Mode of connection, local / ssh / nrpe
 
-	-v N		Verbosity level, 0 off, 1 informational, 4 full
+Options passed straight to logdaemoer reader for use with NRPE 2.5.2 
+with multiline patch
+    -L <num>    Option pased to logdaemonreader to limit per line length
+    -M <num>    Option pased to logdaemonreader to limit total output length
 
-*	-H host		Hostname to check, FQDN
-*	-c file		Path to Config file: default $logdaemon_conf
-
-*?	-l file		Path to logfile to check for outfile messages
-*?	-t label	Label of logfile
-
-*	-m [mode]	Mode of connection, local / ssh / nrpe
-
 _END_
 }

_______________________________________________
Opsview-checkins mailing list
[email protected]
http://lists.opsview.org/lists/listinfo/opsview-checkins

Reply via email to