Code

merged updated win32 port by Larry Adams and the cacti team
[rrdtool-all.git] / contrib / rrdexplorer / png.cgi
1 #!/usr/bin/perl
2 # Create rrdtool graph ... 
4 use CGI::Carp;
5 use CGI;
6 use POSIX;
7 use lib qw( /usr/local/rrdtool/lib/perl );
8 use RRDs;
10 my $query = new CGI;
12 # Get params from URL
13 $rrd = $query->param("rrd"); # RRD absolute path
14 $start = $query->param("start"); # start time
15 $end = $query->param("end"); # end time
16 $hight = $query->param("hight"); # Image sizes
17 $width = $query->param("width");
18 $use = $query->param("use"); # which DS shal I print
20 # List of colors for graphs
21 @color = ("#FF0000","#00FF00","#FFFF00","#0000FF","#FF00FF","#00FFFF","#FFFFFF",
22           "#800000","#008000","#808000","#000080","#800080","#008080","#808080");
24 # title of graph with start / end time
25 $title = $rrd.": ".scalar(localtime($start))." / ".scalar(localtime($end));
27 # Formated date(now)
28 $expiredate = strftime "%a, %e %b %Y %H:%M:%S GMT", gmtime(time);
30 print "Content-type: image/png\n"; # Use html
31 print "Cache-Control: no-cache\n"; # Ensure no cashing of page
32 print "Expires: $expiredate\n\n"; # Expire now
34 $root = $ENV{"DOCUMENT_ROOT"};
35 # see rrdfetchnames
36 ($begin,$step,$names,$data) = RRDs::fetch "$root$rrd", "AVERAGE", "--start", "now","--end","start+1";
37 if ( my $ERR = RRDs::error) {
38   die "ERROR while fetching data from $NAME $ERR\n";
39 }
40 @names = @$names; # list of def's "@$name"
41 $j = @names; # how many DS's
43 # Append DEF's see examples/shared-demp.pl
44 for ($i = 0; $i < $j; $i++) {
45   $val = substr($use, $i, 1);
46   if ( $val == "1" ) {
47     @options = (@options, "DEF:l$i=$root$rrd:@names[$i]:AVERAGE","LINE2:l$i@color[$i]:@names[$i]");
48   }
49 }
51 # Draw the graph to std.out ("-")
52 ($avg,$xsize,$ysize) = RRDs::graph "-","--title", "$title","--height","$hight","--width",
53   "$width","--start",$start,"--end",$end,"-a","PNG",@options;
54 if ($ERROR = RRDs::error) {
55   print "ERROR: $ERROR\n";
56 }