#!/usr/bin/perl # CS357 ASSIGNMENT 3 # Workstation status monitor # Dan J. Fraser (1219229) # done in perl with permission of Jim Mullin #name of the input file $inputfile = 'hosts'; #path to store machine information, must end in '/' $outpath = '/gaul/s9/student/1995/fraser1/stats/'; #open the input file, exit if we can't find it. open(FOO,$inputfile) or die "can't open input file: "; #ping timeout, in seconds $timeout = 2; #amount of time to sleep between checks $sleep = 1800; # 1/2 hour # ------------------------- #load the contents of the input file into an array @machines = ; close FOO; # clsoe the input file while (1) { # loop forever foreach $machine (@machines) { # for every machine on the list do... ($ip,$name) = split(' ',$machine); # split the /etc/hosts format into IP and name if ($ip =~ /^\d+\.\d+\.\d+\.\d+$/) { # ensure we have a valid IP # create/append a file for the output data open(MACHINE,">>$outpath$name") or die "couldn't open machine file"; #now call the ping command and check the returncode if (system("ping $ip $timeout >/dev/null")) { # the machine is down. write that information to the file! print "$name is down.\n"; print MACHINE time." down\n"; } else { # the machine is up. write that information to the file! print "$name is up.\n"; print MACHINE time." up\n"; } close MACHINE; # close the output file } } print "done checking. Waiting $sleep seconds.\n"; sleep $sleep; # wait for a bit. }