check_disk_smb.pl 9.1 KB

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