1 #!/usr/bin/perl
2 # Explore rrd via clickable graphs by. james@type-this.com (Claus Norrbohm)
4 # Basic idea: click high -> zoom out, click low -> zoom in,
5 # click left -> back history, click right -> forward history
7 use CGI::Carp;
8 use CGI;
9 use POSIX;
10 use lib qw( /usr/local/rrdtool/lib/perl );
11 use RRDs;
13 my $query = new CGI;
15 # modify as needed
16 $hight = 300; # Image size
17 $width = 600;
18 $refresh = 3600;
19 $expiredate = strftime "%a, %e %b %Y %H:%M:%S GMT", gmtime(time); # Format date(now)
20 $root = $ENV{"DOCUMENT_ROOT"}; # Location of rrd
22 print "Content-type: text/html\n"; # Use html
23 print "Cache-Control: no-cache\n"; # Ensure no cashing of page
24 print "Expires: $expiredate\n"; # Expire now
25 print "Refresh: $refresh\n\n";
27 print $query->start_html("Clickable rrd-graph"); # Title of html page
29 if ($query->param()) { # the form has already been filled out
31 $rrd = $query->param("rrd"); # which rrd file are we tracking
32 $start = $query->param("start"); # Start time
33 $end = $query->param("end"); # End time
34 $x = $query->param("img.x"); # x/y cordinates of click
35 $y = $query->param("img.y");
37 # see contrib/rrdfetchnames
38 my ($begin,$step,$name,$data) = RRDs::fetch "$root$rrd","AVERAGE","--start","now","--end","start+1";
39 if ( my $ERR = RRDs::error) {
40 die "ERROR while fetching data from $NAME $ERR\n";
41 }
42 @names = @$name; # list of DS's "@$name"
43 $j = @names;
44 $esu = "";
46 for ($i = 0; $i < $j; $i++) { # here we find which DS we are curently tracking
47 if ($query->param("@names[$i]") == "1") {
48 @use[$i] = 1;
49 $esu .= "1"; # DS included
50 } else {
51 @use[$i] = 0;
52 $esu .= "0"; # DS not included
53 }
54 }
56 $intv = $end - $start; # Last used interval
57 $zoom = ($hight + 100 - $y) / $hight; # Find zoom factor + 100 because hight is not exact
58 $center = $start + $intv * $x / $width; # Find time corresponding to click
60 $start = int($center - $intv * $zoom); # Calc new start
61 $end = int($center + $intv * $zoom); # Calc new end
63 } else { # first time through, so present clean form
65 $rrd = $ENV{"REQUEST_URI"}; # Location of rrd
67 $end = time(); # use now for end
68 $start = $end - 86400; # and go back 24 hours
70 # see rrdfetchnames
71 my ($begin,$step,$name,$data) = RRDs::fetch "$root$rrd","AVERAGE","--start","now","--end","start+1";
72 if ( my $ERR = RRDs::error) {
73 die "ERROR while fetching data from $NAME $ERR\n";
74 }
75 @names = @$name; # list of DS's "@$name"
76 $j = @names;
77 $esu = "";
79 for ($i = 0; $i < $j; $i++) { # All DS is included first time
80 @use[$i] = 1;
81 $esu .= "1";
82 }
84 }
86 # Create a form with clickable image see page xxx in: Wallace, Shawn P.
87 # Programming Web Graphics with Perl
88 # and GNU Software
89 # O'Reilly UK,1999, UK, Paperback
91 print "<FORM ACTION=\"$rrd\">\n";
93 print "<TABLE border=\"0\">\n";
94 print "<TR><TD colspan=\"3\" align=\"center\">Click on top to zoom out</TD></TR>\n";
95 print "<TR><TD align=\"right\">Click<BR>left<BR>to<BR>go<BR>back<BR>in<BR>time</TD>";
96 print "<TD>\n";
97 # png.cgi prints the rrd graph, by printing to std.out (browser)
98 print "<INPUT TYPE=\"image\" NAME=\"img\" SRC=\"/cgi-bin/png.cgi?rrd=$rrd&start=$start&end=$end&hight=$hight&width=$width&use=$esu\">\n";
99 print "<INPUT TYPE=\"hidden\" NAME=\"start\" VALUE=\"$start\">\n";
100 print "<INPUT TYPE=\"hidden\" NAME=\"end\" VALUE=\"$end\">\n";
101 print "<INPUT TYPE=\"hidden\" NAME=\"rrd\" VALUE=\"$rrd\">\n";
102 print "</TD>";
103 print "<TD align=\"left\">Click<BR>right<BR>to<BR>go<BR>forward<BR>in<BR>time</TD></TR>\n";
104 print "<TR><TD colspan=\"3\" align=\"center\">Click on bottom to zoom in</TD></TR>\n";
105 print "</TABLE>\n";
107 print "<BR><HR><BR><TABLE border=\"0\"></TD><TD>Select / Deselect DS: </TD>\n";
108 for ($i = 0; $i < $j; $i++) { # present user with list of DS to select / deselect
109 if (@use[$i] == 0) {
110 print "<TD><INPUT TYPE=\"checkbox\" NAME=\"@names[$i]\" VALUE=1>@names[$i] </TD>\n";
111 } else {
112 print "<TD><INPUT TYPE=\"checkbox\" NAME=\"@names[$i]\" VALUE=1 CHECKED>@names[$i] </TD>\n";
113 }
114 }
115 print "</TR></TABLE>\n";
117 print "</FORM>\n";
119 print "<P ALIGN=\"RIGHT\">Created by - Claus Norrbohm - <A HREF=\"mailto:james\@type-this.com\">james\@type-this.com</A></P>";
121 print $query->end_html(); # lmth ....