check_http_speed


#1

I´m using the plugin lwp-download-speed, http://www.may.co.at/opensource-unter-linux/nagios-exchange.html, I installed it but when I test the plugin, it doesn´t make the return POSIX codes to warn Nagios. I looked at source but I´m a little lost in it and I don´t not sure where to alter the code fix it. Does anyone know this plugin or know other plugin that can measure download speed? The source code of plugin is bellow:

[code]#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
if 0; # not running under some shell

=head1 NAME

lwp-download-speed - Fetch large files from the web(nagios-plugin)

=head1 SYNOPSIS

B -v] -w ] -c ] -u -H

=head1 DESCRIPTION

The B program will save the file at I to /dev/null.

This is a modified version of b, so that it is usable as a nagios-plugin.

The I program is implemented using the I
library. It is better suited to down load big files than the
I program because it does not store the file in memory.
Another benefit is that it will keep you updated about its progress
and that you don’t have much options to worry about.

=head1 EXAMPLE

Fetch the file http://www.example.com/download/big_file.img and show the speed(–verbose):

$ lwp-download-speed -v -w 440 -c 409 -H www.example.com -u /download/big_file.img
10 MB received in 24 seconds (427 KB/sec)
OK

Do a nagios-check without speed-output:
$ lwp-download-speed -w 440 -c 409 -H www.example.com -u /download/big_file.img
10 MB received in 24 seconds (427 KB/sec)
OK

=head1 AUTHOR

Gisle Aas gisle@aas.no
rewritten by Wolfgang Hotwagner whotwagner@may.co.at

=cut

use strict;

use LWP::UserAgent ();
use LWP::MediaTypes qw(guess_media_type media_suffix);
use URI ();
use HTTP::Date ();

my $progname = $0;
$progname =~ s,./,; # only basename left in progname
$progname =~ s,.
\, if $^O eq “MSWin32”;
$progname =~ s/.\w*$//; # strip extension if any

#parse option
#use Getopt::Std;
#my %opt;
#unless (getopts(‘a’, %opt)) {

usage();

#}

use Getopt::Long qw(:config no_ignore_case);
my ($opt_h, $opt_c, $opt_w, $opt_u, $opt_H,$opt_v);
GetOptions
(“h” => $opt_h, “help” => $opt_h,
“v” => $opt_v, “verbose” => $opt_v,
“c=i” => $opt_c, “critical=i” => $opt_c,
“w=i” => $opt_w, “warning=i” => $opt_w,
“u=s” => $opt_u, “url=s” => $opt_u,
“H=s” => $opt_H, “host=s” => $opt_H
) or exit 3;

if($opt_h){usage();}
usage() if not defined($opt_H);
usage() if not defined($opt_u);
if (not defined $opt_w)
{
print “Error: no value for CRITICAL defined!\n”;
return 2;
}

if (not defined $opt_w)
{
print “Error: no value for WARNING defined!\n”;
return 2;
}

if($opt_w < $opt_c)
{
print “Error: warning-value smaller than critical-value\n”;
return 2;
}

my $url = URI->new(“http://”.$opt_H.$opt_u);
#my $url = URI->new(shift || usage());
my $argfile = “/dev/null”;
usage() if defined($argfile) && !length($argfile);
my $VERSION = “5.813”;

my $ua = LWP::UserAgent->new(
agent => "lwp-download-speed/$VERSION ",
keep_alive => 1,
env_proxy => 1,
);

my $file; # name of file we download into
my $length; # total number of bytes to download
my $flength; # formatted length
my $size = 0; # number of bytes received
my $start_t; # start time of download
my $last_dur; # time of last callback

my $shown = 0; # have we called the show() function yet

$SIG{INT} = sub { die “Interrupted\n”; };

$| = 1; # autoflush

my $res = $ua->request(HTTP::Request->new(GET => $url),
sub {
unless(defined $file) {
my $res = $_[1];

  my $directory;
  if (defined $argfile && -d $argfile) {
      ($directory, $argfile) = ($argfile, undef);
  }

  unless (defined $argfile) {
      # find a suitable name to use
      $file = $res->filename;

      # if this fails we try to make something from the URL
      unless ($file) {
	  my $req = $res->request;  # not always there
	  my $rurl = $req ? $req->url : $url;

	  $file = ($rurl->path_segments)-1];
	  if (!defined($file) || !length($file)) {
	      $file = "index";
	      my $suffix = media_suffix($res->content_type);
	      $file .= ".$suffix" if $suffix;
	  }
	  elsif ($rurl->scheme eq 'ftp' ||
		   $file =~ /\.t[bg]z$/   ||
		   $file =~ /\.tar(\.(Z|gz|bz2?))?$/
		  ) {
	      # leave the filename as it was
	  }
	  else {
	      my $ct = guess_media_type($file);
	      unless ($ct eq $res->content_type) {
		  # need a better suffix for this type
		  my $suffix = media_suffix($res->content_type);
		  $file .= ".$suffix" if $suffix;
	      }
	  }
      }

      # validate that we don't have a harmful filename now.  The server
      # might try to trick us into doing something bad.
      if (!length($file) ||
              $file =~ s/(^a-zA-Z0-9_\.\-\+\~])/sprintf "\\x%02x", ord($1)/ge)
          {
	  die "Will not save <$url> as \"$file\".\nPlease override file name on the command line.\n";
      }

      if (defined $directory) {
          require File::Spec;
          $file = File::Spec->catfile($directory, $file);
      }

      # Check if the file is already present
      if (-l $file) {
	  die "Will not save <$url> to link \"$file\".\nPlease override file name on the command line.\n";
      }
      elsif (-f _) {
	  die "Will not save <$url> as \"$file\" without verification.\nEither run from terminal or override file name on the command line.\n"
	      unless -t;
	  $shown = 1;
	  print "Overwrite $file? [y] ";
	  my $ans = <STDIN>;
	  unless (defined($ans) && $ans =~ /^y?\n/) {
	      if (defined $ans) {
		  print "Ok, aborting.\n";
	      }
	      else {
		  print "\nAborting.\n";
	      }
	      exit 3;
	  }
	  $shown = 0;
      }
      elsif (-e _) {
	  die "Will not save <$url> as \"$file\".  Path exists.\n";
      }
      else {
	  print "Saving to '$file'...\n";
      }
  }
  else {
      $file = $argfile;
  }
  open(FILE, ">$file") || die "Can't open $file: $!\n";
      binmode FILE; #unless $opt{a};
  $length = $res->content_length;
  $flength = fbytes($length) if defined $length;
  $start_t = time;
  $last_dur = 0;
  }

  print FILE $_[0] or die "Can't write to $file: $!\n";
  $size += length($_[0]);

  if (defined $length) {
  my $dur  = time - $start_t;
  if ($dur != $last_dur) {  # don't update too often
      $last_dur = $dur;
      my $perc = $size / $length;
      my $speed;
      $speed = fbytes($size/$dur) . "/sec" if $dur > 3;
      my $secs_left = fduration($dur/$perc - $dur);
      $perc = int($perc*100);
      my $show = "$perc% of $flength";
      $show .= " (at $speed, $secs_left remaining)" if $speed;
      show($opt_v,$show, 1);
  }
  }
  else {
  show($opt_v, fbytes($size) . " received");
  }

}
);

if (fileno(FILE)) {
close(FILE) || die “Can’t write to $file: $!\n”;

show($opt_v,"");  # clear text
print "\r";
print fbytes($size);
print " of ", fbytes($length) if defined($length) && $length != $size;
print " received";
my $dur = time - $start_t;
my $speed_numeric = undef;
if ($dur) {
my $speed = fbytes($size/$dur) . "/sec";
print " in ", fduration($dur), " ($speed)";
if($speed =~ m/(\d+)/g){ $speed_numeric = $1; }
}
print "\n";

if (my $mtime = $res->last_modified) {
utime time, $mtime, $file;
}

if ($res->header("X-Died") || !$res->is_success) {
if (my $died = $res->header("X-Died")) {
    print "$died\n";
}
if (-t) {
    print "Transfer aborted.  Delete $file? [n] ";
    my $ans = <STDIN>;
    if (defined($ans) && $ans =~ /^y\n/) {
	unlink($file) && print "Deleted.\n";
    }
    elsif ($length > $size) {
	print "Truncated file kept: ", fbytes($length - $size), " missing\n";
    }
    else {
	print "File kept.\n";
    }
        exit 3;
}
else {
    print "Transfer aborted, $file kept\n";
}
}

exit 0 if not defined $speed_numeric;
exit 2 if $speed_numeric <= $opt_c;
exit 1 if $speed_numeric <= $opt_w;
print "OK\n";
exit 0;

}

Did not manage to create any file

print “\n” if $shown;
if (my $xdied = $res->header(“X-Died”)) {
print “$progname: Aborted\n$xdied\n”;
}
else {
print "$progname: ", $res->status_line, “\n”;
}
exit 3;

sub fbytes
{
my $n = int(shift);
if ($n >= 1024 * 1024) {
return sprintf “%.3g MB”, $n / (1024.0 * 1024);
}
elsif ($n >= 1024) {
return sprintf “%.3g KB”, $n / 1024.0;
}
else {
return “$n bytes”;
}
}

sub fduration
{
use integer;
my $secs = int(shift);
my $hours = $secs / (6060);
$secs -= $hours * 60
60;
my $mins = $secs / 60;
$secs %= 60;
if ($hours) {
return “$hours hours $mins minutes”;
}
elsif ($mins >= 2) {
return “$mins minutes”;
}
else {
$secs += $mins * 60;
return “$secs seconds”;
}
}

BEGIN {
my @ani = qw(- \ | /);
my $ani = 0;

sub show
{
    my($verb,$mess, $show_ani) = @_;
if(defined $verb)
{
	print "\r$mess" . (" " x (75 - length $mess));
	print $show_ani ? "$ani$ani++]\b" : " ";
}
    $ani %= @ani;
    $shown++;
}

}

sub usage
{
print “Usage: $progname -c -w -u -H \n”;
exit 3;
}[/code]