File Coverage

blib/lib/Nagios/Plugin/ByGmond.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Nagios::Plugin::ByGmond;
2              
3 1     1   27616 use strict;
  1         3  
  1         38  
4 1     1   5 use warnings;
  1         2  
  1         33  
5 1     1   439 use XML::Simple;
  0            
  0            
6             use IO::Socket::INET;
7             use base 'Nagios::Plugin';
8              
9             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             sub new {
36             my $class = shift;
37             my $self = $class->SUPER::new(
38             usage => <
39             Usage: %s [ -v|--verbose ] [-H|--host=] [-p|--port=] [-m|--metric=]
40             [ -c|--critical= ]
41             [ -w|--warning= ]
42             END_USAGE
43             version => $Nagios::Plugin::POP3VERSION,
44             blurb => q{Nagios plugin for receive metric data from gmond},
45             );
46             $self->add_arg(
47             spec => 'warning|w=s',
48             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             $self->add_arg(
55             spec => 'critical|c=s',
56             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             $self->add_arg(
63             spec => 'host|H=s',
64             default => 'localhost.localdomain',
65             help => <
66             -H, --host
67             Gmond Host (defaults to localhost.localdomain)
68             END_HELP
69             );
70             $self->add_arg(
71             spec => 'port|p=s',
72             default => '8649',
73             help => <
74             -p, --port
75             Gmond Port (defaults to 8649)
76             END_HELP
77             );
78             $self->add_arg(
79             spec => 'metric|m=s',
80             help => <
81             -m, --metric
82             Gmetric name
83             END_HELP
84             );
85             return $self;
86             }
87              
88             sub run {
89             my $self = shift;
90              
91             # Parse arguments and process standard ones (e.g. usage, help, version)
92             $self->getopts;
93             if ( !defined $self->opts->warning
94             && !defined $self->opts->critical
95             && !defined $self->opts->metric )
96             {
97             $self->nagios_die("You need to specify a threshold argument");
98             }
99              
100             #Connect and got data
101             my $sock = IO::Socket::INET->new(
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             my $data;
109             while (<$sock>) { $data .= $_ }
110             my $gmond_ref = XMLin($data)->{CLUSTER}->{HOST};
111             my $host_ref;
112             if ( ref($gmond_ref) eq 'ARRAY' ) {
113             for my $host ( @{$gmond_ref} ) {
114             if ( $host->{IP} eq $self->opts->host
115             or $host->{NAME} eq $self->opts->host ) {
116             $host_ref = $host;
117             last;
118             }
119             }
120             } else {
121             $host_ref = $gmond_ref;
122             }
123             my $metric_arrayref = $host_ref->{METRIC};
124             for my $metric (@$metric_arrayref) {
125             next unless $self->opts->metric eq $metric->{NAME};
126             $self->add_perfdata(
127             label => $metric->{NAME},
128             value => $metric->{VAL},
129             uom => $metric->{UNITS},
130             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             );
138             }
139              
140             }
141              
142             1;
143              
144             =head1 AUTHOR
145              
146             chenryn, C<< >>
147              
148             =head1 BUGS
149              
150             Please report any bugs or feature requests to C, or through
151             the web interface at L. 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
171              
172             =item * AnnoCPAN: Annotated CPAN documentation
173              
174             L
175              
176             =item * CPAN Ratings
177              
178             L
179              
180             =item * Search CPAN
181              
182             L
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
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