Logoj0ke.net Open Build Service > Projects > server:monitoring > nagios-plugins-nfs > check_nfsmounts
Sign Up | Log In

File check_nfsmounts of Package nagios-plugins-nfs

x
 
1
#!/usr/bin/perl
2
# vim: ts=2 sts=2 sw=2:et ai:
3
#
4
# Usage: check_nfsmounts [ -t nfs timeout ] [ -w ]
5
# Description: determines whether there are stale NFS mounts on the host.
6
# Author: Clint Byrum <clint@adicio.com>
7
#
8
#    Copyright 2007 Adicio, Inc.
9
#
10
#    This program is free software; you can redistribute it and/or modify
11
#    it under the terms of the GNU General Public License as published by
12
#    the Free Software Foundation; either version 3 of the License, or
13
#    (at your option) any later version.
14
#
15
#    This program is distributed in the hope that it will be useful,
16
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
#    GNU General Public License for more details.
19
#
20
#    You should have received a copy of the GNU General Public License
21
#    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
22
#
23
24
use lib "/usr/lib/nagios/plugins";
25
use utils qw{$TIMEOUT %ERRORS};
26
use Time::HiRes qw{time alarm};
27
use Getopt::Long;
28
use strict;
29
30
my $version="1.0";
31
32
my $nfs_timeout=$TIMEOUT;
33
my $nfs_warn=-1;
34
my $writemode=0;
35
my $help=0;
36
37
sub usage {
38
  print STDERR "NFS UNKNOWN: version $version, check_nfs_client [ --nfs-critical|-c seconds ]\n";
39
  print STDERR "             [ --nfs-warning seconds ][ --writemode|-w ]\n";
40
  exit $ERRORS{'UNKNOWN'};
41
}
42
43
if(!GetOptions('nfs-timeout|nfstimeout|critical|c|t=f' => \$nfs_timeout,
44
               'nfs-warning=f' => \$nfs_warn,
45
               'writemode|write|w' => \$writemode,
46
               'help' => \$help,
47
          )) {
48
  &usage;
49
}
50
51
if($help) {
52
  &usage;
53
}
54
55
if($nfs_timeout <= 0) {
56
  print STDERR "timeout must be greater than 0\n";
57
  &usage;
58
}
59
60
our $dir; # Because its a signal handler, we have to 
61
sub alarm_handler {
62
  print "NFS CRITICAL:  Stale NFS mount point - $dir.\n";
63
  exit $ERRORS{'CRITICAL'};
64
}
65
66
sub bad_mount {
67
  my $mountpoint=shift();
68
  my $emsg=shift();
69
  print "NFS CRITICAL: cannot operate on mount point $mountpoint. [$emsg]\n";
70
  exit $ERRORS{'CRITICAL'};
71
}
72
73
#my @dirs = `mount | grep " type nfs " | awk '{print \$3}'`;
74
if(!open MTAB,"< /etc/mtab") {
75
  print "NFS UNKNOWN: could not open mtab.\n";
76
  exit $ERRORS{'UNKNOWN'};
77
}
78
79
my @dirs=();
80
my %mountmodes=();
81
while(my $line=<MTAB>) {
82
  if($line =~ /^[^ ]+ [^ ]+ nfs /) {
83
    my @fields=split(/\s+/,$line);
84
    my $mountpoint=$fields[1];
85
    push(@dirs,$mountpoint);
86
    #my %modes=split(/,/,$fields[3]);
87
    my $modes = {};
88
    foreach my $mode (split(/,/,$fields[3])) {
89
      $modes->{$mode}=1;
90
    }
91
    $mountmodes{$mountpoint}=$modes;
92
  }
93
}
94
close MTAB;
95
96
if(@dirs < 1) {
97
  print "NFS OK: no NFS mounts found.\n";
98
  exit $ERRORS{'OK'};
99
}
100
101
my @ages=();
102
my @warnings=();
103
foreach $dir (@dirs) {
104
  chomp $dir;
105
  $SIG{ALRM} = \&alarm_handler;
106
  my $start=time;
107
  my $pid=fork;
108
  if($pid==0) {
109
    chdir $dir or &bad_mount($dir,$!);
110
    if($writemode and exists($mountmodes{$dir}->{"rw"})) {
111
      open X,"> $dir/.nfscheck" or exit $?;
112
      print X $ENV{HOSTNAME}."\n".localtime()."\n"; # XXX Full disk may fail..
113
      close X or exit $?;
114
    }
115
    exit 0;
116
  } else {
117
    alarm $nfs_timeout;
118
    waitpid $pid,0;
119
    if($?) {
120
      &bad_mount($dir,$?);
121
    };
122
    alarm 0;
123
  }
124
  my $age=time()-$start;
125
  if($nfs_warn > 0 and $age > $nfs_warn) {
126
    push(@warnings,sprintf("$dir took %7.5f to complete all operations ",$age));
127
  }
128
  push(@ages,$age);
129
}
130
131
my $x=0;
132
my $agetot=0;
133
my $maxage=0;
134
foreach my $age (@ages) {
135
  $agetot+=$age;
136
  if($age > $maxage) {
137
    $maxage=$age;
138
  }
139
  $x++;
140
}
141
my $avgage=$agetot/$x;
142
143
my $perfdata=sprintf("maxtime=%9.7f;avgtime=%9.7f;mountpoints=$x",$maxage,$avgage);
144
145
if(@warnings) {
146
  print "NFS WARNING: @warnings|$perfdata\n";
147
  exit $ERRORS{'WARNING'};
148
}
149
  
150
printf "NFS OK: $x mount points avg of %7.5f secs, max %7.5f secs.|$perfdata\n",$avgage,$maxage,$maxage,$avgage;
151
exit $ERRORS{'OK'};
152