#----------------------------------------------------------------# # Use for-loop to allow two tries on red alerts. # #----------------------------------------------------------------# for (my $n=1; $n<=$retry; $n++) { @http_err = ""; # Reinitialize every time through. $ua = ""; # '' $response = ""; # '' $request = ""; # '' $headers = ""; # '' $body = ""; # '' $code = ""; # '' $desc = ""; # '' #----------------------------------------------------------# # If serialization of the URL requested, append hostname # # and seconds (from time() function) patameters. # #----------------------------------------------------------# if ($opt_s or $serialize) { sleep(2); $url = "$http_cmd"; $url_suffix = "hostname=$host&seqnumber=" . time(); $url .= ($url =~ /\?/) ? "\&${url_suffix}" : "\?${url_suffix}"; } $ua = LWP::UserAgent->new(); $ua->env_proxy(); # Check proxy settings. echo("$n. Checking connection to $uiPID\n"); echo("\$ua->agent('DMC $0/%I%')\n"); $ua->agent("DMC $0/%I%"); echo("\$response = \$ua->timeout($timeout)\n"); $response = $ua->timeout($timeout); ($opt_v or $URL_opt_v) && print "${sp}\$response=|$response|\n"; #----------------------------------------------------------------# # Wrap this in alarm trap to (hopefully) kill hanging # # requests. # # # $http_OK = 1; # Hope for the best. # $msg = "Alarm Timeout! ($url)"; # local $SIG{ALRM} = sub { # echo("$msg [\$@=|$@|]\n"); # $http_OK = 0; # }; # $timeout_alarm = $timeout + 5; # ($opt_v or $URL_opt_v) && echo("alarm($timeout_alarm)\n"); # alarm($timeout_alarm); # # echo("\$request = new HTTP::Request('GET', '$url')\n"); # $request = new HTTP::Request("GET", "$url"); # ($opt_v or $URL_opt_v) && print "${sp}\$request=|$request|\n"; # # echo("\$response = \$ua->request($request)\n"); # $response = $ua->request($request); # # ($opt_v or $URL_opt_v) && echo("alarm(0)\n"); # alarm(0); # Halt alarm. # # if ($@) # Propagate any errors (if any). # { # @http_err = "$@"; # foreach (@http_err) # Propagate any errors (if any). { print "${sp}\$@=|$_|\n"; } # $@ = ""; # } # if ($@) # # # #----------------------------------------------------------------# $null_n = $response =~ s/\x00//g; # Strip nulls $null_n ||= 0; if ($null_n) { $msg = "${sp}Removed $null_n nulls from \$response\n"; $len = length($response); $msg .= "${sp}\$response now $len bytes\n"; $subject = "$surname: NOTICE Nulls In HTTP Response"; email("$subject", "$msg", $support); if ($opt_v) { print "${sp}Removed $null_n nulls from \$response\n"; $len = length($response); print "${sp}\$response now at $len bytes\n"; } } ($opt_v or $URL_opt_v) && print "${sp}\$response=|$response|\n"; #----------------------------------------------------------# # In case of Alarm Timeout, $http_OK will be zero. We'll # # still try to capture the following info, but we'll also # # be testing both $http_OK and $response_OK before giving # # our blessing for the connectivity test. # #----------------------------------------------------------# $response_OK = ($response->is_success); echo("\$code = \$response->code\n"); $code = $response->code; ($opt_v or $URL_opt_v) && print "${sp}\$code = |$code|\n"; echo("\$desc = HTTP::Status::status_message($code)\n"); $desc = HTTP::Status::status_message($code); ($opt_v or $URL_opt_v) && print "${sp}\$desc = |$desc|\n"; #----------------------------------------------------------# # Get headers # #----------------------------------------------------------# echo("\$headers = \$response->headers_as_string\n"); $headers = $response->headers_as_string; if ($opt_v or $URL_opt_v) { @headers = split(/\n/,$headers); # Split on newlines foreach (@headers) { print "${sp}headers=$_\n"; } } #--------------------------------------------------------------# # $http_OK means we didn't alarm() out. $response_OK is from # # "$response_OK = ($response->is_success);" above. If both # # of these look good, then we check next for any HTTP output # # we're supposed to find. # #--------------------------------------------------------------# if ($http_OK and $response_OK) { #----------------------------------------------------------# # If we have expected or alternate responses, then check # # them before sounding the "All clear." # #----------------------------------------------------------# if ( (defined($expd_response) and $expd_response ne "") or (defined($alt_response ) and $alt_response ne "") ) { #----------------------------------------------------# # Get body -- print as needed. # #----------------------------------------------------# echo("\$body = \$response->content\n"); $body = $response->content; @body = split(/\n/,$body); # Split body on newlines if ($opt_v or $URL_opt_v) { foreach (@body) { print "${sp}content=$_\n" } } # if ($opt_v or $URL_opt_v) @grep_response = grep(/$expd_response/, @body); $response_n = @grep_response; #----------------------------------------------------# # If 1st response found, look for any optional/add'l.# #----------------------------------------------------# if ( $response_n > 0 && defined($addl_response) && $addl_response ne "" ) { @grep_response = grep(/$addl_response/, @body); $response_n = @grep_response; } #----------------------------------------------------# # Response not found, any alternate response? # #----------------------------------------------------# if ($response_n == 0) { if (defined($alt_response) and $alt_response ne "") { @grep_response = grep(/$alt_response/, @body); $response_n = @grep_response; } } #----------------------------------------------------# # Neither expected nor alternate response found. # #----------------------------------------------------# if ($response_n == 0) { if ($n > 1) # for (my $n=1; $n<=$retry; $n++) { $epage_msg = "$host: Web connection to $uiPID " . "failed (expected response not found)"; notify_support(); # Modifies $epage_msg $msg = "$host|$uiPID|$url|$alert|$epage_msg"; no strict 'refs'; ${$alert}++; use strict 'refs'; ELS_Agent("$msg"); } next; # Iterate "for (my $n=1; $n<=$retry; $n++)" } } # if (defined($expd_response) and .. #------------------------------------------------------# # Else, $http_OK is sufficient for "All clear." # #------------------------------------------------------# $shortdesc = "Successful web connection to $uiPID"; $n++; # Incr $n--no need to try again; it was successful. $msg = "$host|$uiPID|$url|green|$shortdesc|"; $green++; ELS_Agent("$msg"); echo("$0: $shortdesc\n"); last; # for (my $n=0; $n<=1; $n++) } # if ($http_OK) elsif ($n > ($retry - 1)) # Something's amuck. { $epage_msg = "$host: Web connection to $uiPID failed"; if (@http_err) { $epage_msg .= " ["; foreach (@http_err) { $epage_msg .= "$_ "; } $epage_msg =~ s/\s+$/\]/; } notify_support(); # Modifies $epage_msg (appends to it). $msg = "$host|$uiPID|$url|$alert|$epage_msg"; no strict 'refs'; ${$alert}++; use strict 'refs'; ELS_Agent("$msg"); } # if ($http_OK); elsif ($n > 1) else { sleep(2); } # if ($http_OK); elsif ($n > 1); else } # for (my $n=0; $n<=1; $n++)