-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_smb_share
executable file
·206 lines (188 loc) · 5.38 KB
/
check_smb_share
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/bash
# Check for sharename on SMB with nagios
# by Gerrit Doornenbal (g.doornenbal@gmail.com)
#
# 2017-12-27
# added smb.conf option to disable spnego(iation) for some old samba servers.
#
# 2017-02-24
# fixed login problem for MS servers
# added option to check inside directories
# added option to check for number of existing files
# changed filecheck option to search using wildcards
# added option to check upon file age, older than .. seconds.
# added option to store login credentials inside a file.
#
# 2011-11-23
# added file check option, to check if share is really available
# Modified version of check_smb_share byMichael Hodges <michael@va.com.au> 2011-03-04
# Modified version of check_smb by Dave Love <fx@gnu.org>
REVISION=1.1
PROGNAME=`/usr/bin/basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
help () {
echo "\
Nagios plugin to check SMB Share.
Usage:
$PROGNAME -H <host> -s <sharename> [-d <dirname>] [-f <filename>] [-u <user>] [-p <password>] [-n] [-v] [-h --help] [-V --version]
-H hostname of the SMB host
-s sharename to look for
-f file- or directoryname to look for. You can use partial filenames, do not give a * (wildcard).
-d directory name to look for files or directory
-u username used to login. if no username is supplied, 'guest' wil be used.
for domain login use -u <domainname>/<username>
-o disable spnego(tiaton).
-p password for the username supplied.
-l Logincredential file location. Format of file is:
username=<DOMAIN>/<username>
password=<password> (escape special characters with a \)
-n number of files expected. Default is 1 or more.
-a age of file in seconds. When you're checking file's which should be away in specified time, you can set max age here.
-v Verbose output, comes handy when troubleshooting errors with smbclient.
--help this help message
examples:
$PROGNAME -H server.domain.com -s yourshare
Check for the existence of 'yourshare' on server.domain.com.
$PROGNAME -H server.domain.com -s yourshare -u DOMAIN/user -p yourpassword -f testfile.txt
This line looks for *testfile.txt files in \server.domain.com\yourshare, and gives OK when it exist.
$PROGNAME -H server.domain.com -s yourshare -l /path/to/pwfile -f .xml -d yourdir -n 0
This line looks for *.xml files in \server.domain.com\yourshare\yourdir, and gives OK when none exist.
"
}
if [ $# -lt 1 ]; then
help
exit $STATE_UNKNOWN
fi
user="guest"
password=""
nospnego=""
# Read all command-line options
while test -n "$1"; do
case "$1" in
--help | -h)
help
exit $STATE_OK;;
--version | -V)
echo $PROGNAME $REVISION
exit $STATE_OK;;
-H)
shift
host="$1";;
-s)
shift
share="$1";;
-d)
shift
dir="$1";;
-f)
shift
file="$1";;
-n)
shift
count="$1";;
-a)
shift
age="$1";;
-u)
shift
user="$1";;
-p)
shift
password="$1";;
-l)
shift
pwfile="$1";;
-v)
verbose=1;;
-o)
nospnego=" --option=clientusespnego=no";;
*)
help; exit $STATE_UNKNOWN;;
esac
shift
done
if [ ! $share ]; then
help
exit $STATE_UNKNOWN
fi
if [ $pwfile ]; then
source $pwfile
if [ $verbose ]; then
echo pwfile: username: $user pass: $password
fi
fi
#First test for share existence.
if [ ! $password ]; then
login="$user% -N"
else
login="$user%$password"
fi
command="smbclient -U $login -L $host$nospnego"
result=`$command 2>&1`
if [ $verbose ]; then
echo command: $command
echo result : $result
echo
fi
stdout="SMB share found: "`echo "$result" | grep "$share" | head -n 1 | { read first _ ; echo $first ; }`
errout="SMB Sharename "$share" NOT found on "$host
testresult=`echo "$stdout" | grep "$share" | wc -l`
#check to test file on share
if [ $file ]; then
if [ $dir ]; then
dircheck="-D $dir "
dirshare="\\"$dir
fi
command="smbclient -U "$user"%"$password"$nospnego //"$host"/"$share" "$dircheck"-c \"ls *"$file"*\""
result=`$command 2>&1`
testresult=`echo "$result" | grep "$file" | wc -l `
#check timestamp age
resulttime=`echo "$result" | grep "$file" | head -n 1 | tr -s " " | cut -d " " -f 5-9`
filetime=`date --date="$resulttime" +%s`
time=`date +%s`
fileage=$((time-filetime))
if [ $verbose ]; then
echo command: $command
echo result : $result
echo Current time: $time Filetime: $filetime Fileage: $fileage $age
fi
#Do the test for number of files.
if [ $count ]; then
if [ $count -eq $testresult ]; then
stdout="SMB file "$file" found "$testresult" times on "$share$dirshare
testresult=1
else
if [ $age ]; then
if [ $age -gt $fileage ]; then
stdout="SMB file "$file" found ($fileage sec old) on "$share$dirshare
testresult=1
else
errout="SMB file "$file" found ($fileage sec old) on "$share$dirshare
testresult=0
fi
else
errout="SMB file "$file" found "$testresult" times on "$share$dirshare
testresult=0
fi
fi
else
#Do the test for existence of the file
if [ $testresult -gt 1 ]; then
stdout="SMB file "$file" found "$testresult" times on "$share$dirshare
else
stdout="SMB file "$file" found on "$share$dirshare
fi
errout="SMB file "$file" NOT found on "$share$dirshare
fi
fi
if [ $testresult != 0 ]; then
echo "OK "$stdout
exit $STATE_OK
else
echo "CRITICAL "$errout
exit $STATE_CRITICAL
fi