retire da-backup checks
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-afs-space
1 #!/usr/bin/perl -w
2 $ID = q$Id: check_afsspace,v 1.16 2006/03/17 23:06:54 quanah Exp $;
3 #
4 # check_afsspace -- Monitor AFS disk space usage under Nagios.
5 #
6 # Written by Susan Feng <sfeng@stanford.edu>
7 # Updated by Russ Allbery <rra@stanford.edu>
8 # Copyright 2003, 2004 Board of Trustees, Leland Stanford Jr. University
9 #
10 # This program is free software; you may redistribute it and/or modify it
11 # under the same terms as Perl itself.
12 #
13 # Expects a host with the -H option and checks the partition usage with
14 # vos partinfo.  Exits with status 1 if the free space is below a warning
15 # percentage and with status 2 if the free space is above a critical
16 # percentage (this works with the Nagios check architecture).
17
18 ##############################################################################
19 # Site configuration
20 ##############################################################################
21
22 # The default percentage full at which to warn and at which to send a critical
23 # alert.  These can be overridden with the -w and -c command-line options.
24 $WARNINGS = 85;
25 $CRITICAL = 90;
26
27 # The default timeout in seconds (implemented by alarm) for vos partinfo.
28 $TIMEOUT = 300;
29
30 # The full path to vos.  Make sure that this is on local disk so that
31 # monitoring doesn't have an AFS dependency.
32 ($VOS) = grep { -x $_ } qw(/usr/bin/vos /usr/local/bin/vos);
33 $VOS ||= '/usr/bin/vos';
34
35 ##############################################################################
36 # Modules and declarations
37 ##############################################################################
38
39 require 5.003;
40
41 use strict;
42 use vars qw($CRITICAL $ID $TIMEOUT $VOS $WARNINGS);
43
44 use Getopt::Long qw(GetOptions);
45
46 ##############################################################################
47 # Implementation
48 ##############################################################################
49
50 # Parse command line options.
51 my ($help, $host, $version);
52 Getopt::Long::config ('bundling', 'no_ignore_case');
53 GetOptions ('critical|c=i' => \$CRITICAL,
54             'hostname|H=s' => \$host,
55             'help|h'       => \$help,
56             'timeout|t=i'  => \$TIMEOUT,
57             'version|V'    => \$version,
58             'warning|w=i'  => \$WARNINGS) or exit 3;
59 if ($help) {
60     print "Feeding myself to perldoc, please wait....\n";
61     exec ('perldoc', '-t', $0) or die "Cannot fork: $!\n";
62 } elsif ($version) {
63     my $version = join (' ', (split (' ', $ID))[1..3]);
64     $version =~ s/,v\b//;
65     $version =~ s/(\S+)$/($1)/;
66     $version =~ tr%/%-%;
67     print $version, "\n";
68     exit 0;
69 }
70 if (@ARGV) {
71     warn "Usage: $0 [-hv] [-c <level>] [-w <level>] -H <host>\n";
72     exit 3;
73 }
74 if ($WARNINGS > $CRITICAL) {
75     warn "$0: warning level $WARNINGS greater than critical level $CRITICAL\n";
76     exit 3;
77 }
78
79 # Set up the alarm.
80 $SIG{ALRM} = sub {
81     print "AFS CRITICAL - network timeout after $TIMEOUT seconds\n";
82     exit 2;
83 };
84 alarm ($TIMEOUT);
85
86 # Get the partinfo information and calculate the percentage free for each
87 # partition.  Accumulate critical messages in @critical and warnings in
88 # @warnings.  Accumulate all percentages in @all.
89 my (@critical, @warnings, @all);
90 my @data = `$VOS partinfo '$host' 2> /dev/null`;
91 if ($? != 0) {
92     print "AFS CRITICAL - cannot contact server\n";
93     exit 2;
94 }
95 for (@data) {
96     my ($partition, $free, $total) = (split)[4,5,11];
97     my $percent = int ((($total - $free) / $total) * 100);
98     if ($percent >= $CRITICAL) {
99         push (@critical, "$partition$percent% (free $free)");
100     } elsif ($percent >= $WARNINGS) {
101         push (@warnings, "$partition$percent% (free $free)");
102     }
103     push (@all, "$partition$percent%");
104 }
105
106 # Exit with the appropriate error messages.
107 if (@critical) {
108     print "AFS CRITICAL - @critical\n";
109     exit 2;
110 } elsif (@warnings) {
111     print "AFS WARNING - @warnings\n";
112     exit 1;
113 } else {
114     print "AFS OK - @all\n";
115     exit 0;
116 }
117
118 ##############################################################################
119 # Documentation
120 ##############################################################################
121
122 =head1 NAME
123
124 check_afsspace - Monitor AFS disk space usage under Nagios
125
126 =head1 SYNOPSIS
127
128 check_afsspace [B<-hV>] [B<-c> I<threshold>] [B<-w> I<threshold>]
129 [B<-t> I<timeout>] B<-H> I<host>
130
131 =head1 DESCRIPTION
132
133 B<check_afsspace> is a Nagios plugin for checking free space on AFS server
134 partitions.  It uses C<vos partinfo> to obtain the free space on the
135 partitions on an AFS server and will return an alert if the percentage of
136 used space exceeds a threshold.  By default, it returns a critical error if
137 the used space is over 90% and a warning if it is over 85% (changable with
138 the B<-c> and B<-w> options).
139
140 B<check_afsspace> will always print out a single line of output, giving the
141 critical errors if any, otherwise giving the warnings if any, otherwise
142 listing in an abbreviated form the percentage free space for all partitions.
143
144 =head1 OPTIONS
145
146 =over 4
147
148 =item B<-c> I<threshold>, B<--critical>=I<threshold>
149
150 Change the critical percentage threshold to I<threshold>, which should be an
151 integer percentage.  The default is 90.
152
153 =item B<-H> I<host>, B<--hostname>=I<host>
154
155 The AFS file server whose free space B<check_afsspace> should check.  This
156 option is required.
157
158 =item B<-h>, B<--help>
159
160 Print out this documentation (which is done simply by feeding the script
161 to C<perldoc -t>).
162
163 =item B<-t> I<timeout>, B<--timeout>=I<timeout>
164
165 Change the timeout for the C<vos partinfo> command.  The default timeout is
166 10 seconds.
167
168 =item B<-V>, B<--version>
169
170 Print out the version of B<check_afsspace> and quit.
171
172 =item B<-w> I<threshold>, B<--warning>=I<threshold>
173
174 Change the warning percentage threshold to I<threshold>, which should be an
175 integer percentage.  The default is 85.
176
177 =back
178
179 =head1 EXIT STATUS
180
181 B<check_afsspace> follows the standard Nagios exit status requirements.
182 This means that it will exit with status 0 if there are no problems, with
183 status 2 if there is at least one critical partition for that server, and
184 with status 1 if there are no critical partitions but at least one warning
185 partition.  For other errors, such as invalid syntax, B<check_afsspace> will
186 exit with status 3.
187
188 =head1 BUGS
189
190 The standard B<-v> verbose Nagios plugin option is not supported and should
191 be.  (For example, under B<-vv> we would want to show the actual total,
192 free, and used byte counts, not just the percentages.)
193
194 The usage message for invalid options and for the B<-h> option doesn't
195 conform to Nagios standards.
196
197 =head1 CAVEATS
198
199 This script does not use the Nagios util library or any of the defaults that
200 it provides, which makes it somewhat deficient as a Nagios plugin.  This is
201 intentional, though, since this script can be used with other monitoring
202 systems as well.  It's not clear what a good solution to this would be.
203
204 =head1 SEE ALSO
205
206 vos(1)
207
208 The current version of this and other AFS monitoring plugins for Nagios are
209 available from the AFS monitoring tools page at
210 L<http://www.eyrie.org/~eagle/software/afs-monitor/>.
211
212 =head1 AUTHORS
213
214 Originally written by Susan Feng for use with mon.  Updated by Quanah
215 Gibson-Mount to work with Nagios, and then further updated by Russ Allbery
216 <rra@stanford.edu> to support more standard options and to use a more
217 uniform coding style.
218
219 =head1 COPYRIGHT AND LICENSE
220
221 Copyright 2003, 2004 Board of Trustees, Leland Stanford Jr. University.
222
223 This program is free software; you may redistribute it and/or modify it
224 under the same terms as Perl itself.
225
226 =cut