Advertisement
Guest User

Untitled

a guest
Oct 5th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.44 KB | None | 0 0
  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-- SSH 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. # 18-AUG-2011 - Mariusz Derela
  19. # Modified to work with SSH
  20. #
  21.  
  22. require 5.004;
  23. use POSIX;
  24. use strict;
  25. use Getopt::Long;
  26. use Net::SSH qw(sshopen2);
  27. use vars qw($opt_P $opt_V $opt_h $opt_H $opt_d $opt_W $opt_u $opt_p $opt_w $opt_c $verbose);
  28. use vars qw($PROGNAME);
  29. use lib "/usr/share/nagios/libexec"  ;
  30. use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
  31.  
  32. sub print_help ();
  33. sub print_usage ();
  34.  
  35. $PROGNAME = "check_remote_disk";
  36.  
  37.  
  38. Getopt::Long::Configure('bundling');
  39. GetOptions
  40.       ("v"  => \$verbose, "verbose"    => \$verbose,
  41.        "P=s" => \$opt_P, "port=s"    => \$opt_P,
  42.        "V"  => \$opt_V, "version"    => \$opt_V,
  43.        "h"  => \$opt_h, "help"      => \$opt_h,
  44.        "w=s" => \$opt_w, "warning=s"  => \$opt_w,
  45.        "c=s" => \$opt_c, "critical=s" => \$opt_c,
  46.        "u=s" => \$opt_u, "username=s" => \$opt_u,
  47.        "d=s" => \$opt_d, "directory=s"    => \$opt_d,
  48.        "H=s" => \$opt_H, "hostname=s" => \$opt_H);
  49.  
  50. if ($opt_V) {
  51.       print_revision($PROGNAME,'2.1'); #'
  52.       exit $ERRORS{'OK'};
  53. }
  54.  
  55. if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
  56.  
  57.  
  58.  
  59. # Options checking
  60.  
  61. ($opt_H) || ($opt_H = shift) || usage("Host name not specified\n");
  62. my $host = $1 if ($opt_H =~ /^([-_.A-Za-z0-9]+\$?)$/);
  63. ($host) || usage("Invalid host: $opt_H\n");
  64.  
  65. ($opt_d) || ($opt_d = shift) || usage("Share volume not specified\n");
  66. my $share = $opt_d;
  67. ($share) || usage("Invalid share: $opt_d\n");
  68.  
  69. ($opt_u) || ($opt_u = shift) || ($opt_u = "guest");
  70. my $user = $1 if ($opt_u =~ /^([-_.A-Za-z0-9\/]+)$/);
  71. ($user) || usage("Invalid user: $opt_u\n");
  72.  
  73. ($opt_p) || ($opt_p = shift) || ($opt_p = "");
  74. my $pass = $1 if ($opt_p =~ /(.*)/);
  75.  
  76. ($opt_w) || ($opt_w = shift) || ($opt_w = 85);
  77. my $warn = $1 if ($opt_w =~ /^([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])$/);
  78. ($warn) || usage("Invalid warning threshold: $opt_w\n");
  79.  
  80. ($opt_c) || ($opt_c = shift) || ($opt_c = 95);
  81. my $crit = $1 if ($opt_c =~ /^([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])$/);
  82. ($crit) || usage("Invalid critical threshold: $opt_c\n");
  83.  
  84. my $cmd= "df 2>/dev/null | grep $opt_d" ;
  85.  
  86. # split the type from the unit value
  87. #Check $warn and $crit for type (%/M/G) and set up for tests
  88. #P = Percent, K = KBytes
  89. my $warn_type;
  90. my $crit_type;
  91.  
  92. if ($opt_w =~ /^([0-9]+)\%?$/) {
  93.       $warn = "$1";
  94.       $warn_type = "P";
  95. } elsif ($opt_w =~ /^([0-9]+)k$/) {
  96.       $warn_type = "K";
  97.       $warn = $1;
  98. } elsif ($opt_w =~ /^([0-9]+)M$/) {
  99.       $warn_type = "K";
  100.       $warn = $1 * 1024;
  101. } elsif ($opt_w =~ /^([0-9]+)G$/) {
  102.       $warn_type = "K";
  103.       $warn = $1 * 1048576;
  104. }
  105. if ($opt_c =~ /^([0-9]+)\%?$/) {
  106.       $crit = "$1";
  107.       $crit_type = "P";
  108. } elsif ($opt_c =~ /^([0-9]+)k$/) {
  109.       $crit_type = "K";
  110.       $crit = $1;
  111. } elsif ($opt_c =~ /^([0-9]+)M$/) {
  112.       $crit_type = "K";
  113.       $crit = $1 * 1024;
  114. } elsif ($opt_c =~ /^([0-9]+)G$/) {
  115.       $crit_type = "K";
  116.       $crit = $1 * 1048576;
  117. }
  118.  
  119. # check if both warning and critical are percentage or size
  120. unless( ( $warn_type eq "P" && $crit_type eq "P" ) || ( $warn_type ne "P" && $crit_type ne "P" ) ){
  121.       $opt_w =~ s/\%/\%\%/g;
  122.       $opt_c =~ s/\%/\%\%/g;
  123.       usage("Both warning and critical should be same type- warning: $opt_w critical: $opt_c \n");
  124. }
  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.  
  139. my $workgroup = $1 if (defined($opt_W) && $opt_W =~ /(.*)/);
  140.  
  141. # end of options checking
  142.  
  143.  
  144. my $state = "OK";
  145. my $answer = undef;
  146. my $res = undef;
  147.  
  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.  
  155. &sshopen2("$opt_u\@$opt_H", *READER, *WRITER, "$cmd");
  156. my ($avail_tmp,$usage_tmp,$proc_tmp);
  157. while (<READER>) {
  158.       chomp;
  159.       if (/(\d+)\s+(\d+)\s+(\d+)\s+(\d+)%.*$opt_d$/g) {
  160.             $avail_tmp=$3 ; $usage_tmp=$2; $proc_tmp=$4;
  161.       }
  162. }
  163. close (READER); close(WRITER);
  164. print "Avail_tmp: $avail_tmp, usage_tmp: $usage_tmp, proc_tmp $proc_tmp\n";
  165.  
  166. #Turn off alarm
  167. alarm(0);
  168.  
  169. #Process the last line to get free space.
  170. #If line does not match required regexp, return an UNKNOWN error
  171.  
  172.       my ($avail) = $avail_tmp;
  173.       my ($avail_bytes) = $avail;
  174.       my ($capper) = $proc_tmp;
  175.       my ($mountpt) = "$share";
  176.  
  177.  
  178.       if (int($avail / 1024) > 0) {
  179.             $avail = int($avail / 1024);
  180.             if (int($avail /1024) > 0) {
  181.                   $avail = (int(($avail / 1024)*100))/100;
  182.                   $avail = $avail ."G";
  183.             } else {
  184.                   $avail = $avail ."M";
  185.             }
  186.       } else {
  187.             $avail = $avail ."K";
  188.       }
  189.  
  190. #print ":$warn:$warn_type:\n";
  191. #print ":$crit:$crit_type:\n";
  192. #print ":$avail:$avail_bytes:$capper:$mountpt:\n";
  193.  
  194.       if ((($warn_type eq "P") && (100 - $capper) < $warn) || (($warn_type eq "K") && ($avail_bytes > $warn))) {
  195.             $answer = "Disk OK - $avail ($capper%) free on $mountpt\n";
  196.       } elsif ((($crit_type eq "P") && (100 - $capper) < $crit) || (($crit_type eq "K") && ($avail_bytes > $crit))) {
  197.             $state = "WARNING";
  198.             $answer = "WARNING: Only $avail (zajete $capper%) free on $mountpt\n";
  199.       } else {
  200.             $state = "CRITICAL";
  201.             $answer = "CRITICAL: Only $avail (zajete $capper%) free on$mountpt\n";
  202.       }
  203.  
  204.  
  205. print $answer;
  206. print "$state\n" if ($verbose);
  207. exit $ERRORS{$state};
  208.  
  209. sub print_usage () {
  210.       print "Usage: $PROGNAME -H <host> -s <share> -u <user> -p <password>
  211.      -w <warn> -c <crit> [-W <workgroup>] [-P <port>]\n";
  212. }
  213.  
  214. sub print_help () {
  215.       print_revision($PROGNAME,'2.1');
  216.       print "Copyright (c) 2000 Michael Anthon/Karl DeBisschop/Mariusz Derela
  217. Perl Check <stroke>SMB</stroke> SSH plugin for Nagios
  218.  
  219. ";
  220.       print_usage();
  221.       print "
  222. -H, --hostname=HOST
  223.    Name of the server
  224. -s, --share=STRING
  225.  Share name to be tested
  226. -u, --user=STRING
  227.  Username to log in to server. (Default is root)
  228. -w, --warning=INTEGER or INTEGER[kMG]
  229.  Percent of used space at which a warning will be generated (Default: 85%)
  230. -c, --critical=INTEGER or INTEGER[kMG]
  231.  Percent of used space at which a critical will be generated (Defaults: 95%)
  232. -P, --port=INTEGER
  233.  Port to be used to connect to.
  234.  
  235.  If thresholds are followed by either a k, M, or G then check to see if that
  236.  much disk space is available (kilobytes, Megabytes, Gigabytes)
  237.  
  238.  Warning percentage should be less than critical
  239.  Warning (remaining) disk space should be greater than critical.
  240.  
  241. ";
  242.       support();
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement