Assign IP of the host in nagios config by a database ?!


#1

Hello

I have a database with the IP addresses of many of hosts which needs to be monitored. I can add them manually in the localhost.cfg from nagios but as they are a lot and it would be a hell of work.

Also this host may change their IP because they are connected to a home gateway and behind NAT. I was wondering also if there’s posibility in some way in the host detail that the host is really online (I am checking services with NRPE but online/offline status is made just by IP). Maybe there’s the possibility to change the online/offline status of the host depending of the service monitored?(i.e. when a service is criticall put the host status offline even if the ip is responding)

Thank you in advance!
Saludos
:slight_smile:


#2

Hi!

I don’t know of something like that… BUT, here is the way I do it:
we also have a huge database containing quite a lot of data about what we have to supervise.
So we just wrote a few scripts that fetch the data in the database and generate a .cfg file, then place it in the right folder and do a “nagios-init reload” every day.

Hope this helps :slight_smile:


#3

Thanks for the idea! Looks clever!
Are those scripts public? Could I have them or view the structure of the script so I can build mine?

Thank you in advance!
Cheers!


#4

well, they are not public, but I don’t mind if it can help :slight_smile:
but, they are very specific, so you may have a hard time to re write them so they can work :slight_smile:

anyway, first script is used to just list the data:

[code]#!/usr/bin/perl

use strict;

my $user;
my $password;

$user = “my_login”;
$password = “my_pass”;
my $base = “my_base”;

my $pwd = “$ENV{NAGIOS_HOME}/provisioning/appli”;
chomp($pwd);

open(OUT,">$pwd/data/liste.lst");
my $command = “sqlplus -s $user/$password@$base << EOF 2>&1
set pagesize 0
set space 0
SET LINESIZE 150
SET SERVEROUTPUT ON
SET HEADING OFF
SET VERIFY OFF
SET FEEDBACK OFF
select BASE||’;’||FOLDER||’;’||WORKFLOW||’;’||SEUIL||’;’||TIME||’;’||DOMAIN||’;’||APP_APPLICATION
from XORT_PWMART
where SUP='Y’
ORDER BY FOLDER ASC
;
EOF`”;

my @output = $command;
print OUT @output;

close(OUT);[/code]

as you can see, we are using sqlplus (too lazy to install DB modules for perl ;)) and this script produces a basic csv file.
From this file, we create the cfg file:

[code]#!/usr/bin/perl

my $pwd = “$ENV{NAGIOS_HOME}/provisioning/appli”;
chomp($pwd);

open(IN,"$pwd/data/liste.lst");
my @lines = ;
close(IN);

my $SEP="####################################################################################";

Initialisations -----------------------------------------------------------------------

my $HOSTS = “$SEP\n# HOSTS DEFINITION\n$SEP\n”;
my $HOSTGROUPS = “$SEP\n# HOST GROUPS DEFINITION\n$SEP\n”;
my $SERVICES = “$SEP\n# SERVICES DEFINITION\n$SEP”;
my $HOSTS_LIST = “”;

foreach my $line (@lines) {
my ($base,$folder,$workflow,$seuil,$duree,$domaine,$app) = split(/;/,$line);
chomp($app);

    $HOSTS_LIST .= $workflow.",";
    $HOSTS .= "define host{\n\tuse\t\toracle-generic-host\n\thost_name\t$workflow\n\talias\t\tSupSI_Pwmart_".$folder."\n\tcontact_groups\tnetcool-group\n\taddress\t\t$base\n\t}\n";
    my $frequence=10;
    $seuil++; # on ajoute 1 au seuil, histoire de laisser un peu de marge!



    # Statut:
    $SERVICES .= "\ndefine service{\n\tuse\t\t\tPwmart-service\n\thost_name\t\t$workflow\n\tservice_description\tStatut\n\tcheck_command\t\tmon_check_pwmart!DUMMY\n\t}";
    # Time:
    $SERVICES .= "\ndefine service{\n\tuse\t\t\tPwmart-service\n\thost_name\t\t$workflow\n\tservice_description\tTime\n\tcheck_command\t\tmon_check_pwmart!DUMMY\n\t}";
    # Duree:
    if ( $domaine =~ /GP/ ) {
            $SERVICES .= "\ndefine service{\n\tuse\t\t\tPwmart-service\n\thost_name\t\t$workflow\n\tservice_description\tDuree\n\t\n\tcheck_command\t\tmon_check_pwmart!DUMMY\n\t}";
    }

}

$HOSTGROUPS .= “define hostgroup{\n\thostgroup_name\tSupSI_Pwmart\n\talias\t\tSupSI_Pwmart\n\tmembers\t\t$HOSTS_LIST\n\t}”;

my $DEF = "################################################################################

COMMANDS DEFINITION

################################################################################
define command{
command_name mon_check_pwmart
command_line $USER1$/mon_check_wflow_Pwmart.pl “$ARG1$” “$ARG2$” “$ARG3$” “$ARG4$” “$ARG5$”
} ; Domaine workflow base seuil seuil duree

################################################################################

SERVICE TEMPLATE DEFINITION

################################################################################
define service{
name Pwmart-service
use service
max_check_attempts 2
normal_check_interval 30
retry_check_interval 2
contact_groups netcool-exploit-group
notification_interval 30
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMLATE
}\n";

Constitution du fichier de conf nagios ------------------------------------------------

open(OUT,">$pwd/data/Pwmart.cfg");
print OUT $DEF."\n";
print OUT $HOSTGROUPS."\n";
print OUT $HOSTS."\n";
print OUT $SERVICES."\n";
close(OUT);
[/code]

it’s a bit messy, but it works :slight_smile: (I’ve just erased the comments ('cause they were in french …))

I hope it will help you to write your own scripts quickly.


#5

Thank you! I will take a look at them and tell you wheter they work for me :slight_smile:

Saludos!