#!/usr/bin/perl -w # httprequests.pl gets the number of HTTP requests from Apache access_log file, and uses # RRDtool to save the data in a Round Robin Database and to display it graphically. The # results are shown in two images: one for the last 24 hours and the other for the # current month. The images are updated every 10 minutes. # Copyright 2002-2006 by Susanna Winter # Version history: # v0.1 - Initial version in 2002 # v0.2 - March 2003: colors in parameters, added version history # v0.2.1 - April 2005: better y-axis scaling # v0.2.2 - February 2006: updated to be compatible with RRDtool 1.2.11, added trendlines and removed horizontal rules use strict; use Time::Local; ### files my $log = "/path/to/access.log"; my $rrd = "/path/to/httprequests.rrd"; my $png = "/path/to/httprequests.png"; my $png2 = "/path/to/httprequests_monthly.png"; ### colors my $color_http = "#9499AC"; my $color_font = "#000000"; my $color_daily = "#A80C0E"; my $color_monthly = "#0C12B7"; my $color_grid = "#8A91AF"; my $color_arrow = "#48627D"; my $color_mgrid = "#000000"; my $color_bg = "#FFFFFF"; my $color_shadea = "#FFFFFF"; my $color_shadeb = "#FFFFFF"; my $color_frame = "#7B819C"; my $color_canvas = "#E6EAF6"; my $wc=0; $wc = `wc -l $log`; $wc =~ s/^\s+//; ($wc, my $junk) = split(/ /, $wc); `rrdtool update $rrd N:$wc`; my @temp; my $min=0; my $max=0; my $avg=0; my $last=0; my $min_month=0; my $max_month=0; my $avg_month=0; my $last_month=0; @temp = `rrdtool graph /dev/null --start -25h --end +1h DEF:http=$rrd:http:AVERAGE CDEF:http2=http,60,* PRINT:http2:MAX:%.2lf PRINT:http2:MIN:%.2lf PRINT:http2:AVERAGE:%.2lf PRINT:http2:LAST:%.2lf`; foreach (@temp) { chomp $_; } $max = $temp[1]; $min = $temp[2]; $avg = $temp[3]; $last = $temp[4]; my ($sec,$minute,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); my $start_time = 0; $start_time = timelocal (0, 0, 0, 1, $mon, $year); my $end_time = 0; if ($mon == 11) { $end_time = timelocal (0, 0, 0, 1, 0, $year+1); } else { $end_time = timelocal (0, 0, 0, 1, $mon+1, $year); } @temp = `rrdtool graph /dev/null --start $start_time --end $end_time DEF:http=$rrd:http:AVERAGE CDEF:http2=http,60,* PRINT:http2:MAX:%.2lf PRINT:http2:MIN:%.2lf PRINT:http2:AVERAGE:%.2lf PRINT:http2:LAST:%.2lf`; foreach (@temp) { chomp $_; } $max_month = $temp[1]; $min_month = $temp[2]; $avg_month = $temp[3]; $last_month = $temp[4]; my $month = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')[ (localtime)[4] ]; my $month2 = ('January','February','March','April','May','June','July','August','September','October','November','December')[ (localtime)[4] ]; $year += 1900; if ($hour < 10 ) { $hour = "0" . $hour}; if ($minute < 10 ) { $minute = "0" . $minute}; my $my_time = "$mday $month $year $hour\\:$minute"; `rrdtool graph $png -a PNG --start -25h --end +1h -E -B 7 -R normal -n TITLE:12: -c CANVAS$color_canvas -c BACK$color_bg -c FONT$color_font -c MGRID$color_mgrid -c GRID$color_grid -c SHADEA$color_shadea -c SHADEB$color_shadeb -c ARROW$color_arrow -t \"HTTP Requests - Last 24 hours\" -w 370 -h 130 -M -X 0 -L 5 -Y -x HOUR:2:HOUR:6:HOUR:2:0:%H -l 0 DEF:http=$rrd:http:AVERAGE CDEF:http2=http,60,* COMMENT:\"min $min max $max last $last\\c\" COMMENT:\"daily avg $avg monthly avg $avg_month\\c\" COMMENT:\"\\n\" AREA:http2$color_http:\"HTTP requests per minute\\l\" LINE1:http2$color_arrow CDEF:smoothed=http2,3600,TREND LINE1:smoothed$color_mgrid:\"60 min moving average\\l\" COMMENT:\"$my_time\\r\"`; `rrdtool graph $png2 -a PNG --start $start_time --end $end_time -E -B 7 -R normal -n TITLE:12: -Y -x DAY:2:WEEK:1:WEEK:1:86400:"%d" -l 0 -w 370 -h 130 -M -X 0 -L 5 -c CANVAS$color_canvas -c BACK$color_bg -c FONT$color_font -c MGRID$color_mgrid -c GRID$color_grid -c SHADEA$color_shadea -c SHADEB$color_shadeb -c ARROW$color_arrow -t \"HTTP Requests - $month2 $year\" DEF:http=$rrd:http:AVERAGE CDEF:http2=http,60,* COMMENT:\"min $min_month max $max_month last $last_month\\c\" COMMENT:\"daily avg $avg monthly avg $avg_month\\c\" COMMENT:\"\\n\" AREA:http2$color_http:\"HTTP requests per minute\\l\" LINE1:http2$color_arrow CDEF:smoothed=http2,43200,TREND LINE1:smoothed$color_mgrid:\"12 hour moving average\\l\" COMMENT:\"$my_time\\r\"`;