check_disk_smb.pl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 $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. if ($opt_V) {
  48. print_revision($PROGNAME,'@NP_VERSION@'); #'
  49. exit $ERRORS{'OK'};
  50. }
  51. if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
  52. my $smbclient= "$utils::PATH_TO_SMBCLIENT " ;
  53. my $smbclientoptions= $opt_P ? "-p $opt_P " : "";
  54. # Options checking
  55. ($opt_H) || ($opt_H = shift) || usage("Host name not specified\n");
  56. my $host = $1 if ($opt_H =~ /^([-_.A-Za-z0-9]+\$?)$/);
  57. ($host) || usage("Invalid host: $opt_H\n");
  58. ($opt_s) || ($opt_s = shift) || usage("Share volume not specified\n");
  59. my $share = $1 if ($opt_s =~ /^([-_.A-Za-z0-9]+\$?)$/);
  60. ($share) || usage("Invalid share: $opt_s\n");
  61. ($opt_u) || ($opt_u = shift) || ($opt_u = "guest");
  62. my $user = $1 if ($opt_u =~ /^([-_.A-Za-z0-9\\]+)$/);
  63. ($user) || usage("Invalid user: $opt_u\n");
  64. ($opt_p) || ($opt_p = shift) || ($opt_p = "");
  65. my $pass = $1 if ($opt_p =~ /(.*)/);
  66. ($opt_w) || ($opt_w = shift) || ($opt_w = 85);
  67. my $warn = $1 if ($opt_w =~ /^([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])$/);
  68. ($warn) || usage("Invalid warning threshold: $opt_w\n");
  69. ($opt_c) || ($opt_c = shift) || ($opt_c = 95);
  70. my $crit = $1 if ($opt_c =~ /^([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])$/);
  71. ($crit) || usage("Invalid critical threshold: $opt_c\n");
  72. # split the type from the unit value
  73. #Check $warn and $crit for type (%/M/G) and set up for tests
  74. #P = Percent, K = KBytes
  75. my $warn_type;
  76. my $crit_type;
  77. if ($opt_w =~ /^([0-9]+)\%?$/) {
  78. $warn = "$1";
  79. $warn_type = "P";
  80. } elsif ($opt_w =~ /^([0-9]+)k$/) {
  81. $warn_type = "K";
  82. $warn = $1;
  83. } elsif ($opt_w =~ /^([0-9]+)M$/) {
  84. $warn_type = "K";
  85. $warn = $1 * 1024;
  86. } elsif ($opt_w =~ /^([0-9]+)G$/) {
  87. $warn_type = "K";
  88. $warn = $1 * 1048576;
  89. }
  90. if ($opt_c =~ /^([0-9]+)\%?$/) {
  91. $crit = "$1";
  92. $crit_type = "P";
  93. } elsif ($opt_c =~ /^([0-9]+)k$/) {
  94. $crit_type = "K";
  95. $crit = $1;
  96. } elsif ($opt_c =~ /^([0-9]+)M$/) {
  97. $crit_type = "K";
  98. $crit = $1 * 1024;
  99. } elsif ($opt_c =~ /^([0-9]+)G$/) {
  100. $crit_type = "K";
  101. $crit = $1 * 1048576;
  102. }
  103. # check if both warning and critical are percentage or size
  104. unless( ( $warn_type eq "P" && $crit_type eq "P" ) || ( $warn_type ne "P" && $crit_type ne "P" ) ){
  105. $opt_w =~ s/\%/\%\%/g;
  106. $opt_c =~ s/\%/\%\%/g;
  107. usage("Both warning and critical should be same type- warning: $opt_w critical: $opt_c \n");
  108. }
  109. # verify warning is less than critical
  110. if ( $warn_type eq "K") {
  111. unless ( $warn > $crit) {
  112. usage("Disk size: warning ($opt_w) should be greater than critical ($opt_c) \n");
  113. }
  114. }else{
  115. unless ( $warn < $crit) {
  116. $opt_w =~ s/\%/\%\%/g;
  117. $opt_c =~ s/\%/\%\%/g;
  118. usage("Percentage: warning ($opt_w) should be less than critical ($opt_c) \n");
  119. }
  120. }
  121. my $workgroup = $1 if (defined($opt_W) && $opt_W =~ /(.*)/);
  122. # end of options checking
  123. my $state = "OK";
  124. my $answer = undef;
  125. my $res = undef;
  126. my @lines = undef;
  127. # Just in case of problems, let's not hang Nagios
  128. $SIG{'ALRM'} = sub {
  129. print "No Answer from Client\n";
  130. exit $ERRORS{"UNKNOWN"};
  131. };
  132. alarm($TIMEOUT);
  133. # Execute an "ls" on the share using smbclient program
  134. # get the results into $res
  135. if (defined($workgroup)) {
  136. $res = qx/$smbclient \/\/$host\/$share -W $workgroup -U $user%$pass $smbclientoptions -c ls/;
  137. } else {
  138. print "$smbclient " . "\/\/$host\/$share" ." $pass -U $user $smbclientoptions -c ls\n" if ($verbose);
  139. $res = qx/$smbclient \/\/$host\/$share -U $user%$pass $smbclientoptions -c ls/;
  140. }
  141. #Turn off alarm
  142. alarm(0);
  143. #Split $res into an array of lines
  144. @lines = split /\n/, $res;
  145. #Get the last line into $_
  146. $_ = $lines[$#lines];
  147. #print "$_\n";
  148. #Process the last line to get free space.
  149. #If line does not match required regexp, return an UNKNOWN error
  150. if (/\s*(\d*) blocks of size (\d*)\. (\d*) blocks available/) {
  151. my ($avail) = ($3*$2)/1024;
  152. my ($avail_bytes) = $avail;
  153. my ($capper) = int(($3/$1)*100);
  154. my ($mountpt) = "\\\\$host\\$share";
  155. if (int($avail / 1024) > 0) {
  156. $avail = int($avail / 1024);
  157. if (int($avail /1024) > 0) {
  158. $avail = (int(($avail / 1024)*100))/100;
  159. $avail = $avail ."G";
  160. } else {
  161. $avail = $avail ."M";
  162. }
  163. } else {
  164. $avail = $avail ."K";
  165. }
  166. #print ":$warn:$warn_type:\n";
  167. #print ":$crit:$crit_type:\n";
  168. #print ":$avail:$avail_bytes:$capper:$mountpt:\n";
  169. if ((($warn_type eq "P") && (100 - $capper) < $warn) || (($warn_type eq "K") && ($avail_bytes > $warn))) {
  170. $answer = "Disk ok - $avail ($capper%) free on $mountpt\n";
  171. } elsif ((($crit_type eq "P") && (100 - $capper) < $crit) || (($crit_type eq "K") && ($avail_bytes > $crit))) {
  172. $state = "WARNING";
  173. $answer = "WARNING: Only $avail ($capper%) free on $mountpt\n";
  174. } else {
  175. $state = "CRITICAL";
  176. $answer = "CRITICAL: Only $avail ($capper%) free on $mountpt\n";
  177. }
  178. } else {
  179. $answer = "Result from smbclient not suitable\n";
  180. $state = "UNKNOWN";
  181. foreach (@lines) {
  182. if (/(Access denied|NT_STATUS_LOGON_FAILURE)/) {
  183. $answer = "Access Denied\n";
  184. $state = "CRITICAL";
  185. last;
  186. }
  187. if (/(Unknown host \w*|Connection.*failed)/) {
  188. $answer = "$1\n";
  189. $state = "CRITICAL";
  190. last;
  191. }
  192. if (/(You specified an invalid share name|NT_STATUS_BAD_NETWORK_NAME)/) {
  193. $answer = "Invalid share name \\\\$host\\$share\n";
  194. $state = "CRITICAL";
  195. last;
  196. }
  197. }
  198. }
  199. print $answer;
  200. print "$state\n" if ($verbose);
  201. exit $ERRORS{$state};
  202. sub print_usage () {
  203. print "Usage: $PROGNAME -H <host> -s <share> -u <user> -p <password>
  204. -w <warn> -c <crit> [-W <workgroup>] [-P <port>]\n";
  205. }
  206. sub print_help () {
  207. print_revision($PROGNAME,'@NP_VERSION@');
  208. print "Copyright (c) 2000 Michael Anthon/Karl DeBisschop
  209. Perl Check SMB Disk plugin for Nagios
  210. ";
  211. print_usage();
  212. print "
  213. -H, --hostname=HOST
  214. NetBIOS name of the server
  215. -s, --share=STRING
  216. Share name to be tested
  217. -W, --workgroup=STRING
  218. Workgroup or Domain used (Defaults to \"WORKGROUP\")
  219. -u, --user=STRING
  220. Username to log in to server. (Defaults to \"guest\")
  221. -p, --password=STRING
  222. Password to log in to server. (Defaults to an empty password)
  223. -w, --warning=INTEGER or INTEGER[kMG]
  224. Percent of used space at which a warning will be generated (Default: 85%)
  225. -c, --critical=INTEGER or INTEGER[kMG]
  226. Percent of used space at which a critical will be generated (Defaults: 95%)
  227. -P, --port=INTEGER
  228. Port to be used to connect to. Some Windows boxes use 139, others 445 (Defaults to smbclient default)
  229. If thresholds are followed by either a k, M, or G then check to see if that
  230. much disk space is available (kilobytes, Megabytes, Gigabytes)
  231. Warning percentage should be less than critical
  232. Warning (remaining) disk space should be greater than critical.
  233. ";
  234. support();
  235. }