blib/lib/Rex/Group/Lookup/Nagios.pm

filter: Statementstmt 56.6% 34 / 60 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 90.9% 10 / 11 Podpod 100% 1 / 1 totaltotal 50.5% 45 / 89 CC 10 SCAR 40.2
CC
10
Cov
47%
groups_nagios
110.0
BEGIN
1.0
BEGIN
1.0
CRAP 55.5
↑ Summary
1 package Rex::Group::Lookup::Nagios;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.0.2';
7  
8 1 ▶use Rex -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Rex::Group::Entry::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Nagios::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 require Exporter;
16 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 @EXPORT = qw(groups_nagios);
20  
21 0 ▶sub groups_nagios {
Subroutine groups_nagios: not called CC=10 SCAR=47.0
Pod documented
22 0 my %option = @_;
23  
24 0 ▶ my $path = $option{path} || '/etc/nagios3';
Condition0%0/2
$option{'path'} || '/etc/nagios3'
l!l
00
MC/DC0%0/1
$option{'path'} || '/etc/nagios3'
$option{'path'}
Truth table0%0/2
A
A= $option{'path'}
Aresult
11
00
25 0 ▶ my $cfg = $option{cfg} || 'nagios.cfg';
Condition0%0/2
$option{'cfg'} || 'nagios.cfg'
l!l
00
MC/DC0%0/1
$option{'cfg'} || 'nagios.cfg'
$option{'cfg'}
Truth table0%0/2
A
A= $option{'cfg'}
Aresult
11
00
26  
27 0 my $nagios_cfg = Nagios::Config->new( filename => File::Spec->catfile( $path, $cfg ) );
28 0 my @hg = $nagios_cfg->list_hostgroups();
29  
30 0 my %group;
31 my %all_hosts;
32  
33 0 foreach my $g (@hg) {
34 0 foreach my $host ( @{ $g->members } ) {
35 0 my $name = $host->name;
36 0 ▶ if ($option{filter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"filter"}) 0 0 0
37 0 $name = &{$option{filter}}($name, $host->address);
38 0 ▶ next unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
39 }
40 0 my $add = {};
41 0 Rex::Logger::debug('Add host '. $name);
42 my $rex_host = Rex::Group::Entry::Server->new(
43 name => $name,
44 0 %{$add}
45 );
46 0 push @{ $group{ $g->name } }, $rex_host;
47 0 $all_hosts{ $host->name } = $rex_host;
48 }
49 }
50  
51 0 for my $g ( keys %group ) {
52 0 group( "$g" => @{ $group{$g} } );
53 }
54  
55 0 ▶ if ( exists $option{create_all_group} && $option{create_all_group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option{"create_all_group"} and $option{"create_all_group"}) 0 0 0
Condition0%0/3
exists $option{"create_all_group"} and $option{"create_all_group"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $option{"create_all_group"} and $option{"create_all_group"}
exists $option{"create_all_group"} $option{"create_all_group"}
Truth table0%0/3
A and B
A= exists $option{"create_all_group"}B= $option{"create_all_group"}
ABresult
0X0
100
111
56 0 group( "all", values %all_hosts );
57 }
58 }
59  
60 1 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<Nagios::Config> 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, E<lt>rolfschau@cpan.orgE<gt>
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  
↑ Summary