blib/lib/Nagios/Passive.pm

filter: Statementstmt 92.5% 25 / 27 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 90.2% 37 / 41 CC 5 SCAR 12.0
CC
5
Cov
89%
create
5.2
BEGIN
1.0
BEGIN
1.0
CRAP 3.3
« Build ↑ Summary blib/lib/Nagios/Passive/Base.pm »
1 package Nagios::Passive;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Class::Load qw/load_class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use version; our $VERSION = qv('0.3.6');
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 sub create {
8 6 ▶ my $this = shift;
Subroutine create: called CC=5 SCAR=16.4
Pod documented
9 6 ▶ my %opts = ref($_[0]) eq 'HASH' ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 6 6
10 6 my $class;
11 6 ▶ if(exists $opts{command_file}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $opts{'command_file'}) { } 2 4 6
elsif (exists $opts{'checkresults_dir'}) { } 3 1 4
elsif (exists $opts{'gearman'}) { } 1 0 1
12 2 $class = 'Nagios::Passive::CommandFile';
13 2 load_class($class);
14 } elsif(exists $opts{checkresults_dir}) {
15 3 $class = 'Nagios::Passive::ResultPath';
16 3 load_class($class);
17 } elsif(exists $opts{gearman}) {
18 1 $class = 'Nagios::Passive::Gearman';
19 1 load_class($class);
20 } else {
21 0 croak("no backend specified");
22 }
23 2 return $class->new(%opts);
24 }
25 5 1;
26 __END__
27  
28 =head1 NAME
29  
30 Nagios::Passive - submit passive check results to nagios
31  
32 =head1 SYNOPSIS
33  
34 my $nw = Nagios::Passive->create(
35 command_file => $command_file,
36 service_description => $service_description,
37 check_name => $check_name,
38 host_name => $hostname,
39 return_code => 0, # 1 2 3
40 output => 'looks (good|bad|horrible) | performancedata'
41 );
42 $nw->submit;
43  
44 =head1 DESCRIPTION
45  
46 This is the factory class, currently it creates either a
47 Nagios::Passive::CommandFile or a Nagios::Passive::ResultPath
48 object. Which object is created depends on the keys of the hash
49 you supply to the the create method.
50  
51 =head1 FACTORY METHODS
52  
53 =head2 create( %ARGS )
54  
55 If there is a key named
56  
57 =over 4
58  
59 =item * C<checkresults_dir>, a Nagios::Passive::ResultPath
60  
61 =item * C<command_file>, a Nagios::Passive::CommandFile
62  
63 =item * C<gearman>, a Nagios::Passive::Gearman
64  
65 =back
66  
67 object ist created.
68  
69 If you're using checkresults_dir, you may also wan't to take a look at
70 L<Nagios::Passive::BulkResult>.
71  
72 The gearman constructor also accepts a C<key> for the
73 optional shared secret.
74  
75 Other required keys are C<host_name> and C<check_name>.
76  
77 C<host_name> is the hostname for which you want to report a check
78 result to nagios.
79  
80 The typical output of a nagios plugin looks like this:
81  
82 CHECK_NAME STATUS - MESSAGE
83  
84 CHECK_NAME is replaced by C<check_name>. MESSAGE is replaced by
85 C<output>.
86  
87 STATUS can either be set by setting C<return_code> to 0,1,2 or 3
88 (See nagios documentation for details) or by using the
89 C<set_thresholds> and C<set_status> methods. return_code
90 default's to 0 if not set somehow.
91  
92 C<service_description> is optional, if it's omitted the
93 check result belongs to the host check of host_name.
94  
95 All of the attributes (except the required ones) can also be set
96 afterwards, by calling the setter methods of the same name, i.e.:
97  
98 $nw->return_code(0);
99 $nw->output("everything ok");
100 # results to: CHECK_NAME OK - everything ok
101  
102 =head1 METHODS
103  
104 On the object you gathered from the C<create> method, you can
105 perform the following operations.
106  
107 =head2 output STRING
108  
109 Sets MESSAGE to STRING. If STRING is omitted, it returns
110 the current value of output.
111  
112 =head2 add_output STRING
113  
114 Equivalent to:
115  
116 $nw->output($nw->output . STRING)
117  
118 =head2 set_thresholds HASH
119  
120 $nw->set_thresholds(
121 warning => ':91',
122 critical => ':97',
123 );
124  
125 This creates a Nagios::Plugin::Threshold object. It can be used
126 to set the C<return_code> with C<set_status>.
127  
128 =head2 set_status VALUE
129  
130 Sets the C<return_code> according the the threshold object
131 created with set_thresholds and the given VALUE. For example:
132  
133 $nw->set_thresholds(warning => ':4', critical => ':8');
134 $nw->set_status(6);
135 $nw->output("6 is a warning");
136 # return_code is now 1, and the output shown in nagios will be
137 # CHECK_NAME WARNING - 6 is a warning
138  
139 =head2 add_perf HASH
140  
141 This can be used to add performance data to the check result.
142 Read L<Nagios::Plugin::Performance> to get the idea of how to use
143 this.
144  
145 =head2 submit
146  
147 This writes the data out. In case of the CommandFile this will
148 write the result into nagios' external_command_file. In case of
149 ResultPath this will drop a file into nagios' check_result_path.
150  
151 =head1 LIMITATIONS
152  
153 This module is in an early stage of development, the API is
154 likely to brake in the future.
155  
156 Nagios::Passive::ResultPath interacts with an undocumented feature of Nagios.
157 This feature may disappear in the future.
158 (Well, that feature works for years now, so ....)
159  
160 =head1 DEVELOPMENT
161  
162 Development takes place on github:
163  
164 L<http://github.com/datamuc/Nagios-Passive>
165  
166 =head1 AUTHOR
167  
168 Danijel Tasov, <data@cpan.org>
169  
170 =head1 COPYRIGHT
171  
172 Copyright (C) 2009, Danijel Tasov
173  
174 This library is free software; you can redistribute it and/or modify
175 it under the same terms as Perl itself.
176  
177 =cut
« Build ↑ Summary blib/lib/Nagios/Passive/Base.pm »