check_disk_smb.pl 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #!/usr/bin/perl -w
  2. #
  3. #
  4. # check_disk.pl <host> <share> <user> <pass> [warn] [critical] [port]
  5. #
  6. # Nagios host script to get the disk usage from a SMB share
  7. #
  8. # Changes and Modifications
  9. # =========================
  10. # 7-Aug-1999 - Michael Anthon
  11. # Created from check_disk.pl script provided with netsaint_statd (basically
  12. # cause I was too lazy (or is that smart?) to write it from scratch)
  13. # 8-Aug-1999 - Michael Anthon
  14. # Modified [warn] and [critical] parameters to accept format of nnn[M|G] to
  15. # allow setting of limits in MBytes or GBytes. Percentage settings for large
  16. # drives is a pain in the butt
  17. # 2-May-2002 - SGhosh fix for embedded perl
  18. #
  19. #
  20. require 5.004;
  21. use POSIX;
  22. use strict;
  23. use Getopt::Long;
  24. use vars qw($opt_P $opt_V $opt_h $opt_H $opt_s $opt_W $opt_u $opt_p $opt_w $opt_c $opt_a $verbose);
  25. use vars qw($PROGNAME);
  26. use lib utils.pm ;
  27. use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
  28. sub print_help ();
  29. sub print_usage ();
  30. $PROGNAME = "check_disk_smb";
  31. $ENV{'PATH'}='';
  32. $ENV{'BASH_ENV'}='';
  33. $ENV{'ENV'}='';
  34. Getopt::Long::Configure('bundling');
  35. GetOptions
  36. ("v" => \$verbose, "verbose" => \$verbose,
  37. "P=s" => \$opt_P, "port=s" => \$opt_P,
  38. "V" => \$opt_V, "version" => \$opt_V,
  39. "h" => \$opt_h, "help" => \$opt_h,
  40. "w=s" => \$opt_w, "warning=s" => \$opt_w,
  41. "c=s" => \$opt_c, "critical=s" => \$opt_c,
  42. "p=s" => \$opt_p, "password=s" => \$opt_p,
  43. "u=s" => \$opt_u, "username=s" => \$opt_u,
  44. "s=s" => \$opt_s, "share=s" => \$opt_s,
  45. "W=s" => \$opt_W, "workgroup=s" => \$opt_W,
  46. "H=s" => \$opt_H, "hostname=s" => \$opt_H,
  47. "a=s" => \$opt_a, "address=s" => \$opt_a);
  48. if ($opt_V) {
  49. print_revision($PROGNAME,'@NP_VERSION@'); #'
  50. exit $ERRORS{'OK'};
  51. }
  52. if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
  53. my $smbclient= "$utils::PATH_TO_SMBCLIENT " ;
  54. my $smbclientoptions= $opt_P ? "-p $opt_P " : "";
  55. # Options checking
  56. ($opt_H) || ($opt_H = shift @ARGV) || usage("Host name not specified\n");
  57. my $host = $1 if ($opt_H =~ /^([-_.A-Za-z0-9 ]+\$?)$/);
  58. ($host) || usage("Invalid host: $opt_H\n");
  59. ($opt_s) || ($opt_s = shift @ARGV) || usage("Share volume not specified\n");
  60. my $share = $1 if ($opt_s =~ /^([-_.A-Za-z0-9]+\$?)$/);
  61. ($share) || usage("Invalid share: $opt_s\n");
  62. ($opt_u) || ($opt_u = shift @ARGV) || ($opt_u = "guest");
  63. my $user = $1 if ($opt_u =~ /^([-_.A-Za-z0-9\\]+)$/);
  64. ($user) || usage("Invalid user: $opt_u\n");
  65. ($opt_p) || ($opt_p = shift @ARGV) || ($opt_p = "");
  66. my $pass = $1 if ($opt_p =~ /(.*)/);
  67. $pass = "-N" if ($opt_p eq "");
  68. ($opt_w) || ($opt_w = shift @ARGV) || ($opt_w = 85);
  69. my $warn = $1 if ($opt_w =~ /^([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])$/);
  70. ($warn) || usage("Invalid warning threshold: $opt_w\n");
  71. ($opt_c) || ($opt_c = shift @ARGV) || ($opt_c = 95);
  72. my $crit = $1 if ($opt_c =~ /^([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])$/);
  73. ($crit) || usage("Invalid critical threshold: $opt_c\n");
  74. # split the type from the unit value
  75. #Check $warn and $crit for type (%/M/G) and set up for tests
  76. #P = Percent, K = KBytes
  77. my $warn_type;
  78. my $crit_type;
  79. if ($opt_w =~ /^([0-9]+)\%?$/) {
  80. $warn = "$1";
  81. $warn_type = "P";
  82. } elsif ($opt_w =~ /^([0-9]+)k$/) {
  83. $warn_type = "K";
  84. $warn = $1;
  85. } elsif ($opt_w =~ /^([0-9]+)M$/) {
  86. $warn_type = "K";
  87. $warn = $1 * 1024;
  88. } elsif ($opt_w =~ /^([0-9]+)G$/) {
  89. $warn_type = "K";
  90. $warn = $1 * 1048576;
  91. }
  92. if ($opt_c =~ /^([0-9]+)\%?$/) {
  93. $crit = "$1";
  94. $crit_type = "P";
  95. } elsif ($opt_c =~ /^([0-9]+)k$/) {
  96. $crit_type = "K";
  97. $crit = $1;
  98. } elsif ($opt_c =~ /^([0-9]+)M$/) {
  99. $crit_type = "K";
  100. $crit = $1 * 1024;
  101. } elsif ($opt_c =~ /^([0-9]+)G$/) {
  102. $crit_type = "K";
  103. $crit = $1 * 1048576;
  104. }
  105. # check if both warning and critical are percentage or size
  106. unless( ( $warn_type eq "P" && $crit_type eq "P" ) || ( $warn_type ne "P" && $crit_type ne "P" ) ){
  107. $opt_w =~ s/\%/\%\%/g;
  108. $opt_c =~ s/\%/\%\%/g;
  109. usage("Both warning and critical should be same type- warning: $opt_w critical: $opt_c \n");
  110. }
  111. # verify warning is less than critical
  112. if ( $warn_type eq "K") {
  113. unless ( $warn > $crit) {
  114. usage("Disk size: warning ($opt_w) should be greater than critical ($opt_c) \n");
  115. }
  116. }else{
  117. unless ( $warn < $crit) {
  118. $opt_w =~ s/\%/\%\%/g;
  119. $opt_c =~ s/\%/\%\%/g;
  120. usage("Percentage: warning ($opt_w) should be less than critical ($opt_c) \n");
  121. }
  122. }
  123. my $workgroup = $1 if (defined($opt_W) && $opt_W =~ /(.*)/);
  124. my $address = $1 if (defined($opt_a) && $opt_a =~ /(.*)/);
  125. # end of options checking
  126. my $state = "OK";
  127. my $answer = undef;
  128. my $res = undef;
  129. my @lines = undef;
  130. # Just in case of problems, let's not hang Nagios
  131. $SIG{'ALRM'} = sub {
  132. print "No Answer from Client\n";
  133. exit $ERRORS{"UNKNOWN"};
  134. };
  135. alarm($TIMEOUT);
  136. # Execute an "ls" on the share using smbclient program
  137. # get the results into $res
  138. if (defined($workgroup)) {
  139. if (defined($address)) {
  140. print "$smbclient " . "\/\/$host\/$share" ." $pass -W $workgroup -U $user $smbclientoptions -I $address -c ls\n" if ($verbose);
  141. $res = qx/$smbclient "\/\/$host\/$share" $pass -W $workgroup -U $user $smbclientoptions -I $address -c ls/;
  142. } else {
  143. print "$smbclient " . "\/\/$host\/$share" ." $pass -W $workgroup -U $user $smbclientoptions -c ls\n" if ($verbose);
  144. $res = qx/$smbclient "\/\/$host\/$share" $pass -W $workgroup -U $user $smbclientoptions -c ls/;
  145. }
  146. } else {
  147. if (defined($address)) {
  148. print "$smbclient " . "\/\/$host\/$share" ." $pass -U $user $smbclientoptions -I $address -c ls\n" if ($verbose);
  149. $res = qx/$smbclient "\/\/$host\/$share" $pass -U $user $smbclientoptions -I $address -c ls/;
  150. } else {
  151. print "$smbclient " . "\/\/$host\/$share" ." $pass -U $user $smbclientoptions -c ls\n" if ($verbose);
  152. $res = qx/$smbclient "\/\/$host\/$share" $pass -U $user $smbclientoptions -c ls/;
  153. }
  154. }
  155. #Turn off alarm
  156. alarm(0);
  157. #Split $res into an array of lines
  158. @lines = split /\n/, $res;
  159. #Get the last line into $_
  160. $_ = $lines[$#lines];
  161. #print "$_\n";
  162. #Process the last line to get free space.
  163. #If line does not match required regexp, return an UNKNOWN error
  164. if (/\s*(\d*) blocks of size (\d*)\. (\d*) blocks available/) {
  165. my ($avail) = ($3*$2)/1024;
  166. my ($avail_bytes) = $avail;
  167. my ($capper) = int(($3/$1)*100);
  168. my ($mountpt) = "\\\\$host\\$share";
  169. if (int($avail / 1024) > 0) {
  170. $avail = int($avail / 1024);
  171. if (int($avail /1024) > 0) {
  172. $avail = (int(($avail / 1024)*100))/100;
  173. $avail = $avail ."G";
  174. } else {
  175. $avail = $avail ."M";
  176. }
  177. } else {
  178. $avail = $avail ."K";
  179. }
  180. #print ":$warn:$warn_type:\n";
  181. #print ":$crit:$crit_type:\n";
  182. #print ":$avail:$avail_bytes:$capper:$mountpt:\n";
  183. if ((($warn_type eq "P") && (100 - $capper) < $warn) || (($warn_type eq "K") && ($avail_bytes > $warn))) {
  184. $answer = "Disk ok - $avail ($capper%) free on $mountpt\n";
  185. } elsif ((($crit_type eq "P") && (100 - $capper) < $crit) || (($crit_type eq "K") && ($avail_bytes > $crit))) {
  186. $state = "WARNING";
  187. $answer = "WARNING: Only $avail ($capper%) free on $mountpt\n";
  188. } else {
  189. $state = "CRITICAL";
  190. $answer = "CRITICAL: Only $avail ($capper%) free on $mountpt\n";
  191. }
  192. } else {
  193. $answer = "Result from smbclient not suitable\n";
  194. $state = "UNKNOWN";
  195. foreach (@lines) {
  196. if (/(Access denied|NT_STATUS_LOGON_FAILURE)/) {
  197. $answer = "Access Denied\n";
  198. $state = "CRITICAL";
  199. last;
  200. }
  201. if (/(Unknown host \w*|Connection.*failed)/) {
  202. $answer = "$1\n";
  203. $state = "CRITICAL";
  204. last;
  205. }
  206. if (/(You specified an invalid share name|NT_STATUS_BAD_NETWORK_NAME)/) {
  207. $answer = "Invalid share name \\\\$host\\$share\n";
  208. $state = "CRITICAL";
  209. last;
  210. }
  211. }
  212. }
  213. print $answer;
  214. print "$state\n" if ($verbose);
  215. exit $ERRORS{$state};
  216. sub print_usage () {
  217. print "Usage: $PROGNAME -H <host> -s <share> -u <user> -p <password>
  218. -w <warn> -c <crit> [-W <workgroup>] [-P <port>] [-a <IP>]\n";
  219. }
  220. sub print_help () {
  221. print_revision($PROGNAME,'@NP_VERSION@');
  222. print "Copyright (c) 2000 Michael Anthon/Karl DeBisschop
  223. Perl Check SMB Disk plugin for Nagios
  224. ";
  225. print_usage();
  226. print "
  227. -H, --hostname=HOST
  228. NetBIOS name of the server
  229. -s, --share=STRING
  230. Share name to be tested
  231. -W, --workgroup=STRING
  232. Workgroup or Domain used (Defaults to \"WORKGROUP\")
  233. -a, --address=IP
  234. IP-address of HOST (only necessary if HOST is in another network)
  235. -u, --user=STRING
  236. Username to log in to server. (Defaults to \"guest\")
  237. -p, --password=STRING
  238. Password to log in to server. (Defaults to an empty password)
  239. -w, --warning=INTEGER or INTEGER[kMG]
  240. Percent of used space at which a warning will be generated (Default: 85%)
  241. -c, --critical=INTEGER or INTEGER[kMG]
  242. Percent of used space at which a critical will be generated (Defaults: 95%)
  243. -P, --port=INTEGER
  244. Port to be used to connect to. Some Windows boxes use 139, others 445 (Defaults to smbclient default)
  245. If thresholds are followed by either a k, M, or G then check to see if that
  246. much disk space is available (kilobytes, Megabytes, Gigabytes)
  247. Warning percentage should be less than critical
  248. Warning (remaining) disk space should be greater than critical.
  249. ";
  250. support();
  251. }