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