Code

Fix for regex input of '|', being output causing problems with Nagios' parsing of
[nagiosplug.git] / contrib / check_inodes-freebsd.pl
1 #!/usr/bin/perl
3 # check_inodes.pl for FreeBSD
4 # Designed on FreeBSD 4.6 (although this should not matter)
5 # parses df output, splits, and then takes variables
6 # df.pl -f mountpoint -w warningnumber -c critical number
7 # USE NUMBERS AND NOT PERCENTS FOR wanring and critical values
8 # -h is help
9 # -v is version
10 # Mountpoints:
11 # like / or /usr or /var (whatever you mount drives NOT the device names)
12 # Andrew Ryder - 20020804 - atr@mrcoffee.org
15 use strict;
16 use Getopt::Long;
17 use vars qw($opt_V $opt_h $opt_w $opt_c $opt_f $verbose $PROGNAME);
18 use lib "/usr/local/libexec/nagios" ;
19 use utils qw($TIMEOUT %ERRORS &print_revision &support);
21 my $df = "/bin/df";
22 my $grep = "/usr/bin/grep";
24 $PROGNAME="df.pl";
26 sub print_help ();
27 sub print_usage ();
30 $ENV{'PATH'}='';
31 $ENV{'BASH_ENV'}='';
32 $ENV{'ENV'}='';
34 Getopt::Long::Configure('bundling');
35 GetOptions
36         ("V"   => \$opt_V, "version"    => \$opt_V,
37          "h"   => \$opt_h, "help"       => \$opt_h,
38          "w=s" => \$opt_w, "warning=s"  => \$opt_w, 
39          "c=s" => \$opt_c, "critical=s" => \$opt_c,
40          "f=s" => \$opt_f, "filesystem=s" => \$opt_f);
41         
43 if ($opt_V) {
44         print_revision($PROGNAME,'$Revision: 72 $ ');
45         exit $ERRORS{'OK'};
46 }
48 if ($opt_h) {
49         print_help();
50         exit $ERRORS{'OK'};
51 }
53 ($opt_w) || ($opt_w = shift) || ($opt_w = 50);
54 my $warning = $1 if ($opt_w =~ /([0-9]+)/);
56 ($opt_c) || ($opt_c = shift) || ($opt_c = 75);
57 my $critical = $1 if ($opt_c =~ /([0-9]+)/);
59 if ($opt_c < $opt_w) {
60         print "Critical offset should be larger than warning offset\n";
61         print_usage();
62         exit $ERRORS{"UNKNOWN"};
63 }
65 ($opt_f) || ($opt_f = "/");
68 unless (-e $df) {
69         print "UNKNOWN: $df is not where df is\n";
70         exit $ERRORS{'UNKNOWN'};
71         }
73 unless (-e $grep) {
74         print "UNKNOWN: $grep is not where grep is\n";
75         exit $ERRORS{'UNKNOWN'};
76         }
78 unless (-d $opt_f) {
79         print "UNKNOWN: $opt_f is not a mount point\n"; 
80         exit $ERRORS{'UNKNOWN'};
81         }
84 my $state = $ERRORS{'UNKNOWN'};
85 my $answer;
87 open(DF, "$df -i $opt_f| $grep -v Filesystem |");
89 while (<DF>) {
90         
91                 my ($fs,$onek,$used,$avail,$capacity,$iused,$ifree,$ipercent,$mounted) = split;
92                 $ipercent =~ s/%//s;
94                         if ($ipercent > $opt_w) {
95                                 $state = $ERRORS{'WARNING'};
96                                 $answer =  "WARNING: $ipercent percent inodes free on $opt_f\n";
97                         } elsif ($ipercent > $opt_w) {
98                                 $state = $ERRORS{'CRITCAL'};
99                                 $answer =  "CRITICAL: $ipercent percent inodes free on $opt_f\n";
100                         } elsif ($ipercent < $opt_w) {
101                                 $state = $ERRORS{'OK'};
102                                 $answer = "OK: $ipercent percent inodes free on $opt_f\n";
103                         }
106 close(DF);
108 print "$answer";
109 exit $state;
111 sub print_usage () {
112         print "Usage: $PROGNAME <filesystem> [-w <warn>] [-c <crit>]\n";
113         print "Example: $PROGNAME /dev/ad0s1a -w 50 -c 75\n";
115                 
116 sub print_help () {
117         print_revision($PROGNAME,'$Revision: 72 $');
118         print "Copyright (c) 2002 Andrew Ryder\n";
119         print "\n";
120         print_usage();
121         print "\n";
122         print "<warn> = Inode Percent at which a warning message is returned. Defaults to 50.\n";
123         print "<crit> = Inode Percent at which a critical message is returned..\n        Defaults to 75.\n\n";
124         support();
126