check_disk_smb.pl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #! /usr/bin/perl -wT
  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. # $Id$
  20. #
  21. require 5.004;
  22. use POSIX;
  23. use strict;
  24. use Getopt::Long;
  25. use vars qw($opt_V $opt_h $opt_H $opt_s $opt_W $opt_u $opt_p $opt_w $opt_c $verbose);
  26. use vars qw($PROGNAME);
  27. use FindBin;
  28. use lib "$FindBin::Bin";
  29. use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
  30. sub print_help ();
  31. sub print_usage ();
  32. $ENV{'PATH'}='';
  33. $ENV{'BASH_ENV'}='';
  34. $ENV{'ENV'}='';
  35. Getopt::Long::Configure('bundling');
  36. GetOptions
  37. ("v" => \$verbose, "verbose" => \$verbose,
  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,'$Revision$'); #'
  49. exit $ERRORS{'OK'};
  50. }
  51. if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
  52. my $smbclient="/usr/bin/smbclient";
  53. my $smbclientoptions="";
  54. ($opt_H) || ($opt_H = shift) || usage("Host name not specified\n");
  55. my $host = $1 if ($opt_H =~ /([-_.A-Za-z0-9]+)/);
  56. ($host) || usage("Invalid host: $opt_H\n");
  57. ($opt_s) || ($opt_s = shift) || usage("Share volume not specified\n");
  58. my $share = $1 if ($opt_s =~ /([-_.A-Za-z0-9]+)/);
  59. ($share) || usage("Invalid share: $opt_s\n");
  60. ($opt_u) || ($opt_u = shift) || ($opt_u = "guest");
  61. my $user = $1 if ($opt_u =~ /([-_.A-Za-z0-9]+)/);
  62. ($user) || usage("Invalid user: $opt_u\n");
  63. ($opt_p) || ($opt_p = shift) || ($opt_p = "guest");
  64. my $pass = $1 if ($opt_p =~ /(.*)/);
  65. ($opt_w) || ($opt_w = shift) || ($opt_w = 85);
  66. my $warn = $1 if ($opt_w =~ /([0-9]{1,2}\%?|100\%?|[0-9]+[kmKM])+/);
  67. ($warn) || usage("Invalid warning threshold: $opt_w\n");
  68. ($opt_c) || ($opt_c = shift) || ($opt_c = 95);
  69. my $crit = $1 if ($opt_c =~ /([0-9]{1,2}\%?|100\%?|[0-9]+[kmKM])/);
  70. ($crit) || usage("Invalid critical threshold: $opt_c\n");
  71. my $workgroup = $1 if (defined($opt_W) && $opt_W =~ /(.*)/);
  72. my $state = "OK";
  73. my $answer = undef;
  74. my $res = undef;
  75. my @lines = undef;
  76. # Just in case of problems, let's not hang Nagios
  77. $SIG{'ALRM'} = sub {
  78. print "No Answer from Client\n";
  79. exit $ERRORS{"UNKNOWN"};
  80. };
  81. alarm($TIMEOUT);
  82. # Execute an "ls" on the share using smbclient program
  83. # get the results into $res
  84. if (defined($workgroup)) {
  85. $res = qx/$smbclient \/\/$host\/$share $pass -W $workgroup -U $user $smbclientoptions -c ls/;
  86. } else {
  87. $res = qx/$smbclient \/\/$host\/$share $pass -U $user $smbclientoptions -c ls/;
  88. }
  89. #Turn off alarm
  90. alarm(0);
  91. #Split $res into an array of lines
  92. @lines = split /\n/, $res;
  93. #Get the last line into $_
  94. $_ = $lines[$#lines];
  95. #print "$_\n";
  96. #Process the last line to get free space.
  97. #If line does not match required regexp, return an UNKNOWN error
  98. if (/\s*(\d*) blocks of size (\d*)\. (\d*) blocks available/) {
  99. my ($avail) = ($3*$2)/1024;
  100. my ($avail_bytes) = $avail;
  101. my ($capper) = int(($3/$1)*100);
  102. my ($mountpt) = "\\\\$host\\$share";
  103. #Check $warn and $crit for type (%/M/G) and set up for tests
  104. #P = Percent, K = KBytes
  105. my $warn_type;
  106. my $crit_type;
  107. if ($warn =~ /^([0-9]+$)/) {
  108. $warn_type = "P";
  109. } elsif ($warn =~ /^([0-9]+)k$/) {
  110. my ($warn_type) = "K";
  111. $warn = $1;
  112. } elsif ($warn =~ /^([0-9]+)M$/) {
  113. $warn_type = "K";
  114. $warn = $1 * 1024;
  115. } elsif ($warn =~ /^([0-9]+)G$/) {
  116. $warn_type = "K";
  117. $warn = $1 * 1048576;
  118. }
  119. if ($crit =~ /^([0-9]+$)/) {
  120. $crit_type = "P";
  121. } elsif ($crit =~ /^([0-9]+)k$/) {
  122. $crit_type = "K";
  123. $crit = $1;
  124. } elsif ($crit =~ /^([0-9]+)M$/) {
  125. $crit_type = "K";
  126. $crit = $1 * 1024;
  127. } elsif ($crit =~ /^([0-9]+)G$/) {
  128. $crit_type = "K";
  129. $crit = $1 * 1048576;
  130. }
  131. if (int($avail / 1024) > 0) {
  132. $avail = int($avail / 1024);
  133. if (int($avail /1024) > 0) {
  134. $avail = (int(($avail / 1024)*100))/100;
  135. $avail = $avail."G";
  136. } else {
  137. $avail = $avail."M";
  138. }
  139. } else {
  140. $avail = $avail."K";
  141. }
  142. #print ":$warn:$warn_type:\n";
  143. #print ":$crit:$crit_type:\n";
  144. #print ":$avail:$avail_bytes:$capper:$mountpt:\n";
  145. if ((($warn_type eq "P") && (100 - $capper) < $warn) || (($warn_type eq "K") && ($avail_bytes > $warn))) {
  146. $answer = "Disk ok - $avail ($capper%) free on $mountpt\n";
  147. } elsif ((($crit_type eq "P") && (100 - $capper) < $crit) || (($crit_type eq "K") && ($avail_bytes > $crit))) {
  148. $state = "WARNING";
  149. $answer = "Only $avail ($capper%) free on $mountpt\n";
  150. } else {
  151. $state = "CRITICAL";
  152. $answer = "Only $avail ($capper%) free on $mountpt\n";
  153. }
  154. } else {
  155. $answer = "Result from smbclient not suitable\n";
  156. $state = "UNKNOWN";
  157. foreach (@lines) {
  158. if (/Access denied/) {
  159. $answer = "Access Denied\n";
  160. $state = "CRITICAL";
  161. last;
  162. }
  163. if (/(Unknown host \w*)/) {
  164. $answer = "$1\n";
  165. $state = "CRITICAL";
  166. last;
  167. }
  168. if (/(You specified an invalid share name)/) {
  169. $answer = "Invalid share name \\\\$host\\$share\n";
  170. $state = "CRITICAL";
  171. last;
  172. }
  173. }
  174. }
  175. print $answer;
  176. print "$state\n" if ($verbose);
  177. exit $ERRORS{$state};
  178. sub print_usage () {
  179. print "Usage: $PROGNAME -H <host> -s <share> -u <user> -p <password>
  180. -w <warn> -c <crit> [-W <workgroup>]\n";
  181. }
  182. sub print_help () {
  183. print_revision($PROGNAME,'$Revision$');
  184. print "Copyright (c) 2000 Michael Anthon/Karl DeBisschop
  185. Perl Check SMB Disk plugin for Nagios
  186. ";
  187. print_usage();
  188. print "
  189. -H, --hostname=HOST
  190. NetBIOS name of the server
  191. -s, --share=STRING
  192. Share name to be tested
  193. -W, --workgroup=STRING
  194. Workgroup or Domain used (Defaults to \"WORKGROUP\")
  195. -u, --user=STRING
  196. Username to log in to server. (Defaults to \"guest\")
  197. -p, --password=STRING
  198. Password to log in to server. (Defaults to \"guest\")
  199. -w, --warning=INTEGER
  200. Percent of used space at which a warning will be generated (Default: 85%)
  201. -c, --critical=INTEGER
  202. Percent of used space at which a critical will be generated (Defaults: 95%)
  203. ";
  204. support();
  205. }