Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/perl -w
- #
- #
- # check_disk.pl <host> <share> <user> <pass> [warn] [critical] [port]
- #
- # Nagios host script to get the disk usage from a --SMB-- SSH share
- #
- # Changes and Modifications
- # =========================
- # 7-Aug-1999 - Michael Anthon
- # Created from check_disk.pl script provided with netsaint_statd (basically
- # cause I was too lazy (or is that smart?) to write it from scratch)
- # 8-Aug-1999 - Michael Anthon
- # Modified [warn] and [critical] parameters to accept format of nnn[M|G] to
- # allow setting of limits in MBytes or GBytes. Percentage settings for large
- # drives is a pain in the butt
- # 2-May-2002 - SGhosh fix for embedded perl
- # 18-AUG-2011 - Mariusz Derela
- # Modified to work with SSH
- #
- require 5.004;
- use POSIX;
- use strict;
- use Getopt::Long;
- use Net::SSH qw(sshopen2);
- use vars qw($opt_P $opt_V $opt_h $opt_H $opt_d $opt_W $opt_u $opt_p $opt_w $opt_c $verbose);
- use vars qw($PROGNAME);
- use lib "/usr/share/nagios/libexec" ;
- use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
- sub print_help ();
- sub print_usage ();
- $PROGNAME = "check_remote_disk";
- Getopt::Long::Configure('bundling');
- GetOptions
- ("v" => \$verbose, "verbose" => \$verbose,
- "P=s" => \$opt_P, "port=s" => \$opt_P,
- "V" => \$opt_V, "version" => \$opt_V,
- "h" => \$opt_h, "help" => \$opt_h,
- "w=s" => \$opt_w, "warning=s" => \$opt_w,
- "c=s" => \$opt_c, "critical=s" => \$opt_c,
- "u=s" => \$opt_u, "username=s" => \$opt_u,
- "d=s" => \$opt_d, "directory=s" => \$opt_d,
- "H=s" => \$opt_H, "hostname=s" => \$opt_H);
- if ($opt_V) {
- print_revision($PROGNAME,'2.1'); #'
- exit $ERRORS{'OK'};
- }
- if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
- # Options checking
- ($opt_H) || ($opt_H = shift) || usage("Host name not specified\n");
- my $host = $1 if ($opt_H =~ /^([-_.A-Za-z0-9]+\$?)$/);
- ($host) || usage("Invalid host: $opt_H\n");
- ($opt_d) || ($opt_d = shift) || usage("Share volume not specified\n");
- my $share = $opt_d;
- ($share) || usage("Invalid share: $opt_d\n");
- ($opt_u) || ($opt_u = shift) || ($opt_u = "guest");
- my $user = $1 if ($opt_u =~ /^([-_.A-Za-z0-9\/]+)$/);
- ($user) || usage("Invalid user: $opt_u\n");
- ($opt_p) || ($opt_p = shift) || ($opt_p = "");
- my $pass = $1 if ($opt_p =~ /(.*)/);
- ($opt_w) || ($opt_w = shift) || ($opt_w = 85);
- my $warn = $1 if ($opt_w =~ /^([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])$/);
- ($warn) || usage("Invalid warning threshold: $opt_w\n");
- ($opt_c) || ($opt_c = shift) || ($opt_c = 95);
- my $crit = $1 if ($opt_c =~ /^([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])$/);
- ($crit) || usage("Invalid critical threshold: $opt_c\n");
- my $cmd= "df 2>/dev/null | grep $opt_d" ;
- # split the type from the unit value
- #Check $warn and $crit for type (%/M/G) and set up for tests
- #P = Percent, K = KBytes
- my $warn_type;
- my $crit_type;
- if ($opt_w =~ /^([0-9]+)\%?$/) {
- $warn = "$1";
- $warn_type = "P";
- } elsif ($opt_w =~ /^([0-9]+)k$/) {
- $warn_type = "K";
- $warn = $1;
- } elsif ($opt_w =~ /^([0-9]+)M$/) {
- $warn_type = "K";
- $warn = $1 * 1024;
- } elsif ($opt_w =~ /^([0-9]+)G$/) {
- $warn_type = "K";
- $warn = $1 * 1048576;
- }
- if ($opt_c =~ /^([0-9]+)\%?$/) {
- $crit = "$1";
- $crit_type = "P";
- } elsif ($opt_c =~ /^([0-9]+)k$/) {
- $crit_type = "K";
- $crit = $1;
- } elsif ($opt_c =~ /^([0-9]+)M$/) {
- $crit_type = "K";
- $crit = $1 * 1024;
- } elsif ($opt_c =~ /^([0-9]+)G$/) {
- $crit_type = "K";
- $crit = $1 * 1048576;
- }
- # check if both warning and critical are percentage or size
- unless( ( $warn_type eq "P" && $crit_type eq "P" ) || ( $warn_type ne "P" && $crit_type ne "P" ) ){
- $opt_w =~ s/\%/\%\%/g;
- $opt_c =~ s/\%/\%\%/g;
- usage("Both warning and critical should be same type- warning: $opt_w critical: $opt_c \n");
- }
- # verify warning is less than critical
- if ( $warn_type eq "K") {
- unless ( $warn > $crit) {
- usage("Disk size: warning ($opt_w) should be greater than critical ($opt_c) \n");
- }
- }else{
- unless ( $warn < $crit) {
- $opt_w =~ s/\%/\%\%/g;
- $opt_c =~ s/\%/\%\%/g;
- usage("Percentage: warning ($opt_w) should be less than critical ($opt_c) \n");
- }
- }
- my $workgroup = $1 if (defined($opt_W) && $opt_W =~ /(.*)/);
- # end of options checking
- my $state = "OK";
- my $answer = undef;
- my $res = undef;
- # Just in case of problems, let's not hang Nagios
- $SIG{'ALRM'} = sub {
- print "No Answer from Client\n";
- exit $ERRORS{"UNKNOWN"};
- };
- alarm($TIMEOUT);
- &sshopen2("$opt_u\@$opt_H", *READER, *WRITER, "$cmd");
- my ($avail_tmp,$usage_tmp,$proc_tmp);
- while (<READER>) {
- chomp;
- if (/(\d+)\s+(\d+)\s+(\d+)\s+(\d+)%.*$opt_d$/g) {
- $avail_tmp=$3 ; $usage_tmp=$2; $proc_tmp=$4;
- }
- }
- close (READER); close(WRITER);
- print "Avail_tmp: $avail_tmp, usage_tmp: $usage_tmp, proc_tmp $proc_tmp\n";
- #Turn off alarm
- alarm(0);
- #Process the last line to get free space.
- #If line does not match required regexp, return an UNKNOWN error
- my ($avail) = $avail_tmp;
- my ($avail_bytes) = $avail;
- my ($capper) = $proc_tmp;
- my ($mountpt) = "$share";
- if (int($avail / 1024) > 0) {
- $avail = int($avail / 1024);
- if (int($avail /1024) > 0) {
- $avail = (int(($avail / 1024)*100))/100;
- $avail = $avail ."G";
- } else {
- $avail = $avail ."M";
- }
- } else {
- $avail = $avail ."K";
- }
- #print ":$warn:$warn_type:\n";
- #print ":$crit:$crit_type:\n";
- #print ":$avail:$avail_bytes:$capper:$mountpt:\n";
- if ((($warn_type eq "P") && (100 - $capper) < $warn) || (($warn_type eq "K") && ($avail_bytes > $warn))) {
- $answer = "Disk OK - $avail ($capper%) free on $mountpt\n";
- } elsif ((($crit_type eq "P") && (100 - $capper) < $crit) || (($crit_type eq "K") && ($avail_bytes > $crit))) {
- $state = "WARNING";
- $answer = "WARNING: Only $avail (zajete $capper%) free on $mountpt\n";
- } else {
- $state = "CRITICAL";
- $answer = "CRITICAL: Only $avail (zajete $capper%) free on$mountpt\n";
- }
- print $answer;
- print "$state\n" if ($verbose);
- exit $ERRORS{$state};
- sub print_usage () {
- print "Usage: $PROGNAME -H <host> -s <share> -u <user> -p <password>
- -w <warn> -c <crit> [-W <workgroup>] [-P <port>]\n";
- }
- sub print_help () {
- print_revision($PROGNAME,'2.1');
- print "Copyright (c) 2000 Michael Anthon/Karl DeBisschop/Mariusz Derela
- Perl Check <stroke>SMB</stroke> SSH plugin for Nagios
- ";
- print_usage();
- print "
- -H, --hostname=HOST
- Name of the server
- -s, --share=STRING
- Share name to be tested
- -u, --user=STRING
- Username to log in to server. (Default is root)
- -w, --warning=INTEGER or INTEGER[kMG]
- Percent of used space at which a warning will be generated (Default: 85%)
- -c, --critical=INTEGER or INTEGER[kMG]
- Percent of used space at which a critical will be generated (Defaults: 95%)
- -P, --port=INTEGER
- Port to be used to connect to.
- If thresholds are followed by either a k, M, or G then check to see if that
- much disk space is available (kilobytes, Megabytes, Gigabytes)
- Warning percentage should be less than critical
- Warning (remaining) disk space should be greater than critical.
- ";
- support();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement