#!/usr/bin/perl -w # uptime.pl reads the system uptime in seconds since the Epoch from /proc/uptime and # converts it into textual form. This script is located at # # The output is only an HTML fragment that can be inserted to a web page with an SSI tag: # # Copyright 2003-2005 by Susanna Winter # Version history: # v0.1 - Jan 2003: initial version # v0.2 - Mar 2005: switched language to English use strict; use Time::Local; my $proc_uptime = "/proc/uptime"; ### main open(INF, $proc_uptime); my $up = ; close(INF); chomp $up; $up =~ s/\.[0-9]{2}\s+.*//; my ($sec,$minute,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); my $weekday = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')[ (localtime)[6] ]; $mon = $mon + 1; $mon = ('January','February','March','April','May','June','July','August','September','October','November','December')[ (localtime)[4] ]; $year = $year + 1900; if ($sec < 10) { $sec = "0" . $sec; } if ($minute < 10) { $minute = "0" . $minute; } if ($hour < 10) { $hour = "0" . $hour; } my $avg_month = (365/12); my $up_years = int($up/(24*60*60*365)); my $up_months = ($up/(24*60*60*$avg_month))%12; my $up_days = ($up/(24*60*60))%365; if ($up_months > 0) { $up_days = int($up_days - ($up_months * $avg_month)); } my $up_hours = ($up/(60*60))%24; my $up_mins = ($up/60)%60; my $up_secs = $up%60; my $up_string = ""; if ($up_years > 1) { $up_string = "$up_years years"; } elsif ($up_years == 1) { $up_string = "$up_years year"; } unless (($up_string eq "") || ($up_months == 0)) { $up_string = $up_string . ", "; } if ($up_months > 1) { $up_string = $up_string . "$up_months months"; } elsif ($up_months == 1) { $up_string = $up_string . "$up_months month"; } unless (($up_string eq "") || ($up_days == 0)) { $up_string = $up_string . ", "; } if ($up_days > 1) { $up_string = $up_string . "$up_days days"; } elsif ($up_days == 1) { $up_string = $up_string . "$up_days day"; } unless (($up_string eq "") || ($up_hours == 0)) { $up_string = $up_string . ", "; } if ($up_hours > 1) { $up_string = $up_string . "$up_hours hours"; } elsif ($up_hours == 1) { $up_string = $up_string . "$up_hours hour"; } unless (($up_string eq "") || ($up_mins == 0)) { $up_string = $up_string . ", "; } if ($up_mins > 1) { $up_string = $up_string . "$up_mins minutes"; } elsif ($up_mins == 1) { $up_string = $up_string . "$up_mins minute"; } unless (($up_string eq "") || ($up_secs == 0)) { $up_string = $up_string . " and "; } if ($up_secs > 1) { $up_string = $up_string . "$up_secs seconds"; } elsif ($up_secs == 1) { $up_string = $up_string . "$up_secs second"; } $up_string = $up_string . "."; print "Content-type:text/html\n\n"; print "Uptime $weekday $mday $mon, $year at [$hour:$minute:$sec]: $up_string\n";