#!/usr/bin/perl use warnings; use strict; use Tk; use Net::Ping; my ( $ip, $hostname, $mw, $hostfile, $int, $frame, $stat, %list, $body ); # Process arguements while ( @ARGV and $ARGV[0] =~ /^-/ ) { $_ = shift; last if /^--$/; if (/^-f(.*)/) { $hostfile = $1 } elsif (/^-i(.*)/) { $int = $1 } else { usage(); } } # Set default values if no arguments were supplied unless ( defined($hostfile) ) { $hostfile = "/etc/hosts" } unless ( defined($int) ) { $int = "30" } open HOSTFILE, "< $hostfile" or die "can't read $hostfile: $!"; # Create a hash of the hostfile; omitting comments, localhost, and # blank lines while () { s/#.*//; # remove any comments next if /^\s+$/; next if /^127\./; ( $ip, $hostname ) = split; $list{$hostname}{hostname} = $hostname; $list{$hostname}{ip} = $ip; my ( $status, $color, $p ); $p = Net::Ping->new("icmp"); if ( $p->ping( $ip, 1 ) ) { $list{$hostname}{status} = "UP"; $list{$hostname}{color} = "green"; } else { $list{$hostname}{status} = "DOWN"; $list{$hostname}{color} = "red"; } } $mw = MainWindow->new(); $mw->title("NetMon Tool"); $mw->geometry('+10+10'); $mw->configure( -background => "black" ); # create topmenu menu my $tm = $mw->Frame( -relief => 'groove', -borderwidth => 3, -background => 'white', )->pack( -side => 'top', -fill => 'x' ); # create frames $body = $mw->Frame( -background => "black", )->pack( -side => 'top', -fill => 'x' ); sub pingagain { # Ping each host once and label UP or DOWN my ( $host, $p ); $p = Net::Ping->new("icmp"); foreach $host ( keys %list ) { if ( $p->ping( $list{$host}{ip}, 1 ) ) { $list{$host}{status} = "UP"; $list{$hostname}{color} = "green"; $stat->configure(-background => 'green'); } else { $list{$host}{status} = "DOWN"; $list{$hostname}{color} = "red"; $stat->configure(-background => 'red'); } } textdisp(); } sub textdisp { my ($host); # system("clear"); foreach $host ( keys %list ) { print "$list{$host}{hostname} \t"; print "$list{$host}{ip}\t"; print "$list{$host}{status}\n"; } } sub usage { print STDERR <<"_EOT_"; Usage: netmon Where options are: -i : The interval, in seconds, at which netmon should ping hosts Default: 30 -f : The configuration file containing space delimited IP addresses and hostnames to be monitored. Default: /etc/hosts Example: netmon -i5 -f/tmp/myhostfile _EOT_ exit(1); } sub statdisp { my ($host); foreach $host ( keys %list ) { $frame = $body->Frame( -background => "$list{$host}{color}", )->pack( -side => 'top', -fill => 'x' ); $stat = $frame->Label( -background => "$list{$host}{color}" )->pack( -padx => 12, -side => "left", -expand => 1, -fill => "x" ); $stat->configure( -text => $list{$host}{hostname} ); $stat = $frame->Label( -background => "$list{$host}{color}" )->pack( -padx => 16, -side => "left", -expand => 1, -fill => "x" ); $stat->configure( -text => $list{$host}{ip} ); $stat = $frame->Label( -background => "$list{$host}{color}" )->pack( -padx => 4, -side => "right", ); $stat->configure( -text => "$list{$host}{status}" ); } } textdisp(); statdisp(); $mw->repeat( 1000, \&pingagain ); $mw->update; MainLoop();