#!/usr/bin/perl # systemstatus.cgi = shows a servers usage via CGI and a web browser # Copyright (c) 1999 Jeremy C. Reed. All Rights Reserved. # license change 2001 # This program is free software; you may redistribute it and/or modify # it as long as you retain this entire copyright and notice. # This software is provided as is and the author is not liable # for anything resulting from its use. # was converting GMT to local use Time::Local; #*timelocal::cheat = \&Time::Local::cheat; #$time_difference = 28800; ## seconds from GMT $systemstatus_cgi_version = "0.21"; $html_refresh = ''; ## use a number for seconds for html meta refresh $bgcolor='WHITE'; $title_color = 'GREEN'; $text_color = 'BLACK'; $link_color = 'BLUE'; $vlink_color = 'PURPLE'; $warn_color = 'ORANGE'; $danger_color = 'RED'; $fixed_font_face = 'Clean, Fixed, Courier New, Courier, Terminal, Screen'; #$httpd_log_file = "/var/log/apache-httpd/access_log"; $httpd_log_file = '/var/log/boa/access_log'; $tail_amt = 250; ## set to check for last amount from httpd_log_file if ($html_refresh) { $meta_tag = ""; } @months = ("January","February","March","April","May","June","July","August","September","October","November","December"); @short_months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); ($c_seconds,$c_minutes,$c_hour,$c_day,$c_month,$c_year,$c_wday,$c_yday,$c_isdst) = localtime(time); ## get current time and date info $c_year = 1900 + $c_year; if ($c_minutes < 10) { $c_minutes = "0$c_minutes"; } print "Content-type: text/html\n\n"; print qq{ $ENV{SERVER_NAME} -- systemstatus $meta_tag
[General | HTTPD/WWW | Memory | File System | Users | Processes]

Status for $ENV{SERVER_NAME} as of $c_hour:$c_minutes on $months[$c_month] $c_day, $c_year

}; &uptime; print "
\n"; &www_status; print "
\n"; &display_free; print "
\n"; &free_disk_space; print "
\n"; &who; print "
\n"; &processes; print qq{
[General | HTTPD/WWW | Memory | File System | Users | Processes]

The source code is freely available.
http://www.reedmedia.net/projects/systemstatus/ Version $systemstatus_cgi_version
}; ## end of main sub uptime { chop($uptime = `uptime`); $uptime =~ s/\t/ /g; $uptime =~ s/\s/\ \;/g; print qq{ System status $uptime

}; } ## uptime sub www_status { $total = 0; $today_requests = 0; $month_requests = 0; $currently = "$short_months[$c_month]\/$c_year"; open (LOG, "$httpd_log_file") || &error ("Could not open $httpd_log_file file\n"); while ($line = ) { if ($line =~ /$currently/) { $month_requests++; if ($line =~ /$c_day\/$currently/) { $today_requests++; } } if ($total == 0) { ($host, $rfc931, $authuser, $timestamp, $request, $status, $bytes) = $line =~ /^(\S+) (\S+) (\S+) \[(.+)\] \"(.+)\" (\S+) (\S+)\s/; ($day, $month, $year, $hour, $minute, $seconds, $timezone) = split ("\/|\:|\ ", $timestamp); $_ = $timestamp; ($d,$m,$y,$hh,$mm,$ss) = m|(\d+)/(\w\w\w)/(\d+):(\d+):(\d+):(\d+).*|; $m=index("JanFebMarAprMayJunJulAugSepOctNovDec",$m)/3; # $real_time= &timelocal($ss,$mm,$hh,$d,$m,$y-1900) + $time_difference; $epoch_time = &timelocal ($ss,$mm,$hh,$d,$m,$y-1900); # ($seconds,$minutes,$hour,$day,$month,$year,$wday,$yday,$isdst) = # localtime($real_time); if ($minutes < 10) { $minutes = "0$minutes"; } # $year = 1900 + $year; $timestamp = "$hour:$minute on $months[$m] $day, $year"; # $timestamp = "$hour:$minute on $months[$month] $day, $year"; } $total++; } close(LOG); $total_days = ((time - $epoch_time) / 86400); $total_days = sprintf ("%.0f", $total_days); if ($total_days > 0) { $requests_per_day = ($total / $total_days); $requests_per_day = sprintf ("%.0f", $requests_per_day); if ($requests_per_day > 0) { $requests_per_day = "This works out to about $requests_per_day requests per day.
" } else { $requests_per_day = "This is less than one request per day.
"; } } print qq{ $ENV{SERVER_NAME} HTTPD Status
Since $timestamp ($total_days days ago), the web server has had $total requests.
$requests_per_day Today, it already has $today_requests requests.
And, this month the server has had $month_requests requests.

}; } ## www_status sub display_free { @info = `free -o`; $line = shift @info; ## get rid of header $line = shift @info; my ($type, $total, $used, $free, $shared, $buffers, $cached) = split(/\s+/, $line); $capacity = (($used/$total) * 100); if ($capacity < 50) { $color = $text_color; } elsif ($capacity < 90) { $color = $warn_color; } else { $color = $danger_color; } print "\n"; print "Memory Usage   "; printf ("%3.0f%% memory used,", $capacity); print qq{ $free K free
              (shared: $shared, buffers: $buffers, cached: $cached).
}; $line = shift @info; my ($type, $total, $used, $free, $shared, $buffers, $cached) = split(/\s+/, $line); $capacity = (($used/$total) * 100); if ($capacity < 50) { $color = $text_color; } elsif ($capacity < 90) { $color = $warn_color; } else { $color = $danger_color; } print "Swap Usage   "; printf ("%3.0f%% used", $capacity); print ", $free K free.

\n"; } ## display_free sub free_disk_space { @df = `df -h`; $title_line = shift @df; $title_line =~ s/\t/ /g; $title_line =~ s/\s/\ \;/g; print "\n"; print "$title_line
\n"; foreach $line (@df) { my ($dev, $size, $used, $avail, $capacity, $mount) = split(/\s+/, $line); $line =~ s/\t/ /g; $line =~ s/\s/\ \;/g; $capacity =~ s/%//; if ($capacity < 50) { $color = $text_color; } elsif ($capacity < 90) { $color = $warn_color; } else { $color = $danger_color; } $line =~ s/$capacity%/\$capacity%\<\/FONT\>/; print "$line
\n"; } ## for each line print "
\n"; } ## free_disk_space sub who { @who = `w`; shift @who; $title_line = shift @who; $title_line =~ s/\t/ /g; $title_line =~ s/\s/\ \;/g; print "\n"; print "$title_line\n"; foreach $w (@who) { ## commented out code is for setting up warning for idle time over 4 hours ## but not done because havne't parsed info correctly # ($user, $tty, $from, $login, $idle, $jcpu, $pcpu, $what) = # split (/\s\s+/, $w); # $idle =~ /(\d+)\:(\d+)(.)$/; # $sec_or_min = $3; # print "$sec_or_min SM\n"; $w =~ s/\t/ /g; $w =~ s/\s/\ \;/g; print "
\n$w\n"; # print "idle: $idle
\n"; } } ## who sub processes { open (PS,"/bin/ps aux | /usr/bin/sort -rn +4 |"); @ps_info = ; close (PS); $title_line = pop (@ps_info); ## get rid of last line $title_line =~ s/\t/ /g; $title_line =~ s/\s/\ \;/g; print qq{ $title_line
}; foreach $line (@ps_info) { $line =~ s/\t/ /g; $line =~ s/\s/\ \;/g; print "$line
\n"; } print "
\n"; } ## processes sub error { print "$_[0]
\n"; exit; }