Monitoring-Generator-TestConfig

 view release on metacpan or  search on metacpan

lib/Monitoring/Generator/TestConfig.pm  view on Meta::CPAN

                                                              .$self->_create_object_conf('contact',       $objects->{'contact'})           },
        { file => $obj_prefix.'/commands.cfg',         data => $self->_create_object_conf('command',       $objects->{'command'})           },
        { file => $plg_prefix.'/test_servicecheck.pl', data => Monitoring::Generator::TestConfig::ServiceCheckData->get_test_servicecheck() },
        { file => $plg_prefix.'/test_hostcheck.pl',    data => Monitoring::Generator::TestConfig::HostCheckData->get_test_hostcheck()       },
        { file => '/recreate.pl',                      data => $self->_get_recreate_pl($self->_get_used_libs($self->{'output_dir'}.'/recreate.pl')) },
    ];

    if($self->{'layout'} ne 'omd') {
        push(@{$exportedFiles}, { file => $obj_prefix.'/timeperiods.cfg',   data => $self->_create_object_conf('timeperiod',    $objects->{'timeperiod'})   });
        push(@{$exportedFiles}, { file => '/etc/resource.cfg',              data => '$USER1$='.$self->{'output_dir'}."/plugins\n" });
    }

    if ($self->{'layout'} eq 'nagios' or $self->{'layout'} eq 'icinga') {
        push(@{$exportedFiles}, { file => '/plugins/p1.pl',  data => Monitoring::Generator::TestConfig::P1Data->get_p1_script() });
        push(@{$exportedFiles}, { file => '/init.d/'.$init,  data => Monitoring::Generator::TestConfig::InitScriptData->get_init_script(
                            $self->{'output_dir'},
                            $self->{'binary'},
                            $self->{'user'},
                            $self->{'group'},
                            $self->{'layout'}
                  )});
    }
    if ($self->{'layout'} eq 'shinken') {
        push(@{$exportedFiles}, { file => '/etc/shinken-specific.cfg',  data => Monitoring::Generator::TestConfig::Modules::Shinken::_get_shinken_specific_cfg($self) });
        push(@{$exportedFiles}, { file => '/etc/schedulerd.cfg',        data => Monitoring::Generator::TestConfig::Modules::Shinken::_get_shinken_schedulerd_cfg($self) });
        push(@{$exportedFiles}, { file => '/etc/pollerd.cfg',           data => Monitoring::Generator::TestConfig::Modules::Shinken::_get_shinken_pollerd_cfg($self) });
        push(@{$exportedFiles}, { file => '/etc/brokerd.cfg',           data => Monitoring::Generator::TestConfig::Modules::Shinken::_get_shinken_brokerd_cfg($self) });
        push(@{$exportedFiles}, { file => '/etc/reactionnerd.cfg',      data => Monitoring::Generator::TestConfig::Modules::Shinken::_get_shinken_reactionnerd_cfg($self) });
        push(@{$exportedFiles}, { file => '/init.d/'.$init,             data => Monitoring::Generator::TestConfig::ShinkenInitScriptData->get_init_script(
                            $self->{'output_dir'},
                            $self->{'binary'},
        ) });
    }

    # export service dependencies
    my $servicedependency = "";
    unless ($self->{'skip_dependencies'} ) {
        $objects = $self->_set_servicedependency_cfg($objects);
        $servicedependency = $self->_create_object_conf('servicedependency', $objects->{'servicedependency'});
    }
    push(@{$exportedFiles}, { file => $obj_prefix.'/dependencies.cfg', data => $servicedependency });

    if( !-d $self->{'output_dir'}."/".$obj_prefix ) {
        mkdir($self->{'output_dir'}."/".$obj_prefix) or croak('failed to create output_dir '.$self->{'output_dir'}."/".$obj_prefix.':'.$!);
    }

    for my $exportFile (@{$exportedFiles}) {
        open(my $fh, '>', $self->{'output_dir'}.$exportFile->{'file'}) or die('cannot write '.$self->{'output_dir'}.$exportFile->{'file'}.': '.$!);
        print $fh $exportFile->{'data'};
        close $fh;
    }

    chmod 0755, $self->{'output_dir'}.$plg_prefix.'/test_servicecheck.pl' or die("cannot change modes: $!");
    chmod 0755, $self->{'output_dir'}.$plg_prefix.'/test_hostcheck.pl'    or die("cannot change modes: $!");
    chmod 0755, $self->{'output_dir'}.'/plugins/p1.pl';
    chmod 0755, $self->{'output_dir'}.'/init.d/'.$init;
    chmod 0755, $self->{'output_dir'}.'/recreate.pl';

    # check user/group
    if( $^O ne "MSWin32" and $< == 0 ) {
        `chown -R $self->{'user'}:$self->{'group'} $self->{'output_dir'}`;
    }

    if($self->{'layout'} eq 'omd') {
        print "exported omd test config to: ".$self->{'output_dir'}.$obj_prefix."\n";
        print "check your configuration with: ~/etc/init.d/nagios checkconfig\n";
    } else {
        print "exported ".$self->{'layout'}." test config to: $self->{'output_dir'}\n";
        print "check your configuration with: $self->{'output_dir'}/init.d/".$init." checkconfig\n";
    }
    print "configuration can be adjusted and recreated with $self->{'output_dir'}/recreate.pl\n";

    return 1;
}


########################################
sub _set_hosts_cfg {
    my($self, $objects, $contactnames, $contactgroupnames) = @_;

    $objects->{'host'} = [] unless defined $objects->{'host'};

    my $hostconfig = {
        'name'                           => 'generic-mgt-test-host',
        'notifications_enabled'          => 1,
        'event_handler_enabled'          => 1,
        'flap_detection_enabled'         => 1,
        'failure_prediction_enabled'     => 1,
        'process_perf_data'              => 1,
        'retain_status_information'      => 1,
        'retain_nonstatus_information'   => 1,
        'max_check_attempts'             => 5,
        'check_interval'                 => 1,
        'retry_interval'                 => 1,
        'check_period'                   => '24x7',
        'notification_interval'          => 0,
        'notification_period'            => '24x7',
        'notification_options'           => 'd,u,r',
        'register'                       => 0,
    };

    my $merged = $self->_merge_config_hashes($hostconfig, $self->{'host_settings'});
    push @{$objects->{'host'}}, $merged;
    my @router;

    # router
    if($self->{'routercount'} > 0) {
        my @routertypes = @{$self->_fisher_yates_shuffle($self->_get_types($self->{'routercount'}, $self->{'router_types'}))};

        my $nr_length = $self->{'fixed_length'} || length($self->{'routercount'});
        for(my $x = 0; $x < $self->{'routercount'}; $x++) {
            my $hostgroup = "router";
            my $nr        = sprintf("%0".$nr_length."d", $x);
            my $type      = shift @routertypes;
            push @router, $self->{'prefix'}."router_$nr";

            my $host = {
                'host_name'     => $self->{'prefix'}."router_".$nr,
                'alias'         => $self->{'prefix'}.$type."_".$nr,
                'use'           => 'generic-mgt-test-host',
                'address'       => '127.0.'.$x.'.1',



( run in 1.034 second using v1.01-cache-2.11-cpan-71847e10f99 )