Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

perl script not working after moved to new host

by prabhakard (Novice)
on Jan 25, 2016 at 10:24 UTC ( [id://1153547]=perlquestion: print w/replies, xml ) Need Help??

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

I could not able to run this perl script after I moved to another host, perl versions are same.

Here the code --------- use Time::Local; #use warnings qw(FATAL); #use diagnostics; #$SIG{__WARN__} = sub { $DB::single = 1; CORE::warn(@_) }; # variable settings $target="raksan"; $wwwpath="/$target"; $wwwrealpath="/home/nagios/stat/$target/htdocs"; $node = param("node"); $tmpgraph="rrd-$$.gif"; $now=time; $recenttimes = $now - 3600 * 36; $aix = 1 if ($node =~ /amsr/); # # Generate HTML page # system("/home/nagios/stat/scripts/ng_rrd2img.sh $target $node dig $rec +enttimes now 300 130 > $wwwrealpath/tmp/$tmpgraph"); print "Content-type: text/html\n\n"; print <<EOF <html><header><meta http-equiv="refresh" content="300"><title>NMON Per +formance Graphs</title></header><body bgcolor=#FFFFFF link=#006699 al +ink=#0000ff vlink=#006699> <body><BR><FONT FACE="arial"> <CENTER><h1>NMON Performance Graphs</h1></CENTER> EOF ; # # get the current day ,month, year for the form # ($sec,$min,$hour,$mday,$month,$year,$wday,$yday) = localtime($now); #($sec,$min,$mday,$month,$year,$wday,$yday) = localtime($now); $today = $mday; $tomonth = $month + 1; $toyear = $year + 1900; # # Create a row for each node, with the FORM, the sysinfo, and a graph +in it. # The nodes are all systems entered at the command line. # print <<EOF <table border="0" cellspacing="1"> <tr bgcolor="#A0A0FF"> <td colspan=5 rowspan=1 ><FONT SIZE=-5>&nbsp;<a name=$node></a></td> <form name="graph_$node" method=post action=/$target/cgi-bin/ng.pl tar +get=_blank> <input type=hidden name=node value=$node> <tr bgcolor="#C0C0C0"> <TD> <B>Select Graph(s)</B><BR> <select name=graph size=12 multiple> <option selected>CPU <option>Memory Use <option>Memory Totals <option>Memory Stats <option>Memory Ratios <option>Paging Act <option>Network Read <option>Network Write <option>Processes <option>Context Switches <option>Run Queue <option>Filesystem IO EOF ; if ($aix) { print <<EOF <option>File IO <option>IO Adapters Read <option>IO Adapters Write <option>System Calls EOF ; } print <<EOF </select> </TD> <TD> <table border="0" cellspacing="1"> <tr bgcolor="#C0C0C0"> <TD> <B>Select&nbsp;Range</B><BR> <select name=time size=9 > <option>Custom<br> <option selected>Today<br> <option>Yesterday<br> <option>This Week<br> <option>Last Week<br> <option>This Month<br> <option>Last Month<br> <option>This Year<br> <option>Last Year<br> </select> </TD> </tr> <tr bgcolor="#C0C0C0"> <td colspan=3 rowspan=1 align=center><BR><input type=submit value +="Create Graph!"></td> </tr> </table> </TD> <TD> <table border="0" cellspacing="1"> <tr bgcolor="#C0C0C0"> <td colspan=3 rowspan=1 align=center><B>Custom Range</B></td> </tr> <tr bgcolor="#A0A0FF"> <td colspan=3 rowspan=1 align=center>Start Date</td> </tr> <tr bgcolor="#C0C0C0"> <td colspan=1 rowspan=1 ><FONT SIZE=-1>Day</td> <td colspan=1 rowspan=1 ><FONT SIZE=-1>Month</td> <td colspan=1 rowspan=1 ><FONT SIZE=-1>Year</td> </tr> <tr bgcolor="#C0C0C0"> <TD> EOF ; print "<select name=startday>\n"; $i = 1; while ( $i <= 31 ) { $selected = ($i == $today) ? "selected" : ""; printf "<option $selected>%02d", $i; $i++; } print "</select></TD><TD>\n"; print "<select name=startmonth>\n"; $i = 1; while ( $i <= 12) { $selected = ($i == $tomonth) ? "selected" : ""; printf "<option $selected>%02d", $i; $i++; } print "</select></TD><TD>\n"; $i = 2015; print "<select name=startyear>\n"; while ( $i <= 2025 ) { $selected = ($i == $toyear) ? "selected" : ""; printf "<option $selected>%04d", $i; $i++; } print "</select></TD>\n"; print <<EOF </tr> <tr bgcolor="#A0A0FF"> <td colspan=3 rowspan=1 align=center>End Date</td> </tr> <tr bgcolor="#C0C0C0"> <td colspan=1 rowspan=1 ><FONT SIZE=-1>Day</td> <td colspan=1 rowspan=1 ><FONT SIZE=-1>Month</td> <td colspan=1 rowspan=1 ><FONT SIZE=-1>Year</td> </tr> <tr bgcolor="#C0C0C0"> <TD> EOF ; print "<select name=endday>\n"; $i = 1; while ( $i <= 31 ) { $selected = ($i == $today) ? "selected" : ""; printf "<option $selected>%02d", $i; $i++; } print "</select></TD><TD>\n"; print "<select name=endmonth>\n"; $i = 1; while ( $i <= 12 ) { $selected = ($i == $tomonth) ? "selected" : ""; printf "<option $selected>%02d", $i; $i++; } print "</select></TD><TD>\n"; $i = 2015; print "<select name=endyear>\n"; while ( $i <= 2025 ) { $selected = ($i == $toyear) ? "selected" : ""; printf "<option $selected>%04d", $i; $i++; } print "</select></TD>\n"; print <<EOF </tr> <tr bgcolor="#C0C0C0"> <td colspan=3 rowspan=1 >&nbsp;</td> </tr> <tr bgcolor="#A0A0FF"> <td colspan=3 rowspan=1 align=center>Specify Graph Size</td> </tr> <tr bgcolor="#C0C0C0"> <td colspan=3 rowspan=1 align=center><FONT SIZE=-1>Width:<input t +ype=text name=width value=400 size=3>Height:<input type=text name=hei +ght value=300 size=3></td> </tr> </table> </form> <TD ALIGN=CENTER> <H2>$node</H2> EOF ; $sysinfo = `sudo -u rsnagios ssh $node bin/sysinfo|sed s/-/_/`; ($model, $num_procs, $memory, $pssize, $kernelbits, $aixlevel, $uptime +) = split(/,/, $sysinfo); print <<EOF <table border="0" cellspacing="1"> <tr bgcolor="#A0A0FF"> <tr bgcolor="#C0C0C0"> <td colspan=1 rowspan=1 >Model</td> <td colspan=1 rowspan=1 >:&nbsp;$model</td> <tr bgcolor="#C0C0C0"> <td colspan=1 rowspan=1 >Procs</td> <td colspan=1 rowspan=1 >:&nbsp;$num_procs</td> <tr bgcolor="#C0C0C0"> <td colspan=1 rowspan=1 >Memory</td> <td colspan=1 rowspan=1 >:&nbsp;${memory}MB</td> <tr bgcolor="#C0C0C0"> <td colspan=1 rowspan=1 >Paging</td> <td colspan=1 rowspan=1 >:&nbsp;${pssize}MB</td> <tr bgcolor="#C0C0C0"> <td colspan=1 rowspan=1 >Kernel</td> <td colspan=1 rowspan=1 >:&nbsp;$kernelbits&nbsp;bit</td> <tr bgcolor="#C0C0C0"> <td colspan=1 rowspan=1 >OS</td> <td colspan=1 rowspan=1 >:&nbsp;$aixlevel</td> <tr bgcolor="#C0C0C0"> <td colspan=1 rowspan=1 >Uptime</td> <td colspan=1 rowspan=1 >:&nbsp;$uptime</td> </table> </TD> <td colspan=1 rowspan=1> <IMG src="${wwwpath}/tmp/$tmpgraph"> </tr> </table> <p><FONT SIZE=-1>The Performance data is recorded on the systems using + nmon and stored in Round Robin Databases by rrdtool. This interface +is created by nmon_grapher. </p> </body></html> EOF ; ------code ends here

Original content restored above and code tags added by GrandFather

What is the basic difference between run perl script from shell and via browser. how the result can be different on each method?
--------- <c> use Time::Local; #use warnings qw(FATAL); #use diagnostics; #$SIG{__WARN__} = sub { $DB::single = 1; CORE::warn(@_) }; # variable settings $target="raksan"; $wwwpath="/$target"; $wwwrealpath="/home/nagios/stat/$target/htdocs"; $node = param("node"); $tmpgraph="rrd-$$.gif"; $now=time; $recenttimes = $now - 3600 * 36; $aix = 1 if ($node =~ /amsr/);

Replies are listed 'Best First'.
Re: perl script not working after moved to new host
by Corion (Patriarch) on Jan 25, 2016 at 10:30 UTC

    You find the real error message in your web server error log. Look at that and remedy the cause.

    Also, I really suggest you rewrite this script. It is horribly insecure and allows for arbitrary remote code execution:

    $node = param("node"); ... $sysinfo = `sudo -u rsnagios ssh $node bin/sysinfo|sed s/-/_/`;

    $node is not sanitized or checked against a list of valid hostnames. If you call your script with node="wget http://perlmonks.org" it will run the wget executable. As a first fix, you should only allow hostnames that match \w+ in $node.

      I have tried by replace node= "actual valid hostname" but still it is not negerating when I run from shell like this it is working fine, gif image file $ perl -w ngcf.pl node=<hostname> but from browser if we run it is generating empty gif image file.
Re: perl script not working after moved to new host
by choroba (Cardinal) on Jan 25, 2016 at 11:12 UTC
    system("/home/nagios/stat/scripts/ng_rrd2img.sh $target $node dig $rec +enttimes now 300 130 > $wwwrealpath/tmp/$tmpgraph");

    You should always check the exit code of the system command.

    my $status = system "..."; if ($status) { die "system error: $?" }
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      now I made changes as to exit now on browser says "Internal Server Error"

      my $status = system("/home/nagios/stat/scripts/ng_rrd2img.sh $target $ +node dig $recenttimes now 300 130 > $wwwrealpath/tmp/$tmpgraph"); if ($status) { die "system error: $?" }

      Is there any way can get errors on browser that says where exit or stop

        Is there any way can get errors on browser that says where exists
        If you put this line at the top you should see the error in the browser
        use CGI::Carp('fatalsToBrowser');
Re: perl script not working after moved to new host
by choroba (Cardinal) on Jan 25, 2016 at 10:34 UTC
    What do you mean by "I could not able to run"? Do you get an error? What does it say?

    Most of the code you posted is probably not relevant to the question. Before posting, you should try reducing the code to only the lines related to the problem. Try removing some lines (I'd start with the HTML), be sure the problem is still there.

    See also Short, Self Contained, Correct Example and How do I post a question effectively?.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      I am new to perl, I can't see where it get stops, on this host machine, so I posted completed code. Pls note that this same perl script is working fine on another host, on shell and browser as well. but here it is give this errors

      perl -wd ngcf.pl node=servername

      Loading DB routines from perl5db.pl version 1.32 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help.

      Name "main::wday" used only once: possible typo at ngcf.pl line 41. at ngcf.pl line 41

      Name "main::hour" used only once: possible typo at ngcf.pl line 41. at ngcf.pl line 41

      Name "main::yday" used only once: possible typo at ngcf.pl line 41. at ngcf.pl line 41

      Name "main::min" used only once: possible typo at ngcf.pl line 41. at ngcf.pl line 41

      Name "main::sec" used only once: possible typo at ngcf.pl line 41. at ngcf.pl line 41

      main::(ngcf.pl:12): $target="raksan"; Use of uninitialized value $tid in concatenation (.) or string at /usr/share/perl5/perl5db.pl line 2237. at /usr/share/perl5/perl5db.pl line 2237 DB::DB called at ngcf.pl line 12

        in this host from shell it runs and generates gif graph image file with data,

        but from browser it is generating graph file with empty data.

        I have compared perl and its modules wt both hosts, all are same, also checked permission, browser can able to write empty file

        but still not able to find where it is get struck when this script from browser.

        And what happened after you fixed these errors?

        ----
        I Go Back to Sleep, Now.

        OGB

    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://1153547]
Approved by shmem
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-19 04:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found