Title: [opsview] [5678] Major new slave results sending mechanism using NRD

Property changes on: trunk
___________________________________________________________________
Modified: svn:mergeinfo
   - /branches/enterprise/BRAN-3.10:5539,5623,5628,5630,5673
   + /branches/DEV-nrd:5559-5677
/branches/enterprise/BRAN-3.10:5539,5623,5628,5630,5673

Modified: trunk/CHANGES
===================================================================
--- trunk/CHANGES	2011-01-13 09:35:38 UTC (rev 5677)
+++ trunk/CHANGES	2011-01-13 12:06:58 UTC (rev 5678)
@@ -1,12 +1,23 @@
 This documents the major changes between releases
 
-???? 
+3.11.0 ????
     FEATURES:
     Access to monitoring objects now controlled at roles, rather than at contacts. This is easier to manage
       for systems with lots of contacts with similar profiles
     Keyword edit page now allows filtering of service checks based on hosts selected, to simplify choosing of related service checks 
     Viewport summary list and cells displays a handled icon if all hosts and services within the viewport are handled. This
       means a user can tell if the viewport requires further investigation
+    New slave results mechanism with the following improvements:
+      * ability to send results with multiline plugin output
+      * no limitations on amount of data sent, so long plugin results are passed up
+      * performance improvement for uploading of results by up to 80% when sending a large number of results
+      * result queuing on the slave, with discards for results over 1 minute old
+      * results written to checkresults directory, cutting out a Nagios processing loop
+      * transactional results, so if a sending failure occurs, whole transaction is aborted
+      * dynamic preforking and expanding of number of servers on master as more slaves are created
+      * timestamp now based on client send time, not server receive time
+      * flexible sized packets, so only amount of data necessary to transfer
+      * ability to switch to old NSCA mechanism if desired
 
     ENHANCEMENTS:
     Added easyXDM library for cross domain REST communication

Modified: trunk/opsview-base/Makefile
===================================================================
--- trunk/opsview-base/Makefile	2011-01-13 09:35:38 UTC (rev 5677)
+++ trunk/opsview-base/Makefile	2011-01-13 12:06:58 UTC (rev 5678)
@@ -48,6 +48,7 @@
 FLOT = flot-0.5
 JQUERYSPARKLINES=jquery.sparkline.min.1.5.1.js
 EASYXDM = easyXDM-2.4.9.102
+NRD = NRD-Daemon-0.01
 
 ICOJOY4 = icojoy-webicons-4
 ICOJOY4_TYPE = noshadow/standart/png/24x24
@@ -139,7 +140,7 @@
 	${NDOUTILS} nrpe.cfg opsview-base.spec opsview-agent.spec version \
 	allmibs.tar.gz ${CHECK_RADIUS_IH} ${AUTOSSH} ${OVERLIB} ${HYPERGRAPH} ${NMIS} ${FABTABULOUS} \
 	${NAGVIS} ${DATATABLES} ${TIMELINE} ${TANGOICONS} ${WEBICONS} ${ICOJOY4} ${FLOT} \
-	${JQUERYSPARKLINES} ${EASYXDM}
+	${JQUERYSPARKLINES} ${EASYXDM} ${NRD}
 
 all: ${GENERATED}
 
@@ -154,6 +155,7 @@
 	${FLOT}-install \
 	${ICOJOY4}-install ${FABTABULOUS}-install ${TANGOICONS}-install ${WEBICONS}-install ${NAGVIS}-install \
 	${EASYXDM}-install \
+	${NRD}-install \
 	jsscripts-install install-fladmin post-test
 
 install-dev: install
@@ -233,6 +235,15 @@
 
 icojoy4-install: ${ICOJOY4}-install
 
+nrd: ${NRD}
+
+${NRD}:
+	gunzip -c ${NRD}.tar.gz | tar -xf -
+	cd ${NRD} && perl Makefile.PL PREFIX=${NAGIOS_DIR}/perl LIB=${NAGIOS_DIR}/perl/lib INSTALLDIRS=site INSTALLSITEMANDIR=${NAGIOS_DIR}/perl/man1 INSTALLSITEMAN3DIR=${NAGIOS_DIR}/perl/man3 && make
+
+${NRD}-install:
+	cd ${NRD} && make install
+
 easyxdm: ${EASYXDM}
 
 ${EASYXDM}:

Copied: trunk/opsview-base/NRD-Daemon-0.01.tar.gz (from rev 5677, branches/DEV-nrd/opsview-base/NRD-Daemon-0.01.tar.gz)
===================================================================
(Binary files differ)

Modified: trunk/opsview-core/bin/create_and_send_configs
===================================================================
--- trunk/opsview-core/bin/create_and_send_configs	2011-01-13 09:35:38 UTC (rev 5677)
+++ trunk/opsview-core/bin/create_and_send_configs	2011-01-13 12:06:58 UTC (rev 5678)
@@ -78,6 +78,7 @@
 my $master = Opsview::Monitoringserver->get_master or die "No monitoringservers are defined";
 my $host_dir = "$config_dir/" . $master->name;
 
+my $master_files          = "nrd.conf";
 my $transfer_files        = "*.cfg htpasswd.* *.csv nmis.conf plugins";
 my $transfer_files_conf_d = "conf.d";
 my $transfer_files_slave  = "*.dat slavenode.id";
@@ -296,7 +297,7 @@
     run_on_start => sub {
         umask 077
             && chdir "$host_dir"
-            && exec "tar --gzip -hcf $configs_tmp_dir/$configs_tar_gz $transfer_files $transfer_files_conf_d";
+            && exec "tar --gzip -hcf $configs_tmp_dir/$configs_tar_gz $transfer_files $transfer_files_conf_d $master_files";
     },
     run_after => ["master_merge_conf_d"],
     name      => "master_tar",

Copied: trunk/opsview-core/bin/import_slaveresultsd (from rev 5677, branches/DEV-nrd/opsview-core/bin/import_slaveresultsd)
===================================================================
--- trunk/opsview-core/bin/import_slaveresultsd	                        (rev 0)
+++ trunk/opsview-core/bin/import_slaveresultsd	2011-01-13 12:06:58 UTC (rev 5678)
@@ -0,0 +1,244 @@
+#!/usr/bin/perl
+#
+#
+# SYNTAX:
+#	import_slaveresultsd
+#
+# DESCRIPTION:
+#	Imports slave results from Nagios to Opsview master
+#   Takes cache log files and then pushes those via NRD to master
+#	Allows Nagios to continue processing while this is asynchronously updating
+#	Will import files from the var/slaveresults directory
+#	in filename order (which should be timestamped)
+#
+# AUTHORS:
+#	Copyright (C) 2003-2010 Opsera Limited. All rights reserved
+#
+#    This file is part of Opsview
+#
+#    Opsview 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.
+#
+#    Opsview 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 Opsview; if not, write to the Free Software
+#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+#
+
+use warnings;
+use strict;
+use FindBin qw($Bin);
+use lib $Bin, "$Bin/../lib", "$Bin/../etc", "$Bin/../perl/lib";
+use Log::Log4perl;
+use POSIX;
+use Time::HiRes qw(sleep gettimeofday);
+use Try::Tiny;
+
+use NRD::Client;
+
+# Allow a cache of 5 minutes
+my $max_file_age = 5 * 60;
+
+$0 = "import_slaveresultsd";
+
+my $rootdir    = "/usr/local/nagios";
+my $import_dir = "$rootdir/var/slaveresults";
+
+Log::Log4perl::init_and_watch( "$rootdir/etc/Log4perl-slave.conf", 30 );
+
+my $logger = Log::Log4perl->get_logger("import_slaveresultsd");
+
+$logger->info("Starting");
+
+my $pidfile = "/usr/local/nagios/var/import_slaveresultsd.pid";
+if ( -e $pidfile ) {
+    open( my $fh, "< $pidfile" ) or $logger->logdie("Cannot read pid file - is daemon running?");
+    my $pid = <$fh>;
+    close $fh;
+    if ( kill 0, $pid ) {
+        $logger->logdie("import_slaveresultsd already running as process $pid");
+    }
+    else {
+        $logger->warn("Crash from previous run. Deleting pid file");
+        unlink $pidfile or $logger->logdie("Cannot delete pidfile");
+    }
+}
+
+# Do cleanup on exit
+my $pid;
+END { cleanup() unless $pid }
+
+if (1) {
+    $pid = fork;
+    exit if $pid;
+    $logger->logdie("Couldn't fork: $!") unless defined $pid;
+    POSIX::setsid() or $logger->logdie("Cannot daemonize");
+
+    $logger->info("Daemonised");
+
+    open PID, "> $pidfile" or die "Could not write to pid file $pidfile";
+    print PID "$$";
+    close PID;
+
+    for my $handle ( *STDIN, *STDOUT, *STDERR ) {
+        open( $handle, "+<", "/dev/null" ) || die "can't reopen $handle to /dev/null: $!";
+    }
+}
+
+my $break = 0;
+$SIG{INT}  = \&catch_signals;
+$SIG{TERM} = \&catch_signals;
+$SIG{PIPE} = 'IGNORE';
+
+my $log_duration_threshold = 12;
+
+if ( !-e $import_dir ) {
+    mkdir $import_dir || $logger->logdie("Cannot create $import_dir");
+}
+chdir($import_dir) or $logger->logdie("Cannot chdir: $!");
+my $last_update = 0;
+my $c           = 0;
+
+my ( $host, $port );
+my $conf = {};
+setup();
+
+MAINLOOP:
+until ($break) {
+
+    # Not sure why, but can't use IMPORT_DIR filehandle
+    my $dir_update = ( stat(".") )[9];
+    my $found      = 0;
+    if ( $dir_update != $last_update ) {
+        opendir( IMPORT_DIR, "." ) or $logger->logdie("Cannot open dir: $!");
+
+        # sorting required to get files in timestamped order
+        my @files = sort ( grep !/^\.\.?\z/, readdir IMPORT_DIR );
+        closedir IMPORT_DIR;
+
+    RETRY:
+        while ( my $file = shift @files ) {
+            last MAINLOOP if ($break);
+
+            my $error = undef;
+            try {
+                process_each_file($file);
+            }
+            catch {
+                $error = $_;
+            };
+            if ( defined $error ) {
+                chomp $error;
+                $error =~ s/\n/ /g;
+                $logger->error("Error processing data [$error] - retry in 5 seconds");
+                unshift @files, $file;
+                sleep 5;
+                next RETRY;
+            }
+
+            $found = 1;
+        }
+        $last_update = $dir_update;
+    }
+
+    # Directory would have changed from our last import, so expect above if to succeed
+    # But allows a next check to run if more files have been added
+    sleep 0.25 unless $found;
+}
+
+sub cleanup {
+    $logger->info("Stopping $0");
+    unlink $pidfile;
+}
+
+sub process_each_file {
+    my $file      = shift;
+    my $start     = gettimeofday();
+    my $file_size = -s $file;
+
+    $logger->debug("Processing $file, size $file_size");
+
+    if ( $file_size == 0 ) {
+        unlink $file;
+        return;
+    }
+
+    # Drop file if too old
+    my $file_age = $start - $file;
+    if ( $file_age > $max_file_age ) {
+        $logger->warn( "Dropping $file (" . scalar localtime($file) . ") because file age ($file_age) is greater than max file age ($max_file_age)" );
+        unlink $file;
+        return;
+    }
+
+    unless ( open NAGIOSDATA, $file ) {
+        $logger->error("Error opening $file: $!");
+        return;
+    }
+
+    my $client = NRD::Client->connect_with_serializer(
+        $conf->{'serializer'},
+        $conf,
+        PeerAddr => $host,
+        PeerPort => $port,
+        Proto    => 'tcp',
+    );
+    $client->send_results_from_lines(*NAGIOSDATA);
+    $client->end;
+    close NAGIOSDATA;
+
+    if ( $logger->is_debug() ) {
+        my $debug_dir = "/usr/local/nagios/var/slaveresults.archive";
+        unless ( -d $debug_dir ) {
+            mkdir $debug_dir;
+        }
+        rename( $file, "$debug_dir/$file" );
+    }
+    else {
+        unlink $file;
+    }
+
+    my $duration = gettimeofday() - $start;
+    $logger->debug("Finished $file. Duration=$duration");
+    if ( $duration > $log_duration_threshold ) {
+        my $nice_duration = sprintf( "%0.2f", $duration );
+        $logger->warn("Import of $file, size=$file_size, took $nice_duration seconds > $log_duration_threshold seconds");
+    }
+}
+
+sub catch_signals {
+    if ( $break++ == 0 ) {
+        $logger->warn("Received kill signal - gracefully shutting down");
+    }
+    else {
+        $logger->logdie("Received kill signal - forced death");
+    }
+}
+
+sub setup {
+    my $conf_file = "/usr/local/nagios/etc/send_nrd.cfg";
+    {
+        local *CONF;
+        open CONF, "<", $conf_file or next;
+        while ( my $line = <CONF> ) {
+            chomp $line;
+            next if ( $line =~ m/^\s*#/ );
+            next if ( $line =~ m/^\s*$/ );
+            $line =~ s/^\s*(\w+)\s+(.{1,}?)\s*$/$conf->{$1} = $2;/ge;
+        }
+    }
+
+    $host = $conf->{'host'} ||= 'localhost';
+    $port = $conf->{'port'} ||= 5669;
+
+    $conf->{'serializer'}   ||= 'none';
+    $conf->{'encrypt_type'} ||= '';
+    $conf->{'encrypt_key'}  ||= '';
+
+}

Modified: trunk/opsview-core/bin/nagconfgen.pl
===================================================================
--- trunk/opsview-core/bin/nagconfgen.pl	2011-01-13 09:35:38 UTC (rev 5677)
+++ trunk/opsview-core/bin/nagconfgen.pl	2011-01-13 12:06:58 UTC (rev 5678)
@@ -266,6 +266,7 @@
         write_trapinfocfg();
         write_notificationmethodvariables();
         write_nscacfg();
+        write_nrdcfg();
         write_send_nscacfg();
         write_ndo2dbcfg();
         write_ndomodcfg();
@@ -1812,30 +1813,27 @@
 #
 ################################################################################
 
-# For nagiosgraph
+# For nagiosgraph, master only
 define command{
  command_name process-service-perfdata-nagiosgraph
  command_line /usr/local/nagios/bin/rotate_async_log perfdata.log perfdatarrd
  }
-
-# ocsp_command for distributed monitoring
 define command {
-	command_name	ocsp_command
-	command_line	/usr/local/nagios/bin/ocsp_append_to_file
+ command_name	process-host-cache-data
+ command_line    /usr/local/nagios/bin/process-cache-data cache_host.log
 }
 define command {
-	command_name	ochp_command
-	command_line	/usr/local/nagios/bin/ochp_append_to_file
+ command_name	process-service-cache-data
+ command_line /usr/local/nagios/bin/process-cache-data cache_service.log
 }
-
-# for processing cached obsessive compulsive host and service data on slaves
+# for slave results via NRD
 define command {
-	command_name	process-host-cache-data
-	command_line	/usr/local/nagios/bin/process-cache-data cache_host.log
+ command_name	process-host-cache-nrd
+ command_line /usr/local/nagios/bin/rotate_async_log -n cache_host.log slaveresults
 }
 define command {
-	command_name	process-service-cache-data
-	command_line	/usr/local/nagios/bin/process-cache-data cache_service.log
+ command_name	process-service-cache-nrd
+ command_line /usr/local/nagios/bin/rotate_async_log -n cache_service.log slaveresults
 }
 define command {
 	command_name	rotate_ndo_log
@@ -2465,9 +2463,6 @@
 # event handlers may take longer than 10 seconds. Watch out as nagios stops new checks over this period
 event_handler_timeout=60
 notification_timeout=30
-# ocsp timeout needs to be > 10 seconds because of nsca timeouts
-ocsp_timeout=12
-ochp_timeout=10
 perfdata_timeout=12
 
 
@@ -2769,55 +2764,17 @@
 
 
 # OBSESS OVER SERVICE CHECKS OPTION
-# This determines whether or not Nagios will obsess over service
-# checks and run the ocsp_command defined below.  Unless you're
-# planning on implementing distributed monitoring, do not enable
-# this option.  Read the HTML docs for more information on
-# implementing distributed monitoring.
-# Values: 1 = obsess over services, 0 = do not obsess (default)
-
+# OBSESS OVER HOST CHECKS OPTION
 obsess_over_services=%OBSESS%
-
-
-
+obsess_over_hosts=%OBSESS%
 # OBSESSIVE COMPULSIVE SERVICE PROCESSOR COMMAND
-# This is the command that is run for every service check that is
-# processed by Nagios.  This command is executed only if the
-# obsess_over_services option (above) is set to 1.  The command 
-# argument is the short name of a command definition that you
-# define in your host configuration file. Read the HTML docs for
-# more information on implementing distributed monitoring.
-
-ocsp_command=ocsp_command
-ochp_command=ochp_command
-
-
-
 # OBSESS OVER HOST CHECKS OPTION
-# This determines whether or not Nagios will obsess over host
-# checks and run the ochp_command defined below.  Unless you're
-# planning on implementing distributed monitoring, do not enable
-# this option.  Read the HTML docs for more information on
-# implementing distributed monitoring.
-# Values: 1 = obsess over hosts, 0 = do not obsess (default)
+# Not used in Opsview
+#ocsp_command=
+#ochp_command=
 
-obsess_over_hosts=%OBSESS%
 
 
-
-
-# OBSESSIVE COMPULSIVE HOST PROCESSOR COMMAND
-# This is the command that is run for every host check that is
-# processed by Nagios.  This command is executed only if the
-# obsess_over_hosts option (above) is set to 1.  The command
-# argument is the short name of a command definition that you
-# define in your host configuration file. Read the HTML docs for
-# more information on implementing distributed monitoring.
-
-#ochp_command=somecommand
-
-
-
 # TRANSLATE PASSIVE HOST CHECKS OPTION
 # This determines whether or not Nagios will translate
 # DOWN/UNREACHABLE passive host check results into their proper
@@ -3266,12 +3223,10 @@
         $nagios_cfg =~ s/%NOTIFICATIONS%/0/g;
     }
     if ( $monitoringserver->is_slave ) {
-        $nagios_cfg =~ s/%NOTIFICATIONS%/1/g;                                              # Slaves notify for own servers
-        $nagios_cfg =~ s/%PROCESS_PERFORMANCE_DATA%/1/g;                                   # CHANGED BEHAVIOUR - RT#324
+        $nagios_cfg =~ s/%NOTIFICATIONS%/1/g;                  # Slaves notify for own servers
+        $nagios_cfg =~ s/%PROCESS_PERFORMANCE_DATA%/1/g;       # CHANGED BEHAVIOUR - RT#324
         $nagios_cfg =~ s/%PROCESS_PERFORMANCE_HST_MODE%/a/g;
         $nagios_cfg =~ s/%PROCESS_PERFORMANCE_SVC_MODE%/a/g;
-        $nagios_cfg =~ s/%PROCESS_PERFORMANCE_SVC_COMMAND%/process-service-cache-data/g;
-        $nagios_cfg =~ s/%PROCESS_PERFORMANCE_HST_COMMAND%/process-host-cache-data/g;
 
         # Hosts have to be run as well as services, because otherwise the cache_host.log file is
         # not reopened correctly
@@ -3281,9 +3236,22 @@
         $nagios_cfg =~ s!%PROCESS_PERFORMANCE_SVC_LOG%!/usr/local/nagios/var/cache_service.log!g;
         $nagios_cfg =~ s'%NODE_CFG%'cfg_file=node.cfg'g;
         $nagios_cfg =~ s/%DIST_CFG%/cfg_file=slave.cfg/g;
-        $host_template    = '$HOSTNAME$\t$HOSTSTATEID$\t$HOSTOUTPUT$|$HOSTPERFDATA$';
-        $service_template = '$HOSTNAME$\t$SERVICEDESC$\t$SERVICESTATEID$\t$SERVICEOUTPUT$|$SERVICEPERFDATA$';
 
+        if ( Opsview::Config->slave_send_method eq "nsca" ) {
+            $host_template    = '$HOSTNAME$\t$HOSTSTATEID$\t$HOSTOUTPUT$|$HOSTPERFDATA$';
+            $service_template = '$HOSTNAME$\t$SERVICEDESC$\t$SERVICESTATEID$\t$SERVICEOUTPUT$|$SERVICEPERFDATA$';
+            $nagios_cfg =~ s/%PROCESS_PERFORMANCE_SVC_COMMAND%/process-service-cache-data/g;
+            $nagios_cfg =~ s/%PROCESS_PERFORMANCE_HST_COMMAND%/process-host-cache-data/g;
+        }
+        else {
+
+            # Need 3 \s because the final nagios.cfg consists of '\\n'
+            $host_template    = '$HOSTNAME$\t$HOSTSTATEID$\t$HOSTOUTPUT$\\\n$LONGHOSTOUTPUT$|$HOSTPERFDATA$';
+            $service_template = '$HOSTNAME$\t$SERVICEDESC$\t$SERVICESTATEID$\t$SERVICEOUTPUT$\\\n$LONGSERVICEOUTPUT$|$SERVICEPERFDATA$';
+            $nagios_cfg =~ s/%PROCESS_PERFORMANCE_SVC_COMMAND%/process-service-cache-nrd/g;
+            $nagios_cfg =~ s/%PROCESS_PERFORMANCE_HST_COMMAND%/process-host-cache-nrd/g;
+        }
+
         # Slaves read the sync file
         $sync_retention_file = "sync_retention_file=/usr/local/nagios/var/sync.dat";
     }
@@ -3555,6 +3523,72 @@
     print "Written nsca.cfg", $/;
 }
 
+sub write_nrdcfg {
+
+    # Could possibly change the max_servers value dynamically based on number of slave nodes
+    # But this would require a restart of nrd. Perhaps patch Net::Server to monitor this config
+    # file and re-read when changed
+    my $nrd = <<'EOF';
+# NRD configuration - generated by nagconfgen
+server_type PreFork
+min_servers 4
+min_spare_servers 1
+max_spare_servers 2
+max_servers 12
+
+user nagios
+group nagios
+background 1
+reverse_lookups off
+host 127.0.0.1
+port 5669
+timeout 120
+
+# logging
+log_file Log::Log4perl
+log_level 2
+log4perl_conf /usr/local/nagios/etc/Log4perl.conf
+log4perl_logger nrd
+pid_file /usr/local/nagios/var/nrd.pid
+
+# access control
+cidr_allow 127.0.0.0/8
+
+encryption_method 2
+serializer crypt
+encrypt_type Blowfish
+encrypt_key %NRD_SHARED_PASSWORD%
+writer resultdir
+check_result_path /usr/local/nagios/var/spool/checkresults
+batch_results 1
+EOF
+
+    my $nrd_shared_password = Opsview::Config->nrd_shared_password;
+    $nrd =~ s/%NRD_SHARED_PASSWORD%/$nrd_shared_password/;
+
+    open OUTFILE, "> $configfilepath/nrd.conf" or die "Can't open file $configfilepath/nrd.conf $!";
+    print OUTFILE $nrd;
+    close OUTFILE;
+    chmod 0600, "$configfilepath/nrd.conf";
+    print "Written nrd.conf", $/;
+
+    my $send_nrd = <<'EOF';
+# send_nrd configuration - generated by nagconfgen
+host 127.0.0.1
+serializer crypt
+encrypt_type Blowfish
+encrypt_key %NRD_SHARED_PASSWORD%
+EOF
+
+    $send_nrd =~ s/%NRD_SHARED_PASSWORD%/$nrd_shared_password/;
+
+    open OUTFILE, "> $configfilepath/send_nrd.cfg" or die "Can't open file $configfilepath/send_nrd.cfg: $!";
+    print OUTFILE $send_nrd;
+    close OUTFILE;
+    chmod 0600, "$configfilepath/send_nrd.cfg";
+    print "Written send_nrd.cfg", $/;
+}
+
 sub write_send_nscacfg {
     my $send_nsca = <<"HEADER". <<'EOF';
 ################################################################################

Copied: trunk/opsview-core/bin/nrd (from rev 5677, branches/DEV-nrd/opsview-core/bin/nrd)
===================================================================
--- trunk/opsview-core/bin/nrd	                        (rev 0)
+++ trunk/opsview-core/bin/nrd	2011-01-13 12:06:58 UTC (rev 5678)
@@ -0,0 +1,12 @@
+#!/usr/bin/perl
+# This is a minimal script to invoke NRD - amended from the core nrd script
+
+eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
+    if 0;    # not running under some shell
+
+use NRD::Daemon;
+
+# Do not use a hash ref here as the nrd.conf will not get read correctly
+NRD::Daemon->run( conf_file => '/usr/local/nagios/etc/nrd.conf' );
+
+exit;

Modified: trunk/opsview-core/bin/opsviewd
===================================================================
--- trunk/opsview-core/bin/opsviewd	2011-01-13 09:35:38 UTC (rev 5677)
+++ trunk/opsview-core/bin/opsviewd	2011-01-13 12:06:58 UTC (rev 5678)
@@ -318,9 +318,10 @@
 
         # Remember to update rc.opsview-slave too
         # 5667 - nsca
+        # 5669 - nrd
         # 2345 - NMIS master.pl
         # 4125 - activemq
-        my @command = ( "ssh", "-n", "-N", "-T", "-2", @ssh_options, "-R", "4125:localhost:4125", "-R", "5667:localhost:5667", "-R", "2345:localhost:2345", 'nagios@' . $ip );
+        my @command = ( "ssh", "-n", "-N", "-T", "-2", @ssh_options, "-R", "5669:localhost:5669", "-R", "4125:localhost:4125", "-R", "5667:localhost:5667", "-R", "2345:localhost:2345", 'nagios@' . $ip );
         my $pid = fork;
         if ( $pid && $pid != 0 ) {
 

Modified: trunk/opsview-core/bin/rc.opsview
===================================================================
--- trunk/opsview-core/bin/rc.opsview	2011-01-13 09:35:38 UTC (rev 5677)
+++ trunk/opsview-core/bin/rc.opsview	2011-01-13 12:06:58 UTC (rev 5678)
@@ -327,6 +327,9 @@
 		if [ $MASTER = "yes" ] ; then
 			$prefix/bin/opsviewd || die "Cannot startup opsviewd"
 			$prefix/bin/nsca -c $prefix/etc/nsca.cfg --single || die "Cannot startup nsca"
+            $prefix/bin/nrd || die "Cannot startup nrd"
+        else
+            $prefix/bin/import_slaveresultsd || die "Cannot startup import_slaveresultsd"
 		fi
 
 		# Remove the reload flag, if a reload happens to have been requested during this outage period
@@ -340,8 +343,11 @@
 		fi
 		printf "Stopping opsview... "
 		if [ $MASTER = "yes" ] ; then
+            stop_application nrd $prefix/var/nrd.pid
 			stop_application nsca $NSCARun
 			stop_application opsviewd $OpsviewRun
+        else
+            stop_application import_slaveresultsd $prefix/var/import_slaveresultsd.pid
 		fi
 		stop_application nagios $NagiosRun
 		if [ $MASTER = "yes" ] ; then

Modified: trunk/opsview-core/bin/rotate_async_log
===================================================================
--- trunk/opsview-core/bin/rotate_async_log	2011-01-13 09:35:38 UTC (rev 5677)
+++ trunk/opsview-core/bin/rotate_async_log	2011-01-13 12:06:58 UTC (rev 5678)
@@ -1,12 +1,13 @@
 #!/usr/bin/perl
 #
 # SYNTAX:
-#	rotate_async_log {logfile} {targetdir}
+#	rotate_async_log [-n] {logfile} {targetdir}
 #
 # DESCRIPTION:
 #	Moves /usr/local/nagios/var/{logfile} into /usr/local/nagios/var/{targetdir} for import_ndologs
 #   or import_perfdatarrd daemon to asynchronously update db
 #   Will then overwrite existing log file
+#   -n means do not rotate zero bytes files
 #
 # AUTHORS:
 #	Copyright (C) 2003-2010 Opsera Limited. All rights reserved
@@ -33,6 +34,12 @@
 use File::Copy;
 use Time::HiRes qw( gettimeofday );
 
+my $ignore_zero_files;
+if ( $ARGV[0] eq "-n" ) {
+    $ignore_zero_files = 1;
+    shift @ARGV;
+}
+
 my $var       = "/usr/local/nagios/var";
 my $logfile   = "$var/" . shift @ARGV;
 my $newlogdir = "$var/" . shift @ARGV;
@@ -41,7 +48,9 @@
 
 my $dest = sprintf( "$newlogdir/%d.%06d", $time[0], $time[1] );
 
-move( $logfile, $dest );
+if ( !$ignore_zero_files || ( -s $logfile > 0 ) ) {
+    move( $logfile, $dest );
+}
 
 umask 077;
 open( F, ">", $logfile );

Copied: trunk/opsview-core/bin/send_nrd (from rev 5677, branches/DEV-nrd/opsview-core/bin/send_nrd)
===================================================================
--- trunk/opsview-core/bin/send_nrd	                        (rev 0)
+++ trunk/opsview-core/bin/send_nrd	2011-01-13 12:06:58 UTC (rev 5678)
@@ -0,0 +1,64 @@
+#!/usr/bin/perl
+
+eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
+    if 0;    # not running under some shell
+
+use strict;
+use warnings;
+
+use Getopt::Std;
+use IO::Socket;
+use NRD::Packet;
+use NRD::Serialize;
+
+my %opts;
+getopt( 'c', \%opts );
+my $conf_file = $opts{'c'} || '/etc/send_nrd.cfg';
+my ( $host, $port );
+my $conf = {};
+{
+    local *CONF;
+    open CONF, "<", $conf_file or next;
+    while ( my $line = <CONF> ) {
+        chomp $line;
+        next if ( $line =~ m/^\s*#/ );
+        next if ( $line =~ m/^\s*$/ );
+        $line =~ s/^\s*(\w+)\s+(.{1,}?)\s*$/$conf->{$1} = $2;/ge;
+    }
+}
+
+$host = $conf->{'host'} ||= 'localhost';
+$port = $conf->{'port'} ||= 5669;
+
+$conf->{'serializer'}   ||= 'none';
+$conf->{'encrypt_type'} ||= '';
+$conf->{'encrypt_key'}  ||= '';
+
+#
+#use Data::Dumper;
+#print Dumper($conf, $host, $port);
+#
+my $serializer = NRD::Serialize->instance_of( lc( $conf->{'serializer'} ), $conf );
+
+my $sock = IO::Socket::INET->new(
+    PeerAddr => $host,
+    PeerPort => $port,
+    Proto    => 'tcp',
+) || die "Can't connect [$!]";
+
+my $packer = NRD::Packet->new();
+if ( $serializer->needs_helo ) {
+    print $sock $packer->pack( $serializer->helo );
+}
+
+while ( my $line = <STDIN> ) {
+    chomp $line;
+    my $r = {
+        'time' => time(),
+        %{ $serializer->from_line($line) }
+    };
+    print $sock $packer->pack( $serializer->freeze($r) );
+}
+
+close $sock;
+exit;

Modified: trunk/opsview-core/etc/Log4perl-slave.conf
===================================================================
--- trunk/opsview-core/etc/Log4perl-slave.conf	2011-01-13 09:35:38 UTC (rev 5677)
+++ trunk/opsview-core/etc/Log4perl-slave.conf	2011-01-13 12:06:58 UTC (rev 5678)
@@ -6,6 +6,7 @@
 # Overrides to specific components
 #log4perl.logger.sync_cluster_node_status=DEBUG
 #log4perl.logger.takeover_hosts=DEBUG
+#log4perl.logger.import_slaveresultsd=DEBUG
 
 log4perl.appender.OPSVIEW_SLAVE_LOG=Log::Dispatch::FileRotate
 log4perl.appender.OPSVIEW_SLAVE_LOG.filename=/usr/local/nagios/var/log/opsview-slave.log

Modified: trunk/opsview-core/etc/Log4perl.conf
===================================================================
--- trunk/opsview-core/etc/Log4perl.conf	2011-01-13 09:35:38 UTC (rev 5677)
+++ trunk/opsview-core/etc/Log4perl.conf	2011-01-13 12:06:58 UTC (rev 5678)
@@ -17,6 +17,8 @@
 #log4perl.logger.ndoutils_configdumpend=DEBUG
 #log4perl.logger.exec_and_log=DEBUG
 #log4perl.logger.import_excel=DEBUG
+# You will need to increase the logging at nrd.conf to get debug messages out
+#log4perl.logger.nrd=DEBUG
 
 log4perl.appender.OPSVIEWD_LOGFILE=Log::Dispatch::FileRotate
 log4perl.appender.OPSVIEWD_LOGFILE.filename=/var/log/opsview/opsviewd.log

Modified: trunk/opsview-core/etc/opsview.defaults
===================================================================
--- trunk/opsview-core/etc/opsview.defaults	2011-01-13 09:35:38 UTC (rev 5677)
+++ trunk/opsview-core/etc/opsview.defaults	2011-01-13 12:06:58 UTC (rev 5678)
@@ -149,6 +149,14 @@
 # Set bind address for nsca
 $nsca_server_address = "127.0.0.1";
 
+# NRD password
+# Shared key for slaves sending results back via NRD
+$nrd_shared_password = "initial";
+
+# Slave send method
+# nrd or nsca
+$slave_send_method = "nrd";
+
 ##########################################################################################################
 # The next section contains examples of how to change configuration parameters in nagios.cfg and cgi.cfg
 # If you wish to change any parameters then copy entire section over to opsview.conf including 'EOF' line,

Modified: trunk/opsview-core/filelist
===================================================================
--- trunk/opsview-core/filelist	2011-01-13 09:35:38 UTC (rev 5677)
+++ trunk/opsview-core/filelist	2011-01-13 12:06:58 UTC (rev 5678)
@@ -29,6 +29,7 @@
 f nagios:nagios 0750 /usr/local/nagios/bin/import_runtime      bin/import_runtime
 f nagios:nagios 0750 /usr/local/nagios/bin/ndoutils_configdumpend bin/ndoutils_configdumpend
 f nagios:nagios 0750 /usr/local/nagios/bin/nlcat               bin/nlcat
+f nagios:nagios 0750 /usr/local/nagios/bin/nrd                 bin/nrd
 f nagios:nagios 0750 /usr/local/nagios/bin/opsview_api bin/opsview_api
 f nagios:nagios 0750 /usr/local/nagios/bin/opsview_rest bin/opsview_rest
 f nagios:nagios 0750 /usr/local/nagios/bin/opsview_diag             bin/opsview_diag
@@ -481,6 +482,7 @@
 d nagios:nagios 0755 /usr/local/nagios/var/perfdatarrd
 d nagios:nagios 0775 /usr/local/nagios/var/plugins
 d nagios:nagios 0755 /usr/local/nagios/var/reports
+d nagios:nagios 0755 /usr/local/nagios/var/slaveresults
 d nagios:nagios 0775 /usr/local/nagios/var/spool
 d nagios:nagios 0775 /usr/local/nagios/var/spool/checkresults
 # Need to create this file to get the right permissions
@@ -518,6 +520,7 @@
 f nagios:nagios 0550 /usr/local/nagios/installer/set_profile         installer/set_profile
 f nagios:nagios 0555 /usr/local/nagios/installer/create_initial_opsview_conf installer/create_initial_opsview_conf
 f nagios:nagios 0555 /usr/local/nagios/installer/rrd_converter       installer/rrd_converter
+f nagios:nagios 0755 /usr/local/nagios/installer/update_opsview_conf installer/update_opsview_conf
 f nagios:nagios 0555 /usr/local/nagios/installer/upgradedb.pl        installer/upgradedb.pl
 f nagios:nagios 0555 /usr/local/nagios/installer/upgradedb_odw.pl     installer/upgradedb_odw.pl
 f nagios:nagios 0555 /usr/local/nagios/installer/upgradedb_opsview.pl installer/upgradedb_opsview.pl

Modified: trunk/opsview-core/installer/postinstall
===================================================================
--- trunk/opsview-core/installer/postinstall	2011-01-13 09:35:38 UTC (rev 5677)
+++ trunk/opsview-core/installer/postinstall	2011-01-13 12:06:58 UTC (rev 5678)
@@ -98,6 +98,8 @@
 BEGIN: { $ug = new Data::UUID; $uuid = $ug->to_string( $ug->create ); }
 s/^\$authtkt_shared_secret\s*=\s*"shared-secret-please-change";$/\$authtkt_shared_secret = "$uuid";/;
 ' /usr/local/nagios/etc/opsview.conf
+
+    /usr/local/nagios/installer/update_opsview_conf
 fi
 
 # Add crontabs

Copied: trunk/opsview-core/installer/update_opsview_conf (from rev 5677, branches/DEV-nrd/opsview-core/installer/update_opsview_conf)
===================================================================
--- trunk/opsview-core/installer/update_opsview_conf	                        (rev 0)
+++ trunk/opsview-core/installer/update_opsview_conf	2011-01-13 12:06:58 UTC (rev 5678)
@@ -0,0 +1,61 @@
+#!/usr/bin/perl
+#
+#
+# update_opsview_conf
+#
+#	This updates opsview.conf with any new variables required
+#	Run as nagios user from postinstall
+#
+# AUTHORS:
+#	Copyright (C) 2003-2010 Opsera Limited. All rights reserved
+#
+#    This file is part of Opsview
+#
+#    Opsview 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.
+#
+#    Opsview 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 Opsview; if not, write to the Free Software
+#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+#
+
+use strict;
+use warnings;
+use lib "/usr/local/nagios/perl/lib";
+use Data::UUID;
+
+my $conf_file = "/usr/local/nagios/etc/opsview.conf";
+open CONF, $conf_file || die "Cannot open $conf_file";
+my @opsview_conf = <CONF>;
+close CONF;
+my $changed = 0;
+
+my $found_nrd_shared_password = 0;
+foreach (@opsview_conf) {
+    $found_nrd_shared_password = 1 if /^\$nrd_shared_password\s*=\s*/;
+}
+
+if ( $opsview_conf[$#opsview_conf] =~ /^\s*1\s*;\s*/ ) {
+    pop @opsview_conf;
+}
+
+if ( !$found_nrd_shared_password ) {
+    my $ug   = new Data::UUID;
+    my $uuid = $ug->to_string( $ug->create );
+    push @opsview_conf, "\$nrd_shared_password = \"$uuid\";\n";
+    $changed++;
+}
+
+if ($changed) {
+    push @opsview_conf, "1;\n";
+    open CONF, ">", $conf_file || die "Cannot open $conf_file for writing: $!";
+    print CONF @opsview_conf;
+    close CONF;
+}

Modified: trunk/opsview-core/lib/Opsview/Config.pm
===================================================================
--- trunk/opsview-core/lib/Opsview/Config.pm	2011-01-13 09:35:38 UTC (rev 5677)
+++ trunk/opsview-core/lib/Opsview/Config.pm	2011-01-13 12:06:58 UTC (rev 5678)
@@ -134,5 +134,7 @@
 sub nmis_retention_days   { return $Settings::nmis_retention_days }
 
 sub nsca_server_address { return $Settings::nsca_server_address }
+sub nrd_shared_password { return $Settings::nrd_shared_password }
+sub slave_send_method   { return $Settings::slave_send_method }
 
 1;

Modified: trunk/opsview-core/t/10master_config.t
===================================================================
--- trunk/opsview-core/t/10master_config.t	2011-01-13 09:35:38 UTC (rev 5677)
+++ trunk/opsview-core/t/10master_config.t	2011-01-13 12:06:58 UTC (rev 5678)
@@ -63,6 +63,8 @@
     daily_backup                      => 1,
     snmptrapexception_retention_days  => 60,
     nsca_server_address => "127.0.0.1",
+    nrd_shared_password => "initial",
+    slave_send_method => "nrd",
 );
 
 my %strings_data = (

_______________________________________________
Opsview-checkins mailing list
Opsview-checkins@lists.opsview.org
http://lists.opsview.org/lists/listinfo/opsview-checkins

Reply via email to