check_disk_smb.pl 6.4 KB

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