#!/usr/bin/perl -w # # tinygraph -- a RRDtool frontend for tinydns statistics # Copyright (C) 2005 Ulrich Zehl # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA use strict; use RRDs; use POSIX qw(uname); my $VERSION = "0.1"; my $host = 'ns.topfen.net'; my $scriptname = 'tinygraph.cgi'; my $xpoints = 750; my $points_per_sample = 3; my $ypoints = 250; my $ypoints_err = 96; my $rrd = '/home/www/admin/stats/dns/tiny/tinygraph.rrd'; # path to where the RRD database is my $tmp_dir = '/tmp/tinygraph'; # temporary directory where to store the images my $rrdtool_1_0 = ($RRDs::VERSION < 1.199908); my @graphs = ( { title => 'Day Graph', seconds => 3600*24, }, { title => 'Week Graph', seconds => 3600*24*7, }, { title => 'Month Graph', seconds => 3600*24*31, }, { title => 'Year Graph', seconds => 3600*24*365, }, ); # These are all available query types (see also tinygraph.pl); if you don't want # some of them graphed, just delete them. #my @querytypes = qw (A NS CNAME SOA PTR HINFO MX TXT RP SIG KEY AAAA A6 IXFR AXFR ANY); my @querytypes = qw (A NS CNAME SOA PTR MX TXT AAAA A6 ANY); my %color = ( A => 'FF0080', NS => 'FF8000', CNAME => 'ff00ff', SOA => '00ffff', PTR => '8080C0', MX => 'AA0000', AAAA => 'ffff00', ANY => 'ff0000', other => '0000ff', ); sub graph($$$) { my $range = shift; my $file = shift; my $title = shift; my $step = $range*$points_per_sample/$xpoints; #my $date = localtime(time); #$date =~ s|:|\\:|g unless $rrdtool_1_0; my @rrdef = map {( "DEF:$_=$rrd:$_:AVERAGE", "DEF:m$_=$rrd:$_:MAX", #"CDEF:r$_=$_,60,*", "CDEF:d$_=$_,UN,0,$_,IF,$step,*", "CDEF:s$_=PREV,UN,d$_,PREV,IF,d$_,+" )} @querytypes; my @rrprint; foreach my $qt (sort @querytypes) { my $qts = sprintf('%7s', $qt); push @rrprint, "AREA:$qt#" . ($color{$qt} || '000000') . ":$qts:STACK"; push @rrprint, "GPRINT:s$qt:MAX:total\\: %6.0lf q"; push @rrprint, "GPRINT:$qt:AVERAGE:avg\\: %.2lf q/s"; push @rrprint, "GPRINT:m$qt:MAX:max\\: %.0lf q/s\l"; # if you want q/m instead of q/s # push @rrprint, "GPRINT:s$qt:MAX:total\\: %8.0lf q"; # push @rrprint, "GPRINT:r$qt:AVERAGE:average\\: %.2lf q/m"; # push @rrprint, "GPRINT:rm$qt:MAX:max\\: %.0lf q/m\\l"; } my ($graphret,$xs,$ys) = RRDs::graph($file, '--imgformat', 'PNG', '--width', $xpoints, '--height', $ypoints, '--start', "-$range", '--vertical-label', 'queries/second', '--lower-limit', 0, '--units-exponent', 0, # don't show milli-messages/s #'--lazy', '--color', 'SHADEA#ffffff', '--color', 'SHADEB#ffffff', '--color', 'BACK#ffffff', @rrdef, @rrprint, ); my $ERR=RRDs::error; die "ERROR: $ERR\n" if $ERR; } sub print_html() { print < tinydns Statistics for $host

tinydns Statistics for $host

HEADER for my $n (0..$#graphs) { print "\t

$graphs[$n]{title}

\n"; print "\t\"$graphs[$n]{title}\"\n\n"; } my $date = localtime(time); print < Created by tinygraph $VERSION on $date.
Based on Mailgraph and bindgraph; built using RRDtool. FOOTER } sub send_image($) { my $file = shift; -r $file or do { print "Content-type: text/plain\n\nERROR: can't find $file\n"; exit 1; }; print "Content-type: image/png\n"; print "Content-length: ".((stat($file))[7])."\n"; print "\n"; open(IMG, $file) or die; my $data; print $data while read IMG, $data, 1024; } sub main() { my $uri = $ENV{REQUEST_URI} || ''; $uri =~ s/\/[^\/]+$//; $uri =~ s/\//,/g; $uri =~ s/(\~|\%7E)/tilde,/g; mkdir ($tmp_dir, 0777) unless (-d $tmp_dir); mkdir ("$tmp_dir/$uri", 0777) unless (-d "$tmp_dir/$uri"); my $img = $ENV{QUERY_STRING}; if(defined $img and $img =~ /^(\d+)$/) { my $file = "$tmp_dir/$uri/tinygraph-$1.png"; graph($graphs[$1]{seconds}, $file, $graphs[$1]{title}); send_image($file); } else { print_html; } } main;