File Coverage

blib/lib/Rex/Group/Lookup/Nagios.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Rex::Group::Lookup::Nagios;
2              
3 1     1   435 use strict;
  1         1  
  1         27  
4 1     1   4 use warnings;
  1         1  
  1         34  
5              
6             our $VERSION = '0.0.2';
7              
8 1     1   168 use Rex -base;
  0            
  0            
9             use Rex::Group::Entry::Server;
10             use Rex::Logger;
11             use Carp;
12             use Nagios::Config;
13             use File::Spec;
14              
15             require Exporter;
16             use base qw(Exporter);
17             use vars qw(@EXPORT);
18              
19             @EXPORT = qw(groups_nagios);
20              
21             sub groups_nagios {
22             my %option = @_;
23              
24             my $path = $option{path} || '/etc/nagios3';
25             my $cfg = $option{cfg} || 'nagios.cfg';
26              
27             my $nagios_cfg = Nagios::Config->new( filename => File::Spec->catfile( $path, $cfg ) );
28             my @hg = $nagios_cfg->list_hostgroups();
29              
30             my %group;
31             my %all_hosts;
32              
33             foreach my $g (@hg) {
34             foreach my $host ( @{ $g->members } ) {
35             my $name = $host->name;
36             if ($option{filter} ) {
37             $name = &{$option{filter}}($name, $host->address);
38             next unless $name;
39             }
40             my $add = {};
41             Rex::Logger::debug('Add host '. $name);
42             my $rex_host = Rex::Group::Entry::Server->new(
43             name => $name,
44             %{$add}
45             );
46             push @{ $group{ $g->name } }, $rex_host;
47             $all_hosts{ $host->name } = $rex_host;
48             }
49             }
50              
51             for my $g ( keys %group ) {
52             group( "$g" => @{ $group{$g} } );
53             }
54              
55             if ( exists $option{create_all_group} && $option{create_all_group} ) {
56             group( "all", values %all_hosts );
57             }
58             }
59              
60             1;
61              
62             =pod
63              
64             =head1 NAME
65              
66             Rex::Group::Lookup::Nagios - read hostnames and groups from a Nagios config
67              
68             =head1 DESCRIPTION
69              
70             With this module you can define hostgroups out of an Nagios configuration.
71             The module requires L to work.
72              
73             =head1 SYNOPSIS
74              
75             use Rex::Group::Lookup::Nagios;
76             groups_nagios (path => '/etc/nagios3')
77              
78             =head1 EXPORTED FUNCTIONS
79              
80             =over 4
81              
82             =item groups_nagios (%options)
83              
84             Reads the given Nagios configfiles and adds hostgroups and hosts defined there to Rex.
85              
86             Valid options are:
87              
88             =over 4
89              
90             =item path
91              
92             Path to nagios config, default = '/etc/nagios3'
93              
94             =item cfg
95              
96             Name of the base config file, default 'nagios.cfg'
97              
98             =item create_all_group
99              
100             Create a group "all_hosts".
101              
102             =item filter => sub {}
103              
104             You can modify the host names added to Rex by defining a filter sub. The sub will be called with two parameters:
105             ( host_name, address ). The returned value is used as host name added to Rex. If you return undef, this host will be skipped.
106              
107             Example:
108              
109             groups_nagios( filter => sub { my ($host_name, $address) =@_; return $host_name . '.my.domain';} );
110              
111             =back
112              
113             =back
114              
115             =CAVEATS
116              
117             This module hasn't been tested on Windows platforms.
118              
119             =AUTHOR
120              
121             Rolf Schaufelberger, Erolfschau@cpan.orgE
122              
123             =head1 COPYRIGHT AND LICENSE
124              
125             Copyright 2015 Rolf Schaufelberger
126              
127             This program is free software; you can redistribute it and/or modify it
128             under the same terms as Perl itself.
129              
130             =cut
131