-
Notifications
You must be signed in to change notification settings - Fork 2
/
check-mount.pl
executable file
·141 lines (134 loc) · 5.07 KB
/
check-mount.pl
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
#!/usr/bin/perl
#
# check_mount.pl - checks various aspects of mountpoints
#
# usage: check_mount.pl [check_type] [options]
#
############################################################################
## ##
## 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/>. ##
## ##
## © 2013 Jesse Wiley <jesse.wiley.viacom.com ##
## ##
############################################################################
use strict;
use Switch;
my $debug = 0 ;
my $check_type = shift ;
my $hostname = `hostname` ;
chomp($hostname) ;
my @options = @ARGV ;
print "$check_type @options\n" if ($debug) ;
switch ($check_type) {
case "space" {
my ( $mount, $warn, $critical ) = @options ;
open ( DF, "df -kP $mount | grep -v Filesystem |" ) ;
while ( my $dfline = <DF> ) {
chomp ( $dfline ) ;
print STDERR "$dfline\n" if ($debug) ;
if ( $dfline =~ /.*\s+$mount$/ ) {
my ($device, $kfree, $pctused) = ("", 0, 100) ;
print "$dfline : matched\n" if ($debug) ;
$dfline =~ /^(\S+).*\s+(\S+)\s+(\S+)\%\s+$mount$/ ;
($device, $kfree, $pctused) = ($1, $2, $3) ;
if ( ( 100 - $pctused ) <= $critical ) {
print "critical: $device on $mount - $pctused% used, ${kfree}KB free\n" ;
exit (2) ;
} elsif ( ( 100 - $pctused ) <= $warn ) {
print "warning: $device on $mount - $pctused% used, ${kfree}KB free\n" ;
exit (1) ;
} elsif ( ( 100 - $pctused ) > $warn ) {
print "ok: $device on $mount - $pctused% used, ${kfree}KB free\n" ;
exit (0) ;
} else {
print "critical: unknown condition\n" ;
exit (2) ;
}
}
}
print "critical: mount $mount not found\n" ;
exit (2) ;
}
case "nfs" {
my %wantopt ;
my ( $remote, $mount, $wantoptions ) = @options ;
my $wrongopt = 0 ;
my @missedopt = () ;
my @wantoptions = split(/,/, $wantoptions) ;
foreach my $wantopt ( @wantoptions ) {
$wantopt{$wantopt} = 1 ;
}
open ( MOUNT, "mount | grep 'type nfs' |" ) ;
while ( my $mountline = <MOUNT> ) {
if ( $mountline =~ /^$remote\s+on\s+$mount\s+.*\((.*)addr/ ) {
# test for stale NFS mount.
open ( DF, "df -kP $mount 2>&1 && echo $? |" ) || die "unable to run DF -- $!\n";
my $ll = ''; my @stdout;
while(<DF>) {
chomp;
push(@stdout, $_);
$ll = $_;
}
close(DF);
if ( $ll =~ /Stale/ ) {
print "CRITICAL: NFS mount: $mount is STALE\n";
exit(2);
}
# this has been tested, and it works.
my @realoptions = split(/,/, $1) ;
if ( $wantopt{rw} == 1 ) {
if ( -d "$mount/.nfs" ) {
if ( system("touch $mount/.nfs/nfstest-$hostname &> /dev/null && rm -f $mount/.nfs/nfstest-$hostname &> /dev/null") ) {
print "critical: $remote mounted on $mount rw, .nfs directory found, not writeable\n";
exit (2) ;
}
} else {
if ( system("grep isteamsite $mount &> /dev/null") ) {
my $realopt = join(',', @realoptions) ;
print "ok: $remote mounted on $mount with options $realopt\n" ;
exit (0);
} else {
print "critical: $remote mounted on $mount rw, but no .nfs directory\n" ;
exit (1) ;
}
}
}
foreach my $realopt ( @realoptions ) {
$wantopt{$realopt} = 0 ;
}
foreach my $finalopt ( keys %wantopt ) {
if ( $wantopt{$finalopt} == 1 ) {
push ( @missedopt, $finalopt ) ;
$wrongopt = 1 ;
}
}
if ( $wrongopt ) {
my $missedopt = join(',', @missedopt) ;
print "critical: $remote mounted on $mount without options $missedopt\n" ;
exit (2) ;
} else {
my $realopt = join(',', @realoptions) ;
print "ok: $remote mounted on $mount with options $realopt\n" ;
exit (0) ;
}
}
}
print "critical: $remote not mounted on $mount\n" ;
exit (2) ;
}
else {
print "unknown: check command not defined correctly! USAGE: check_mount.pl [nfs\|space] OPTIONS\n";
exit (3) ;
}
}