#!/usr/bin/perl -w use strict; use warnings; use RRDs 1.2012; use HTTP::Date 1.47 qw(time2iso); my $db_dir = 'db-new'; my $img_dir = 'img'; my @ranges = qw(1d 1w 1m 1y); #my @ranges = qw(1d); #my @ranges = qw(1y); sub error { my ($str, @args) = @_; if ($#args >= 0) { $str = sprintf $str, @args; } $str =~ s/\n+$//; printf STDERR "%s: %s\n", $0, $str; exit 1; } sub graph { my ($rrd_in, $rrd_out, $range, $title, $file_prefix) = @_; $title = sprintf "%s, %s", $title, $range; printf "%s\n", $title; my $file = sprintf "%s/%s-%s.png", $img_dir, $file_prefix, $range; my $temp_file = sprintf "%s.tmp", $file; my $time = time2iso; $time =~ s/:/\\:/g; RRDs::graph( $temp_file, '--imgformat', 'PNG', '--width', 500, '--height', 150, '--start', sprintf("-%s", $range), '--title', $title, '--vertical-label', 'Network traffic bit/s', '--base', 1000, '--slope-mode', '--interlaced', '--alt-y-grid', '--units-length', 10, sprintf("DEF:inbytes=%s/%s:bytes:AVERAGE", $db_dir, $rrd_in), sprintf("DEF:outbytes=%s/%s:bytes:AVERAGE", $db_dir, $rrd_out), 'CDEF:inbits=inbytes,8,*', 'CDEF:outbits=outbytes,8,*', 'CDEF:outbits_neg=outbits,-1,*', 'COMMENT:\s', 'COMMENT:\s', 'COMMENT:\s', 'COMMENT:\s', 'COMMENT: Max Avg Last\s', 'COMMENT:\s', 'COMMENT:\s', 'COMMENT:\s', 'AREA:inbits#74D2F1:incoming ', 'GPRINT:inbits:MAX:%8.1lf %s', 'GPRINT:inbits:AVERAGE:%8.1lf %s', 'GPRINT:inbits:LAST:%8.1lf %sbit/s\s', 'COMMENT:\s', 'COMMENT:\s', 'LINE1:inbits#45709E:', 'AREA:outbits_neg#6DF8BE:outgoing ', 'GPRINT:outbits:MAX:%8.1lf %s', 'GPRINT:outbits:AVERAGE:%8.1lf %s', 'GPRINT:outbits:LAST:%8.1lf %sbit/s\s', 'COMMENT:\s', 'COMMENT:\s', 'LINE1:outbits_neg#4CAC84:', 'HRULE:0#000000', sprintf("COMMENT:%s%s", ' ' x 65, $time), ); my $error = RRDs::error; error "RRDs error: %s", $error if $error; unless (rename $temp_file, $file) { error "can't move '%s' to '%s'", $temp_file, $file; } } unless (opendir D, $db_dir) { error "can't open %s: %s", $db_dir, $!; } my @files = sort readdir D; closedir D; my @files_in = grep { /^81\.20\.144\.17_.+-IN_.+\.rrd$/ } @files; my @files_out = grep { /^81\.20\.144\.17_.+-OUT_.+\.rrd$/ } @files; unless ($#files_in == $#files_out) { error 'invalid file count'; } print "\n"; my $graph_count = 0; foreach (@files_in) { my $rrd_in = $_; my $rrd_out = shift @files_out; my $file_prefix = $rrd_in; $file_prefix =~ s/^81\.20\.144\.17_//; $file_prefix =~ s/(?:IN|OUT)_//; $file_prefix =~ s/\.rrd$//; my $title = $file_prefix; $title =~ s/-/ /g; foreach my $range (@ranges) { $graph_count++; graph( $rrd_in, $rrd_out, $range, $title, $file_prefix, ); } print "\n"; } printf "\nGenerated %d graph%s in %ds\n\n", $graph_count, $graph_count != 1 ? 's' : '', time - $^T;