check_disk_smb.pl 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. # Options checking
  55. ($opt_H) || ($opt_H = shift @ARGV) || 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 @ARGV) || 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. defined($opt_u) || ($opt_u = shift @ARGV) || ($opt_u = "guest");
  62. my $user = $1 if ($opt_u =~ /^([-_.A-Za-z0-9\\]*)$/);
  63. defined($user) || usage("Invalid user: $opt_u\n");
  64. defined($opt_p) || ($opt_p = shift @ARGV) || ($opt_p = "");
  65. my $pass = $1 if ($opt_p =~ /(.*)/);
  66. ($opt_w) || ($opt_w = shift @ARGV) || ($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 @ARGV) || ($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. # Execute the given command line and return anything it writes to STDOUT and/or
  73. # STDERR. (This might be useful for other plugins, too, so it should possibly
  74. # be moved to utils.pm.)
  75. sub output_and_error_of {
  76. local *CMD;
  77. local $/ = undef;
  78. my $pid = open CMD, "-|";
  79. if (defined($pid)) {
  80. if ($pid) {
  81. return <CMD>;
  82. } else {
  83. open STDERR, ">&STDOUT" and exec @_;
  84. exit(1);
  85. }
  86. }
  87. return undef;
  88. }
  89. # split the type from the unit value
  90. #Check $warn and $crit for type (%/M/G) and set up for tests
  91. #P = Percent, K = KBytes
  92. my $warn_type;
  93. my $crit_type;
  94. if ($opt_w =~ /^([0-9]+)\%?$/) {
  95. $warn = "$1";
  96. $warn_type = "P";
  97. } elsif ($opt_w =~ /^([0-9]+)k$/) {
  98. $warn_type = "K";
  99. $warn = $1;
  100. } elsif ($opt_w =~ /^([0-9]+)M$/) {
  101. $warn_type = "K";
  102. $warn = $1 * 1024;
  103. } elsif ($opt_w =~ /^([0-9]+)G$/) {
  104. $warn_type = "K";
  105. $warn = $1 * 1048576;
  106. }
  107. if ($opt_c =~ /^([0-9]+)\%?$/) {
  108. $crit = "$1";
  109. $crit_type = "P";
  110. } elsif ($opt_c =~ /^([0-9]+)k$/) {
  111. $crit_type = "K";
  112. $crit = $1;
  113. } elsif ($opt_c =~ /^([0-9]+)M$/) {
  114. $crit_type = "K";
  115. $crit = $1 * 1024;
  116. } elsif ($opt_c =~ /^([0-9]+)G$/) {
  117. $crit_type = "K";
  118. $crit = $1 * 1048576;
  119. }
  120. # check if both warning and critical are percentage or size
  121. unless( ( $warn_type eq "P" && $crit_type eq "P" ) || ( $warn_type ne "P" && $crit_type ne "P" ) ){
  122. $opt_w =~ s/\%/\%\%/g;
  123. $opt_c =~ s/\%/\%\%/g;
  124. usage("Both warning and critical should be same type- warning: $opt_w critical: $opt_c \n");
  125. }
  126. # verify warning is less than critical
  127. if ( $warn_type eq "K") {
  128. unless ( $warn > $crit) {
  129. usage("Disk size: warning ($opt_w) should be greater than critical ($opt_c) \n");
  130. }
  131. }else{
  132. unless ( $warn < $crit) {
  133. $opt_w =~ s/\%/\%\%/g;
  134. $opt_c =~ s/\%/\%\%/g;
  135. usage("Percentage: warning ($opt_w) should be less than critical ($opt_c) \n");
  136. }
  137. }
  138. my $workgroup = $1 if (defined($opt_W) && $opt_W =~ /(.*)/);
  139. my $address = $1 if (defined($opt_a) && $opt_a =~ /(.*)/);
  140. # end of options checking
  141. my $state = "OK";
  142. my $answer = undef;
  143. my $res = undef;
  144. my $perfdata = "";
  145. my @lines = undef;
  146. # Just in case of problems, let's not hang Nagios
  147. $SIG{'ALRM'} = sub {
  148. print "No Answer from Client\n";
  149. exit $ERRORS{"UNKNOWN"};
  150. };
  151. alarm($TIMEOUT);
  152. # Execute a "du" on the share using smbclient program
  153. # get the results into $res
  154. my @cmd = (
  155. $smbclient,
  156. "//$host/$share",
  157. "-U", "$user%$pass",
  158. defined($workgroup) ? ("-W", $workgroup) : (),
  159. defined($address) ? ("-I", $address) : (),
  160. defined($opt_P) ? ("-p", $opt_P) : (),
  161. "-c", "du"
  162. );
  163. print join(" ", @cmd) . "\n" if ($verbose);
  164. $res = output_and_error_of(@cmd) or exit $ERRORS{"UNKNOWN"};
  165. #Turn off alarm
  166. alarm(0);
  167. #Split $res into an array of lines
  168. @lines = split /\n/, $res;
  169. #Get the last line into $_
  170. $_ = $lines[$#lines-1];
  171. #print "$_\n";
  172. #Process the last line to get free space.
  173. #If line does not match required regexp, return an UNKNOWN error
  174. if (/\s*(\d*) blocks of size (\d*)\. (\d*) blocks available/) {
  175. my ($avail_bytes) = $3 * $2;
  176. my ($total_bytes) = $1 * $2;
  177. my ($occupied_bytes) = $1 * $2 - $avail_bytes;
  178. my ($avail) = $avail_bytes/1024;
  179. my ($capper) = int(($3/$1)*100);
  180. my ($mountpt) = "\\\\$host\\$share";
  181. # TODO : why is the kB the standard unit for args ?
  182. my ($warn_bytes) = $total_bytes - $warn * 1024;
  183. if ($warn_type eq "P") {
  184. $warn_bytes = $warn * $1 * $2 / 100;
  185. }
  186. my ($crit_bytes) = $total_bytes - $crit * 1024;
  187. if ($crit_type eq "P") {
  188. $crit_bytes = $crit * $1 * $2 / 100;
  189. }
  190. if (int($avail / 1024) > 0) {
  191. $avail = int($avail / 1024);
  192. if (int($avail /1024) > 0) {
  193. $avail = (int(($avail / 1024)*100))/100;
  194. $avail = $avail ."G";
  195. } else {
  196. $avail = $avail ."M";
  197. }
  198. } else {
  199. $avail = $avail ."K";
  200. }
  201. #print ":$warn:$warn_type:\n";
  202. #print ":$crit:$crit_type:\n";
  203. #print ":$avail:$avail_bytes:$capper:$mountpt:\n";
  204. $perfdata = "'" . $share . "'=" . $occupied_bytes . 'B;'
  205. . $warn_bytes . ';'
  206. . $crit_bytes . ';'
  207. . '0;'
  208. . $total_bytes;
  209. if ($occupied_bytes > $crit_bytes) {
  210. $state = "CRITICAL";
  211. $answer = "CRITICAL: Only $avail ($capper%) free on $mountpt";
  212. } elsif ( $occupied_bytes > $warn_bytes ) {
  213. $state = "WARNING";
  214. $answer = "WARNING: Only $avail ($capper%) free on $mountpt";
  215. } else {
  216. $answer = "Disk ok - $avail ($capper%) free on $mountpt";
  217. }
  218. } else {
  219. $answer = "Result from smbclient not suitable";
  220. $state = "UNKNOWN";
  221. foreach (@lines) {
  222. if (/(Access denied|NT_STATUS_LOGON_FAILURE|NT_STATUS_ACCESS_DENIED)/) {
  223. $answer = "Access Denied";
  224. $state = "CRITICAL";
  225. last;
  226. }
  227. if (/(Unknown host \w*|Connection.*failed)/) {
  228. $answer = "$1";
  229. $state = "CRITICAL";
  230. last;
  231. }
  232. if (/(You specified an invalid share name|NT_STATUS_BAD_NETWORK_NAME)/) {
  233. $answer = "Invalid share name \\\\$host\\$share";
  234. $state = "CRITICAL";
  235. last;
  236. }
  237. }
  238. }
  239. print $answer;
  240. print " | " . $perfdata if ($perfdata);
  241. print "\n";
  242. print "$state\n" if ($verbose);
  243. exit $ERRORS{$state};
  244. sub print_usage () {
  245. print "Usage: $PROGNAME -H <host> -s <share> -u <user> -p <password>
  246. -w <warn> -c <crit> [-W <workgroup>] [-P <port>] [-a <IP>]\n";
  247. }
  248. sub print_help () {
  249. print_revision($PROGNAME,'@NP_VERSION@');
  250. print "Copyright (c) 2000 Michael Anthon/Karl DeBisschop
  251. Perl Check SMB Disk plugin for Nagios
  252. ";
  253. print_usage();
  254. print "
  255. -H, --hostname=HOST
  256. NetBIOS name of the server
  257. -s, --share=STRING
  258. Share name to be tested
  259. -W, --workgroup=STRING
  260. Workgroup or Domain used (Defaults to \"WORKGROUP\")
  261. -a, --address=IP
  262. IP-address of HOST (only necessary if HOST is in another network)
  263. -u, --user=STRING
  264. Username to log in to server. (Defaults to \"guest\")
  265. -p, --password=STRING
  266. Password to log in to server. (Defaults to an empty password)
  267. -w, --warning=INTEGER or INTEGER[kMG]
  268. Percent of used space at which a warning will be generated (Default: 85%)
  269. -c, --critical=INTEGER or INTEGER[kMG]
  270. Percent of used space at which a critical will be generated (Defaults: 95%)
  271. -P, --port=INTEGER
  272. Port to be used to connect to. Some Windows boxes use 139, others 445 (Defaults to smbclient default)
  273. If thresholds are followed by either a k, M, or G then check to see if that
  274. much disk space is available (kilobytes, Megabytes, Gigabytes)
  275. Warning percentage should be less than critical
  276. Warning (remaining) disk space should be greater than critical.
  277. ";
  278. support();
  279. }