1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#!/usr/local/bin/perl
=pod
Hi !
I'm trying to use the Nagios::Config module, but I am obviously doing something wrong as I cannot get any result with the find_object( ) method - despite the fact that the host exist in my config files.
=========================
=cut
use lib qw( ../lib ./lib );
use Nagios::Object;
use Nagios::Object::Config;
use Nagios::Config;
use Text::CSV;
use strict;
my $conffile = "jfrancois.cfg";
print "Test 1:\n";
my $nagios = Nagios::Config->new( Filename => $conffile, Version => 1 );
$nagios->parse($conffile);
use Data::Dumper;
# Parse existing Nagios configuration files
#my $nagios= Nagios::Config->new( Version => 1.2 );
$nagios->resolve_objects();
$nagios->register_objects();
# Get an existing host
my $test_host = $nagios->find_object( "debian-master", 'Nagios::Host' );
if ($test_host) {
my $name = $test_host->host_name;
my $address = $test_host->address;
print "\tmy name is $name\n";
print "\tmy address is $address\n";
print "\tDumping ... (\$test_host->dump())\n\n";
print $test_host->dump();
}
=pod
=========================
The print above yeld the following result :
my name is
my address is
define host {
}
The host "debian-master" definition is as follow :
Define host{
use generic-host-ext
host_name debian-master
alias Debian Master
parents PC0534
address <an IP address>
notification_period workhours
}
Where generic-host-ext is a template extended from generic-host.
The Nagios setup is working correctly
The parsing of the nagios.cfg file seems ok, as it takes some time to process.
I also tried using Nagios::Object::Config directly, with equal result.
I tried to register and resolve $test_host, but it doesn't seems to change anything.
If you would like to know the bigger picture, I'm building a script which use Nagios::Config and Text::CVS to parse existing Nagios configuration files and a CSV file.
The CSV file contains basic information about the host we want to monitor (IP address, SNMP community, service we want to be monitored etc.).
The script would make / update the existing nagios configuration file automatically, provided a correct CSV file.
It would dramaticaly help the integration process in a production environment - indeed it's the latest step before I put Nagios servers in.
I googled a lot to find working examples of Nagios::Config, but not find anything apart cpan doc. If you have some URL / mailing list / forum which can help me with this problem, I would be gratefull : )
Thanks for your help
Jean Franois
=cut
=pod
MASURE Jean-Francois
to me
More options 7/13/05
Thanks for your time.
I can proceed without find_object() with this small workaround :
========
=cut
print "\n\nTest2:\n";
my $host = undef;
my @hostlist = $nagios->list_hosts();
foreach my $h (@hostlist) {
if ( $h->name() eq "debian-master" ) {
$host = $h;
last; # found it, abort foreach {}
}
}
if ( $host = undef ) {
print "cannot found debian-master\n";
}
else {
print "found debian-master: $host\n";
}
=pod
========
It might be less effective than a find_object ( O(n) ) but it gets the job done ;)
=cut
|