#!/usr/bin/perl # # Script to monitor DSL conditions and produce a gnuplot graph. I suspect # that the ping times are crappy at regular intervals, like every afternoon, # and am looking to confirm this. I am pinging my upstream router, on the # assumption that it is the first thing on the other end of my DSL, but # you could ping between any two points whose connectivity you wanted to # monitor. # # To do: The data recorded could also be use to calculate quality-of-service # and uptime statistics over any arbitrary period, although this script # doesn't do it. # # Copyright (C) 2003 Michael A. Dickerson. Copying, modification, or # redistribution are permitted under the terms of the Artistic License # as published by the Open Source Initiative. This is OSI Certified # Open Source Software; see http://www.opensource.org. Other scripts, # and possibly a newer revision of this one, are available at # http://www.singingtree.com/software. # # Created 14 Nov 2003 by M. Dickerson. use strict; my @month = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/; my $sum = 0; my $count = 0; my $logcount = 0; my $time = ""; my $debug = 0; open PING, "ping -i 10 64.81.32.1 |" or die("can't ping: $!"); open LOG, ">>ping_log" or die("can't open log: $!"); # turn off output buffering; we want each line immediately select((select(LOG), $| = 1)[0]); while () { next unless m/time=([0-9\.]+) ms/; my $sample = $1; my $now = time(); my $newtime = &format_time($now); if ($newtime eq $time) { # the minute hasn't changed; just record sample $sum += $sample; $count++; print "sample $sample: sum $sum / count $count\n" if $debug; } else { # compute the average for this minute and record in log if ($count > 0) { my $avg = $sum / $count; my $logentry = sprintf("%s %8.3f %d", $time, $avg, $count); print LOG $logentry, "\n"; print "logging $logentry\n" if $debug; } # regenerate the graph every 15 minutes &do_plot($now) if ($logcount++ % 15 == 0); # reset counters for next minute $sum = $sample; $count = 1; $time = $newtime; } } close PING; close LOG; exit 0; sub do_plot() { (my $t) = @_; my $now = &format_time($t); my $yesterday = &format_time($t - 86400); my $lastweek = &format_time($t - 86400 * 7); print "gnuplot: now=$now, day=$yesterday, week=$lastweek\n" if $debug; open PLOT, "|/usr/local/bin/gnuplot" or die("can't run gnuplot: $!"); print PLOT <