#!/usr/bin/perl ## upd.pl - update rrdtool rrds & graphs ## By //YorHel-kun use strict; ## Import vars from the BEGIN (see below) our $DEBUG; our $scriptstart; ## /me wants to know how long this scripts executes, ## this includes loading the modules, so start in a BEGIN :) BEGIN { our $DEBUG = $ARGV[0] || 0; $DEBUG && require Time::HiRes; $DEBUG && ( our $scriptstart = [Time::HiRes::gettimeofday()] ); } use RRDp; use Linux::stat; use Sys::Uptime; use Net::HTTP; use Linux::net::dev; my @graphs = ( [ "1day", 300, 574, "HOUR:1:HOUR:6:HOUR:6:0:%H", "Hours ->", "past 2 days" ], [ "2week", 1800, 670, "DAY:1:WEEK:1:DAY:1:86400:%a", "", "past 2 weeks" ], [ "3month", 7200, 730, "DAY:1:WEEK:1:WEEK:1:604800:%W", "Weeks ->", "past 2 months" ], [ "4year", 43200, 1458, "MONTH:1:YEAR:1:MONTH:2:2592000:%b", "", "past 2 years" ] ); my($rdir, $gdir) = ("/var/www/lero/rrds", "/var/www/lero/graphs"); my $date = `date`; $date =~ s/\:/\\\:/g; my $allops = qq ( --imgformat PNG --height 200 --width 600 --font DEFAULT:8:/home/alemao/andalemono --color BACK#DDDDDD --color SHADEA#DDDDDD --color SHADEB#DDDDDD ); mkdr($gdir) if !-d $gdir; RRDp::start "/usr/bin/rrdtool"; ##### - Apache requests/second - ##### $DEBUG && print "\nApache req/sec:\n"; my $requests = 0; my $httpd_kbytes = 0; ## for Apache bandwidth usage, see below if( my $req = Net::HTTP->new(Host => "localhost") ) { $req->write_request(GET => "/server-status/?auto"); my $data; $req->read_entity_body($data, 1024) if $req->read_response_headers; $requests = $1 if $data =~ /Total\s*Accesses\:\s*([0-9]+)/mi; $httpd_kbytes = $1 if $data =~ /Total\s*kBytes\:\s*([0-9]+)/mi; } $DEBUG && print " updating httpdreq.rrd with requests=$requests\n"; RRDp::cmd qq(update $rdir/httpdreq.rrd N:$requests ); my $answer = RRDp::read; $DEBUG && print " update httpdreq.rrd returned: $$answer\n"; $DEBUG && print " Creating graphs: httpdreq-day, httpdreq-week, httpdreq-month & httpdreq-year\n"; foreach my $cg (@graphs) { my @cg = @$cg; my $end = int(time/$cg[1])*$cg[1]; my $start = $end-(1*$cg[1]*$cg[2]); RRDp::cmd qq (graph $gdir/20httpdreq-$cg[0].png $allops --end $end --start $start --step $cg[1] --vertical-label "Requests/second" --title "//Apache requests/second - $cg[5]" --x-grid $cg[3] --alt-y-grid --units-exponent 0 --alt-autoscale-max --lower-limit 0 DEF:r=$rdir/httpdreq.rrd:httpd_requests:AVERAGE COMMENT:"$cg[4] \\r" LINE1:r#FF0000:"Requests/second" GPRINT:r:LAST:" Current\\: %5.2lf " GPRINT:r:AVERAGE:"Avg\\: %5.2lf " GPRINT:r:MIN:"Min\\: %5.2lf " GPRINT:r:MAX:"Max\\: %5.2lf \\n" COMMENT:"\\n" COMMENT:"Last Updated\\: $date\\r" ); my $answer = RRDp::read; $$answer =~ tr/\r\n/ /; $DEBUG && print " graph 20httpdreq-$cg[0].png returned: $$answer\n"; } ##### - Apache bandwidth usage - ##### $DEBUG && print "\nApache bandwidth:\n"; my $bytes = $httpd_kbytes*1024; $DEBUG && print " updating httpdnet.rrd with httpd_bytes=$bytes\n"; RRDp::cmd qq(update $rdir/httpdnet.rrd N:$bytes ); my $answer = RRDp::read; $DEBUG && print " update httpdnet.rrd returned: $$answer\n"; $DEBUG && print " Creating graphs: httpdnet-day, httpdnet-week, httpdnet-month & httpdnet-year\n"; foreach my $cg (@graphs) { my @cg = @$cg; my $end = int(time/$cg[1])*$cg[1]; my $start = $end-(1*$cg[1]*$cg[2]); RRDp::cmd qq (graph $gdir/21httpdnet-$cg[0].png $allops --end $end --start $start --step $cg[1] --vertical-label "Bytes/second" --title "//Apache bandwidth usage - $cg[5]" --x-grid $cg[3] --alt-y-grid --base 1024 --alt-autoscale-max --lower-limit 0 DEF:r=$rdir/httpdnet.rrd:httpd_bytes:AVERAGE COMMENT:"$cg[4] \\r" LINE1:r#FF0000:"Bytes/second" GPRINT:r:LAST:" Current\\: %6.2lf %sb " GPRINT:r:AVERAGE:"Avg\\: %6.2lf %sb " GPRINT:r:MIN:"Min\\: %6.2lf %sb " GPRINT:r:MAX:"Max\\: %6.2lf %sb\\n" COMMENT:"\\n" COMMENT:"Last Updated\\: $date\\r" ); my $answer = RRDp::read; $$answer =~ tr/\r\n/ /; $DEBUG && print " graph 21httpdnet-$cg[0].png returned: $$answer\n"; }