Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How to PRINT CGI html table to a PNG file

by theravadamonk (Scribe)
on Aug 21, 2018 at 11:03 UTC ( [id://1220770]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I wrote a CGI code to get the load averga of my CentOS server. It gives 1 minute , 5 minutes and 15 minutes Load averages.

I can see it via URL http://ipaddress/cgi-bin/Sys_Load.cgi

Now, What I need is to INSERT this Sys_Load.cgi url to my home.cgi code. My ulitimate goal is to access URL http://ipaddress/cgi-bin/home.cgi.

Then, Everything in home.cgi should be displayed.

I have already dispalyed other codes with "<img src = >"

I can dispaly them since it PRINTS a png image to the browser.

here's my home.cgi

#!/usr/bin/perl use strict; use warnings; use CGI ':standard'; my $date = localtime(); print "Content-type: text/html\n\n"; print "<body bgcolor=\"#c0e0f9\">"; print qq!<br/><center> <h1>Home</h1> <h3>$date</h3> <img src = "root_partition.cgi"><br/><br/><br/> <img src = "var_partition.cgi"><br/><br/><br/> <img src = "stacked_Bar_graph.cgi"><br/><br/><br/> <img src = "topsenders_hbar.cgi"><br/><br/><br/> <img src = "toprecipients_hbar.cgi"><br/><br/><br/></center>!; print "</body>";

Here's my Sys_Load.cgi code. I think If can PRINT CGI html table to a PNG file, It will be OK. I am right aint't I? How can I do it? seeking help...

#!/usr/bin/perl use strict; use warnings; use CGI ':standard'; use Sys::Load qw/getload uptime/; print "Content-type: text/html\n\n"; print "<body bgcolor=\"#c0e0f9\">"; my ($one_m, $five_m, $fifteen_m) = (getload())[0,1,2]; print "<br/>"; print "Load Average \n"; print '<table style=width:20%><tr><th bgcolor=#FFDD00 height=25>1 Minu +te</th><th bgcolor=#FFDD00 height=25>5 Minutes</th><th bgcolor=#FFDD0 +0 height=25>15 Minutes</th></tr>'; print "\n <tr style=\"font-family:verdana\" bgcolor=\"#FFFFFF\"><t +d>$one_m</td><td>$five_m</td><td>$fifteen_m</td></tr>"; print '</table>'; print "</body>";

Replies are listed 'Best First'.
Re: How to PRINT CGI html table to a PNG file (don't do that)
by hippo (Bishop) on Aug 21, 2018 at 11:19 UTC

    If you don't want (or care to have) SysLoad.cgi callable as a separate URL then just move the code from there to home.cgi

    If you do want SysLoad.cgi to continue to exist separately then move the table-generating code out into a module which can then be used by both SysLoad.cgi and home.cgi.

Re: How to PRINT CGI html table to a PNG file
by marto (Cardinal) on Aug 21, 2018 at 11:30 UTC
    print "Content-type: text/html\n\n";

    Since you insist on using CGI despite all the warnings why not just use the header method? You also aren't even printing valid HTML. Make life easier on yourself and make web development fun, CGI::Alternatives. If you want to display an image rather than HTML you have to create an image, which I'm sure you already know since you've previously asked for help creating them.

Re: How to PRINT CGI html table to a PNG file
by Anonymous Monk on Aug 21, 2018 at 12:20 UTC

    No, modern browsers do not support HTML images, but you could load Sys_Load.cgi in an inline frame instead:

    <iframe src="Sys_Load.cgi"></iframe>

    Unless what you are doing is a homework problem, perhaps you should consider looking for existing solutions for building a dashboard with machine usage statistics.

      <iframe src="Sys_Load.cgi"></iframe>

      Hi, Your above code made my life easier.

      Anyway, I changed it in following way since I think without border is better. Pls hv a look at.

      <iframe src = "Sys_Load.cgi" style="border:none;"></iframe>

      >> Unless what you are doing is a homework problem, perhaps you should consider looking for existing solutions for building a dashboard with machine usage statistics.

      I do need server statistics. How can build a dashboard. Where can I hv docs for it?

      What about these?

      https://metacpan.org/pod/GD::Dashboard

      https://metacpan.org/release/GD-Dashboard

      Or do you have some others docs. Pls share

        Yes, that's one of the ways to disable the border. This usage is fine.

        Speaking of existing monitoring solutions, take a look at Munin. It generates an HTML page with plots and it's even written in Perl.

Re: How to PRINT CGI html table to a PNG file
by Anonymous Monk on Aug 22, 2018 at 04:59 UTC
    I'm just going to show you how to use CGI;

    In your program you do "use CGI" but then you don't actually use it! By using ':standard' it gives you an easy way to type HTML that is much cleaner than HTML so it's easy to understand and edit. Here are your 2 exact programs using all the juicy CGI shortcuts for HTML.

    home.cgi:

    #!/usr/bin/perl use strict; use warnings; use CGI qw/:standard center/; my $date = localtime; print header.start_html(-bgcolor=>'#c0e0f9').br. center( h1('Home'). h3($date). img({src=>'root_partition.cgi' }).br.br.br. img({src=>'var_partition.cgi' }).br.br.br. img({src=>'stacked_Bar_graph.cgi' }).br.br.br. img({src=>'topsenders_hbar.cgi' }).br.br.br. img({src=>'toprecipients_hbar.cgi'}).br.br.br ).end_html;
    Sys_Load.cgi:
    #!/usr/bin/perl use strict; use warnings; use CGI ':standard'; use Sys::Load qw/getload uptime/; my ($one_m, $five_m, $fifteen_m) = (getload())[0,1,2]; print header.start_html(-bgcolor=>'#c0e0f9'). table({-style=>'width:20%'},caption('Load Average'), Tr( th({-bgcolor=>'#FFDD00',-height=>'25'},'1 Minute' ). th({-bgcolor=>'#FFDD00',-height=>'25'},'5 Minutes' ). th({-bgcolor=>'#FFDD00',-height=>'25'},'15 Minutes') ), Tr({-style=>'font-family:veranda',-bgcolor=>'#FFFFFF'}, td($one_m), td($five_m), td($fifteen_m) ) ).end_html;
    CGI is awesome. Enjoy!

      It would also only be fair to theravadamonk to point out that the documentation for that very module says, "HTML Generation functions should no longer be used".

        CGI is a core module on millions of computers. Apple ships Perl 5.18.2/CGI with macOS on brand-new Macs as one example. There are countless nodes on Perlmonks recommending and teaching CGI. Just because some newbie hijacked Perl's most popular and useful module does not mean you weirdos who troll every CGI node don't look ridiculous. CGI.pm represents 17 years of development by a huge number of people and trying to throw it away is disrespectful nonsense.

        This is not a video game.
        CGI is not the bad guy.
        Even though you get points here for killing him.

        Points! And an imaginary cup of coffee...

Log In?
Username:
Password:

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

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

    No recent polls found