The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Nagios::Config - Parse Nagios configuration files

SYNOPSIS

  use Nagios::Config ;
  my $nc = new Nagios::Config("/usr/local/nagios/nagios.cfg") ;
  foreach ($nc->get_objects('host')){
    print $_->get('host_name') . "\n" ;
  }

DESCRIPTION

Nagios::Config allows you to the main Nagios configuration file in order to extract all information about template-based objects. All template values are merged recursively, meaning that each object has all its attributes present, even those that were defined in templates.

Note: Only the template-based objects are parsed, not the attributes directly present in nagios.cfg, resource.cfg or cgi.cfg.

Note: Nagios::Config assumes that your Nagios configuration is valid and may react unexpectedly if it is not. Please check your configuration using the "nagios -v" command prior to using Nagios::Config on it.

CONSTRUCTOR

new ([FILE])

Creates a Nagios::Config, which will parse the contents of FILE assuming it is a Nagios Main Configuration File. FILE can be a file name or a reference to an already opened filehandle. If FILE is false, it will default to /usr/local/nagios/nagios.cfg

METHODS

get_types ()

Returns a list of all the object types that where encountered during the parse.

Ex: my @types = $nc->get_types() ;

get_files ()

Returns a list of all the template object files that where parsed.

Ex: my @files = $nc->get_files() ;

get_objects ([TYPE])

Returns a list of all the objects of type TYPE that where found during the parse. Each of these objects will be blessed in the Nagios::Config::Object::TYPE package, which inherits from Nagios::Config::Object. See the SUBCLASSING section below for more information on how to change this behaviour.

Ex: my @hosts = $nc->get_objects('host') ;

OBJECTS

Each object returned by Nagios::Config provides the following methods to access its data:

get_attr ([NAME], [SPLIT])

If SPLIT is false, returns the value for the attribute named NAME from the current object.

If SPLIT is true, returns the value split using /\s*,\s*/. This is useful for attributes that can have multiple values.

Ex: # define host { # host_name h1, h2, h3 # } my $val = $host->get_attr('host_name') ; # $val = 'h1, h2, h3' my @vals = $host->get_attr('host_name', 1) ; # @vals = ('h1', 'h2', 'h3')

get ([NAME], [SPLIT])

A shortcut for get_attr().

SUBCLASSING

If you which to change or extend the functionality in the classes provided by Nagios::Config, you must do 2 things:

  1) Subclass C<Nagios::Config>
  2) Subclass C<Nagios::Config::Object>

This will cause the objects to be blessed in the MyNC::Object::TYPE packages. To implement these packages in advance, make them inherit from MyNC::Object.

Here is an example:

  use Nagios::Config ;

  package MyNC ;
  @MyNC::ISA = qw(Nagios::Config) ;

  package MyNC::Object ;
  @MyNC::Object::ISA = qw(Nagios::Config::Object) ;

  package MyNC::Object::host ;
  @MyNC::Object::host::ISA = qw(MyNC::Object) ;

  sub get {
    my $this = shift ;
    my $name = shift ;

    return "host is " . $this->SUPER::get($name) ;
  }


  package main ;

  my $mnc = MyNC->new("/usr/local/nagios/nagios.cfg") ;
  print $mnc->get_objects('host')->[0]->get('host_name') ; # host is ...

AUTHOR

Patrick LeBoutillier, patl@cpan.org

SEE ALSO

perl(1).