#!/usr/bin/env perl use 5.010; use warnings; use strict; use Net::Ping; use Getopt::Std; use IO::Socket; my $proto; # ping protocol my $host; # remote host my $bind_addr; # local ip interface to bind my $bind_port; # local port to bind my $monitoring_node; # monitoring status node my $monitoring_name; # cantain formatted remote host name to insert into a monitoring node my %options=(); # hash to work with options my $result; # ping probe result my $latency; # latency to remote host my $sock; # network sockect object my $agent_socket; # network sockect object my $interval; # delay between checks $|=1; # Getting options getopts("p:h::n:i:s:d:", \%options); sub CheckArgs() { # Checking for non-mandatory options if (defined ($options{p})) { $proto = $options{p}; } else { $proto = "tcp"; }; if (defined ($options{i})) { $bind_addr = $options{i}; } else { $bind_addr="0.0.0.0"; }; if (defined ($options{s})) { $bind_port = $options{s}; } else { $bind_port = "9906"; }; if (defined ($options{d})) { $interval = $options{d}; } else { $interval = "5"; }; # Checking for mandatory options if (defined ($options{h}) && defined ($options{n})) { $host = $options{h}; $monitoring_node = $options{n}; &Main; } else { &PrintHelp; exit 1; } } sub PrintHelp() { print "Latency monitor v1.5.1\nOptions:\n -p Ping protocol to use: tcp(default), udp, icmp(requires root)\n -h Destination host. IP or DNS name can be specified. Mandatory option, no have default value\n -n status node to report. Mandatory option, no default value\n -d Delay between checks in seconds. Default value 5\n -i IP interface to bind. Default value 0.0.0.0\n -s Socket to bind. Default value 9906\n"; exit; } sub Main() { # New network socket instance $sock = new IO::Socket::INET ( LocalHost => "$bind_addr", LocalPort => "$bind_port", Proto => 'tcp', Listen => 5, Type => SOCK_STREAM, Reuse => 1 ) or die "Can't create socket: $!\n"; # New ping instance my $ping = Net::Ping->new("$proto") or die "Can't initialize ping: $!\n"; $monitoring_name = $host; $monitoring_name =~ s/\./-/; $agent_socket = $sock->accept(); # Main loop while (42) { ($result,$latency) = $ping->ping($host); $latency *= 1000; $latency =~ s/\.\d+//; print $agent_socket $monitoring_node . ".lmonitor." . $monitoring_name . ".rtt.value=" . $latency . "\n"; sleep $interval; } $sock->close; } &CheckArgs;