Parent/child host relationship

Patrick LeBoutillier patrick_leboutillier at hotmail.com
Fri May 23 14:17:12 CEST 2003


Thom,

I too had similar issue. I wanted to know all the services for a host, but
to know that you need
to go through all the services to see where your host is defined.

I've written a (very simplified) Nagios Config parser (in Perl) that I use
to populate synchronize a
database when nagios starts/restarts/reload. It doesn't give you all the
stuff that could be in templates,
just the basic info that you need to define for each host/service.

If you are comfortable with Perl, you should be able to use/change it to do
what you want to do.

You use it like this:

my $cfg = new NagiosConfig("/usr/local/nagios/etc/nagios.cfg") ;

foreach my $h (@{$cfg->{objects}->{host}}){
    my @parents = split(/\s*,\s*/,  $h->{parents}} ;
    ...
}


-------------------------------------
package NagiosConfig ;

use strict ;

use Carp ;
use Data::Dumper ;


my $TYPES = {
 host => 1,
 service => 1,
} ;


sub new {
 my $class = shift ;
 my $nagios_cfg = shift ;

 if (! open(CFG, "<$nagios_cfg")){
  croak("Can't open $nagios_cfg for reading: $!") ;
 }

 my @files = () ;
 while(<CFG>){
  if ($_ =~ /^\s*cfg_file\s*=\s*(.*?)\s*$/){
   push @files, $1 ;
  }
  elsif ($_ =~ /^\s*cfg_dir\s*=\s*(.*?)\s*$/){
   push @files, <$1/*.cfg> ;
  }
 }
 close(CFG) ;

 my $this = {
  nagios_cfg => $nagios_cfg,
  files => \@files,
  objects => {},
 } ;
 foreach my $t (keys %{$TYPES}){
  $this->{objects}->{$t} = [] ;
 }
 bless($this, $class) ;

 $this->process_files() ;

 return $this ;
}


sub process_files {
 my $this = shift ;

 foreach my $f (@{$this->{files}}){
  if (! open(CFG, "<$f")){
   croak("Can't open $f for reading: $!") ;
  }

  my $in = 0 ;
  my $type = '' ;
  my $obj = undef ;
  while (<CFG>){
   my $line = $_ ;
   $line =~ s/^\s+// ;
   $line =~ s/\s+$// ;

   if ($line eq ''){
    next ;
   }
   elsif ($line =~ /^#/){
    next ;
   }
   elsif ($line =~ /^define\s+(\w+)\s*{$/){
    if (! $TYPES->{$1}){
     next ;
    }
    $type = $1 ;
    $obj = {} ;
    $in = 1 ;
   }
   elsif (($in)&&($line =~ /(\w+)\s+(.*?)$/)){
    # We have a property definition
    $obj->{$1} = $2 ;
   }
   elsif (($in)&&($line eq '}')){
    $in = 0 ;
    if ($obj->{register} ne '0'){
     delete $obj->{register} ;
     delete $obj->{use} ;
     push @{$this->{objects}->{$type}}, $obj ;
    }
   }
  }
  close(CFG) ;
 }
}

1 ;


-------------------------

Good luck


---------------------
Patrick LeBoutillier
Laval, Quebec, Canada
----- Original Message -----
From: "Thom Smith" <thoms at clinicomp.com>
To: <nagios-users at lists.sourceforge.net>
Sent: Thursday, May 22, 2003 11:34 AM
Subject: [Nagios-users] Parent/child host relationship


> Greetings,
>
> For various reasons, I need to know the children of the hosts.  Because
> the relationship is defined by the children (i.e. I know my parents but
> not my children), this seems difficult.
>
> Does this relationship exist anywhere that is easily accessible or will
> I have to build this.  If I do need to build this, any ideas on a good
> way to do it?  I was thinking about writing a script that runs at
> startup that builds a DB that contains the host, children and
> parents(s).
>
> TIA,
>
> Thom
> --
>
> Thom Smith
> <thoms at clinicomp.com>
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: ObjectStore.
> If flattening out C++ or Java code to make your application fit in a
> relational database is painful, don't do it! Check out ObjectStore.
> Now part of Progress Software. http://www.objectstore.net/sourceforge
> _______________________________________________
> Nagios-users mailing list
> Nagios-users at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/nagios-users
> ::: Please include Nagios version, plugin version (-v) and OS when
reporting any issue.
> ::: Messages without supporting info will risk being sent to /dev/null
>


-------------------------------------------------------
This SF.net email is sponsored by: ObjectStore.
If flattening out C++ or Java code to make your application fit in a
relational database is painful, don't do it! Check out ObjectStore.
Now part of Progress Software. http://www.objectstore.net/sourceforge
_______________________________________________
Nagios-users mailing list
Nagios-users at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null





More information about the Users mailing list