Displaying Server Uptime on the Top Page

« Previous Next »

http://fedorasrv.com/

Using this page as a reference, I set up the server uptime to display on the server’s top page.



Here’s how I did it…

uptime.cgi

#!/usr/bin/perl

use Shell qw(uptime);

$temp=uptime;
@tmp=split(/up/,$temp);
@time=split(/,/,$tmp[1]);
if(index($time[0],"day") != -1){
        @tmp=split(/day/,$time[0]);
        $day=$tmp[0];
        if(index($time[1],"min") != -1){
                $hour=0;
                @tmp=split(/min/,$time[1]);
                $min=$tmp[0];
        } else {
                @tmp=split(/:/,$time[1]);
                $hour=$tmp[0];
                $min=$tmp[1];
        }
        $day=~ s/ //g;
        $min=~ s/ //g;
        $hour=~ s/ //g;
        $data=$day." days and ".$hour." hours ".$min." minutes";
        } elsif(index($time[0],":") != -1){
                @tmp=split(/:/,$time[0]);
                $hour=$tmp[0];
        $min=$tmp[1];
        $hour=~ s/ //g;
        $min=~ s/ //g;
        $data=$hour." hours ".$min." minutes";
        } elsif(index($time[0],"min") != -1){
                @tmp=split(/min/,$time[0]);
                $min=$tmp[0];
        $min=~ s/ //g;
        $data=$min." minutes";
}


print "Content-type:text/html\n\n";
print<<"EOF";
$data
EOF

exit;

Make the HTML file where you want to display the uptime SSI-compatible,

e.g., rename it to *.shtml, or add +Includes to .htaccess (IncludesNoExec won't work)



Then inside the page, write:

h.keikun.info has been running continuously for <!--#exec cgi="uptime.cgi"-->.

Something like that.



This will display the time since the server last started up.

Leave a Comment

Your email address will not be published. Required fields are marked *