blib/lib/Nagios/Plugin/ByGmond.pm

filter: Statementstmt 36.1% 17 / 47 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 71.4% 5 / 7 Podpod 0.0% 0 / 2 totaltotal 26.8% 22 / 82 CC 12 SCAR 46.5
CC
12
Cov
25%
run
156.0
new
2.0
BEGIN
1.0
CRAP 104.4
↑ Summary
1 package Nagios::Plugin::ByGmond;
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 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use base 'Nagios::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.04';
10  
11 =head1 NAME
12  
13 Nagios::Plugin::ByGmond - Nagios plugin for checking metrics from ganglia monitor daemon TCP output.
14  
15 =head1 SYNOPSIS
16  
17 use Nagios::Plugin::ByGmond;
18  
19 my $npbg = Nagios::Plugin::ByGmond->new();
20 $npbg->run;
21  
22 =head1 DESCRIPTION
23
24 Please setup your nagios config.
25
26 define command {
27 command_name check_memory_by_gmond
28 command_line /usr/local/bin/check_by_gmond -H $HOSTADDRESS$ -w 3 -c 5 -m 'mem_total'
29 }
30
31 This plugin use metric named by ganglia. Please saw documention of ganglia before using.
32  
33 =cut
34  
35 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
36 0 my $class = shift;
37 0 my $self = $class->SUPER::new(
38 usage => <<END_USAGE,
39 Usage: %s [ -v|--verbose ] [-H|--host=<host>] [-p|--port=<port>] [-m|--metric=<metric>]
40 [ -c|--critical=<critical threshold> ]
41 [ -w|--warning=<warning threshold> ]
42 END_USAGE
43 version => $Nagios::Plugin::POP3VERSION,
44 blurb => q{Nagios plugin for receive metric data from gmond},
45 );
46 0 $self->add_arg(
47 spec => 'warning|w=s',
48 help => <<END_HELP,
49 -w, --warning=INTEGER:INTEGER
50 Minimum and maximum number of allowable result, outside of which a
51 warning will be generated. If omitted, no warning is generated.
52 END_HELP
53 );
54 0 $self->add_arg(
55 spec => 'critical|c=s',
56 help => <<END_HELP,
57 -c, --critical=INTEGER:INTEGER
58 Minimum and maximum number of the generated result, outside of
59 which a critical will be generated.
60 END_HELP
61 );
62 0 $self->add_arg(
63 spec => 'host|H=s',
64 default => 'localhost.localdomain',
65 help => <<END_HELP,
66 -H, --host
67 Gmond Host (defaults to localhost.localdomain)
68 END_HELP
69 );
70 0 $self->add_arg(
71 spec => 'port|p=s',
72 default => '8649',
73 help => <<END_HELP,
74 -p, --port
75 Gmond Port (defaults to 8649)
76 END_HELP
77 );
78 0 $self->add_arg(
79 spec => 'metric|m=s',
80 help => <<END_HELP,
81 -m, --metric
82 Gmetric name
83 END_HELP
84 );
85 0 return $self;
86 }
87  
88 0 ▶sub run {
Subroutine run: not called CC=12 SCAR=50.5
Pod undocumented
89 0 my $self = shift;
90  
91 # Parse arguments and process standard ones (e.g. usage, help, version)
92 0 $self->getopts;
93 0 ▶ if ( !defined $self->opts->warning
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->opts->warning or defined $self->opts->critical or defined $self->opts->metric) 0 0 0
Condition0%0/6
defined $self->opts->warning or defined $self->opts->critical or defined $self->opts->metric
l!l&&r!l&&!r
000
defined $self->opts->warning or defined $self->opts->critical
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $self->opts->warning or defined $self->opts->critical or defined $self->opts->metric
defined $self->opts->warning defined $self->opts->critical defined $self->opts->metric
Truth table0%0/4
A or B or C
A= defined $self->opts->warningB= defined $self->opts->criticalC= defined $self->opts->metric
ABCresult
1XX1
01X1
0011
0000
94 && !defined $self->opts->critical
95 && !defined $self->opts->metric )
96 {
97 0 $self->nagios_die("You need to specify a threshold argument");
98 }
99  
100 #Connect and got data
101 0 ▶ my $sock = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::INET"->new("PeerAddr", $self->opts->host, "PeerPort", $self->opts->port, "Proto", "tcp") or $self->nagios_die('Connect to ' . $self->opts->host . ' error: ' . $!) 0 0 0
102 PeerAddr => $self->opts->host,
103 PeerPort => $self->opts->port,
104 Proto => 'tcp'
105 )
106 or
107 $self->nagios_die( 'Connect to ' . $self->opts->host . ' error: ' . $! );
108 0 my $data;
109 0 ▶ while (<$sock>) { $data .= $_ }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $sock)) 0 0 0
110 0 my $gmond_ref = XMLin($data)->{CLUSTER}->{HOST};
111 0 my $host_ref;
112 0 ▶ if ( ref($gmond_ref) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $gmond_ref eq 'ARRAY') { } 0 0 0
113 0 for my $host ( @{$gmond_ref} ) {
114 0 ▶ if ( $host->{IP} eq $self->opts->host
Branch: 1 branch
BranchTrueFalseTotal
if ($host->{"IP"} eq $self->opts->host or $host->{"NAME"} eq $self->opts->host) 0 0 0
Condition0%0/3
$host->{"IP"} eq $self->opts->host or $host->{"NAME"} eq $self->opts->host
l!l&&r!l&&!r
000
MC/DC0%0/2
$host->{"IP"} eq $self->opts->host or $host->{"NAME"} eq $self->opts->host
$host->{"IP"} eq $self->opts->host $host->{"NAME"} eq $self->opts->host
Truth table0%0/3
A or B
A= $host->{"IP"} eq $self->opts->hostB= $host->{"NAME"} eq $self->opts->host
ABresult
1X1
011
000
115 or $host->{NAME} eq $self->opts->host ) {
116 0 $host_ref = $host;
117 0 last;
118 }
119 }
120 } else {
121 0 $host_ref = $gmond_ref;
122 }
123 0 my $metric_arrayref = $host_ref->{METRIC};
124 0 for my $metric (@$metric_arrayref) {
125 0 ▶ next unless $self->opts->metric eq $metric->{NAME};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->opts->metric eq $metric->{"NAME"} 0 0 0
126 $self->add_perfdata(
127 label => $metric->{NAME},
128 value => $metric->{VAL},
129 uom => $metric->{UNITS},
130 0 threshold => $self->threshold,
131 );
132 $self->nagios_exit(
133 return_code => $self->check_threshold( $metric->{VAL} ),
134 message => sprintf "%s: %f %s\n",
135 $metric->{EXTRA_DATA}->{EXTRA_ELEMENT}->[1]->{VAL}, $metric->{VAL},
136 $metric->{UNITS},
137 0 );
138 }
139  
140 }
141  
142 1 1;
143  
144 =head1 AUTHOR
145  
146 chenryn, C<< <rao.chenlin at gmail.com> >>
147  
148 =head1 BUGS
149  
150 Please report any bugs or feature requests to C<bug-nagios-plugin-bygmond at rt.cpan.org>, or through
151 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Nagios-Plugin-ByGmond>. I will be notified, and then you'll
152 automatically be notified of progress on your bug as I make changes.
153  
154  
155  
156  
157 =head1 SUPPORT
158  
159 You can find documentation for this module with the perldoc command.
160  
161 perldoc Nagios::Plugin::ByGmond
162  
163  
164 You can also look for information at:
165  
166 =over 4
167  
168 =item * RT: CPAN's request tracker (report bugs here)
169  
170 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Nagios-Plugin-ByGmond>
171  
172 =item * AnnoCPAN: Annotated CPAN documentation
173  
174 L<http://annocpan.org/dist/Nagios-Plugin-ByGmond>
175  
176 =item * CPAN Ratings
177  
178 L<http://cpanratings.perl.org/d/Nagios-Plugin-ByGmond>
179  
180 =item * Search CPAN
181  
182 L<http://search.cpan.org/dist/Nagios-Plugin-ByGmond/>
183  
184 =back
185  
186  
187 =head1 ACKNOWLEDGEMENTS
188  
189  
190 =head1 LICENSE AND COPYRIGHT
191  
192 Copyright 2013 chenryn.
193  
194 This program is free software; you can redistribute it and/or modify it
195 under the terms of the the Artistic License (2.0). You may obtain a
196 copy of the full license at:
197  
198 L<http://www.perlfoundation.org/artistic_license_2_0>
199  
200 Any use, modification, and distribution of the Standard or Modified
201 Versions is governed by this Artistic License. By using, modifying or
202 distributing the Package, you accept this license. Do not use, modify,
203 or distribute the Package, if you do not accept this license.
204  
205 If your Modified Version has been derived from a Modified Version made
206 by someone other than you, you are nevertheless required to ensure that
207 your Modified Version complies with the requirements of this license.
208  
209 This license does not grant you the right to use any trademark, service
210 mark, tradename, or logo of the Copyright Holder.
211  
212 This license includes the non-exclusive, worldwide, free-of-charge
213 patent license to make, have made, use, offer to sell, sell, import and
214 otherwise transfer the Package with respect to any patent claims
215 licensable by the Copyright Holder that are necessarily infringed by the
216 Package. If you institute patent litigation (including a cross-claim or
217 counterclaim) against any party alleging that the Package constitutes
218 direct or contributory patent infringement, then this Artistic License
219 to you shall terminate on the date that such litigation is filed.
220  
221 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
222 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
223 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
224 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
225 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
226 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
227 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
228 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
229  
230  
231 =cut
↑ Summary