git.lirion.de

Of git, get, and gud

summaryrefslogtreecommitdiffstats
path: root/nagios-plugins-contrib-24.20190301~bpo9+1/check_nfsmounts/check_nfsmounts
blob: fbfb1f8f2244759c06276246ac75cd73d3822002 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/perl
# vim: ts=2 sts=2 sw=2:et ai:
#
# Usage: check_nfsmounts [ -t nfs timeout ] [ -w ]
# Description: determines whether there are stale NFS mounts on the host.
# Author: Clint Byrum <clint@adicio.com>
#
#    Copyright 2007 Adicio, Inc.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
#

use utils qw{$TIMEOUT %ERRORS};
use Time::HiRes qw{time alarm};
use Getopt::Long;
use strict;

my $version="1.0";

my $nfs_timeout=$TIMEOUT;
my $nfs_warn=-1;
my $writemode=0;
my $help=0;

sub usage {
  print STDERR "NFS UNKNOWN: version $version, check_nfs_client [ --nfs-critical|-c seconds ]\n";
  print STDERR "             [ --nfs-warning seconds ][ --writemode|-w ]\n";
  exit $ERRORS{'UNKNOWN'};
}

if(!GetOptions('nfs-timeout|nfstimeout|critical|c|t=f' => \$nfs_timeout,
               'nfs-warning=f' => \$nfs_warn,
               'writemode|write|w' => \$writemode,
               'help' => \$help,
          )) {
  &usage;
}

if($help) {
  &usage;
}

if($nfs_timeout <= 0) {
  print STDERR "timeout must be greater than 0\n";
  &usage;
}

our $dir; # Because its a signal handler, we have to 
sub alarm_handler {
  print "NFS CRITICAL:  Stale NFS mount point - $dir.\n";
  exit $ERRORS{'CRITICAL'};
}

sub bad_mount {
  my $mountpoint=shift();
  my $emsg=shift();
  print "NFS CRITICAL: cannot operate on mount point $mountpoint. [$emsg]\n";
  exit $ERRORS{'CRITICAL'};
}

#my @dirs = `mount | grep " type nfs " | awk '{print \$3}'`;
if(!open MTAB,"< /etc/mtab") {
  print "NFS UNKNOWN: could not open mtab.\n";
  exit $ERRORS{'UNKNOWN'};
}

my @dirs=();
my %mountmodes=();
while(my $line=<MTAB>) {
  if($line =~ /^[^ ]+ [^ ]+ nfs /) {
    my @fields=split(/\s+/,$line);
    my $mountpoint=$fields[1];
    push(@dirs,$mountpoint);
    #my %modes=split(/,/,$fields[3]);
    my $modes = {};
    foreach my $mode (split(/,/,$fields[3])) {
      $modes->{$mode}=1;
    }
    $mountmodes{$mountpoint}=$modes;
  }
}
close MTAB;

if(@dirs < 1) {
  print "NFS OK: no NFS mounts found.\n";
  exit $ERRORS{'OK'};
}

my @ages=();
my @warnings=();
foreach $dir (@dirs) {
  chomp $dir;
  $SIG{ALRM} = \&alarm_handler;
  my $start=time;
  my $pid=fork;
  if($pid==0) {
    chdir $dir or &bad_mount($dir,$!);
    if($writemode and exists($mountmodes{$dir}->{"rw"})) {
      open X,"> $dir/.nfscheck" or exit $?;
      print X $ENV{HOSTNAME}."\n".localtime()."\n"; # XXX Full disk may fail..
      close X or exit $?;
    }
    exit 0;
  } else {
    alarm $nfs_timeout;
    waitpid $pid,0;
    if($?) {
      &bad_mount($dir,$?);
    };
    alarm 0;
  }
  my $age=time()-$start;
  if($nfs_warn > 0 and $age > $nfs_warn) {
    push(@warnings,sprintf("$dir took %7.5f to complete all operations ",$age));
  }
  push(@ages,$age);
}

my $x=0;
my $agetot=0;
my $maxage=0;
foreach my $age (@ages) {
  $agetot+=$age;
  if($age > $maxage) {
    $maxage=$age;
  }
  $x++;
}
my $avgage=$agetot/$x;

my $perfdata=sprintf("maxtime=%9.7f;avgtime=%9.7f;mountpoints=$x",$maxage,$avgage);

if(@warnings) {
  print "NFS WARNING: @warnings|$perfdata\n";
  exit $ERRORS{'WARNING'};
}
  
printf "NFS OK: $x mount points avg of %7.5f secs, max %7.5f secs.|$perfdata\n",$avgage,$maxage,$maxage,$avgage;
exit $ERRORS{'OK'};