/[nagios-plugins-perl]/trunk/plugins/check_rrd.pl
ViewVC logotype

Contents of /trunk/plugins/check_rrd.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 121 - (show annotations) (download)
Thu Nov 12 13:29:04 2009 UTC (11 years ago) by racvision
File MIME type: text/plain
File size: 4065 byte(s)
convert source structure from autotools to rpm-build.sh
1 #!/usr/bin/perl -w
2 #
3 # Copyright (c) 2009 - St├ęphane Urbanovski <stephane.urbanovski@ac-nancy-metz.fr>
4 # Copyright (c) 2008 - Florian Duma
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty
13 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # you should have received a copy of the GNU General Public License
17 # along with this program (or with Nagios); if not, write to the
18 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 # Boston, MA 02111-1307, USA
20 #
21 #
22 # A Nagios check to get data from an rrd graph
23 # nagios: -epn
24 #
25 # $Id: check_rrd.pl,v 1.1 2009-06-11 13:00:27 uid5000 Exp $
26
27
28 use strict;
29 use warnings;
30 use utf8;
31
32 use File::Basename;
33 use Locale::gettext;
34 use Nagios::Plugin ;
35 use POSIX qw(setlocale);
36 # use Time::HiRes; # get microtime
37 use RRDs;
38 # use Data::Dumper;
39
40
41 my $PROGNAME = basename($0);
42 '$Revision: 1.1 $' =~ /^.*(\d+.\d+) \$$/; # Use The Revision from RCS/CVS/SVN
43 my $VERSION = $1;
44
45 my $DEBUG = 1;
46 my $TIMEOUT = 5;
47
48 my $RRDTOOL_CMD="/usr/bin/rrdtool";
49 my $RRDPATH="/var/lib/cacti08/";
50
51 my $RRD_AGE = 600;
52
53 # i18n :
54 setlocale(LC_MESSAGES, '');
55 textdomain('nagios-plugins-rrd');
56
57
58 my $np = Nagios::Plugin->new(
59 version => $VERSION,
60 blurb => _gt("Plugin to check Cacti RRD bases"),
61 usage => "Usage: %s [ -v|--verbose ] [-t <timeout>] [ -c|--critical=<threshold> ] [ -w|--warning=<threshold> ]",
62 timeout => $TIMEOUT+1
63 );
64
65 $np->add_arg (
66 spec => 'debug|d',
67 help => _gt('Debug level'),
68 default => 0,
69 required => 0,
70 );
71
72 $np->add_arg (
73 spec => 'host|H=s',
74 help => _gt('Hostname'),
75 required => 1,
76 );
77
78 $np->add_arg (
79 spec => 'key|k=s',
80 help => _gt('Key'),
81 required => 1,
82 );
83
84 $np->add_arg (
85 spec => 'ds=s',
86 help => _gt('Datasource name or index'),
87 required => 0,
88 default => 1
89 );
90
91 $np->add_arg (
92 spec => 'warning|w=s',
93 help => _gt('Warning threshold'),
94 );
95
96 $np->add_arg (
97 spec => 'critical|c=s',
98 help => _gt('Critical threshold'),
99 );
100
101 $np->add_arg (
102 spec => 'msg=s',
103 help => _gt('Message'),
104 default => _gt('Value is %f'),
105 required => 0,
106 );
107 $np->getopts;
108
109 $DEBUG = $np->opts->get('debug');
110
111 my $host = $np->opts->get('host');
112 my $key = $np->opts->get('key');
113 my $msg = $np->opts->get('msg');
114
115 # Thresholds :
116 my $warn = $np->opts->get('warning');
117 my $crit = $np->opts->get('critical');
118
119 my $ds = $np->opts->get('ds');
120
121 my $find = $host."_".$key.".*\.rrd";
122
123
124 if ( ! opendir(DIR, $RRDPATH) ) {
125 $np->nagios_exit(CRITICAL, sprintf(_gt("Can't open directory '%s': %s"),$RRDPATH,$!));
126 }
127 my @rrdFiles=grep { /$find/ && -f "$RRDPATH/$_" } readdir(DIR);
128 closedir(DIR);
129
130 my $rrdfile = $RRDPATH.$rrdFiles[0];
131
132
133 debug("Opening rrd file $rrdfile");
134 my @fetch = RRDs::fetch( $rrdfile, 'AVERAGE',"-s -$RRD_AGE");
135
136 if ( my $err = RRDs::error ) {
137 $np->nagios_exit(CRITICAL, sprintf(_gt("RRD error: %s"),$err));
138 }
139
140 my $selectedDs = -1;
141 for ( my $i = 0 ; $i <= $#{$fetch[2]}; $i++ ) {
142 if ( $ds eq $fetch[2][$i] || $ds eq ($i+1)."" ) {
143 debug ( "Datasource selected: ".$fetch[2][$i] );
144 $selectedDs = $i;
145 } else {
146 debug ( "Datasource: ".$fetch[2][$i] );
147 }
148 }
149 if ( $selectedDs == -1 ) {
150 $np->nagios_exit(UNKNOWN, _gt("Unknown datasource !"));
151 }
152
153 my $value = undef;
154 foreach my $row (@{$fetch[3]}) {
155 next unless ( defined($row->[$selectedDs]) );
156 $value = $row->[$selectedDs];
157 }
158
159 if ( ! defined($value) ) {
160 $np->nagios_exit(CRITICAL, sprintf(_gt("Can't get a value in %s last seconds"),$RRD_AGE));
161 }
162
163 debug ( "value=$value");
164
165 my $status = $np->check_threshold(
166 'check' => $value,
167 'warning' => $warn,
168 'critical' => $crit,
169 );
170
171 $np->nagios_exit($status, sprintf($msg,$value) );
172
173 # Print debug information if $DEBUG > 0
174 sub debug {
175 print STDERR "[DEBUG] ".$_[0]."\n" if $DEBUG;
176 }
177
178 # Gettext wrapper
179 sub _gt {
180 return gettext($_[0]);
181 }

  ViewVC Help
Powered by ViewVC 1.1.8