#!/usr/bin/perl # mothership.pl # Ted Fiedler (tfiedler@gmail.com) # See NOTES for more info use strict; use warnings; use IO::Socket::INET; # does network socket connections # use POSIX qw(strftime); # time # use File::Copy; # file copy operations # use Data::Dumper; # for testing # ################################################### # User configurable variables # ################################################### my $path_to_ship = "/home/tfiedler/network"; my $configfile = "mothership.cfg"; ################################################### # configuration file is here # ################################################### open( CONFIGFILE, "$path_to_ship/$configfile" ) || die "$!\n"; my @information = ; # pull config into array ################################################### # This file will hold _new_ status # ################################################### open( NEWSTATS, "> status.new" ) || die "$!\n"; ################################################### # Copy _previous_ stats to _old_ stats file # # for comparison # ################################################### my $copystats = ( -e "status.msp" ) ? copy( "status.msp", "status.old" ) : warn "Unable to copy status.msp to status.old $!\n"; ################################################### # iterate through each line in config and do # # processing here. # ################################################### foreach $_ (@information) { chomp($_); # break each line into individual vars # my ( $hostname, $ipadd, $port, $proto, $description ) = split( /,/, $_ ); # create a hash services with key hostname and values # # $ipadd => ipaddress, $port => port number, $proto => protocol # my %services = ( $hostname => [ $ipadd, $port, $proto ] ); my %status; # initially everything is up # $status{$_} = 'Up' for keys %services; while ( my ( $name, $svc ) = each %services ) { my $sock = IO::Socket::INET->new( Timeout => 2, PeerAddr => $svc->[0], PeerPort => $svc->[1], Proto => $svc->[2] ) # or else it is down # or $status{$name} = 'Down'; } # end of while statement # # This will be changed to reflect a correct last up and down times # # eventually for comparison and evaluation of total uptime # # my $nowstring = strftime '%F %R', localtime; # my $oldtime; # for future use # my $nowstring = time; # in seconds # # this next line is EACH name (aka host) and its corresponding status # while ( my ( $name, $sts ) = each %status ) { # find out what nervice this is # # man getservbyport for more info # my $svcwatch = ( $name !~ /^#/ ) ? getservbyport( $port, $proto ) : next; ################################################### # pick through the data and skip anything that # # doesnt qualify # ################################################### next unless ($hostname); next unless ( $ipadd =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ ); next unless ( $port =~ /^(\d+)/ ); next unless ( $proto eq "udp" || $proto eq "tcp" || $proto eq "icmp" ); unless ( $description =~ m/\w/ ) { $description = "No Description entered"; } # end of unless # # print out the data to status.new # # this will eventually get changed to status.msp for now # # I use status.new for testing # print NEWSTATS "$name,$sts,$svcwatch,$description,$nowstring\n"; print Dumper "$name,$sts,$svcwatch,$description,$nowstring\n"; } # End of while ( my ($name, $sts ... ) } # end of toplevel ( 1st foreach ) close(NEWSTATS); # copy status.new to status.msp # copy( "status.new", "status.msp" ) || die "Unable to copy status.new to status.msp\n"; # EOF # #### #Server name, IP Address, port number, protocol (tcp/udp), description (optional) linux,127.0.0.1,22,tcp, linux,127.0.0.1,8080,tcp,Local web server linux,127.0.0.1,22,udp,Local SSH using udp linux,127.0.0.1,504,tcp,Citadel server on local