#!/usr/bin/perl require 5.002; use strict; use sigtrap; use Socket; use POSIX; ################################################################ # prox_monitor # # checks if the proxy is functioning # if not, it would restart the proxy ################################################################ $ENV{PATH} = "$ENV{PATH}:/usr/bin:/bin"; my ($hostname, $i, $res); my (@fails) = (0, 0, 0); my (@ports) = (3124, 3127, 3128); # get my hostname chomp($hostname = `/bin/hostname`); # see if the proxy can fetch a local web page sub check_wget { my $port = shift; my ($paddr, $rin, $win) = ("", "", ""); my ($nfound, $timeleft, $data); my $request = "GET http://127.0.0.1:3126/? HTTP/1.0\r\nHost: 127.0.0.1\r\n\r\n"; print STDERR "testing port $port\n"; # create a socket socket(SOCK_WGET, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or do { print STDERR "$port : socket creation failed\n"; return -1;}; # make it nonblocking fcntl(SOCK_WGET, F_SETFL(), O_NONBLOCK()); # connect to the local proxy $paddr = sockaddr_in($port, inet_aton("127.0.0.1")); connect(SOCK_WGET, $paddr) or do { if ($! != EINPROGRESS()) { print STDERR "$port : connection failed\n"; close(SOCK_WGET); return -1; }}; # wait for the response for 3 sec vec($win, fileno(SOCK_WGET), 1) = 1; ($nfound, $timeleft) = select undef, $win, undef, 3; if ($nfound < 1 && $timeleft == 0) { print STDERR "$port : timeout occurred in connecting\n"; close(SOCK_WGET); return -1; } # send a request defined(send(SOCK_WGET, $request, 0)) or do { print STDERR "$port : send failed\n"; close(SOCK_WGET); return -1;}; # wait for the response for 3 sec vec($rin, fileno(SOCK_WGET), 1) = 1; ($nfound, $timeleft) = select $rin, undef, undef, 3; # if timeout ? => then it's failing if ($nfound < 1 && $timeleft == 0) { print STDERR "$port : timeout occurred in response\n"; close(SOCK_WGET); return -1; } # try to receive the data if (defined(recv(SOCK_WGET, $data, 1024, 0))) { print STDERR "port $port is working\n"; close(SOCK_WGET); return 0; } print STDERR "$port : recv failed\n"; close(SOCK_WGET); return -1; } ############################################################################# sub prepare { my $i; for ($i = 0; $i <= $#ports; $i++) { $fails[$i] = 0; } # wait until the proxy stabilizes sleep(5*60); } ############################################################################# # main modules # if there's another instance already running, # then i'll kill myself chomp($res = `/bin/ps -A | /bin/grep prox_monitor\$ | /usr/bin/wc -l`); if ($res > 1) { die "Another instance is already running\n"; } # initialize prepare(); # main loop while (1) { my $restart = 0; my $res; # if the proxy is dead, execute ./node_livetst chomp($res = `/bin/ps -A | /bin/grep prox\$ | /usr/bin/wc -l`); if ($res == 0) { `./node_livetst`; sleep(60); next; } # see if ports on my proxy are working for ($i = 0; $i <= $#ports; $i++) { if (check_wget($ports[$i]) == -1) { # wget failed $fails[$i]++; if ($fails[$i] >= 6) { $restart = 1; last; } } else { # wget succeeded, let's forgive the past behavior $fails[$i] = 0; } } # see if we need to restart if ($restart) { print STDERR "the proxy is not responding,", "so we're going to restart it\n"; # restart # `./prox_stop`; do not call prox_stop - prox_start does it `./prox_start`; open filename, '>> /home/princeton_codeen/exitlog'; my $now = localtime time; print filename "$now restarted by prox_monitor @fails\n"; close filename; prepare(); } else { print STDERR "everything is fine\n"; } # wait fifteen seconds sleep(15); }