blib/lib/Nagios/Plugin/Simple.pm

filter: Statementstmt 100% 46 / 46 Branchbran 50.0% 3 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 9 / 9 totaltotal 90.9% 70 / 77 CC 5 SCAR 5.2
CC
5
Cov
91%
status
3.0
codes
2.1
new
2.0
CRAP 1.7
↑ Summary
1 package Nagios::Plugin::Simple;
2 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 10 our $VERSION='0.06';
6  
7 =head1 NAME
8  
9 Nagios::Plugin::Simple - Simple and Minimalistic Nagios Plugin Package
10  
11 =head1 SYNOPSIS
12  
13 use Nagios::Plugin::Simple;
14 my $nps=Nagios::Plugin::Simple->new;
15 $nps->ok("I'm OK") if &ok;
16 $nps->warning("I'm a bit sickly") if &sick;
17 $nps->critical("Barf...");
18 $nps->unknown("Huh?");
19  
20 In the true spirit of Perl you can even do a one-liner.
21  
22 perl -MNagios::Plugin::Simple -e 'Nagios::Plugin::Simple->new->ok("")';echo $?
23  
24  
25 =head1 DESCRIPTION
26  
27 This is the package that I use mostly because I feel the L<Nagios::Plugin> is too encompassing. I feel that it is the scripts responsibility to handle arguments and thus this package does not do that nor will do that. If you want argument handling use one of the GetOpt packages.
28  
29 =head1 USAGE
30  
31 use Nagios::Plugin::Simple;
32 my $nps=Nagios::Plugin::Simple->new;
33 if (&ok) {$nps->ok("good!")} else {$nps->critical("bad!")};
34  
35  
36 =head1 CONSTRUCTOR
37  
38 =head2 new
39  
40 my $nps=Nagios::Plugin::Simple->new();
41  
42 =cut
43  
44 10 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
45 10 my $this = shift();
46 10 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0100
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
47 10 my $self = {};
48 10 bless $self, $class;
49 10 $self->initialize(@_);
50 10 return $self;
51 }
52  
53 =head1 METHODS
54  
55 =head2 initialize
56  
57 =cut
58  
59 10 ▶sub initialize {
Subroutine initialize: called CC=1 SCAR=0.0
Pod documented
60 10 my $self=shift;
61 10 %$self=@_;
62 }
63  
64 =head2 ok
65  
66 Exits script with ok status code.
67  
68 $nps->ok("I'm OK");
69  
70 Prints "OK: %s" and exits with a code 0.
71  
72 STDOUT => "OK: I'm OK\n", EXIT=>0
73  
74 =cut
75  
76 1 ▶sub ok {
Subroutine ok: called CC=1 SCAR=0.0
Pod documented
77 1 my $self=shift;
78 1 my $string=shift;
79 1 $self->status(OK=>$string);
80 }
81  
82 =head2 warning
83  
84 Exits script with warning status code.
85  
86 $nps->warning("I'm a bit sickly");
87  
88 Prints "Warning: %s" and exits with a code 1.
89  
90 STDOUT => "Warning: I'm a bit sickly\n", EXIT=>1
91  
92 =cut
93  
94 1 ▶sub warning {
Subroutine warning: called CC=1 SCAR=0.0
Pod documented
95 1 my $self=shift;
96 1 my $string=shift;
97 1 $self->status(Warning=>$string);
98 }
99  
100 =head2 critical
101  
102 Exits script with critical status code.
103  
104 $nps->critical("Barf...");
105  
106 Prints "Critical: %s" and exits with a code 2.
107  
108 STDOUT => "Critical: Barf...\n", EXIT=>2
109  
110 =cut
111  
112 1 ▶sub critical {
Subroutine critical: called CC=1 SCAR=0.0
Pod documented
113 1 my $self=shift;
114 1 my $string=shift;
115 1 $self->status(Critical=>$string);
116 }
117  
118 =head2 unknown
119  
120 Exits script with unknown status code.
121  
122 $nps->unknown("Huh?")
123  
124 Prints "Unknown: %s" and exits with a code 3.
125  
126 STDOUT => "Unknown: Huh?\n", EXIT=>3
127  
128 =cut
129  
130 1 ▶sub unknown {
Subroutine unknown: called CC=1 SCAR=0.0
Pod documented
131 1 my $self=shift;
132 1 my $string=shift;
133 1 $self->status(Unknown=>$string);
134 }
135  
136 =head2 code
137  
138 Exits script by status code. This works best if your status is actually stored as a code 0, 1, 2, or 3 in a variable.
139  
140 $nps->code($code => $string);
141  
142 Examples:
143  
144 $nps->code(0 => "I'm OK!");
145 $nps->code(1 => "I'm a bit sickly");
146 $nps->code(2 => "Barf...");
147 $nps->code(3 => "Huh?")
148  
149 Prints ``$status: %s'' and exits with $code.
150  
151 =cut
152  
153 2 ▶sub code {
Subroutine code: called CC=1 SCAR=0.0
Pod documented
154 2 my $self=shift;
155 2 my $code=shift;
156 2 my $string=shift;
157 2 my %status=reverse $self->codes;
158 2 my $status=$status{$code};
159 2 $self->status($status, $string);
160 }
161  
162 =head2 status
163  
164 Exits script by status string. This works best if your string is actually stored as "OK", "Warning", etc in a variable
165  
166 $nps->status($status => $string);
167  
168 Examples:
169  
170 $nps->status("OK" => "I'm OK!");
171 $nps->status("Warning" => "I'm a bit sickly");
172 $nps->status("Critical" => "Barf...");
173 $nps->status("Unknown" => "Huh?")
174  
175 Prints ``$status: %s'' and exits with correct code.
176  
177 =cut
178  
179 8 ▶sub status {
Subroutine status: called CC=3 SCAR=11.1
Pod documented
180 8 my $self=shift;
181 8 my $status=shift;
182 8 my $string=shift;
183 8 ▶ $string='' unless defined($string);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 8 8
184 8 my %codes=map {uc($_)} $self->codes;
185 #use Data::Dumper;
186 #print Dumper([\%codes]);
187 8 my $code=$codes{uc($status)};
188 8 ▶ die(qq{Error: Exit code not defined for "$status"}) unless defined($code);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 8 8
189 8 printf "%s: %s\n", $status, $string;
190 8 exit $code;
191 }
192  
193 =head2 codes
194  
195 Returns a hash of the Nagios status codes.
196  
197 my %codes=$nps->codes; #(OK=>0, Warning=>1, Critical=>2, Unknown=>3)
198 my $codes=$nps->codes; #{OK=>0, Warning=>1, Critical=>2, Unknown=>3}
199 my %status=reverse $self->codes; #(0=>"OK", 1=>"Warning", ...)
200  
201 =cut
202  
203 10 ▶sub codes {
Subroutine codes: called CC=2 SCAR=7.2
Pod documented
204 #my $self=shift;
205 10 my @data=(OK=>0, Warning=>1, Critical=>2, Unknown=>3);
206 10 ▶ return wantarray ? @data : {@data};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 10 0 10
207 }
208  
209 =head1 BUGS
210  
211 =head1 SUPPORT
212  
213 =head1 AUTHOR
214  
215 Michael R. Davis
216 CPAN ID: MRDVT
217 STOP, LLC
218 account=>perl,tld=>com,domain=>michaelrdavis
219 http://www.stopllc.com/
220  
221 =head1 COPYRIGHT
222  
223 This program is free software licensed under the...
224  
225 The BSD License
226  
227 The full text of the license can be found in the
228 LICENSE file included with this module.
229  
230 =head1 SEE ALSO
231  
232 L<Nagios::Plugin>, L<Getopt::Std>, L<Getopt::Long>
233  
234 =cut
235  
236 10 1;
↑ Summary