centreon / nagiosToCentreon

Import your nagios config files into Centreon DB... And it rocks !
Apache License 2.0
9 stars 13 forks source link

ARRAY probleme #41

Closed DeadPixhell closed 7 years ago

DeadPixhell commented 7 years ago

Hi everyone,

I encounter an issue with nagiosToCentreon.pl

When i use the Script i have this 2 error

For the first error i have edited the script Before

 # Add contactgroups to host
                        if ( defined ( $host->contact_groups )  && $host->contact_groups ne '' ) {
                                my $contactgroups_list = "";
                                if ( scalar @{$host->contact_groups} > 1 ) {
                                        foreach my $item ( @{$host->contact_groups} ) {
                                                $contactgroups_list .= sprintf ( "%s", $item) . "|";
                                        }
                                        $contactgroups_list =~ s/\|$//;
                                        printf ( "%s;setcontactgroup;%s;%s\n", $type, $host_name, $contactgroups_list );
                                } elsif ( defined (@{$host->contact_groups}) )  {
                                        foreach my $item ( @{$host->contact_groups} ) {
                                            printf ( "%s;addcontactgroup;%s;%s\n", $type, $host_name, $item->contactgroup_name );
                                        }
                                } else {
                                        printf ( "%s;addcontactgroup;%s;%s\n", $type, $host_name, $host->contact_groups );
                                }
                        }

After

# Add contactgroups to host
                        if ( defined ( $host->contact_groups )  && $host->contact_groups ne '' ) {
                                my $contactgroups_list = "";
                                if ( scalar @{$host->contact_groups} > 1 ) {
                                        foreach my $item ( @{$host->contact_groups} ) {
                                                $contactgroups_list .= sprintf ( "%s", $item) . "|";
                                        }
                                        $contactgroups_list =~ s/\|$//;
                                        printf ( "%s;setcontactgroup;%s;%s\n", $type, $host_name, $contactgroups_list );
                                } elsif (@{$host->contact_groups} )  {
                                        foreach my $item ( @{$host->contact_groups} ) {
                                            printf ( "%s;addcontactgroup;%s;%s\n", $type, $host_name, $item->contactgroup_name );
                                        }
                                } else {
                                        printf ( "%s;addcontactgroup;%s;%s\n", $type, $host_name, $host->contact_groups );
                                }
                        }

The first error disappear. but i dont know what to do with the second. Nagios version : 3.5.1 Perl version 5.16.3

Thanks for your help

Sims24 commented 7 years ago

Hi @DeadPixhell ,

Thanks for using the script, today we are working along with @cgagnaire on this point to make this script more efficient through code and algorithm optimizations.

You can for now use the script version available on the v2 branch, you should not meet this kind of problem.

Soon we'll commit some brand new code on v3-sims branch that is the target version about code logic and performance.

Let us know if you still run in any issues. Thanks

DeadPixhell commented 7 years ago

Thanks for reply @Sims24 ,

I have get the v2 branch, and now i have another kind of problem.

What i have done: getting the v2 branch git clone -b v2 https://github.com/centreon/nagiosToCentreon.git Executing the perl script perl nagios_reader_to_centreon_clapi.pl -P Central-FO --config /etc/nagios/nagios.cfg > /tmp/centreon_clapi_import_commands.txt The output:

Use of uninitialized value in pattern match (m//) at nagios_reader_to_centreon_clapi.pl line 183.
Use of uninitialized value $contact_fullname in substitution (s///) at nagios_reader_to_centreon_clapi.pl line 186.
Use of uninitialized value $contact_fullname in concatenation (.) or string at nagios_reader_to_centreon_clapi.pl line 187.
Can't use string ("notify-host-by-email") as an ARRAY ref while "strict refs" in use at nagios_reader_to_centreon_clapi.pl line 193.

Do you want the full output with use diagnoses ?

My environment: Nagios version: 3.5.1 Perl version: 5.16.3 OS: Red Hat Enterprise Linux Server release 7.2 Kernel: Linux version 3.10.0-327.el7.x86_64

Thanks

Sims24 commented 7 years ago

Waiting for version 3 please use this workaround and try to launch the script again.

Make the foreach loop around line 183 look like this (add line in the middle of code block below) :

    foreach my $contact (@contacts_array) {
        next if (!defined($contact->contact_name));
        next if ($contact->contact_name =~ m/centreon\-bam|\_Module\_BAM/);

We try to be fully compliant with version 3.2.3, are you aware of any new thing on 3.5.1 ? On our side we are using Nagios Perl libraries, we will try to find out the root cause of the issue above if it still exists with our v3 but I guess we will not make any PR on Nagios related project.

Sims24 commented 7 years ago

sorry there was a typo i've edited my post @DeadPixhell

DeadPixhell commented 7 years ago

@Sims24 Its mostly work with the workaround you give me, but one bug die, one bug raise, a new bug appear on line 533. I have run the script with use diagnistics;

Can't use an undefined value as an ARRAY reference at
        nagios_reader_to_centreon_clapi.pl line 533 (#1)
    (F) A value used as either a hard reference or a symbolic reference must
    be a defined value.  This helps to delurk some insidious errors.

Uncaught exception from user code:
        Can't use an undefined value as an ARRAY reference at nagios_reader_to_centreon_clapi.pl line 533.
        main::export_services('Nagios::Service=HASH(0x26440b8)', 'Nagios::Service=HASH(0x2644730)', 'Nagios::Service=HASH(0x2644e38)', 'Nagios::Service=HASH(0x268a9b0)', 'Nagios::Service=HASH(0x268aef0)', 'Nagios::Service=HASH(0x268b3d0)', 'Nagios::Service=HASH(0x268d738)', 'Nagios::Service=HASH(0x268dca8)', 'Nagios::Service=HASH(0x2691318)', ...) called at nagios_reader_to_centreon_clapi.pl line 673

I have not seen Major change since Nagios 3.2.3 if their changelog is good they have only made many fixe and some enhancements. https://www.nagios.org/projects/nagios-core/history/3x/

If you want i can test your v3.

cgagnaire commented 7 years ago

Hi @DeadPixhell,

Can you test the v3-sims branch please ? The '--config' option now requires the directory path where your nagios configuration files are, not the main configuration file path.

We're waiting for your feedback. Thanks.

cgagnaire commented 7 years ago

And to be complete :

DeadPixhell commented 7 years ago

Hi @cgagnaire

I have test the v3-sims branch I have get the directory path in /etc/nagios/nagios.cfg (ex. cfg_file=/etc/nagios/objects/commands.cfg) My resulting command : perl nagios_reader_to_centreon_clapi.pl -P Central-FO --version 3 --config /etc/nagios/objects > /tmp/centreon_clapi_import_commands.txt

And i have the following error :

Error: Unexpected start of object definition in file '/etc/nagios/objects/.commands.cfg.swp' on line 8.  Make sure you close preceding objects before starting a new one.
 at nagios_reader_to_centreon_clapi.pl line 667.

configuration file in /etc/nagios/objects

check_age_flux.cfg
commands.cfg
contacts.old
hostsgroups.cfg
printer.cfg
switch.cfg
templates.cfg
timeperiods.cfg
check_oldflux.cfg
contacts.cfg
hosts.cfg
localhost.cfg 
services.cfg 
windows.cfg

I have not used option '--version 3' i have seen it by default in the script

cgagnaire commented 7 years ago

Hi @DeadPixhell,

Can you use the last version of this branch please ? Thanks

DeadPixhell commented 7 years ago

If i remeber well (i am off work), in the script header it was writen 3.0.0

Sims24 commented 7 years ago

@DeadPixhell this is because you're editing commands.cfg file during the script execution or that your tty has crashed.

@cgagnaire please keep script as flexible as possible. Please find my thoughts on the code actually.

For this block :

opendir (DIR, $OPTION{'config'}) or die $!;
while (my $file = readdir(DIR)) {
    next if (!grep { $file eq $_ } @config_files);
    $objects->parse($OPTION{'config'}."/".$file);
}
closedir DIR;

Thanks for your work and feedbacks on this we're close of some HUGE improvments and optimizations

DeadPixhell commented 7 years ago

Hi, I have run : lsof | grep nagios to see if a file in /etc/nagios/objects was open I have closed all my tty, and stop the nagios process, and i still have the error.

result of : perl nagios_reader_to_centreon_clapi.pl -h

######################################################
#    Copyright (c) 2005-2017 Centreon                #
#    Bugs to http://github.com/nagiosToCentreon      #
######################################################

Version: 3.0.0
Usage: nagios_reader_to_centreon_clapi.pl
    -C (--config)      Path to nagios configuration files (must be a directory)
    -V (--version)     Nagios version of the configuration files
    -P (--poller)      Name of the targeted poller
    -p (--prefix)      Add a prefix before commands, contacts, templates, groups, etc.
    -h (--help)        Usage help

I will look if a reboot of the server is possible and i retry.

Sims24 commented 7 years ago

@DeadPixhell would it be possible to send us a tar.gz of your /etc/nagios/ directory ? We'll keep it confidential and delete it after our tests. If so, please find my email on my github profile.

Thanks

DeadPixhell commented 7 years ago

It's sent :)

cgagnaire commented 7 years ago

Hi @DeadPixhell, can you test the newest version of the v3-sims branch ? Know that services by hostgroups won't be retrieved as is, but will be transformed in services templates with unitary service created for each hosts. And futhermore, hostgroups exclusions (i.e. hostgroup_name !Windows) won't be taken into account, at least for now.

@Sims24 I know how much you love kittens...

DeadPixhell commented 7 years ago

Hi, i will test the new version today !

DeadPixhell commented 7 years ago

It's work, we will import the result in centreon to check if everything is fine. But the script run without any error.

Kittens are so cute :)

cgagnaire commented 7 years ago

Hi @DeadPixhell, You should try the latest because your configuration showed us some other unseen problems. Thanks!

DeadPixhell commented 7 years ago

Hi @cgagnaire,

I have trie the lastest update, it work fine for the export and the import.

Thanks for you help !

cgagnaire commented 7 years ago

Glad to hear it ! Thanks for the feedback.