blib/lib/RackMan/Format/Nagios.pm

filter: Statementstmt 27.7% 20 / 72 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 60.0% 6 / 10 Podpod 25.0% 1 / 4 totaltotal 25.0% 27 / 108 CC 9 SCAR 35.0
CC
9
Cov
22%
write
72.0
config_for_server
6.0
config_for_switch
2.0
CRAP 33.2
« blib/lib/RackMan/Template.pm ↑ Summary blib/lib/RackMan/Format/Bacula.pm »
1 package RackMan::Format::Nagios;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use List::MoreUtils qw< uniq >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use RackMan::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 CONFIG_SECTION => "format:nagios",
13 DEFAULT_PATH => "/etc/nagios/hosts",
14 2 };
15  
16  
17 2 my %type2group = (
18 PDU => "pdus",
19 Server => "servers",
20 Switch => "switches",
21 );
22  
23  
24 #
25 # write()
26 # -----
27 0 ▶sub write {
Subroutine write: not called CC=8 SCAR=42.8
Pod documented
28 0 my ($class, $args) = @_;
29  
30 0 my $rackdev = $args->{rackdev};
31 0 my $rackman = $args->{rackman};
32 0 my $config = RackMan::File->new;
33 0 my $type = $rackdev->object_type;
34  
35 # fetch the common name and FQDN of the host
36 0 my $name = $rackdev->object_name;
37 0 my $fqdn = $rackdev->attributes->{FQDN};
38 0 ▶ RackMan->error("RackObject '$name' lacks a FQDN") unless $fqdn;
Branch: 1 branch
BranchTrueFalseTotal
unless $fqdn 0 0 0
39  
40 # find the first IP address of the device, excluding the iLO
41 0 my @ip_addrs = ( $rackdev->regular_ipv4addrs, $rackdev->regular_ipv6addrs );
42 0 ▶ RackMan->error("RackObject '$name' lacks an IPv4 address") unless @ip_addrs;
Branch: 1 branch
BranchTrueFalseTotal
unless @ip_addrs 0 0 0
43 0 ▶ my $address = $ip_addrs[0]{addr} || $name;
Condition0%0/3
$ip_addrs[0]{'addr'} || $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$ip_addrs[0]{'addr'} || $name
$ip_addrs[0]{'addr'} $name
Truth table0%0/3
A || B
A= $ip_addrs[0]{'addr'}B= $name
ABresult
1X1
011
000
44  
45 # determine the parents of this device
46 my $parents = join ", ", uniq map {
47 defined $_->{peer_object_name} && $_->{name} !~ /^(?:PWR\d|ilo)$/i
48 ? $_->{peer_object_name} : ()
49 0 ▶ } @{ $rackdev->ports };
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'peer_object_name'} && !($_->{'name'} =~ /^(?:PWR\d|ilo)$/i) ? : 0 0 0
Condition0%0/3
defined $_->{"peer_object_name"} and not $_->{"name"} =~ /^(?:PWR\d|ilo)$/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_->{"peer_object_name"} and not $_->{"name"} =~ /^(?:PWR\d|ilo)$/i
defined $_->{"peer_object_name"} not $_->{"name"} =~ /^(?:PWR\d|ilo)$/i
Truth table0%0/3
A and not(B)
A= defined $_->{"peer_object_name"}B= not $_->{"name"} =~ /^(?:PWR\d|ilo)$/i
ABresult
0X0
100
111
50  
51 # construct the RackTables url for this device
52 0 my $rto_url = "";
53 0 ▶ if (my $base_url = $rackman->config->val(general => "racktables_url")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $base_url = $rackman->config->val("general", "racktables_url")) 0 0 0
54 0 $base_url =~ s:/$::;
55 0 $rto_url = sprintf "$base_url/index.php?page=object&object_id=%d",
56 $rackdev->object_id;
57 }
58  
59 # store the generic config
60 $config->add_content(
61 0 "define host {\n",
62 " use tmpl-generic-host\n",
63 " host_name $name\n",
64 " alias $fqdn\n",
65 " address $address\n",
66 " parents $parents\n",
67 " hostgroups $type2group{$type}\n",
68 " check_command check-host-alive\n",
69 " notes RackTables object\n",
70 " notes_url $rto_url\n",
71 "}\n\n",
72 );
73  
74 # call the appropriate function for generating the specific config
75 # corresponding to the type of the device
76 0 my $config_maker = "config_for_\L$type";
77 0 $class->$config_maker({ config => $config, rackdev => $rackdev });
78  
79 # write the configuration on disk
80 0 $config->name("$name.conf");
81 0 $config->path($rackman->config->val(CONFIG_SECTION, "path", DEFAULT_PATH));
82 0 ▶ print " + writing ", $config->fullpath, $/ if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
83 0 my $scm = $rackman->get_scm({ path => $config->path });
84 0 $scm->update;
85 0 $config->write;
86 0 $scm->add($config->name);
87 0 $scm->commit($config->name, "generated by $class / $::PROGRAM v$::VERSION");
88 }
89  
90  
91 #
92 # config_for_server()
93 # -----------------
94 0 ▶sub config_for_server {
Subroutine config_for_server: not called CC=2 SCAR=17.9
Pod undocumented
95 0 my ($class, $args) = @_;
96  
97 0 my $rackdev = $args->{rackdev};
98 0 my $config = $args->{config};
99 0 my $name = $rackdev->object_name;
100 0 my $fqdn = $rackdev->attributes->{FQDN};
101  
102 0 $config->add_content(
103 "define service {\n",
104 " use tmpl-server-service\n",
105 " host_name $name\n",
106 " service_description check_disk\n",
107 " check_command check_disk\n",
108 "}\n\n",
109 );
110  
111 0 $config->add_content(
112 "define service {\n",
113 " use tmpl-server-service\n",
114 " host_name $name\n",
115 " service_description check_proc\n",
116 " check_command check_proc\n",
117 "}\n\n",
118 );
119  
120 0 $config->add_content(
121 "define service {\n",
122 " use tmpl-server-service\n",
123 " host_name $name\n",
124 " service_description check_raid_bsd\n",
125 " check_command check_raid_bsd\n",
126 "}\n\n",
127 );
128  
129 0 $config->add_content(
130 "define service {\n",
131 " use tmpl-server-service\n",
132 " host_name $name\n",
133 " service_description check_snmp_volume\n",
134 " check_command check_snmp_volume\n",
135 "}\n\n",
136 );
137  
138 0 ▶ if ($rackdev->has_ilo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rackdev->has_ilo) 0 0 0
139 0 my $ilo_name = $rackdev->ilo_fqdn;
140 0 my $ilo_addr = $rackdev->ilo_ipv4addr->{addr};
141  
142 0 $config->add_content(
143 "define host {\n",
144 " use tmpl-generic-host\n",
145 " host_name $ilo_name\n",
146 " address $ilo_addr\n",
147 " check_command check-host-alive\n",
148 " notes iLO subsystem of $name\n",
149 "}\n\n",
150 );
151 }
152  
153 }
154  
155  
156 #
157 # config_for_switch()
158 # -----------------
159 0 ▶sub config_for_switch {
Subroutine config_for_switch: not called CC=1 SCAR=6.9
Pod undocumented
160 0 my ($class, $args) = @_;
161  
162 0 my $rackdev = $args->{rackdev};
163 0 my $config = $args->{config};
164 0 my $name = $rackdev->object_name;
165  
166 0 $config->add_content(
167 "define service {\n",
168 " use tmpl-switch-service\n",
169 " host_name $name\n",
170 " service_description check_media_switch\n",
171 " check_command check_media_switch\n",
172 "}\n\n",
173 );
174 }
175  
176  
177 #
178 # config_for_pdu()
179 # --------------
180 0 ▶sub config_for_pdu {
Subroutine config_for_pdu: not called CC=1 SCAR=6.9
Pod undocumented
181 0 my ($class, $args) = @_;
182  
183 0 my $rackdev = $args->{rackdev};
184 0 my $config = $args->{config};
185 0 my $name = $rackdev->object_name;
186  
187 0 $config->add_content(
188 "define service {\n",
189 " use tmpl-pdu-service\n",
190 " host_name $name\n",
191 " service_description check_pdu\n",
192 " check_command check_pdu\n",
193 "}\n\n",
194 );
195 }
196  
197  
198 2 __PACKAGE__
199  
200 __END__
« blib/lib/RackMan/Template.pm ↑ Summary blib/lib/RackMan/Format/Bacula.pm »