1 #! @PERL@
4 END {
5 print "not ok 1\n" unless $loaded;
6 unlink "demo.rrd";
7 }
9 sub ok
10 {
11 my($what, $result) = @_ ;
12 $ok_count++;
13 print "not " unless $result;
14 print "ok $ok_count $what\n";
15 }
17 #makes programm work AFTER install
18 use lib qw( @prefix@/lib/perl );
20 use strict;
21 use vars qw(@ISA $loaded);
23 use RRDs;
24 $loaded = 1;
25 my $ok_count = 1;
27 ok("loading",1);
29 ######################### End of black magic.
31 my $STEP = 100;
32 my $RUNS = 500;
33 my $GRUNS = 4;
34 my $RRD1 = "shared-demo.rrd";
35 my $RRD2 = "shared-demob.rrd";
36 my $PNG1 = "shared-demo1.png";
37 my $PNG2 = "shared-demo2.png";
38 my $time = 30*int(time/30);
39 my $START = $time-$RUNS*$STEP;
41 my @options = ("-b", $START, "-s", $STEP,
42 "DS:a:GAUGE:2000:U:U",
43 "DS:b:GAUGE:200:U:U",
44 "DS:c:GAUGE:200:U:U",
45 "DS:d:GAUGE:200:U:U",
46 "DS:e:DERIVE:200:U:U",
47 "RRA:AVERAGE:0.5:1:5000",
48 "RRA:AVERAGE:0.5:10:500");
50 print "* Creating RRD $RRD1 starting at $time.\n\n";
51 RRDs::create $RRD1, @options;
53 my $ERROR = RRDs::error;
54 ok("create A", !$ERROR); # 2
55 if ($ERROR) {
56 die "$0: unable to create `$RRD1': $ERROR\n";
57 }
59 print "* Creating RRD $RRD2 starting at $time.\n\n";
60 RRDs::create $RRD2, @options;
62 $ERROR= RRDs::error;
63 ok("create B",!$ERROR); # 3
64 if ($ERROR) {
65 die "$0: unable to create `$RRD2': $ERROR\n";
66 }
68 my $last = RRDs::last $RRD1;
69 if ($ERROR = RRDs::error) {
70 die "$0: unable to get last `$RRD1': $ERROR\n";
71 }
72 ok("last A", $last == $START); # 4
74 $last = RRDs::last $RRD2;
75 if ($ERROR = RRDs::error) {
76 die "$0: unable to get last `$RRD2': $ERROR\n";
77 }
78 ok("last B", $last == $START); # 5
80 print "* Filling $RRD1 and $RRD2 with $RUNS*5 values. One moment please ...\n";
81 print "* If you are running over NFS this will take *MUCH* longer\n\n";
83 srand(int($time / 100));
85 @options = ();
87 my $counter = 1e7;
88 for (my $t=$START+1;
89 $t<$START+$STEP*$RUNS;
90 $t+=$STEP+int((rand()-0.5)*7)){
91 $counter += int(2500*sin($t/2000)*$STEP);
92 my $data = (1000+500*sin($t/1000)).":".
93 (1000+900*sin($t/2330)).":".
94 (2000*cos($t/1550)).":".
95 (3220*sin($t/3420)).":$counter";
96 push(@options, "$t:$data");
97 RRDs::update $RRD1, "$t:$data";
98 if ($ERROR = RRDs::error) {
99 die "$0: unable to update `$RRD1': $ERROR\n";
100 }
101 }
103 RRDs::update $RRD2, @options;
105 if ($ERROR = RRDs::error) {
106 die "$0: unable to update `$RRD2': $ERROR\n";
107 }
109 print "* Creating $GRUNS graphs: $PNG1 & $PNG2\n\n";
110 my $now = $time;
111 for (my $i=0;$i<$GRUNS;$i++) {
112 my @rrd_pngs = ($RRD1, $PNG1, $RRD2, $PNG2);
113 while (@rrd_pngs) {
114 my $RRD = shift(@rrd_pngs);
115 my $PNG = shift(@rrd_pngs);
116 my ($graphret,$xs,$ys) = RRDs::graph $PNG, "--title", 'Test GRAPH',
117 '--base', '1024',
118 "--vertical-label", 'Dummy Units', "--start", (-$RUNS*$STEP),
119 "--end", $time,
120 "--interlace", "--imgformat","PNG",
121 "DEF:alpha=$RRD:a:AVERAGE",
122 "DEF:beta=$RRD:b:AVERAGE",
123 "DEF:gamma=$RRD:c:AVERAGE",
124 "DEF:delta=$RRD:d:AVERAGE",
125 "DEF:epsilon=$RRD:e:AVERAGE",
126 "CDEF:calc=alpha,beta,+,2,/,100,*,102,/",
127 "AREA:alpha#0022e9:Short",
128 "GPRINT:calc:MAX:Max calc %1.2lf",
129 "STACK:beta#00b871:Demo Text",
130 "GPRINT:calc:AVERAGE:Average calc %1.2lf",
131 "STACK:beta#0ad871:Demo Text 2",
132 "LINE1:gamma#ff0000:Line 1",
133 "LINE2:delta#888800:Line 2",
134 "LINE3:calc#00ff44:Line 3",
135 "LINE3:epsilon#000000:Line 4",
136 "HRULE:1500#ff8800:Horizontal Line at 1500",
137 "PRINT:alpha:AVERAGE:Average Alpha %1.2lf",
138 "PRINT:alpha:MIN:Min Alpha %1.2lf",
139 "PRINT:alpha:MAX:Max Alpha %1.2lf",
140 "GPRINT:calc:MIN:Min calc %1.2lf",
141 "VRULE:".($now-3600)."#008877:60 Minutes ago",
142 "VRULE:".($now-7200)."#008877:120 Minutes ago";
144 if ($ERROR = RRDs::error) {
145 die "ERROR: $ERROR\n";
146 } else {
147 print "Image Size: ${xs}x${ys}\n";
148 print "Graph Return:\n",(join "\n", @$graphret),"\n\n";
149 }
150 }
151 }
155 my ($start,$step,$names,$array) = RRDs::fetch $RRD1, "AVERAGE";
156 $ERROR = RRDs::error;
157 die "ERROR: $ERROR\n" if $ERROR ;
158 print "start=$start, step=$step\n";
159 print " ";
160 map {printf("%12s",$_)} @$names ;
161 print "\n";
162 foreach my $line (@$array){
163 print "".localtime($start)," ";
164 $start += $step;
165 foreach my $val (@$line) {
166 printf "%12.1f", $val;
167 }
168 print "\n";
169 }
173 my ($start,$end,$step,$col_cnt,$legend,$data) =
174 RRDs::xport ("-m", 400,
175 "--start", "now-1day",
176 "--end", "now",
177 "DEF:alpha=$RRD1:a:AVERAGE",
178 "DEF:beta=$RRD1:d:AVERAGE",
179 "CDEF:calc=alpha,beta,+,2,/,100,*,102,/",
180 "XPORT:alpha:original ds",
181 "XPORT:calc:calculated values",
182 );
184 my $ERROR = RRDs::error;
185 die "$0: unable to xport: $ERROR\n" if $ERROR;
187 print "\nrrdxport test:\n\n";
188 print "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n\n";
189 print "<xport>\n";
190 print " <meta>\n";
191 print " <start>$start</start>\n";
192 print " <step>$step</step>\n";
193 print " <end>$end</end>\n";
194 print " <rows>", $#$data + 1, "</rows>\n";
195 print " <columns>$col_cnt</columns>\n";
196 print " <legend>\n";
197 foreach my $entry (@$legend) {
198 print " <entry>$entry</entry>\n";
199 }
200 print " </legend>\n";
201 print " </meta>\n";
202 print " <data>\n";
203 my $row_counter = 0;
204 foreach my $row (@$data) {
205 $row_counter++;
206 print " <row id=\"$row_counter\"><t is=\"", scalar localtime($start), "\">$start</t>";
207 $start += $step;
208 foreach my $val (@$row) {
209 printf ("<v>%1.10e</v>",$val) if $val ne '';
210 print "<v>NaN</v>" if $val eq '';
211 }
212 print "</row>\n";
213 }
214 print " </data>\n";
215 print "</xport>\n";