Ad Widget

Collapse

Howto Monitor your Homepage via Public Proxies

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shayco
    Junior Member
    • Mar 2013
    • 2

    #1

    Howto Monitor your Homepage via Public Proxies

    HI,

    Attaching a small perl script to check latency of a given URL
    via several pre-defined Proxy Servers.
    Proxy are hard coded within the script (for now, I guess) and can be modified freely (on the top side of the script, no Perl knowledge is required)

    This script is intended to be run via Cron on the Zabbix Server.
    On my setup, I have added two items to the Zabbix Server Host, defined as Zabbix External with the name 'www[REGIONtime]' e.g: www[eutime]

    Comments / enhancements are greatly appreciated!

    Usage is as following:
    ./proxy_wget.pl [-u <url>] [-c <count>] [-r <region>] [-v <level>] [-h]
    url: URL to be tested
    count: Number of iterations to calculate
    region: Which proxy / Region to use
    level: Verbosity level
  • extress
    Member
    • Jul 2012
    • 32

    #2
    and where is the attachment ?

    Comment

    • shayco
      Junior Member
      • Mar 2013
      • 2

      #3
      Sorry, file attachment did not work for me :/

      This is it:

      #!/usr/bin/perl -w
      #
      use Data:umper;
      $regionDefault = 'eu';
      $urlDefault = "www.google.com";
      # The following lines defines proxy servers for each region to be tested, each proxy definition includes an IP address and a port of the proxy server and a URL to test the latency from this machine to the proxy.Normally this should be as nearst to the proxy as possible
      $publicProxies->{'br'} = [ ['187.84.48.34:3128','http://www.bommtempo.com.br/portal/'] ];
      $publicProxies->{'eu'} = [ ['195.251.248.6:3128','http://grnet.gr/'] ];
      $wgetArgs = "-t1 -T10 --no-cache --no-check-certificate -O /dev/null";
      $wgetBin = "/usr/bin/wget";
      $zsendBin = "/usr/bin/zabbix_sender";
      $zsendArgs = "-c /etc/zabbix/zabbix_agentd.conf";
      $verbose=-1;

      #$publicProxies= [ ['187.84.48.34:3128','http://www.bommtempo.com.br/portal/'] , ['187.75.157.163:8080','http://www.google.com.br'] ];
      sub webGet;
      sub getArgs;
      sub showHelp;
      getArgs;
      showHelp if $help;

      $url = $urlDefault unless defined $url;
      if (defined $region) {
      my $regionOK=0;
      foreach $tmpRegion (keys %$publicProxies) {
      $regionOK = 1 if ( $region =~ /^$tmpRegion$/ )
      }
      if ( $regionOK == 0 ) {
      print STDERR "ERR: Region must be one of the following:" . keys (%$publicProxies) . "\n" . "\tSetting defult to $regionDefault\n";
      }
      } else {
      $region = $regionDefault;
      }
      $region = "br" unless defined $region;
      $count = 3 unless defined $count;

      print STDERR "DBG: \$count = $count , \$url = $url\n" if $verbose > 1;
      my $t=0;
      my $failedRun=0;
      foreach $proxy (@{$publicProxies->{$region}}) {
      my $i=0;
      $proxy->[1] = "www.google.com" unless defined $proxy->[1];
      while ($i<$count) {
      $times->[$t]{'dest'} = webGet {'url' => $url , 'http_proxy' => $proxy->[0]};
      $times->[$t]{'local'} = webGet {'url' => $proxy->[1] , 'http_proxy' => $proxy->[0]};
      $failedRun = 1 if (($times->[$t]{'dest'}==-1) or ($times->[$t]{'local'}==-1));
      $i++; $t++;
      }
      }

      foreach $t (@$times) {
      $t->{'diff'} = $t->{'dest'} - $t->{'local'};
      $total += $t->{'diff'};
      $t_count++;
      }
      if ($failedRun == 0 ) {
      $output = int( ($total / $t_count) + 0.5 );
      } else {
      $output = -1;
      }
      $cmd = "$zsendBin $zsendArgs -k 'www[".$region."time]' -o" . $output ."|";
      print STDERR "$cmd\n" if $verbose > 1;
      if (defined $dry) {
      print "Dry running. Would run: $cmd\n";
      } else {
      open ZSEND,$cmd or die "Can not run $cmd";
      while (<ZSEND>) {
      chomp;
      if (/Failed [1-9]/) {
      die "$_\n";
      }
      }
      }

      sub webGet {
      my $args = shift;
      $args->{'https_proxy'} = $args->{'http_proxy'} unless (defined $args->{'https_proxy'});
      $cmd = "export http_proxy=\"".$args->{'http_proxy'}."\"; export https_proxy=\""
      .$args->{'https_proxy'}."\"; (time $wgetBin $wgetArgs ".$args->{'url'}.") 2>&1|" ;
      print STDERR "DBG: \$cmd = $cmd\n" if $verbose > 1;
      open WGET,$cmd or die "Cannot run time $wgetBin";
      while (<WGET>) {
      chomp;
      print STDERR "DBG: \$_ = $_\n" if $verbose > 2;
      # Connecting to 187.84.48.3:3128... failed: Connection timed out
      if ( (/Connecting to ([0-9.:]+).*Connection timed out/) or (/Giving up./) ) {
      my $ret=-1;
      print STDERR "DBG: \$ret = $ret\n" if $verbose > 0;
      return $ret;
      }
      # real 0m10.734s
      if (/^\s*real\s*(\d)m([0-9.]+)s/) {
      my $min=$1;
      my $sec=$2;
      my $ret=($min*60)+$sec;
      print STDERR "DBG: \$ret = $ret\n" if $verbose > 0;
      return $ret;
      }
      }
      }

      sub getArgs {
      use Getopt::Long;
      &Getopt::Long::config('bundling');
      my $status = GetOptions(
      "h|help" => \$help,
      "u|url=s" => \$url,
      "c|count=i" => \$count,
      "r|region=s" => \$region,
      "d|dry" => \$dry,
      "v|verbose=i" => \$verbose,
      );
      }
      sub showHelp {
      print "$0 [-u <url>] [-c <count>] [-r <region>] [-v <level>] [-h]\n";
      exit(1);
      }

      Comment

      Working...