check_disk_smb.pl 7.5 KB

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