Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Perl not updating Log File

by Appy16 (Sexton)
on May 10, 2010 at 09:09 UTC ( [id://839183]=perlquestion: print w/replies, xml ) Need Help??

Appy16 has asked for the wisdom of the Perl Monks concerning the following question:

I have this Perl CGI script using which i want to ping and fetch files from desired URLs and if a failure occurs the a log file is updated and mailed to desired email addresses.

For this i created a HTML page where the Required values are needed to be inserted.

But the problem is that when the program is unable to Ping the desired URL, the details are neither added into a log file nor the Log file is mailed.

I'm Posting my Script Below.

#!/usr/bin/perl -w use CGI::Ajax; use CGI; use LWP::Simple; use Email::Send; use Email::Send::Gmail; use Email::MIME::Creator; use Net::Ping::External qw(ping); use HTML::Template; sub Show_HTML { my $html = <<EOT; <HTML> <HEAD><title>Ping or Fetch</title> </HEAD> <BODY> <script type="text/javascript"> function CheckIsNumeric() { var AsciiCode = event.keyCode; if ((AsciiCode < 48) || (AsciiCode > 57)) { alert('Please enter only numbers.'); event.cancelBubble = true; event.returnValue = false; } } function display(selector,inputID) { var selector=document.getElementById(selector); var inputID=document.getElementById(inputID); if (selector[selector.selectedIndex].value=="Other") { inputID.style.display= "block"; } else { inputID.style.display="none"; } } </script> <form name="orderform" method="GET" > <input type="radio" name="pick" value="Ping" checked="checked">Ping &nbsp; <input type="radio" name="pick" value="Fetch" />Fetch<br /> <br> Enter a number : &nbsp; &nbsp; <input type="text" name="Number" id="myText" size="6" onkeypress = +"CheckIsNumeric()"/> <br> <br> Enter a URL/URI : <input type="text" name="URL" id="URI" size="25"> <br> <br> Enter Email ID : &nbsp; &nbsp; <input type="text" name="Email" id="Email" size="40"> <br> <br> <input type="submit" value=" Submit " /> </Form> <form name = "Log" method = "POST" Action="http://localhost/~Appy/l +og.txt"> Click Here to View the Log file : <input type="submit" value="Log File" /> <br> <hr> <br> </Form> </BODY> </HTML> EOT return $html; } my $cgi = new CGI(); my $pjx = new CGI::Ajax( 'ping_and_fetch' => \&ping_and_fetch ); print $pjx->build_html($cgi,\&Show_HTML); $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "GET") { &ping_and_fetch(); } else {} sub ping_and_fetch { print "Radio Button's Value is : " . $cgi->url_param('pick'); print "<br>"; print "Number's Value is : " . $cgi->url_param('Number'); print "<br>"; print "URL's Value is : " . $cgi->url_param('URL'); print "<br>"; print "Email's Value is : " . $cgi->url_param('Email'); print "<br>"; print "<br>"; $Ping_or_Fetch = $cgi->url_param('pick'); my $number = $cgi->url_param('Number'); my $URI = $cgi->url_param('URL'); my $Email = $cgi->url_param('Email'); my $from = "abc\@gmail.com"; my $count = 0; my $date = localtime(); my $logfilelocation = "/Users/Appy/Desktop/"; if ($number eq "" || $Email eq "" || $URI eq "" ) { print "Please enter all the values"; } else { print "Thank You"; print "<br>"; print "<br>"; if ($Ping_or_Fetch eq 'Ping') { &myping($URI,$URL); sub myping { my $newURI = $_[0]; my $newURL = $_[1]; my $alive = ping(host => "$newURI", timeout => 15); if ($alive) { print "$newURI is active.\n"; $count = 0; } else { print "$newURI is inactive\n"; print "<br>"; if ($count<$number) { $count = $count + 1; &myping; } else { print "<br> $date"; my $logfile = $logfilelocation . "log.txt"; my $logmsg = "$date cannot ping $newURI"; open LOGFILE, ">>$logfile" or die "cannot open logfile $logfile for ap +pend: $!"; print LOGFILE $logmsg, "\n"; close LOGFILE; my $email = Email::MIME->create( header => [ From => $from, To => $Email, Subject => $logmsg, ], attributes => { filename =>"log.txt", content_type =>"application/text", disposition =>"attachment", Name =>"/Users/Appy/Sites/log.txt" +, }, body => "$newURI is not working" ); my $sender = Email::Send->new( { mailer => 'Gmail', mailer_args => [ username => 'abc@gmail.com', password => '1234qwerty', ] } ); eval { $sender->send($email) }; die "Error sending email: $@" if $@ } } } } } }
Please tell me Why is the code unable to update the log file ?

Kindly Help. Thanx in Advance .

Replies are listed 'Best First'.
Re: Perl not updating Log File
by marto (Cardinal) on May 10, 2010 at 10:08 UTC
Re: Perl not updating Log File
by JavaFan (Canon) on May 10, 2010 at 09:47 UTC
    The layout of your code makes it hard to read. Your indentation isn't consistent, and defining subs inside if constructs doesn't win any prices for readability either.

    But I see your print statement is inside a couple of if/then/else constructs. Perhaps the conditions aren't what you think they are? Does the email get send?

    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://839183]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-05-30 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found