1 /****************************************************************************
2 * RRDtool 1.1.x Copyright Tobias Oetiker, 1997 - 2002
3 ****************************************************************************
4 * rrd__graph.c make creates ne rrds
5 ****************************************************************************/
17 #include "rrd_graph.h"
18 #include "rrd_graph_helper.h"
20 /* some constant definitions */
23 #ifndef RRD_DEFAULT_FONT
24 #define RRD_DEFAULT_FONT "/usr/share/fonts/truetype/openoffice/ariosor.ttf"
25 /* #define RRD_DEFAULT_FONT "/usr/share/fonts/truetype/Arial.ttf" */
29 text_prop_t text_prop[] = {
30 { 10.0, RRD_DEFAULT_FONT }, /* default */
31 { 12.0, RRD_DEFAULT_FONT }, /* title */
32 { 8.0, RRD_DEFAULT_FONT }, /* axis */
33 { 10.0, RRD_DEFAULT_FONT }, /* unit */
34 { 10.0, RRD_DEFAULT_FONT } /* legend */
38 {0, TMT_SECOND,30, TMT_MINUTE,5, TMT_MINUTE,5, 0,"%H:%M"},
39 {2, TMT_MINUTE,1, TMT_MINUTE,5, TMT_MINUTE,5, 0,"%H:%M"},
40 {5, TMT_MINUTE,2, TMT_MINUTE,10, TMT_MINUTE,10, 0,"%H:%M"},
41 {10, TMT_MINUTE,5, TMT_MINUTE,20, TMT_MINUTE,20, 0,"%H:%M"},
42 {30, TMT_MINUTE,10, TMT_HOUR,1, TMT_HOUR,1, 0,"%H:%M"},
43 {60, TMT_MINUTE,30, TMT_HOUR,2, TMT_HOUR,2, 0,"%H:%M"},
44 {180, TMT_HOUR,1, TMT_HOUR,6, TMT_HOUR,6, 0,"%H:%M"},
45 /*{300, TMT_HOUR,3, TMT_HOUR,12, TMT_HOUR,12, 12*3600,"%a %p"}, this looks silly*/
46 {600, TMT_HOUR,6, TMT_DAY,1, TMT_DAY,1, 24*3600,"%a"},
47 {1800, TMT_HOUR,12, TMT_DAY,1, TMT_DAY,2, 24*3600,"%a"},
48 {3600, TMT_DAY,1, TMT_WEEK,1, TMT_WEEK,1, 7*24*3600,"Week %V"},
49 {3*3600, TMT_WEEK,1, TMT_MONTH,1, TMT_WEEK,2, 7*24*3600,"Week %V"},
50 {6*3600, TMT_MONTH,1, TMT_MONTH,1, TMT_MONTH,1, 30*24*3600,"%b"},
51 {48*3600, TMT_MONTH,1, TMT_MONTH,3, TMT_MONTH,3, 30*24*3600,"%b"},
52 {10*24*3600, TMT_YEAR,1, TMT_YEAR,1, TMT_YEAR,1, 365*24*3600,"%y"},
53 {-1,TMT_MONTH,0,TMT_MONTH,0,TMT_MONTH,0,0,""}
56 /* sensible logarithmic y label intervals ...
57 the first element of each row defines the possible starting points on the
58 y axis ... the other specify the */
60 double yloglab[][12]= {{ 1e9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
61 { 1e3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
62 { 1e1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
63 /* { 1e1, 1, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, */
64 { 1e1, 1, 2.5, 5, 7.5, 0, 0, 0, 0, 0, 0, 0 },
65 { 1e1, 1, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0 },
66 { 1e1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0 },
67 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }};
69 /* sensible y label intervals ...*/
87 gfx_color_t graph_col[] = /* default colors */
88 { 0xFFFFFFFF, /* canvas */
89 0xF0F0F0FF, /* background */
90 0xD0D0D0FF, /* shade A */
91 0xA0A0A0FF, /* shade B */
92 0x909090FF, /* grid */
93 0xE05050FF, /* major grid */
94 0x000000FF, /* font */
95 0x000000FF, /* frame */
96 0xFF0000FF /* arrow */
103 # define DPRINT(x) (void)(printf x, printf("\n"))
109 /* initialize with xtr(im,0); */
111 xtr(image_desc_t *im,time_t mytime){
114 pixie = (double) im->xsize / (double)(im->end - im->start);
117 return (int)((double)im->xorigin
118 + pixie * ( mytime - im->start ) );
121 /* translate data values into y coordinates */
123 ytr(image_desc_t *im, double value){
128 pixie = (double) im->ysize / (im->maxval - im->minval);
130 pixie = (double) im->ysize / (log10(im->maxval) - log10(im->minval));
132 } else if(!im->logarithmic) {
133 yval = im->yorigin - pixie * (value - im->minval) + 0.5;
135 if (value < im->minval) {
138 yval = im->yorigin - pixie * (log10(value) - log10(im->minval)) + 0.5;
141 /* make sure we don't return anything too unreasonable. GD lib can
142 get terribly slow when drawing lines outside its scope. This is
143 especially problematic in connection with the rigid option */
146 } else if ((int)yval > im->yorigin) {
147 return im->yorigin+2;
148 } else if ((int) yval < im->yorigin - im->ysize){
149 return im->yorigin - im->ysize - 2;
157 /* conversion function for symbolic entry names */
160 #define conv_if(VV,VVV) \
161 if (strcmp(#VV, string) == 0) return VVV ;
163 enum gf_en gf_conv(char *string){
165 conv_if(PRINT,GF_PRINT)
166 conv_if(GPRINT,GF_GPRINT)
167 conv_if(COMMENT,GF_COMMENT)
168 conv_if(HRULE,GF_HRULE)
169 conv_if(VRULE,GF_VRULE)
170 conv_if(LINE,GF_LINE)
171 conv_if(AREA,GF_AREA)
172 conv_if(STACK,GF_STACK)
173 conv_if(TICK,GF_TICK)
175 conv_if(CDEF,GF_CDEF)
176 conv_if(VDEF,GF_VDEF)
177 conv_if(PART,GF_PART)
182 enum gfx_if_en if_conv(char *string){
190 enum tmt_en tmt_conv(char *string){
192 conv_if(SECOND,TMT_SECOND)
193 conv_if(MINUTE,TMT_MINUTE)
194 conv_if(HOUR,TMT_HOUR)
196 conv_if(WEEK,TMT_WEEK)
197 conv_if(MONTH,TMT_MONTH)
198 conv_if(YEAR,TMT_YEAR)
202 enum grc_en grc_conv(char *string){
204 conv_if(BACK,GRC_BACK)
205 conv_if(CANVAS,GRC_CANVAS)
206 conv_if(SHADEA,GRC_SHADEA)
207 conv_if(SHADEB,GRC_SHADEB)
208 conv_if(GRID,GRC_GRID)
209 conv_if(MGRID,GRC_MGRID)
210 conv_if(FONT,GRC_FONT)
211 conv_if(FRAME,GRC_FRAME)
212 conv_if(ARROW,GRC_ARROW)
217 enum text_prop_en text_prop_conv(char *string){
219 conv_if(DEFAULT,TEXT_PROP_DEFAULT)
220 conv_if(TITLE,TEXT_PROP_TITLE)
221 conv_if(AXIS,TEXT_PROP_AXIS)
222 conv_if(UNIT,TEXT_PROP_UNIT)
223 conv_if(LEGEND,TEXT_PROP_LEGEND)
233 im_free(image_desc_t *im)
236 if (im == NULL) return 0;
237 for(i=0;i<im->gdes_c;i++){
238 if (im->gdes[i].data_first){
239 /* careful here, because a single pointer can occur several times */
240 free (im->gdes[i].data);
241 if (im->gdes[i].ds_namv){
242 for (ii=0;ii<im->gdes[i].ds_cnt;ii++)
243 free(im->gdes[i].ds_namv[ii]);
244 free(im->gdes[i].ds_namv);
247 free (im->gdes[i].p_data);
248 free (im->gdes[i].rpnp);
251 gfx_destroy(im->canvas);
255 /* find SI magnitude symbol for the given number*/
258 image_desc_t *im, /* image description */
265 char *symbol[] = {"a", /* 10e-18 Atto */
266 "f", /* 10e-15 Femto */
267 "p", /* 10e-12 Pico */
268 "n", /* 10e-9 Nano */
269 "u", /* 10e-6 Micro */
270 "m", /* 10e-3 Milli */
275 "T", /* 10e12 Tera */
276 "P", /* 10e15 Peta */
282 if (*value == 0.0 || isnan(*value) ) {
286 sindex = floor(log(fabs(*value))/log((double)im->base));
287 *magfact = pow((double)im->base, (double)sindex);
288 (*value) /= (*magfact);
290 if ( sindex <= symbcenter && sindex >= -symbcenter) {
291 (*symb_ptr) = symbol[sindex+symbcenter];
299 /* find SI magnitude symbol for the numbers on the y-axis*/
302 image_desc_t *im /* image description */
306 char symbol[] = {'a', /* 10e-18 Atto */
307 'f', /* 10e-15 Femto */
308 'p', /* 10e-12 Pico */
309 'n', /* 10e-9 Nano */
310 'u', /* 10e-6 Micro */
311 'm', /* 10e-3 Milli */
316 'T', /* 10e12 Tera */
317 'P', /* 10e15 Peta */
323 if (im->unitsexponent != 9999) {
324 /* unitsexponent = 9, 6, 3, 0, -3, -6, -9, etc */
325 digits = floor(im->unitsexponent / 3);
327 digits = floor( log( max( fabs(im->minval),fabs(im->maxval)))/log((double)im->base));
329 im->magfact = pow((double)im->base , digits);
332 printf("digits %6.3f im->magfact %6.3f\n",digits,im->magfact);
335 if ( ((digits+symbcenter) < sizeof(symbol)) &&
336 ((digits+symbcenter) >= 0) )
337 im->symbol = symbol[(int)digits+symbcenter];
342 /* move min and max values around to become sensible */
345 expand_range(image_desc_t *im)
347 double sensiblevalues[] ={1000.0,900.0,800.0,750.0,700.0,
348 600.0,500.0,400.0,300.0,250.0,
349 200.0,125.0,100.0,90.0,80.0,
350 75.0,70.0,60.0,50.0,40.0,30.0,
351 25.0,20.0,10.0,9.0,8.0,
352 7.0,6.0,5.0,4.0,3.5,3.0,
353 2.5,2.0,1.8,1.5,1.2,1.0,
354 0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0.0,-1};
356 double scaled_min,scaled_max;
363 printf("Min: %6.2f Max: %6.2f MagFactor: %6.2f\n",
364 im->minval,im->maxval,im->magfact);
367 if (isnan(im->ygridstep)){
368 if(im->extra_flags & ALTAUTOSCALE) {
369 /* measure the amplitude of the function. Make sure that
370 graph boundaries are slightly higher then max/min vals
371 so we can see amplitude on the graph */
374 delt = im->maxval - im->minval;
376 fact = 2.0 * pow(10.0,
377 floor(log10(max(fabs(im->minval), fabs(im->maxval)))) - 2);
379 adj = (fact - delt) * 0.55;
381 printf("Min: %6.2f Max: %6.2f delt: %6.2f fact: %6.2f adj: %6.2f\n", im->minval, im->maxval, delt, fact, adj);
387 else if(im->extra_flags & ALTAUTOSCALE_MAX) {
388 /* measure the amplitude of the function. Make sure that
389 graph boundaries are slightly higher than max vals
390 so we can see amplitude on the graph */
391 adj = (im->maxval - im->minval) * 0.1;
395 scaled_min = im->minval / im->magfact;
396 scaled_max = im->maxval / im->magfact;
398 for (i=1; sensiblevalues[i] > 0; i++){
399 if (sensiblevalues[i-1]>=scaled_min &&
400 sensiblevalues[i]<=scaled_min)
401 im->minval = sensiblevalues[i]*(im->magfact);
403 if (-sensiblevalues[i-1]<=scaled_min &&
404 -sensiblevalues[i]>=scaled_min)
405 im->minval = -sensiblevalues[i-1]*(im->magfact);
407 if (sensiblevalues[i-1] >= scaled_max &&
408 sensiblevalues[i] <= scaled_max)
409 im->maxval = sensiblevalues[i-1]*(im->magfact);
411 if (-sensiblevalues[i-1]<=scaled_max &&
412 -sensiblevalues[i] >=scaled_max)
413 im->maxval = -sensiblevalues[i]*(im->magfact);
417 /* adjust min and max to the grid definition if there is one */
418 im->minval = (double)im->ylabfact * im->ygridstep *
419 floor(im->minval / ((double)im->ylabfact * im->ygridstep));
420 im->maxval = (double)im->ylabfact * im->ygridstep *
421 ceil(im->maxval /( (double)im->ylabfact * im->ygridstep));
425 fprintf(stderr,"SCALED Min: %6.2f Max: %6.2f Factor: %6.2f\n",
426 im->minval,im->maxval,im->magfact);
431 /* reduce data reimplementation by Alex */
435 enum cf_en cf, /* which consolidation function ?*/
436 unsigned long cur_step, /* step the data currently is in */
437 time_t *start, /* start, end and step as requested ... */
438 time_t *end, /* ... by the application will be ... */
439 unsigned long *step, /* ... adjusted to represent reality */
440 unsigned long *ds_cnt, /* number of data sources in file */
441 rrd_value_t **data) /* two dimensional array containing the data */
443 int i,reduce_factor = ceil((double)(*step) / (double)cur_step);
444 unsigned long col,dst_row,row_cnt,start_offset,end_offset,skiprows=0;
445 rrd_value_t *srcptr,*dstptr;
447 (*step) = cur_step*reduce_factor; /* set new step size for reduced data */
450 row_cnt = ((*end)-(*start))/cur_step;
456 printf("Reducing %lu rows with factor %i time %lu to %lu, step %lu\n",
457 row_cnt,reduce_factor,*start,*end,cur_step);
458 for (col=0;col<row_cnt;col++) {
459 printf("time %10lu: ",*start+(col+1)*cur_step);
460 for (i=0;i<*ds_cnt;i++)
461 printf(" %8.2e",srcptr[*ds_cnt*col+i]);
466 /* We have to combine [reduce_factor] rows of the source
467 ** into one row for the destination. Doing this we also
468 ** need to take care to combine the correct rows. First
469 ** alter the start and end time so that they are multiples
470 ** of the new step time. We cannot reduce the amount of
471 ** time so we have to move the end towards the future and
472 ** the start towards the past.
474 end_offset = (*end) % (*step);
475 start_offset = (*start) % (*step);
477 /* If there is a start offset (which cannot be more than
478 ** one destination row), skip the appropriate number of
479 ** source rows and one destination row. The appropriate
480 ** number is what we do know (start_offset/cur_step) of
481 ** the new interval (*step/cur_step aka reduce_factor).
484 printf("start_offset: %lu end_offset: %lu\n",start_offset,end_offset);
485 printf("row_cnt before: %lu\n",row_cnt);
488 (*start) = (*start)-start_offset;
489 skiprows=reduce_factor-start_offset/cur_step;
490 srcptr+=skiprows* *ds_cnt;
491 for (col=0;col<(*ds_cnt);col++) *dstptr++ = DNAN;
495 printf("row_cnt between: %lu\n",row_cnt);
498 /* At the end we have some rows that are not going to be
499 ** used, the amount is end_offset/cur_step
502 (*end) = (*end)-end_offset+(*step);
503 skiprows = end_offset/cur_step;
507 printf("row_cnt after: %lu\n",row_cnt);
510 /* Sanity check: row_cnt should be multiple of reduce_factor */
511 /* if this gets triggered, something is REALLY WRONG ... we die immediately */
513 if (row_cnt%reduce_factor) {
514 printf("SANITY CHECK: %lu rows cannot be reduced by %i \n",
515 row_cnt,reduce_factor);
516 printf("BUG in reduce_data()\n");
520 /* Now combine reduce_factor intervals at a time
521 ** into one interval for the destination.
524 for (dst_row=0;row_cnt>=reduce_factor;dst_row++) {
525 for (col=0;col<(*ds_cnt);col++) {
526 rrd_value_t newval=DNAN;
527 unsigned long validval=0;
529 for (i=0;i<reduce_factor;i++) {
530 if (isnan(srcptr[i*(*ds_cnt)+col])) {
534 if (isnan(newval)) newval = srcptr[i*(*ds_cnt)+col];
542 newval += srcptr[i*(*ds_cnt)+col];
545 newval = min (newval,srcptr[i*(*ds_cnt)+col]);
548 /* an interval contains a failure if any subintervals contained a failure */
550 newval = max (newval,srcptr[i*(*ds_cnt)+col]);
553 newval = srcptr[i*(*ds_cnt)+col];
558 if (validval == 0){newval = DNAN;} else{
576 srcptr+=(*ds_cnt)*reduce_factor;
577 row_cnt-=reduce_factor;
579 /* If we had to alter the endtime, we didn't have enough
580 ** source rows to fill the last row. Fill it with NaN.
582 if (end_offset) for (col=0;col<(*ds_cnt);col++) *dstptr++ = DNAN;
584 row_cnt = ((*end)-(*start))/ *step;
586 printf("Done reducing. Currently %lu rows, time %lu to %lu, step %lu\n",
587 row_cnt,*start,*end,*step);
588 for (col=0;col<row_cnt;col++) {
589 printf("time %10lu: ",*start+(col+1)*(*step));
590 for (i=0;i<*ds_cnt;i++)
591 printf(" %8.2e",srcptr[*ds_cnt*col+i]);
598 /* get the data required for the graphs from the
602 data_fetch( image_desc_t *im )
606 /* pull the data from the log files ... */
607 for (i=0;i<im->gdes_c;i++){
608 /* only GF_DEF elements fetch data */
609 if (im->gdes[i].gf != GF_DEF)
613 /* do we have it already ?*/
614 for (ii=0;ii<i;ii++){
615 if (im->gdes[ii].gf != GF_DEF)
617 if((strcmp(im->gdes[i].rrd,im->gdes[ii].rrd) == 0)
618 && (im->gdes[i].cf == im->gdes[ii].cf)){
619 /* OK the data it is here already ...
620 * we just copy the header portion */
621 im->gdes[i].start = im->gdes[ii].start;
622 im->gdes[i].end = im->gdes[ii].end;
623 im->gdes[i].step = im->gdes[ii].step;
624 im->gdes[i].ds_cnt = im->gdes[ii].ds_cnt;
625 im->gdes[i].ds_namv = im->gdes[ii].ds_namv;
626 im->gdes[i].data = im->gdes[ii].data;
627 im->gdes[i].data_first = 0;
634 unsigned long ft_step = im->gdes[i].step ;
636 if((rrd_fetch_fn(im->gdes[i].rrd,
642 &im->gdes[i].ds_namv,
643 &im->gdes[i].data)) == -1){
646 im->gdes[i].data_first = 1;
648 if (ft_step < im->gdes[i].step) {
649 reduce_data(im->gdes[i].cf,
657 im->gdes[i].step = ft_step;
661 /* lets see if the required data source is realy there */
662 for(ii=0;ii<im->gdes[i].ds_cnt;ii++){
663 if(strcmp(im->gdes[i].ds_namv[ii],im->gdes[i].ds_nam) == 0){
666 if (im->gdes[i].ds== -1){
667 rrd_set_error("No DS called '%s' in '%s'",
668 im->gdes[i].ds_nam,im->gdes[i].rrd);
676 /* evaluate the expressions in the CDEF functions */
678 /*************************************************************
680 *************************************************************/
683 find_var_wrapper(void *arg1, char *key)
685 return find_var((image_desc_t *) arg1, key);
688 /* find gdes containing var*/
690 find_var(image_desc_t *im, char *key){
692 for(ii=0;ii<im->gdes_c-1;ii++){
693 if((im->gdes[ii].gf == GF_DEF
694 || im->gdes[ii].gf == GF_VDEF
695 || im->gdes[ii].gf == GF_CDEF)
696 && (strcmp(im->gdes[ii].vname,key) == 0)){
703 /* find the largest common denominator for all the numbers
704 in the 0 terminated num array */
709 for (i=0;num[i+1]!=0;i++){
711 rest=num[i] % num[i+1];
712 num[i]=num[i+1]; num[i+1]=rest;
716 /* return i==0?num[i]:num[i-1]; */
720 /* run the rpn calculator on all the VDEF and CDEF arguments */
722 data_calc( image_desc_t *im){
726 long *steparray, rpi;
731 rpnstack_init(&rpnstack);
733 for (gdi=0;gdi<im->gdes_c;gdi++){
734 /* Look for GF_VDEF and GF_CDEF in the same loop,
735 * so CDEFs can use VDEFs and vice versa
737 switch (im->gdes[gdi].gf) {
739 /* A VDEF has no DS. This also signals other parts
740 * of rrdtool that this is a VDEF value, not a CDEF.
742 im->gdes[gdi].ds_cnt = 0;
743 if (vdef_calc(im,gdi)) {
744 rrd_set_error("Error processing VDEF '%s'"
747 rpnstack_free(&rpnstack);
752 im->gdes[gdi].ds_cnt = 1;
753 im->gdes[gdi].ds = 0;
754 im->gdes[gdi].data_first = 1;
755 im->gdes[gdi].start = 0;
756 im->gdes[gdi].end = 0;
761 /* Find the variables in the expression.
762 * - VDEF variables are substituted by their values
763 * and the opcode is changed into OP_NUMBER.
764 * - CDEF variables are analized for their step size,
765 * the lowest common denominator of all the step
766 * sizes of the data sources involved is calculated
767 * and the resulting number is the step size for the
768 * resulting data source.
770 for(rpi=0;im->gdes[gdi].rpnp[rpi].op != OP_END;rpi++){
771 if(im->gdes[gdi].rpnp[rpi].op == OP_VARIABLE){
772 long ptr = im->gdes[gdi].rpnp[rpi].ptr;
773 if (im->gdes[ptr].ds_cnt == 0) {
775 printf("DEBUG: inside CDEF '%s' processing VDEF '%s'\n",
777 im->gdes[ptr].vname);
778 printf("DEBUG: value from vdef is %f\n",im->gdes[ptr].vf.val);
780 im->gdes[gdi].rpnp[rpi].val = im->gdes[ptr].vf.val;
781 im->gdes[gdi].rpnp[rpi].op = OP_NUMBER;
783 if ((steparray = rrd_realloc(steparray, (++stepcnt+1)*sizeof(*steparray)))==NULL){
784 rrd_set_error("realloc steparray");
785 rpnstack_free(&rpnstack);
789 steparray[stepcnt-1] = im->gdes[ptr].step;
791 /* adjust start and end of cdef (gdi) so
792 * that it runs from the latest start point
793 * to the earliest endpoint of any of the
794 * rras involved (ptr)
796 if(im->gdes[gdi].start < im->gdes[ptr].start)
797 im->gdes[gdi].start = im->gdes[ptr].start;
799 if(im->gdes[gdi].end == 0 ||
800 im->gdes[gdi].end > im->gdes[ptr].end)
801 im->gdes[gdi].end = im->gdes[ptr].end;
803 /* store pointer to the first element of
804 * the rra providing data for variable,
805 * further save step size and data source
808 im->gdes[gdi].rpnp[rpi].data = im->gdes[ptr].data + im->gdes[ptr].ds;
809 im->gdes[gdi].rpnp[rpi].step = im->gdes[ptr].step;
810 im->gdes[gdi].rpnp[rpi].ds_cnt = im->gdes[ptr].ds_cnt;
812 /* backoff the *.data ptr; this is done so
813 * rpncalc() function doesn't have to treat
814 * the first case differently
816 } /* if ds_cnt != 0 */
817 } /* if OP_VARIABLE */
818 } /* loop through all rpi */
820 /* move the data pointers to the correct period */
821 for(rpi=0;im->gdes[gdi].rpnp[rpi].op != OP_END;rpi++){
822 if(im->gdes[gdi].rpnp[rpi].op == OP_VARIABLE){
823 long ptr = im->gdes[gdi].rpnp[rpi].ptr;
824 if(im->gdes[gdi].start > im->gdes[ptr].start) {
825 im->gdes[gdi].rpnp[rpi].data += im->gdes[gdi].rpnp[rpi].ds_cnt;
831 if(steparray == NULL){
832 rrd_set_error("rpn expressions without DEF"
833 " or CDEF variables are not supported");
834 rpnstack_free(&rpnstack);
837 steparray[stepcnt]=0;
838 /* Now find the resulting step. All steps in all
839 * used RRAs have to be visited
841 im->gdes[gdi].step = lcd(steparray);
843 if((im->gdes[gdi].data = malloc((
844 (im->gdes[gdi].end-im->gdes[gdi].start)
845 / im->gdes[gdi].step)
846 * sizeof(double)))==NULL){
847 rrd_set_error("malloc im->gdes[gdi].data");
848 rpnstack_free(&rpnstack);
852 /* Step through the new cdef results array and
853 * calculate the values
855 for (now = im->gdes[gdi].start + im->gdes[gdi].step;
856 now<=im->gdes[gdi].end;
857 now += im->gdes[gdi].step)
859 rpnp_t *rpnp = im -> gdes[gdi].rpnp;
861 /* 3rd arg of rpn_calc is for OP_VARIABLE lookups;
862 * in this case we are advancing by timesteps;
863 * we use the fact that time_t is a synonym for long
865 if (rpn_calc(rpnp,&rpnstack,(long) now,
866 im->gdes[gdi].data,++dataidx) == -1) {
867 /* rpn_calc sets the error string */
868 rpnstack_free(&rpnstack);
871 } /* enumerate over time steps within a CDEF */
876 } /* enumerate over CDEFs */
877 rpnstack_free(&rpnstack);
881 /* massage data so, that we get one value for each x coordinate in the graph */
883 data_proc( image_desc_t *im ){
885 double pixstep = (double)(im->end-im->start)
886 /(double)im->xsize; /* how much time
887 passes in one pixel */
889 double minval=DNAN,maxval=DNAN;
891 unsigned long gr_time;
893 /* memory for the processed data */
894 for(i=0;i<im->gdes_c;i++){
895 if((im->gdes[i].gf==GF_LINE) ||
896 (im->gdes[i].gf==GF_AREA) ||
897 (im->gdes[i].gf==GF_TICK) ||
898 (im->gdes[i].gf==GF_STACK)){
899 if((im->gdes[i].p_data = malloc((im->xsize +1)
900 * sizeof(rrd_value_t)))==NULL){
901 rrd_set_error("malloc data_proc");
907 for(i=0;i<im->xsize;i++){
909 gr_time = im->start+pixstep*i; /* time of the
913 for(ii=0;ii<im->gdes_c;ii++){
915 switch(im->gdes[ii].gf){
921 vidx = im->gdes[ii].vidx;
925 ((unsigned long)floor(
926 (double)(gr_time-im->gdes[vidx].start) / im->gdes[vidx].step
928 ) *im->gdes[vidx].ds_cnt
931 if (! isnan(value)) {
933 im->gdes[ii].p_data[i] = paintval;
934 /* GF_TICK: the data values are not relevant for min and max */
935 if (finite(paintval) && im->gdes[ii].gf != GF_TICK ){
936 if (isnan(minval) || paintval < minval)
938 if (isnan(maxval) || paintval > maxval)
942 im->gdes[ii].p_data[i] = DNAN;
959 /* if min or max have not been asigned a value this is because
960 there was no data in the graph ... this is not good ...
961 lets set these to dummy values then ... */
963 if (isnan(minval)) minval = 0.0;
964 if (isnan(maxval)) maxval = 1.0;
966 /* adjust min and max values */
967 if (isnan(im->minval)
968 || ((!im->logarithmic && !im->rigid) /* don't adjust low-end with log scale */
969 && im->minval > minval))
971 if (isnan(im->maxval)
973 && im->maxval < maxval)){
975 im->maxval = maxval * 1.1;
979 /* make sure min and max are not equal */
980 if (im->minval == im->maxval) {
982 if (! im->logarithmic) {
986 /* make sure min and max are not both zero */
987 if (im->maxval == 0.0) {
997 /* identify the point where the first gridline, label ... gets placed */
1001 time_t start, /* what is the initial time */
1002 enum tmt_en baseint, /* what is the basic interval */
1003 long basestep /* how many if these do we jump a time */
1007 tm = *localtime(&start);
1010 tm.tm_sec -= tm.tm_sec % basestep; break;
1013 tm.tm_min -= tm.tm_min % basestep;
1018 tm.tm_hour -= tm.tm_hour % basestep; break;
1020 /* we do NOT look at the basestep for this ... */
1023 tm.tm_hour = 0; break;
1025 /* we do NOT look at the basestep for this ... */
1029 tm.tm_mday -= tm.tm_wday -1; /* -1 because we want the monday */
1030 if (tm.tm_wday==0) tm.tm_mday -= 7; /* we want the *previous* monday */
1037 tm.tm_mon -= tm.tm_mon % basestep; break;
1045 tm.tm_year -= (tm.tm_year+1900) % basestep;
1050 /* identify the point where the next gridline, label ... gets placed */
1053 time_t current, /* what is the initial time */
1054 enum tmt_en baseint, /* what is the basic interval */
1055 long basestep /* how many if these do we jump a time */
1060 tm = *localtime(¤t);
1064 tm.tm_sec += basestep; break;
1066 tm.tm_min += basestep; break;
1068 tm.tm_hour += basestep; break;
1070 tm.tm_mday += basestep; break;
1072 tm.tm_mday += 7*basestep; break;
1074 tm.tm_mon += basestep; break;
1076 tm.tm_year += basestep;
1078 madetime = mktime(&tm);
1079 } while (madetime == -1); /* this is necessary to skip impssible times
1080 like the daylight saving time skips */
1086 /* calculate values required for PRINT and GPRINT functions */
1089 print_calc(image_desc_t *im, char ***prdata)
1091 long i,ii,validsteps;
1094 int graphelement = 0;
1097 double magfact = -1;
1101 if (im->imginfo) prlines++;
1102 for(i=0;i<im->gdes_c;i++){
1103 switch(im->gdes[i].gf){
1106 if(((*prdata) = rrd_realloc((*prdata),prlines*sizeof(char *)))==NULL){
1107 rrd_set_error("realloc prdata");
1111 /* PRINT and GPRINT can now print VDEF generated values.
1112 * There's no need to do any calculations on them as these
1113 * calculations were already made.
1115 vidx = im->gdes[i].vidx;
1116 if (im->gdes[vidx].gf==GF_VDEF) { /* simply use vals */
1117 printval = im->gdes[vidx].vf.val;
1118 printtime = im->gdes[vidx].vf.when;
1119 } else { /* need to calculate max,min,avg etcetera */
1120 max_ii =((im->gdes[vidx].end
1121 - im->gdes[vidx].start)
1122 / im->gdes[vidx].step
1123 * im->gdes[vidx].ds_cnt);
1126 for( ii=im->gdes[vidx].ds;
1128 ii+=im->gdes[vidx].ds_cnt){
1129 if (! finite(im->gdes[vidx].data[ii]))
1131 if (isnan(printval)){
1132 printval = im->gdes[vidx].data[ii];
1137 switch (im->gdes[i].cf){
1140 case CF_DEVSEASONAL:
1144 printval += im->gdes[vidx].data[ii];
1147 printval = min( printval, im->gdes[vidx].data[ii]);
1151 printval = max( printval, im->gdes[vidx].data[ii]);
1154 printval = im->gdes[vidx].data[ii];
1157 if (im->gdes[i].cf==CF_AVERAGE || im->gdes[i].cf > CF_LAST) {
1158 if (validsteps > 1) {
1159 printval = (printval / validsteps);
1162 } /* prepare printval */
1164 if (!strcmp(im->gdes[i].format,"%c")) { /* VDEF time print */
1165 if (im->gdes[i].gf == GF_PRINT){
1166 (*prdata)[prlines-2] = malloc((FMT_LEG_LEN+2)*sizeof(char));
1167 sprintf((*prdata)[prlines-2],"%s (%lu)",
1168 ctime(&printtime),printtime);
1169 (*prdata)[prlines-1] = NULL;
1171 sprintf(im->gdes[i].legend,"%s (%lu)",
1172 ctime(&printtime),printtime);
1176 if ((percent_s = strstr(im->gdes[i].format,"%S")) != NULL) {
1177 /* Magfact is set to -1 upon entry to print_calc. If it
1178 * is still less than 0, then we need to run auto_scale.
1179 * Otherwise, put the value into the correct units. If
1180 * the value is 0, then do not set the symbol or magnification
1181 * so next the calculation will be performed again. */
1182 if (magfact < 0.0) {
1183 auto_scale(im,&printval,&si_symb,&magfact);
1184 if (printval == 0.0)
1187 printval /= magfact;
1189 *(++percent_s) = 's';
1190 } else if (strstr(im->gdes[i].format,"%s") != NULL) {
1191 auto_scale(im,&printval,&si_symb,&magfact);
1194 if (im->gdes[i].gf == GF_PRINT){
1195 (*prdata)[prlines-2] = malloc((FMT_LEG_LEN+2)*sizeof(char));
1196 if (bad_format(im->gdes[i].format)) {
1197 rrd_set_error("bad format for [G]PRINT in '%s'", im->gdes[i].format);
1200 #ifdef HAVE_SNPRINTF
1201 snprintf((*prdata)[prlines-2],FMT_LEG_LEN,im->gdes[i].format,printval,si_symb);
1203 sprintf((*prdata)[prlines-2],im->gdes[i].format,printval,si_symb);
1205 (*prdata)[prlines-1] = NULL;
1209 if (bad_format(im->gdes[i].format)) {
1210 rrd_set_error("bad format for [G]PRINT in '%s'", im->gdes[i].format);
1213 #ifdef HAVE_SNPRINTF
1214 snprintf(im->gdes[i].legend,FMT_LEG_LEN-2,im->gdes[i].format,printval,si_symb);
1216 sprintf(im->gdes[i].legend,im->gdes[i].format,printval,si_symb);
1238 return graphelement;
1242 /* place legends with color spots */
1244 leg_place(image_desc_t *im)
1247 int interleg = im->text_prop[TEXT_PROP_LEGEND].size*2.0;
1248 int box =im->text_prop[TEXT_PROP_LEGEND].size*1.5;
1249 int border = im->text_prop[TEXT_PROP_LEGEND].size*2.0;
1250 int fill=0, fill_last;
1252 int leg_x = border, leg_y = im->ygif;
1256 char prt_fctn; /*special printfunctions */
1259 if( !(im->extra_flags & NOLEGEND) ) {
1260 if ((legspace = malloc(im->gdes_c*sizeof(int)))==NULL){
1261 rrd_set_error("malloc for legspace");
1265 for(i=0;i<im->gdes_c;i++){
1268 leg_cc = strlen(im->gdes[i].legend);
1270 /* is there a controle code ant the end of the legend string ? */
1271 if (leg_cc >= 2 && im->gdes[i].legend[leg_cc-2] == '\\') {
1272 prt_fctn = im->gdes[i].legend[leg_cc-1];
1274 im->gdes[i].legend[leg_cc] = '\0';
1278 /* remove exess space */
1279 while (prt_fctn=='g' &&
1281 im->gdes[i].legend[leg_cc-1]==' '){
1283 im->gdes[i].legend[leg_cc]='\0';
1286 legspace[i]=(prt_fctn=='g' ? 0 : interleg);
1289 /* no interleg space if string ends in \g */
1290 fill += legspace[i];
1292 if (im->gdes[i].gf != GF_GPRINT &&
1293 im->gdes[i].gf != GF_COMMENT) {
1296 fill += gfx_get_text_width(im->canvas, fill+border,
1297 im->text_prop[TEXT_PROP_LEGEND].font,
1298 im->text_prop[TEXT_PROP_LEGEND].size,
1300 im->gdes[i].legend);
1305 /* who said there was a special tag ... ?*/
1306 if (prt_fctn=='g') {
1309 if (prt_fctn == '\0') {
1310 if (i == im->gdes_c -1 ) prt_fctn ='l';
1312 /* is it time to place the legends ? */
1313 if (fill > im->xgif - 2*border){
1328 if (prt_fctn != '\0'){
1330 if (leg_c >= 2 && prt_fctn == 'j') {
1331 glue = (im->xgif - fill - 2* border) / (leg_c-1);
1335 if (prt_fctn =='c') leg_x = (im->xgif - fill) / 2.0;
1336 if (prt_fctn =='r') leg_x = im->xgif - fill - border;
1338 for(ii=mark;ii<=i;ii++){
1339 if(im->gdes[ii].legend[0]=='\0')
1341 im->gdes[ii].leg_x = leg_x;
1342 im->gdes[ii].leg_y = leg_y;
1344 gfx_get_text_width(im->canvas, leg_x,
1345 im->text_prop[TEXT_PROP_LEGEND].font,
1346 im->text_prop[TEXT_PROP_LEGEND].size,
1348 im->gdes[ii].legend)
1351 if (im->gdes[ii].gf != GF_GPRINT &&
1352 im->gdes[ii].gf != GF_COMMENT)
1355 leg_y = leg_y + im->text_prop[TEXT_PROP_LEGEND].size*1.2;
1356 if (prt_fctn == 's') leg_y -= im->text_prop[TEXT_PROP_LEGEND].size*1.2;
1368 /* create a grid on the graph. it determines what to do
1369 from the values of xsize, start and end */
1371 /* the xaxis labels are determined from the number of seconds per pixel
1372 in the requested graph */
1377 horizontal_grid(image_desc_t *im)
1385 char graph_label[100];
1387 int labfact,gridind;
1388 int decimals, fractionals;
1393 range = im->maxval - im->minval;
1394 scaledrange = range / im->magfact;
1396 /* does the scale of this graph make it impossible to put lines
1397 on it? If so, give up. */
1398 if (isnan(scaledrange)) {
1402 /* find grid spaceing */
1404 if(isnan(im->ygridstep)){
1405 if(im->extra_flags & ALTYGRID) {
1406 /* find the value with max number of digits. Get number of digits */
1407 decimals = ceil(log10(max(fabs(im->maxval), fabs(im->minval))));
1408 if(decimals <= 0) /* everything is small. make place for zero */
1411 fractionals = floor(log10(range));
1412 if(fractionals < 0) /* small amplitude. */
1413 sprintf(labfmt, "%%%d.%df", decimals - fractionals + 1, -fractionals + 1);
1415 sprintf(labfmt, "%%%d.1f", decimals + 1);
1416 gridstep = pow((double)10, (double)fractionals);
1417 if(gridstep == 0) /* range is one -> 0.1 is reasonable scale */
1419 /* should have at least 5 lines but no more then 15 */
1420 if(range/gridstep < 5)
1422 if(range/gridstep > 15)
1424 if(range/gridstep > 5) {
1426 if(range/gridstep > 8)
1435 for(i=0;ylab[i].grid > 0;i++){
1436 pixel = im->ysize / (scaledrange / ylab[i].grid);
1437 if (gridind == -1 && pixel > 5) {
1444 if (pixel * ylab[gridind].lfac[i] >= 2 * im->text_prop[TEXT_PROP_AXIS].size) {
1445 labfact = ylab[gridind].lfac[i];
1450 gridstep = ylab[gridind].grid * im->magfact;
1453 gridstep = im->ygridstep;
1454 labfact = im->ylabfact;
1458 x1=im->xorigin+im->xsize;
1460 sgrid = (int)( im->minval / gridstep - 1);
1461 egrid = (int)( im->maxval / gridstep + 1);
1462 scaledstep = gridstep/im->magfact;
1463 for (i = sgrid; i <= egrid; i++){
1464 y0=ytr(im,gridstep*i);
1465 if ( y0 >= im->yorigin-im->ysize
1466 && y0 <= im->yorigin){
1467 if(i % labfact == 0){
1468 if (i==0 || im->symbol == ' ') {
1470 if(im->extra_flags & ALTYGRID) {
1471 sprintf(graph_label,labfmt,scaledstep*i);
1474 sprintf(graph_label,"%4.1f",scaledstep*i);
1477 sprintf(graph_label,"%4.0f",scaledstep*i);
1481 sprintf(graph_label,"%4.1f %c",scaledstep*i, im->symbol);
1483 sprintf(graph_label,"%4.0f %c",scaledstep*i, im->symbol);
1487 gfx_new_text ( im->canvas,
1488 x0-im->text_prop[TEXT_PROP_AXIS].size/1.5, y0,
1489 im->graph_col[GRC_FONT],
1490 im->text_prop[TEXT_PROP_AXIS].font,
1491 im->text_prop[TEXT_PROP_AXIS].size,
1492 im->tabwidth, 0.0, GFX_H_RIGHT, GFX_V_CENTER,
1494 gfx_new_line ( im->canvas,
1497 MGRIDWIDTH, im->graph_col[GRC_MGRID] );
1500 gfx_new_line ( im->canvas,
1503 GRIDWIDTH, im->graph_col[GRC_GRID] );
1511 /* logaritmic horizontal grid */
1513 horizontal_log_grid(image_desc_t *im)
1517 int minoridx=0, majoridx=0;
1518 char graph_label[100];
1520 double value, pixperstep, minstep;
1522 /* find grid spaceing */
1523 pixpex= (double)im->ysize / (log10(im->maxval) - log10(im->minval));
1525 if (isnan(pixpex)) {
1529 for(i=0;yloglab[i][0] > 0;i++){
1530 minstep = log10(yloglab[i][0]);
1531 for(ii=1;yloglab[i][ii+1] > 0;ii++){
1532 if(yloglab[i][ii+2]==0){
1533 minstep = log10(yloglab[i][ii+1])-log10(yloglab[i][ii]);
1537 pixperstep = pixpex * minstep;
1538 if(pixperstep > 5){minoridx = i;}
1539 if(pixperstep > 2 * im->text_prop[TEXT_PROP_LEGEND].size){majoridx = i;}
1543 x1=im->xorigin+im->xsize;
1544 /* paint minor grid */
1545 for (value = pow((double)10, log10(im->minval)
1546 - fmod(log10(im->minval),log10(yloglab[minoridx][0])));
1547 value <= im->maxval;
1548 value *= yloglab[minoridx][0]){
1549 if (value < im->minval) continue;
1551 while(yloglab[minoridx][++i] > 0){
1552 y0 = ytr(im,value * yloglab[minoridx][i]);
1553 if (y0 <= im->yorigin - im->ysize) break;
1554 gfx_new_line ( im->canvas,
1557 GRIDWIDTH, im->graph_col[GRC_GRID] );
1561 /* paint major grid and labels*/
1562 for (value = pow((double)10, log10(im->minval)
1563 - fmod(log10(im->minval),log10(yloglab[majoridx][0])));
1564 value <= im->maxval;
1565 value *= yloglab[majoridx][0]){
1566 if (value < im->minval) continue;
1568 while(yloglab[majoridx][++i] > 0){
1569 y0 = ytr(im,value * yloglab[majoridx][i]);
1570 if (y0 <= im->yorigin - im->ysize) break;
1571 gfx_new_line ( im->canvas,
1574 MGRIDWIDTH, im->graph_col[GRC_MGRID] );
1576 sprintf(graph_label,"%3.0e",value * yloglab[majoridx][i]);
1577 gfx_new_text ( im->canvas,
1578 x0-im->text_prop[TEXT_PROP_AXIS].size/1.5, y0,
1579 im->graph_col[GRC_FONT],
1580 im->text_prop[TEXT_PROP_AXIS].font,
1581 im->text_prop[TEXT_PROP_AXIS].size,
1582 im->tabwidth,0.0, GFX_H_RIGHT, GFX_V_CENTER,
1594 int xlab_sel; /* which sort of label and grid ? */
1597 char graph_label[100];
1598 double x0,y0,y1; /* points for filled graph and more*/
1601 /* the type of time grid is determined by finding
1602 the number of seconds per pixel in the graph */
1605 if(im->xlab_user.minsec == -1){
1606 factor=(im->end - im->start)/im->xsize;
1608 while ( xlab[xlab_sel+1].minsec != -1
1609 && xlab[xlab_sel+1].minsec <= factor){ xlab_sel++; }
1610 im->xlab_user.gridtm = xlab[xlab_sel].gridtm;
1611 im->xlab_user.gridst = xlab[xlab_sel].gridst;
1612 im->xlab_user.mgridtm = xlab[xlab_sel].mgridtm;
1613 im->xlab_user.mgridst = xlab[xlab_sel].mgridst;
1614 im->xlab_user.labtm = xlab[xlab_sel].labtm;
1615 im->xlab_user.labst = xlab[xlab_sel].labst;
1616 im->xlab_user.precis = xlab[xlab_sel].precis;
1617 im->xlab_user.stst = xlab[xlab_sel].stst;
1620 /* y coords are the same for every line ... */
1622 y1 = im->yorigin-im->ysize;
1625 /* paint the minor grid */
1626 for(ti = find_first_time(im->start,
1627 im->xlab_user.gridtm,
1628 im->xlab_user.gridst);
1630 ti = find_next_time(ti,im->xlab_user.gridtm,im->xlab_user.gridst)
1632 /* are we inside the graph ? */
1633 if (ti < im->start || ti > im->end) continue;
1635 gfx_new_line(im->canvas,x0,y0+1, x0,y1-1,GRIDWIDTH, im->graph_col[GRC_GRID]);
1639 /* paint the major grid */
1640 for(ti = find_first_time(im->start,
1641 im->xlab_user.mgridtm,
1642 im->xlab_user.mgridst);
1644 ti = find_next_time(ti,im->xlab_user.mgridtm,im->xlab_user.mgridst)
1646 /* are we inside the graph ? */
1647 if (ti < im->start || ti > im->end) continue;
1649 gfx_new_line(im->canvas,x0,y0+2, x0,y1-2,MGRIDWIDTH, im->graph_col[GRC_MGRID]);
1652 /* paint the labels below the graph */
1653 for(ti = find_first_time(im->start,
1654 im->xlab_user.labtm,
1655 im->xlab_user.labst);
1657 ti = find_next_time(ti,im->xlab_user.labtm,im->xlab_user.labst)
1659 tilab= ti + im->xlab_user.precis/2; /* correct time for the label */
1660 /* are we inside the graph ? */
1661 if (ti < im->start || ti > im->end) continue;
1664 strftime(graph_label,99,im->xlab_user.stst,localtime(&tilab));
1666 # error "your libc has no strftime I guess we'll abort the exercise here."
1668 gfx_new_text ( im->canvas,
1669 xtr(im,tilab), y0+im->text_prop[TEXT_PROP_AXIS].size/1.5,
1670 im->graph_col[GRC_FONT],
1671 im->text_prop[TEXT_PROP_AXIS].font,
1672 im->text_prop[TEXT_PROP_AXIS].size,
1673 im->tabwidth, 0.0, GFX_H_CENTER, GFX_V_TOP,
1686 /* draw x and y axis */
1687 gfx_new_line ( im->canvas, im->xorigin+im->xsize,im->yorigin,
1688 im->xorigin+im->xsize,im->yorigin-im->ysize,
1689 GRIDWIDTH, im->graph_col[GRC_GRID]);
1691 gfx_new_line ( im->canvas, im->xorigin,im->yorigin-im->ysize,
1692 im->xorigin+im->xsize,im->yorigin-im->ysize,
1693 GRIDWIDTH, im->graph_col[GRC_GRID]);
1695 gfx_new_line ( im->canvas, im->xorigin-4,im->yorigin,
1696 im->xorigin+im->xsize+4,im->yorigin,
1697 MGRIDWIDTH, im->graph_col[GRC_GRID]);
1699 gfx_new_line ( im->canvas, im->xorigin,im->yorigin+4,
1700 im->xorigin,im->yorigin-im->ysize-4,
1701 MGRIDWIDTH, im->graph_col[GRC_GRID]);
1704 /* arrow for X axis direction */
1705 gfx_new_area ( im->canvas,
1706 im->xorigin+im->xsize+3, im->yorigin-3,
1707 im->xorigin+im->xsize+3, im->yorigin+4,
1708 im->xorigin+im->xsize+8, im->yorigin+0.5, // LINEOFFSET
1709 im->graph_col[GRC_ARROW]);
1716 grid_paint(image_desc_t *im)
1720 double x0,y0; /* points for filled graph and more*/
1723 /* draw 3d border */
1724 node = gfx_new_area (im->canvas, 0,im->ygif,
1726 2,2,im->graph_col[GRC_SHADEA]);
1727 gfx_add_point( node , im->xgif - 2, 2 );
1728 gfx_add_point( node , im->xgif, 0 );
1729 gfx_add_point( node , 0,0 );
1730 /* gfx_add_point( node , 0,im->ygif ); */
1732 node = gfx_new_area (im->canvas, 2,im->ygif-2,
1733 im->xgif-2,im->ygif-2,
1735 im->graph_col[GRC_SHADEB]);
1736 gfx_add_point( node , im->xgif,0);
1737 gfx_add_point( node , im->xgif,im->ygif);
1738 gfx_add_point( node , 0,im->ygif);
1739 /* gfx_add_point( node , 0,im->ygif ); */
1742 if (im->draw_x_grid == 1 )
1745 if (im->draw_y_grid == 1){
1746 if(im->logarithmic){
1747 res = horizontal_log_grid(im);
1749 res = horizontal_grid(im);
1752 /* dont draw horizontal grid if there is no min and max val */
1754 char *nodata = "No Data found";
1755 gfx_new_text(im->canvas,im->xgif/2, (2*im->yorigin-im->ysize) / 2,
1756 im->graph_col[GRC_FONT],
1757 im->text_prop[TEXT_PROP_AXIS].font,
1758 im->text_prop[TEXT_PROP_AXIS].size,
1759 im->tabwidth, 0.0, GFX_H_CENTER, GFX_V_CENTER,
1764 /* yaxis description */
1765 if (im->canvas->imgformat != IF_PNG) {
1766 gfx_new_text( im->canvas,
1767 7, (im->yorigin - im->ysize/2),
1768 im->graph_col[GRC_FONT],
1769 im->text_prop[TEXT_PROP_AXIS].font,
1770 im->text_prop[TEXT_PROP_AXIS].size, im->tabwidth, 270.0,
1771 GFX_H_CENTER, GFX_V_CENTER,
1774 /* horrible hack until we can actually print vertically */
1777 int l=strlen(im->ylegend);
1779 for (n=0;n<strlen(im->ylegend);n++) {
1780 s[0]=im->ylegend[n];
1782 gfx_new_text(im->canvas,7,im->text_prop[TEXT_PROP_AXIS].size*(l-n),
1783 im->graph_col[GRC_FONT],
1784 im->text_prop[TEXT_PROP_AXIS].font,
1785 im->text_prop[TEXT_PROP_AXIS].size, im->tabwidth, 270.0,
1786 GFX_H_CENTER, GFX_V_CENTER,
1793 gfx_new_text( im->canvas,
1794 im->xgif/2, im->text_prop[TEXT_PROP_TITLE].size,
1795 im->graph_col[GRC_FONT],
1796 im->text_prop[TEXT_PROP_TITLE].font,
1797 im->text_prop[TEXT_PROP_TITLE].size, im->tabwidth, 0.0,
1798 GFX_H_CENTER, GFX_V_CENTER,
1802 if( !(im->extra_flags & NOLEGEND) ) {
1803 for(i=0;i<im->gdes_c;i++){
1804 if(im->gdes[i].legend[0] =='\0')
1807 /* im->gdes[i].leg_y is the bottom of the legend */
1808 x0 = im->gdes[i].leg_x;
1809 y0 = im->gdes[i].leg_y;
1811 if ( im->gdes[i].gf != GF_GPRINT
1812 && im->gdes[i].gf != GF_COMMENT) {
1815 boxH = gfx_get_text_width(im->canvas, 0,
1816 im->text_prop[TEXT_PROP_AXIS].font,
1817 im->text_prop[TEXT_PROP_AXIS].size,
1818 im->tabwidth,"M") * 1.25;
1821 node = gfx_new_area(im->canvas,
1826 gfx_add_point ( node, x0+boxH, y0-boxV );
1827 node = gfx_new_line(im->canvas,
1830 gfx_add_point(node,x0+boxH,y0);
1831 gfx_add_point(node,x0+boxH,y0-boxV);
1832 gfx_close_path(node);
1833 x0 += boxH / 1.25 * 2;
1835 gfx_new_text ( im->canvas, x0, y0,
1836 im->graph_col[GRC_FONT],
1837 im->text_prop[TEXT_PROP_AXIS].font,
1838 im->text_prop[TEXT_PROP_AXIS].size,
1839 im->tabwidth,0.0, GFX_H_LEFT, GFX_V_BOTTOM,
1840 im->gdes[i].legend );
1846 /*****************************************************
1847 * lazy check make sure we rely need to create this graph
1848 *****************************************************/
1850 int lazy_check(image_desc_t *im){
1853 struct stat gifstat;
1855 if (im->lazy == 0) return 0; /* no lazy option */
1856 if (stat(im->graphfile,&gifstat) != 0)
1857 return 0; /* can't stat */
1858 /* one pixel in the existing graph is more then what we would
1860 if (time(NULL) - gifstat.st_mtime >
1861 (im->end - im->start) / im->xsize)
1863 if ((fd = fopen(im->graphfile,"rb")) == NULL)
1864 return 0; /* the file does not exist */
1865 switch (im->canvas->imgformat) {
1867 size = PngSize(fd,&(im->xgif),&(im->ygif));
1877 pie_part(image_desc_t *im, gfx_color_t color,
1878 double PieCenterX, double PieCenterY, double Radius,
1879 double startangle, double endangle)
1883 double step=M_PI/50; /* Number of iterations for the circle;
1884 ** 10 is definitely too low, more than
1885 ** 50 seems to be overkill
1888 /* Strange but true: we have to work clockwise or else
1889 ** anti aliasing nor transparency don't work.
1891 ** This test is here to make sure we do it right, also
1892 ** this makes the for...next loop more easy to implement.
1893 ** The return will occur if the user enters a negative number
1894 ** (which shouldn't be done according to the specs) or if the
1895 ** programmers do something wrong (which, as we all know, never
1896 ** happens anyway :)
1898 if (endangle<startangle) return;
1900 /* Hidden feature: Radius decreases each full circle */
1902 while (angle>=2*M_PI) {
1907 node=gfx_new_area(im->canvas,
1908 PieCenterX+sin(startangle)*Radius,
1909 PieCenterY-cos(startangle)*Radius,
1912 PieCenterX+sin(endangle)*Radius,
1913 PieCenterY-cos(endangle)*Radius,
1915 for (angle=endangle;angle-startangle>=step;angle-=step) {
1917 PieCenterX+sin(angle)*Radius,
1918 PieCenterY-cos(angle)*Radius );
1923 graph_size_location(image_desc_t *im, int elements, int piechart )
1925 /* The actual size of the image to draw is determined from
1926 ** several sources. The size given on the command line is
1927 ** the graph area but we need more as we have to draw labels
1928 ** and other things outside the graph area
1931 /* +-+-------------------------------------------+
1932 ** |l|.................title.....................|
1933 ** |e+--+-------------------------------+--------+
1936 ** |l| l| main graph area | chart |
1939 ** |r+--+-------------------------------+--------+
1940 ** |e| | x-axis labels | |
1941 ** |v+--+-------------------------------+--------+
1942 ** | |..............legends......................|
1943 ** +-+-------------------------------------------+
1945 int Xvertical=0, Yvertical=0,
1946 Xtitle =0, Ytitle =0,
1947 Xylabel =0, Yylabel =0,
1950 Xxlabel =0, Yxlabel =0,
1951 Xlegend =0, Ylegend =0,
1952 Xspacing =10, Yspacing =10;
1954 if (im->ylegend[0] != '\0') {
1955 Xvertical = im->text_prop[TEXT_PROP_LEGEND].size *2;
1956 Yvertical = im->text_prop[TEXT_PROP_LEGEND].size * (strlen(im->ylegend)+1);
1959 if (im->title[0] != '\0') {
1960 /* The title is placed "inbetween" two text lines so it
1961 ** automatically has some vertical spacing. The horizontal
1962 ** spacing is added here, on each side.
1964 Xtitle = gfx_get_text_width(im->canvas, 0,
1965 im->text_prop[TEXT_PROP_TITLE].font,
1966 im->text_prop[TEXT_PROP_TITLE].size,
1968 im->title) + 2*Xspacing;
1969 Ytitle = im->text_prop[TEXT_PROP_TITLE].size*2;
1975 if (im->draw_x_grid) {
1977 Yxlabel=im->text_prop[TEXT_PROP_LEGEND].size *2;
1979 if (im->draw_y_grid) {
1980 Xylabel=im->text_prop[TEXT_PROP_LEGEND].size *6;
1986 im->piesize=im->xsize<im->ysize?im->xsize:im->ysize;
1991 /* Now calculate the total size. Insert some spacing where
1992 desired. im->xorigin and im->yorigin need to correspond
1993 with the lower left corner of the main graph area or, if
1994 this one is not set, the imaginary box surrounding the
1997 /* The legend width cannot yet be determined, as a result we
1998 ** have problems adjusting the image to it. For now, we just
1999 ** forget about it at all; the legend will have to fit in the
2000 ** size already allocated.
2002 im->xgif = Xylabel + Xmain + Xpie + Xspacing;
2003 if (Xmain) im->xgif += Xspacing;
2004 if (Xpie) im->xgif += Xspacing;
2005 im->xorigin = Xspacing + Xylabel;
2006 if (Xtitle > im->xgif) im->xgif = Xtitle;
2008 im->xgif += Xvertical;
2009 im->xorigin += Xvertical;
2013 /* The vertical size is interesting... we need to compare
2014 ** the sum of {Ytitle, Ymain, Yxlabel, Ylegend} with Yvertical
2015 ** however we need to know {Ytitle+Ymain+Yxlabel} in order to
2016 ** start even thinking about Ylegend.
2018 ** Do it in three portions: First calculate the inner part,
2019 ** then do the legend, then adjust the total height of the img.
2022 /* reserve space for main and/or pie */
2023 im->ygif = Ymain + Yxlabel;
2024 if (im->ygif < Ypie) im->ygif = Ypie;
2025 im->yorigin = im->ygif - Yxlabel;
2026 /* reserve space for the title *or* some padding above the graph */
2029 im->yorigin += Ytitle;
2031 im->ygif += Yspacing;
2032 im->yorigin += Yspacing;
2034 /* reserve space for padding below the graph */
2035 im->ygif += Yspacing;
2038 /* Determine where to place the legends onto the image.
2039 ** Adjust im->ygif to match the space requirements.
2041 if(leg_place(im)==-1)
2044 /* last of three steps: check total height of image */
2045 if (im->ygif < Yvertical) im->ygif = Yvertical;
2048 if (Xlegend > im->xgif) {
2050 /* reposition Pie */
2053 /* The pie is placed in the upper right hand corner,
2054 ** just below the title (if any) and with sufficient
2058 im->pie_x = im->xgif - Xspacing - Xpie/2;
2059 im->pie_y = im->yorigin-Ymain+Ypie/2;
2061 im->pie_x = im->xgif/2;
2062 im->pie_y = im->yorigin-Ypie/2;
2068 /* draw that picture thing ... */
2070 graph_paint(image_desc_t *im, char ***calcpr)
2073 int lazy = lazy_check(im);
2075 double PieStart=0.0;
2079 double areazero = 0.0;
2080 enum gf_en stack_gf = GF_PRINT;
2081 graph_desc_t *lastgdes = NULL;
2083 /* if we are lazy and there is nothing to PRINT ... quit now */
2084 if (lazy && im->prt_c==0) return 0;
2086 /* pull the data from the rrd files ... */
2088 if(data_fetch(im)==-1)
2091 /* evaluate VDEF and CDEF operations ... */
2092 if(data_calc(im)==-1)
2095 /* check if we need to draw a piechart */
2096 for(i=0;i<im->gdes_c;i++){
2097 if (im->gdes[i].gf == GF_PART) {
2103 /* calculate and PRINT and GPRINT definitions. We have to do it at
2104 * this point because it will affect the length of the legends
2105 * if there are no graph elements we stop here ...
2106 * if we are lazy, try to quit ...
2108 i=print_calc(im,calcpr);
2110 if(((i==0)&&(piechart==0)) || lazy) return 0;
2112 /* If there's only the pie chart to draw, signal this */
2113 if (i==0) piechart=2;
2115 /* get actual drawing data and find min and max values*/
2116 if(data_proc(im)==-1)
2119 if(!im->logarithmic){si_unit(im);} /* identify si magnitude Kilo, Mega Giga ? */
2121 if(!im->rigid && ! im->logarithmic)
2122 expand_range(im); /* make sure the upper and lower limit are
2125 /**************************************************************
2126 *** Calculating sizes and locations became a bit confusing ***
2127 *** so I moved this into a separate function. ***
2128 **************************************************************/
2129 if(graph_size_location(im,i,piechart)==-1)
2132 /* the actual graph is created by going through the individual
2133 graph elements and then drawing them */
2135 node=gfx_new_area ( im->canvas,
2139 im->graph_col[GRC_BACK]);
2141 gfx_add_point(node,0, im->ygif);
2143 if (piechart != 2) {
2144 node=gfx_new_area ( im->canvas,
2145 im->xorigin, im->yorigin,
2146 im->xorigin + im->xsize, im->yorigin,
2147 im->xorigin + im->xsize, im->yorigin-im->ysize,
2148 im->graph_col[GRC_CANVAS]);
2150 gfx_add_point(node,im->xorigin, im->yorigin - im->ysize);
2152 if (im->minval > 0.0)
2153 areazero = im->minval;
2154 if (im->maxval < 0.0)
2155 areazero = im->maxval;
2161 pie_part(im,im->graph_col[GRC_CANVAS],im->pie_x,im->pie_y,im->piesize*0.5,0,2*M_PI);
2164 for(i=0;i<im->gdes_c;i++){
2165 switch(im->gdes[i].gf){
2176 for (ii = 0; ii < im->xsize; ii++)
2178 if (!isnan(im->gdes[i].p_data[ii]) &&
2179 im->gdes[i].p_data[ii] > 0.0)
2181 /* generate a tick */
2182 gfx_new_line(im->canvas, im -> xorigin + ii,
2183 im -> yorigin - (im -> gdes[i].yrule * im -> ysize),
2187 im -> gdes[i].col );
2193 stack_gf = im->gdes[i].gf;
2195 /* fix data points at oo and -oo */
2196 for(ii=0;ii<im->xsize;ii++){
2197 if (isinf(im->gdes[i].p_data[ii])){
2198 if (im->gdes[i].p_data[ii] > 0) {
2199 im->gdes[i].p_data[ii] = im->maxval ;
2201 im->gdes[i].p_data[ii] = im->minval ;
2207 if (im->gdes[i].col != 0x0){
2208 /* GF_LINE and friend */
2209 if(stack_gf == GF_LINE ){
2211 for(ii=1;ii<im->xsize;ii++){
2212 if ( ! isnan(im->gdes[i].p_data[ii-1])
2213 && ! isnan(im->gdes[i].p_data[ii])){
2215 node = gfx_new_line(im->canvas,
2216 ii-1+im->xorigin,ytr(im,im->gdes[i].p_data[ii-1]),
2217 ii+im->xorigin,ytr(im,im->gdes[i].p_data[ii]),
2218 im->gdes[i].linewidth,
2221 gfx_add_point(node,ii+im->xorigin,ytr(im,im->gdes[i].p_data[ii]));
2230 for(ii=1;ii<im->xsize;ii++){
2232 if ( ! isnan(im->gdes[i].p_data[ii-1])
2233 && ! isnan(im->gdes[i].p_data[ii])){
2236 if (im->gdes[i].gf == GF_STACK) {
2237 ybase = ytr(im,lastgdes->p_data[ii-1]);
2239 ybase = ytr(im,areazero);
2242 node = gfx_new_area(im->canvas,
2243 ii-1+im->xorigin,ybase,
2244 ii-1+im->xorigin,ytr(im,im->gdes[i].p_data[ii-1]),
2245 ii+im->xorigin,ytr(im,im->gdes[i].p_data[ii]),
2249 gfx_add_point(node,ii+im->xorigin,ytr(im,im->gdes[i].p_data[ii]));
2253 if ( node != NULL && (ii+1==im->xsize || isnan(im->gdes[i].p_data[ii]) )){
2254 /* GF_AREA STACK type*/
2255 if (im->gdes[i].gf == GF_STACK ) {
2257 for (iii=ii-1;iii>area_start;iii--){
2258 gfx_add_point(node,iii+im->xorigin,ytr(im,lastgdes->p_data[iii]));
2261 gfx_add_point(node,ii+im->xorigin,ytr(im,areazero));
2266 } /* else GF_LINE */
2267 } /* if color != 0x0 */
2268 /* make sure we do not run into trouble when stacking on NaN */
2269 for(ii=0;ii<im->xsize;ii++){
2270 if (isnan(im->gdes[i].p_data[ii])) {
2273 ybase = ytr(im,lastgdes->p_data[ii-1]);
2275 if (isnan(ybase) || !lastgdes ){
2276 ybase = ytr(im,areazero);
2278 im->gdes[i].p_data[ii] = ybase;
2281 lastgdes = &(im->gdes[i]);
2284 if(isnan(im->gdes[i].yrule)) /* fetch variable */
2285 im->gdes[i].yrule = im->gdes[im->gdes[i].vidx].vf.val;
2287 if (finite(im->gdes[i].yrule)) { /* even the fetched var can be NaN */
2288 pie_part(im,im->gdes[i].col,
2289 im->pie_x,im->pie_y,im->piesize*0.4,
2290 M_PI*2.0*PieStart/100.0,
2291 M_PI*2.0*(PieStart+im->gdes[i].yrule)/100.0);
2292 PieStart += im->gdes[i].yrule;
2301 /* grid_paint also does the text */
2304 /* the RULES are the last thing to paint ... */
2305 for(i=0;i<im->gdes_c;i++){
2307 switch(im->gdes[i].gf){
2309 if(isnan(im->gdes[i].yrule)) { /* fetch variable */
2310 im->gdes[i].yrule = im->gdes[im->gdes[i].vidx].vf.val;
2312 if(im->gdes[i].yrule >= im->minval
2313 && im->gdes[i].yrule <= im->maxval)
2314 gfx_new_line(im->canvas,
2315 im->xorigin,ytr(im,im->gdes[i].yrule),
2316 im->xorigin+im->xsize,ytr(im,im->gdes[i].yrule),
2317 1.0,im->gdes[i].col);
2320 if(im->gdes[i].xrule == 0) { /* fetch variable */
2321 im->gdes[i].xrule = im->gdes[im->gdes[i].vidx].vf.when;
2323 if(im->gdes[i].xrule >= im->start
2324 && im->gdes[i].xrule <= im->end)
2325 gfx_new_line(im->canvas,
2326 xtr(im,im->gdes[i].xrule),im->yorigin,
2327 xtr(im,im->gdes[i].xrule),im->yorigin-im->ysize,
2328 1.0,im->gdes[i].col);
2336 if (strcmp(im->graphfile,"-")==0) {
2338 /* Change translation mode for stdout to BINARY */
2339 _setmode( _fileno( stdout ), O_BINARY );
2343 if ((fo = fopen(im->graphfile,"wb")) == NULL) {
2344 rrd_set_error("Opening '%s' for write: %s",im->graphfile,
2349 gfx_render (im->canvas,im->xgif,im->ygif,0x0,fo);
2350 if (strcmp(im->graphfile,"-") != 0)
2356 /*****************************************************
2358 *****************************************************/
2361 gdes_alloc(image_desc_t *im){
2363 long def_step = (im->end-im->start)/im->xsize;
2365 if (im->step > def_step) /* step can be increassed ... no decreassed */
2366 def_step = im->step;
2370 if ((im->gdes = (graph_desc_t *) rrd_realloc(im->gdes, (im->gdes_c)
2371 * sizeof(graph_desc_t)))==NULL){
2372 rrd_set_error("realloc graph_descs");
2377 im->gdes[im->gdes_c-1].step=def_step;
2378 im->gdes[im->gdes_c-1].start=im->start;
2379 im->gdes[im->gdes_c-1].end=im->end;
2380 im->gdes[im->gdes_c-1].vname[0]='\0';
2381 im->gdes[im->gdes_c-1].data=NULL;
2382 im->gdes[im->gdes_c-1].ds_namv=NULL;
2383 im->gdes[im->gdes_c-1].data_first=0;
2384 im->gdes[im->gdes_c-1].p_data=NULL;
2385 im->gdes[im->gdes_c-1].rpnp=NULL;
2386 im->gdes[im->gdes_c-1].col = 0x0;
2387 im->gdes[im->gdes_c-1].legend[0]='\0';
2388 im->gdes[im->gdes_c-1].rrd[0]='\0';
2389 im->gdes[im->gdes_c-1].ds=-1;
2390 im->gdes[im->gdes_c-1].p_data=NULL;
2394 /* copies input untill the first unescaped colon is found
2395 or until input ends. backslashes have to be escaped as well */
2397 scan_for_col(char *input, int len, char *output)
2402 input[inp] != ':' &&
2405 if (input[inp] == '\\' &&
2406 input[inp+1] != '\0' &&
2407 (input[inp+1] == '\\' ||
2408 input[inp+1] == ':')){
2409 output[outp++] = input[++inp];
2412 output[outp++] = input[inp];
2415 output[outp] = '\0';
2419 /* Some surgery done on this function, it became ridiculously big.
2421 ** - initializing now in rrd_graph_init()
2422 ** - options parsing now in rrd_graph_options()
2423 ** - script parsing now in rrd_graph_script()
2426 rrd_graph(int argc, char **argv, char ***prdata, int *xsize, int *ysize)
2430 rrd_graph_init(&im);
2432 rrd_graph_options(argc,argv,&im);
2433 if (rrd_test_error()) return -1;
2435 if (strlen(argv[optind])>=MAXPATH) {
2436 rrd_set_error("filename (including path) too long");
2439 strncpy(im.graphfile,argv[optind],MAXPATH-1);
2440 im.graphfile[MAXPATH-1]='\0';
2442 rrd_graph_script(argc,argv,&im);
2443 if (rrd_test_error()) return -1;
2445 /* Everything is now read and the actual work can start */
2448 if (graph_paint(&im,prdata)==-1){
2453 /* The image is generated and needs to be output.
2454 ** Also, if needed, print a line with information about the image.
2462 /* maybe prdata is not allocated yet ... lets do it now */
2463 if ((*prdata = calloc(2,sizeof(char *)))==NULL) {
2464 rrd_set_error("malloc imginfo");
2468 if(((*prdata)[0] = malloc((strlen(im.imginfo)+200+strlen(im.graphfile))*sizeof(char)))
2470 rrd_set_error("malloc imginfo");
2473 filename=im.graphfile+strlen(im.graphfile);
2474 while(filename > im.graphfile) {
2475 if (*(filename-1)=='/' || *(filename-1)=='\\' ) break;
2479 sprintf((*prdata)[0],im.imginfo,filename,(long)(im.canvas->zoom*im.xgif),(long)(im.canvas->zoom*im.ygif));
2486 rrd_graph_init(image_desc_t *im)
2490 im->xlab_user.minsec = -1;
2496 im->ylegend[0] = '\0';
2497 im->title[0] = '\0';
2500 im->unitsexponent= 9999;
2505 im->logarithmic = 0;
2506 im->ygridstep = DNAN;
2507 im->draw_x_grid = 1;
2508 im->draw_y_grid = 1;
2513 im->canvas = gfx_new_canvas();
2515 for(i=0;i<DIM(graph_col);i++)
2516 im->graph_col[i]=graph_col[i];
2518 for(i=0;i<DIM(text_prop);i++){
2519 im->text_prop[i].size = text_prop[i].size;
2520 im->text_prop[i].font = text_prop[i].font;
2525 rrd_graph_options(int argc, char *argv[],image_desc_t *im)
2528 char *parsetime_error = NULL;
2529 char scan_gtm[12],scan_mtm[12],scan_ltm[12],col_nam[12];
2530 time_t start_tmp=0,end_tmp=0;
2532 struct time_value start_tv, end_tv;
2535 parsetime("end-24h", &start_tv);
2536 parsetime("now", &end_tv);
2539 static struct option long_options[] =
2541 {"start", required_argument, 0, 's'},
2542 {"end", required_argument, 0, 'e'},
2543 {"x-grid", required_argument, 0, 'x'},
2544 {"y-grid", required_argument, 0, 'y'},
2545 {"vertical-label",required_argument,0,'v'},
2546 {"width", required_argument, 0, 'w'},
2547 {"height", required_argument, 0, 'h'},
2548 {"interlaced", no_argument, 0, 'i'},
2549 {"upper-limit",required_argument, 0, 'u'},
2550 {"lower-limit",required_argument, 0, 'l'},
2551 {"rigid", no_argument, 0, 'r'},
2552 {"base", required_argument, 0, 'b'},
2553 {"logarithmic",no_argument, 0, 'o'},
2554 {"color", required_argument, 0, 'c'},
2555 {"font", required_argument, 0, 'n'},
2556 {"title", required_argument, 0, 't'},
2557 {"imginfo", required_argument, 0, 'f'},
2558 {"imgformat", required_argument, 0, 'a'},
2559 {"lazy", no_argument, 0, 'z'},
2560 {"zoom", required_argument, 0, 'm'},
2561 {"no-legend", no_argument, 0, 'g'},
2562 {"alt-y-grid", no_argument, 0, 257 },
2563 {"alt-autoscale", no_argument, 0, 258 },
2564 {"alt-autoscale-max", no_argument, 0, 259 },
2565 {"units-exponent",required_argument, 0, 260},
2566 {"step", required_argument, 0, 261},
2568 int option_index = 0;
2572 opt = getopt_long(argc, argv,
2573 "s:e:x:y:v:w:h:iu:l:rb:oc:n:m:t:f:a:z:g",
2574 long_options, &option_index);
2581 im->extra_flags |= ALTYGRID;
2584 im->extra_flags |= ALTAUTOSCALE;
2587 im->extra_flags |= ALTAUTOSCALE_MAX;
2590 im->extra_flags |= NOLEGEND;
2593 im->unitsexponent = atoi(optarg);
2596 im->step = atoi(optarg);
2599 if ((parsetime_error = parsetime(optarg, &start_tv))) {
2600 rrd_set_error( "start time: %s", parsetime_error );
2605 if ((parsetime_error = parsetime(optarg, &end_tv))) {
2606 rrd_set_error( "end time: %s", parsetime_error );
2611 if(strcmp(optarg,"none") == 0){
2617 "%10[A-Z]:%ld:%10[A-Z]:%ld:%10[A-Z]:%ld:%ld:%n",
2619 &im->xlab_user.gridst,
2621 &im->xlab_user.mgridst,
2623 &im->xlab_user.labst,
2624 &im->xlab_user.precis,
2625 &stroff) == 7 && stroff != 0){
2626 strncpy(im->xlab_form, optarg+stroff, sizeof(im->xlab_form) - 1);
2627 if((im->xlab_user.gridtm = tmt_conv(scan_gtm)) == -1){
2628 rrd_set_error("unknown keyword %s",scan_gtm);
2630 } else if ((im->xlab_user.mgridtm = tmt_conv(scan_mtm)) == -1){
2631 rrd_set_error("unknown keyword %s",scan_mtm);
2633 } else if ((im->xlab_user.labtm = tmt_conv(scan_ltm)) == -1){
2634 rrd_set_error("unknown keyword %s",scan_ltm);
2637 im->xlab_user.minsec = 1;
2638 im->xlab_user.stst = im->xlab_form;
2640 rrd_set_error("invalid x-grid format");
2646 if(strcmp(optarg,"none") == 0){
2654 &im->ylabfact) == 2) {
2655 if(im->ygridstep<=0){
2656 rrd_set_error("grid step must be > 0");
2658 } else if (im->ylabfact < 1){
2659 rrd_set_error("label factor must be > 0");
2663 rrd_set_error("invalid y-grid format");
2668 strncpy(im->ylegend,optarg,150);
2669 im->ylegend[150]='\0';
2672 im->maxval = atof(optarg);
2675 im->minval = atof(optarg);
2678 im->base = atol(optarg);
2679 if(im->base != 1024 && im->base != 1000 ){
2680 rrd_set_error("the only sensible value for base apart from 1000 is 1024");
2685 long_tmp = atol(optarg);
2686 if (long_tmp < 10) {
2687 rrd_set_error("width below 10 pixels");
2690 im->xsize = long_tmp;
2693 long_tmp = atol(optarg);
2694 if (long_tmp < 10) {
2695 rrd_set_error("height below 10 pixels");
2698 im->ysize = long_tmp;
2701 im->canvas->interlaced = 1;
2707 im->imginfo = optarg;
2710 if((im->canvas->imgformat = if_conv(optarg)) == -1) {
2711 rrd_set_error("unsupported graphics format '%s'",optarg);
2719 im->logarithmic = 1;
2720 if (isnan(im->minval))
2726 col_nam,&color) == 2){
2728 if((ci=grc_conv(col_nam)) != -1){
2729 im->graph_col[ci]=color;
2731 rrd_set_error("invalid color name '%s'",col_nam);
2734 rrd_set_error("invalid color def format");
2739 /* originally this used char *prop = "" and
2740 ** char *font = "dummy" however this results
2741 ** in a SEG fault, at least on RH7.1
2743 ** The current implementation isn't proper
2744 ** either, font is never freed and prop uses
2745 ** a fixed width string
2754 prop,&size,font) == 3){
2756 if((sindex=text_prop_conv(prop)) != -1){
2757 im->text_prop[sindex].size=size;
2758 im->text_prop[sindex].font=font;
2759 if (sindex==0) { /* the default */
2760 im->text_prop[TEXT_PROP_TITLE].size=size;
2761 im->text_prop[TEXT_PROP_TITLE].font=font;
2762 im->text_prop[TEXT_PROP_AXIS].size=size;
2763 im->text_prop[TEXT_PROP_AXIS].font=font;
2764 im->text_prop[TEXT_PROP_UNIT].size=size;
2765 im->text_prop[TEXT_PROP_UNIT].font=font;
2766 im->text_prop[TEXT_PROP_LEGEND].size=size;
2767 im->text_prop[TEXT_PROP_LEGEND].font=font;
2770 rrd_set_error("invalid fonttag '%s'",prop);
2774 rrd_set_error("invalid text property format");
2780 im->canvas->zoom = atof(optarg);
2781 if (im->canvas->zoom <= 0.0) {
2782 rrd_set_error("zoom factor must be > 0");
2787 strncpy(im->title,optarg,150);
2788 im->title[150]='\0';
2793 rrd_set_error("unknown option '%c'", optopt);
2795 rrd_set_error("unknown option '%s'",argv[optind-1]);
2800 if (optind >= argc) {
2801 rrd_set_error("missing filename");
2805 if (im->logarithmic == 1 && (im->minval <= 0 || isnan(im->minval))){
2806 rrd_set_error("for a logarithmic yaxis you must specify a lower-limit > 0");
2810 if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
2811 /* error string is set in parsetime.c */
2815 if (start_tmp < 3600*24*365*10){
2816 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",start_tmp);
2820 if (end_tmp < start_tmp) {
2821 rrd_set_error("start (%ld) should be less than end (%ld)",
2822 start_tmp, end_tmp);
2826 im->start = start_tmp;
2831 rrd_graph_script(int argc, char *argv[], image_desc_t *im)
2835 int linepass = 0; /* stack must follow LINE*, AREA or STACK */
2837 for (i=optind+1;i<argc;i++) {
2842 char funcname[10],vname[MAX_VNAME_LEN+1],sep[1];
2847 /* Each command is one element from *argv[], we call this "line".
2849 ** Each command defines the most current gdes inside struct im.
2850 ** In stead of typing "im->gdes[im->gdes_c-1]" we use "gdp".
2853 gdp=&im->gdes[im->gdes_c-1];
2856 /* function:newvname=string[:ds-name:CF] for xDEF
2857 ** function:vname[#color[:string]] for LINEx,AREA,STACK
2858 ** function:vname#color[:num[:string]] for TICK
2859 ** function:vname-or-num#color[:string] for xRULE,PART
2860 ** function:vname:CF:string for xPRINT
2861 ** function:string for COMMENT
2865 sscanf(line, "%10[A-Z0-9]:%n", funcname,&argstart);
2867 rrd_set_error("Cannot parse function in line: %s",line);
2871 if(sscanf(funcname,"LINE%lf",&linewidth)){
2872 im->gdes[im->gdes_c-1].gf = GF_LINE;
2873 im->gdes[im->gdes_c-1].linewidth = linewidth;
2875 if ((gdp->gf=gf_conv(funcname))==-1) {
2876 rrd_set_error("'%s' is not a valid function name",funcname);
2882 /* If the error string is set, we exit at the end of the switch */
2885 if (rrd_graph_legend(gdp,&line[argstart])==0)
2886 rrd_set_error("Cannot parse comment in line: %s",line);
2892 sscanf(&line[argstart], "%lf%n#%n", &d, &j, &k);
2893 sscanf(&line[argstart], DEF_NAM_FMT "%n#%n", vname, &l, &m);
2895 rrd_set_error("Cannot parse name or num in line: %s",line);
2902 } else if (!rrd_graph_check_vname(im,vname,line)) {
2906 } else break; /* exit due to wrong vname */
2907 if ((j=rrd_graph_color(im,&line[argstart],line,0))==0) break;
2909 if (strlen(&line[argstart])!=0) {
2910 if (rrd_graph_legend(gdp,&line[++argstart])==0)
2911 rrd_set_error("Cannot parse comment in line: %s",line);
2916 rrd_set_error("STACK must follow another graphing element");
2924 sscanf(&line[argstart],DEF_NAM_FMT"%n%1[#:]%n",vname,&j,sep,&k);
2926 rrd_set_error("Cannot parse vname in line: %s",line);
2927 else if (rrd_graph_check_vname(im,vname,line))
2928 rrd_set_error("Undefined vname '%s' in line: %s",line);
2930 k=rrd_graph_color(im,&line[argstart],line,1);
2931 if (rrd_test_error()) break;
2932 argstart=argstart+j+k;
2933 if ((strlen(&line[argstart])!=0)&&(gdp->gf==GF_TICK)) {
2935 sscanf(&line[argstart], ":%lf%n", &gdp->yrule,&j);
2938 if (strlen(&line[argstart])!=0)
2939 if (rrd_graph_legend(gdp,&line[++argstart])==0)
2940 rrd_set_error("Cannot parse legend in line: %s",line);
2946 sscanf(&line[argstart], DEF_NAM_FMT ":%n",gdp->vname,&j);
2948 rrd_set_error("Cannot parse vname in line: '%s'",line);
2952 if (rrd_graph_check_vname(im,gdp->vname,line)) return;
2954 sscanf(&line[argstart], CF_NAM_FMT ":%n",symname,&j);
2956 k=(j!=0)?rrd_graph_check_CF(im,symname,line):1;
2957 #define VIDX im->gdes[gdp->vidx]
2959 case -1: /* looks CF but is not really CF */
2960 if (VIDX.gf == GF_VDEF) rrd_clear_error();
2962 case 0: /* CF present and correct */
2963 if (VIDX.gf == GF_VDEF)
2964 rrd_set_error("Don't use CF when printing VDEF");
2967 case 1: /* CF not present */
2968 if (VIDX.gf == GF_VDEF) rrd_clear_error();
2969 else rrd_set_error("Printing DEF or CDEF needs CF");
2972 rrd_set_error("Oops, bug in GPRINT scanning");
2975 if (rrd_test_error()) break;
2977 if (strlen(&line[argstart])!=0) {
2978 if (rrd_graph_legend(gdp,&line[argstart])==0)
2979 rrd_set_error("Cannot parse legend in line: %s",line);
2980 } else rrd_set_error("No legend in (G)PRINT line: %s",line);
2981 strcpy(gdp->format, gdp->legend);
2987 sscanf(&line[argstart], DEF_NAM_FMT "=%n",gdp->vname,&j);
2989 rrd_set_error("Could not parse line: %s",line);
2992 if (find_var(im,gdp->vname)!=-1) {
2993 rrd_set_error("Variable '%s' in line '%s' already in use\n",
3000 argstart+=scan_for_col(&line[argstart],MAXPATH,gdp->rrd);
3002 sscanf(&line[argstart],
3003 ":" DS_NAM_FMT ":" CF_NAM_FMT "%n%*s%n",
3004 gdp->ds_nam, symname, &j, &k);
3005 if ((j==0)||(k!=0)) {
3006 rrd_set_error("Cannot parse DS or CF in '%s'",line);
3009 rrd_graph_check_CF(im,symname,line);
3013 sscanf(&line[argstart],DEF_NAM_FMT ",%n",vname,&j);
3015 rrd_set_error("Cannot parse vname in line '%s'",line);
3019 if (rrd_graph_check_vname(im,vname,line)) return;
3020 if ( im->gdes[gdp->vidx].gf != GF_DEF
3021 && im->gdes[gdp->vidx].gf != GF_CDEF) {
3022 rrd_set_error("variable '%s' not DEF nor "
3023 "CDEF in VDEF '%s'", vname,gdp->vname);
3026 vdef_parse(gdp,&line[argstart+strstart]);
3029 if (strstr(&line[argstart],":")!=NULL) {
3030 rrd_set_error("Error in RPN, line: %s",line);
3033 if ((gdp->rpnp = rpn_parse(
3038 rrd_set_error("invalid rpn expression in: %s",line);
3043 default: rrd_set_error("Big oops");
3045 if (rrd_test_error()) {
3052 rrd_set_error("can't make a graph without contents");
3053 im_free(im); /* ??? is this set ??? */
3058 rrd_graph_check_vname(image_desc_t *im, char *varname, char *err)
3060 if ((im->gdes[im->gdes_c-1].vidx=find_var(im,varname))==-1) {
3061 rrd_set_error("Unknown variable '%s' in %s",varname,err);
3067 rrd_graph_color(image_desc_t *im, char *var, char *err, int optional)
3070 graph_desc_t *gdp=&im->gdes[im->gdes_c-1];
3072 color=strstr(var,"#");
3075 rrd_set_error("Found no color in %s",err);
3084 rest=strstr(color,":");
3092 sscanf(color,"#%6lx%n",&col,&n);
3093 col = (col << 8) + 0xff /* shift left by 8 */;
3094 if (n!=7) rrd_set_error("Color problem in %s",err);
3097 sscanf(color,"#%8lx%n",&col,&n);
3100 rrd_set_error("Color problem in %s",err);
3102 if (rrd_test_error()) return 0;
3108 rrd_graph_check_CF(image_desc_t *im, char *symname, char *err)
3110 if ((im->gdes[im->gdes_c-1].cf=cf_conv(symname))==-1) {
3111 rrd_set_error("Unknown CF '%s' in %s",symname,err);
3117 rrd_graph_legend(graph_desc_t *gdp, char *line)
3121 i=scan_for_col(line,FMT_LEG_LEN,gdp->legend);
3123 return (strlen(&line[i])==0);
3127 int bad_format(char *fmt) {
3132 while (*ptr != '\0') {
3133 if (*ptr == '%') {ptr++;
3134 if (*ptr == '\0') return 1;
3135 while ((*ptr >= '0' && *ptr <= '9') || *ptr == '.') {
3138 if (*ptr == '\0') return 1;
3142 if (*ptr == '\0') return 1;
3143 if (*ptr == 'e' || *ptr == 'f') {
3145 } else { return 1; }
3147 else if (*ptr == 's' || *ptr == 'S' || *ptr == '%') { ++ptr; }
3156 vdef_parse(gdes,str)
3157 struct graph_desc_t *gdes;
3160 /* A VDEF currently is either "func" or "param,func"
3161 * so the parsing is rather simple. Change if needed.
3168 sscanf(str,"%le,%29[A-Z]%n",¶m,func,&n);
3169 if (n==strlen(str)) { /* matched */
3173 sscanf(str,"%29[A-Z]%n",func,&n);
3174 if (n==strlen(str)) { /* matched */
3177 rrd_set_error("Unknown function string '%s' in VDEF '%s'"
3184 if (!strcmp("PERCENT",func)) gdes->vf.op = VDEF_PERCENT;
3185 else if (!strcmp("MAXIMUM",func)) gdes->vf.op = VDEF_MAXIMUM;
3186 else if (!strcmp("AVERAGE",func)) gdes->vf.op = VDEF_AVERAGE;
3187 else if (!strcmp("MINIMUM",func)) gdes->vf.op = VDEF_MINIMUM;
3188 else if (!strcmp("TOTAL", func)) gdes->vf.op = VDEF_TOTAL;
3189 else if (!strcmp("FIRST", func)) gdes->vf.op = VDEF_FIRST;
3190 else if (!strcmp("LAST", func)) gdes->vf.op = VDEF_LAST;
3192 rrd_set_error("Unknown function '%s' in VDEF '%s'\n"
3199 switch (gdes->vf.op) {
3201 if (isnan(param)) { /* no parameter given */
3202 rrd_set_error("Function '%s' needs parameter in VDEF '%s'\n"
3208 if (param>=0.0 && param<=100.0) {
3209 gdes->vf.param = param;
3210 gdes->vf.val = DNAN; /* undefined */
3211 gdes->vf.when = 0; /* undefined */
3213 rrd_set_error("Parameter '%f' out of range in VDEF '%s'\n"
3227 gdes->vf.param = DNAN;
3228 gdes->vf.val = DNAN;
3231 rrd_set_error("Function '%s' needs no parameter in VDEF '%s'\n"
3246 graph_desc_t *src,*dst;
3250 dst = &im->gdes[gdi];
3251 src = &im->gdes[dst->vidx];
3252 data = src->data + src->ds;
3253 steps = (src->end - src->start) / src->step;
3256 printf("DEBUG: start == %lu, end == %lu, %lu steps\n"
3263 switch (dst->vf.op) {
3264 case VDEF_PERCENT: {
3265 rrd_value_t * array;
3269 if ((array = malloc(steps*sizeof(double)))==NULL) {
3270 rrd_set_error("malloc VDEV_PERCENT");
3273 for (step=0;step < steps; step++) {
3274 array[step]=data[step*src->ds_cnt];
3276 qsort(array,step,sizeof(double),vdef_percent_compar);
3278 field = (steps-1)*dst->vf.param/100;
3279 dst->vf.val = array[field];
3280 dst->vf.when = 0; /* no time component */
3282 for(step=0;step<steps;step++)
3283 printf("DEBUG: %3li:%10.2f %c\n",step,array[step],step==field?'*':' ');
3289 while (step != steps && isnan(data[step*src->ds_cnt])) step++;
3290 if (step == steps) {
3294 dst->vf.val = data[step*src->ds_cnt];
3295 dst->vf.when = src->start + (step+1)*src->step;
3297 while (step != steps) {
3298 if (finite(data[step*src->ds_cnt])) {
3299 if (data[step*src->ds_cnt] > dst->vf.val) {
3300 dst->vf.val = data[step*src->ds_cnt];
3301 dst->vf.when = src->start + (step+1)*src->step;
3308 case VDEF_AVERAGE: {
3311 for (step=0;step<steps;step++) {
3312 if (finite(data[step*src->ds_cnt])) {
3313 sum += data[step*src->ds_cnt];
3318 if (dst->vf.op == VDEF_TOTAL) {
3319 dst->vf.val = sum*src->step;
3320 dst->vf.when = cnt*src->step; /* not really "when" */
3322 dst->vf.val = sum/cnt;
3323 dst->vf.when = 0; /* no time component */
3333 while (step != steps && isnan(data[step*src->ds_cnt])) step++;
3334 if (step == steps) {
3338 dst->vf.val = data[step*src->ds_cnt];
3339 dst->vf.when = src->start + (step+1)*src->step;
3341 while (step != steps) {
3342 if (finite(data[step*src->ds_cnt])) {
3343 if (data[step*src->ds_cnt] < dst->vf.val) {
3344 dst->vf.val = data[step*src->ds_cnt];
3345 dst->vf.when = src->start + (step+1)*src->step;
3352 /* The time value returned here is one step before the
3353 * actual time value. This is the start of the first
3357 while (step != steps && isnan(data[step*src->ds_cnt])) step++;
3358 if (step == steps) { /* all entries were NaN */
3362 dst->vf.val = data[step*src->ds_cnt];
3363 dst->vf.when = src->start + step*src->step;
3367 /* The time value returned here is the
3368 * actual time value. This is the end of the last
3372 while (step >= 0 && isnan(data[step*src->ds_cnt])) step--;
3373 if (step < 0) { /* all entries were NaN */
3377 dst->vf.val = data[step*src->ds_cnt];
3378 dst->vf.when = src->start + (step+1)*src->step;
3385 /* NaN < -INF < finite_values < INF */
3387 vdef_percent_compar(a,b)
3390 /* Equality is not returned; this doesn't hurt except
3391 * (maybe) for a little performance.
3394 /* First catch NaN values. They are smallest */
3395 if (isnan( *(double *)a )) return -1;
3396 if (isnan( *(double *)b )) return 1;
3398 /* NaN doesn't reach this part so INF and -INF are extremes.
3399 * The sign from isinf() is compatible with the sign we return
3401 if (isinf( *(double *)a )) return isinf( *(double *)a );
3402 if (isinf( *(double *)b )) return isinf( *(double *)b );
3404 /* If we reach this, both values must be finite */
3405 if ( *(double *)a < *(double *)b ) return -1; else return 1;