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->yimg;
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->ximg - 2*border){
1328 if (prt_fctn != '\0'){
1330 if (leg_c >= 2 && prt_fctn == 'j') {
1331 glue = (im->ximg - fill - 2* border) / (leg_c-1);
1335 if (prt_fctn =='c') leg_x = (im->ximg - fill) / 2.0;
1336 if (prt_fctn =='r') leg_x = im->ximg - 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->yimg,
1726 2,2,im->graph_col[GRC_SHADEA]);
1727 gfx_add_point( node , im->ximg - 2, 2 );
1728 gfx_add_point( node , im->ximg, 0 );
1729 gfx_add_point( node , 0,0 );
1730 /* gfx_add_point( node , 0,im->yimg ); */
1732 node = gfx_new_area (im->canvas, 2,im->yimg-2,
1733 im->ximg-2,im->yimg-2,
1735 im->graph_col[GRC_SHADEB]);
1736 gfx_add_point( node , im->ximg,0);
1737 gfx_add_point( node , im->ximg,im->yimg);
1738 gfx_add_point( node , 0,im->yimg);
1739 /* gfx_add_point( node , 0,im->yimg ); */
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->ximg/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->ximg/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 imgstat;
1855 if (im->lazy == 0) return 0; /* no lazy option */
1856 if (stat(im->graphfile,&imgstat) != 0)
1857 return 0; /* can't stat */
1858 /* one pixel in the existing graph is more then what we would
1860 if (time(NULL) - imgstat.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->ximg),&(im->yimg));
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,
1952 Xlegend =0, Ylegend =0,
1954 Xspacing =10, Yspacing =10;
1956 if (im->ylegend[0] != '\0') {
1957 Xvertical = im->text_prop[TEXT_PROP_LEGEND].size *2;
1958 Yvertical = im->text_prop[TEXT_PROP_LEGEND].size * (strlen(im->ylegend)+1);
1961 if (im->title[0] != '\0') {
1962 /* The title is placed "inbetween" two text lines so it
1963 ** automatically has some vertical spacing. The horizontal
1964 ** spacing is added here, on each side.
1966 Xtitle = gfx_get_text_width(im->canvas, 0,
1967 im->text_prop[TEXT_PROP_TITLE].font,
1968 im->text_prop[TEXT_PROP_TITLE].size,
1970 im->title) + 2*Xspacing;
1971 Ytitle = im->text_prop[TEXT_PROP_TITLE].size*2;
1977 if (im->draw_x_grid) {
1979 Yxlabel=im->text_prop[TEXT_PROP_LEGEND].size *2;
1981 if (im->draw_y_grid) {
1982 Xylabel=im->text_prop[TEXT_PROP_LEGEND].size *6;
1988 im->piesize=im->xsize<im->ysize?im->xsize:im->ysize;
1993 /* Now calculate the total size. Insert some spacing where
1994 desired. im->xorigin and im->yorigin need to correspond
1995 with the lower left corner of the main graph area or, if
1996 this one is not set, the imaginary box surrounding the
1999 /* The legend width cannot yet be determined, as a result we
2000 ** have problems adjusting the image to it. For now, we just
2001 ** forget about it at all; the legend will have to fit in the
2002 ** size already allocated.
2004 im->ximg = Xylabel + Xmain + Xpie + Xspacing;
2005 if (Xmain) im->ximg += Xspacing;
2006 if (Xpie) im->ximg += Xspacing;
2007 im->xorigin = Xspacing + Xylabel;
2008 if (Xtitle > im->ximg) im->ximg = Xtitle;
2010 im->ximg += Xvertical;
2011 im->xorigin += Xvertical;
2015 /* The vertical size is interesting... we need to compare
2016 ** the sum of {Ytitle, Ymain, Yxlabel, Ylegend} with Yvertical
2017 ** however we need to know {Ytitle+Ymain+Yxlabel} in order to
2018 ** start even thinking about Ylegend.
2020 ** Do it in three portions: First calculate the inner part,
2021 ** then do the legend, then adjust the total height of the img.
2024 /* reserve space for main and/or pie */
2025 im->yimg = Ymain + Yxlabel;
2026 if (im->yimg < Ypie) im->yimg = Ypie;
2027 im->yorigin = im->yimg - Yxlabel;
2028 /* reserve space for the title *or* some padding above the graph */
2031 im->yorigin += Ytitle;
2033 im->yimg += Yspacing;
2034 im->yorigin += Yspacing;
2036 /* reserve space for padding below the graph */
2037 im->yimg += Yspacing;
2040 /* Determine where to place the legends onto the image.
2041 ** Adjust im->yimg to match the space requirements.
2043 if(leg_place(im)==-1)
2046 /* last of three steps: check total height of image */
2047 if (im->yimg < Yvertical) im->yimg = Yvertical;
2050 if (Xlegend > im->ximg) {
2052 /* reposition Pie */
2055 /* The pie is placed in the upper right hand corner,
2056 ** just below the title (if any) and with sufficient
2060 im->pie_x = im->ximg - Xspacing - Xpie/2;
2061 im->pie_y = im->yorigin-Ymain+Ypie/2;
2063 im->pie_x = im->ximg/2;
2064 im->pie_y = im->yorigin-Ypie/2;
2070 /* draw that picture thing ... */
2072 graph_paint(image_desc_t *im, char ***calcpr)
2075 int lazy = lazy_check(im);
2077 double PieStart=0.0;
2081 double areazero = 0.0;
2082 enum gf_en stack_gf = GF_PRINT;
2083 graph_desc_t *lastgdes = NULL;
2085 /* if we are lazy and there is nothing to PRINT ... quit now */
2086 if (lazy && im->prt_c==0) return 0;
2088 /* pull the data from the rrd files ... */
2090 if(data_fetch(im)==-1)
2093 /* evaluate VDEF and CDEF operations ... */
2094 if(data_calc(im)==-1)
2097 /* check if we need to draw a piechart */
2098 for(i=0;i<im->gdes_c;i++){
2099 if (im->gdes[i].gf == GF_PART) {
2105 /* calculate and PRINT and GPRINT definitions. We have to do it at
2106 * this point because it will affect the length of the legends
2107 * if there are no graph elements we stop here ...
2108 * if we are lazy, try to quit ...
2110 i=print_calc(im,calcpr);
2112 if(((i==0)&&(piechart==0)) || lazy) return 0;
2114 /* If there's only the pie chart to draw, signal this */
2115 if (i==0) piechart=2;
2117 /* get actual drawing data and find min and max values*/
2118 if(data_proc(im)==-1)
2121 if(!im->logarithmic){si_unit(im);} /* identify si magnitude Kilo, Mega Giga ? */
2123 if(!im->rigid && ! im->logarithmic)
2124 expand_range(im); /* make sure the upper and lower limit are
2127 /**************************************************************
2128 *** Calculating sizes and locations became a bit confusing ***
2129 *** so I moved this into a separate function. ***
2130 **************************************************************/
2131 if(graph_size_location(im,i,piechart)==-1)
2134 /* the actual graph is created by going through the individual
2135 graph elements and then drawing them */
2137 node=gfx_new_area ( im->canvas,
2141 im->graph_col[GRC_BACK]);
2143 gfx_add_point(node,0, im->yimg);
2145 if (piechart != 2) {
2146 node=gfx_new_area ( im->canvas,
2147 im->xorigin, im->yorigin,
2148 im->xorigin + im->xsize, im->yorigin,
2149 im->xorigin + im->xsize, im->yorigin-im->ysize,
2150 im->graph_col[GRC_CANVAS]);
2152 gfx_add_point(node,im->xorigin, im->yorigin - im->ysize);
2154 if (im->minval > 0.0)
2155 areazero = im->minval;
2156 if (im->maxval < 0.0)
2157 areazero = im->maxval;
2163 pie_part(im,im->graph_col[GRC_CANVAS],im->pie_x,im->pie_y,im->piesize*0.5,0,2*M_PI);
2166 for(i=0;i<im->gdes_c;i++){
2167 switch(im->gdes[i].gf){
2178 for (ii = 0; ii < im->xsize; ii++)
2180 if (!isnan(im->gdes[i].p_data[ii]) &&
2181 im->gdes[i].p_data[ii] > 0.0)
2183 /* generate a tick */
2184 gfx_new_line(im->canvas, im -> xorigin + ii,
2185 im -> yorigin - (im -> gdes[i].yrule * im -> ysize),
2189 im -> gdes[i].col );
2195 stack_gf = im->gdes[i].gf;
2197 /* fix data points at oo and -oo */
2198 for(ii=0;ii<im->xsize;ii++){
2199 if (isinf(im->gdes[i].p_data[ii])){
2200 if (im->gdes[i].p_data[ii] > 0) {
2201 im->gdes[i].p_data[ii] = im->maxval ;
2203 im->gdes[i].p_data[ii] = im->minval ;
2209 if (im->gdes[i].col != 0x0){
2210 /* GF_LINE and friend */
2211 if(stack_gf == GF_LINE ){
2213 for(ii=1;ii<im->xsize;ii++){
2214 if ( ! isnan(im->gdes[i].p_data[ii-1])
2215 && ! isnan(im->gdes[i].p_data[ii])){
2217 node = gfx_new_line(im->canvas,
2218 ii-1+im->xorigin,ytr(im,im->gdes[i].p_data[ii-1]),
2219 ii+im->xorigin,ytr(im,im->gdes[i].p_data[ii]),
2220 im->gdes[i].linewidth,
2223 gfx_add_point(node,ii+im->xorigin,ytr(im,im->gdes[i].p_data[ii]));
2232 for(ii=1;ii<im->xsize;ii++){
2234 if ( ! isnan(im->gdes[i].p_data[ii-1])
2235 && ! isnan(im->gdes[i].p_data[ii])){
2238 if (im->gdes[i].gf == GF_STACK) {
2239 ybase = ytr(im,lastgdes->p_data[ii-1]);
2241 ybase = ytr(im,areazero);
2244 node = gfx_new_area(im->canvas,
2245 ii-1+im->xorigin,ybase,
2246 ii-1+im->xorigin,ytr(im,im->gdes[i].p_data[ii-1]),
2247 ii+im->xorigin,ytr(im,im->gdes[i].p_data[ii]),
2251 gfx_add_point(node,ii+im->xorigin,ytr(im,im->gdes[i].p_data[ii]));
2255 if ( node != NULL && (ii+1==im->xsize || isnan(im->gdes[i].p_data[ii]) )){
2256 /* GF_AREA STACK type*/
2257 if (im->gdes[i].gf == GF_STACK ) {
2259 for (iii=ii-1;iii>area_start;iii--){
2260 gfx_add_point(node,iii+im->xorigin,ytr(im,lastgdes->p_data[iii]));
2263 gfx_add_point(node,ii+im->xorigin,ytr(im,areazero));
2268 } /* else GF_LINE */
2269 } /* if color != 0x0 */
2270 /* make sure we do not run into trouble when stacking on NaN */
2271 for(ii=0;ii<im->xsize;ii++){
2272 if (isnan(im->gdes[i].p_data[ii])) {
2275 ybase = ytr(im,lastgdes->p_data[ii-1]);
2277 if (isnan(ybase) || !lastgdes ){
2278 ybase = ytr(im,areazero);
2280 im->gdes[i].p_data[ii] = ybase;
2283 lastgdes = &(im->gdes[i]);
2286 if(isnan(im->gdes[i].yrule)) /* fetch variable */
2287 im->gdes[i].yrule = im->gdes[im->gdes[i].vidx].vf.val;
2289 if (finite(im->gdes[i].yrule)) { /* even the fetched var can be NaN */
2290 pie_part(im,im->gdes[i].col,
2291 im->pie_x,im->pie_y,im->piesize*0.4,
2292 M_PI*2.0*PieStart/100.0,
2293 M_PI*2.0*(PieStart+im->gdes[i].yrule)/100.0);
2294 PieStart += im->gdes[i].yrule;
2303 /* grid_paint also does the text */
2306 /* the RULES are the last thing to paint ... */
2307 for(i=0;i<im->gdes_c;i++){
2309 switch(im->gdes[i].gf){
2311 if(isnan(im->gdes[i].yrule)) { /* fetch variable */
2312 im->gdes[i].yrule = im->gdes[im->gdes[i].vidx].vf.val;
2314 if(im->gdes[i].yrule >= im->minval
2315 && im->gdes[i].yrule <= im->maxval)
2316 gfx_new_line(im->canvas,
2317 im->xorigin,ytr(im,im->gdes[i].yrule),
2318 im->xorigin+im->xsize,ytr(im,im->gdes[i].yrule),
2319 1.0,im->gdes[i].col);
2322 if(im->gdes[i].xrule == 0) { /* fetch variable */
2323 im->gdes[i].xrule = im->gdes[im->gdes[i].vidx].vf.when;
2325 if(im->gdes[i].xrule >= im->start
2326 && im->gdes[i].xrule <= im->end)
2327 gfx_new_line(im->canvas,
2328 xtr(im,im->gdes[i].xrule),im->yorigin,
2329 xtr(im,im->gdes[i].xrule),im->yorigin-im->ysize,
2330 1.0,im->gdes[i].col);
2338 if (strcmp(im->graphfile,"-")==0) {
2340 /* Change translation mode for stdout to BINARY */
2341 _setmode( _fileno( stdout ), O_BINARY );
2345 if ((fo = fopen(im->graphfile,"wb")) == NULL) {
2346 rrd_set_error("Opening '%s' for write: %s",im->graphfile,
2351 gfx_render (im->canvas,im->ximg,im->yimg,0x0,fo);
2352 if (strcmp(im->graphfile,"-") != 0)
2358 /*****************************************************
2360 *****************************************************/
2363 gdes_alloc(image_desc_t *im){
2365 long def_step = (im->end-im->start)/im->xsize;
2367 if (im->step > def_step) /* step can be increassed ... no decreassed */
2368 def_step = im->step;
2372 if ((im->gdes = (graph_desc_t *) rrd_realloc(im->gdes, (im->gdes_c)
2373 * sizeof(graph_desc_t)))==NULL){
2374 rrd_set_error("realloc graph_descs");
2379 im->gdes[im->gdes_c-1].step=def_step;
2380 im->gdes[im->gdes_c-1].start=im->start;
2381 im->gdes[im->gdes_c-1].end=im->end;
2382 im->gdes[im->gdes_c-1].vname[0]='\0';
2383 im->gdes[im->gdes_c-1].data=NULL;
2384 im->gdes[im->gdes_c-1].ds_namv=NULL;
2385 im->gdes[im->gdes_c-1].data_first=0;
2386 im->gdes[im->gdes_c-1].p_data=NULL;
2387 im->gdes[im->gdes_c-1].rpnp=NULL;
2388 im->gdes[im->gdes_c-1].col = 0x0;
2389 im->gdes[im->gdes_c-1].legend[0]='\0';
2390 im->gdes[im->gdes_c-1].rrd[0]='\0';
2391 im->gdes[im->gdes_c-1].ds=-1;
2392 im->gdes[im->gdes_c-1].p_data=NULL;
2396 /* copies input untill the first unescaped colon is found
2397 or until input ends. backslashes have to be escaped as well */
2399 scan_for_col(char *input, int len, char *output)
2404 input[inp] != ':' &&
2407 if (input[inp] == '\\' &&
2408 input[inp+1] != '\0' &&
2409 (input[inp+1] == '\\' ||
2410 input[inp+1] == ':')){
2411 output[outp++] = input[++inp];
2414 output[outp++] = input[inp];
2417 output[outp] = '\0';
2421 /* Some surgery done on this function, it became ridiculously big.
2423 ** - initializing now in rrd_graph_init()
2424 ** - options parsing now in rrd_graph_options()
2425 ** - script parsing now in rrd_graph_script()
2428 rrd_graph(int argc, char **argv, char ***prdata, int *xsize, int *ysize)
2432 rrd_graph_init(&im);
2434 rrd_graph_options(argc,argv,&im);
2435 if (rrd_test_error()) return -1;
2437 if (strlen(argv[optind])>=MAXPATH) {
2438 rrd_set_error("filename (including path) too long");
2441 strncpy(im.graphfile,argv[optind],MAXPATH-1);
2442 im.graphfile[MAXPATH-1]='\0';
2444 rrd_graph_script(argc,argv,&im);
2445 if (rrd_test_error()) return -1;
2447 /* Everything is now read and the actual work can start */
2450 if (graph_paint(&im,prdata)==-1){
2455 /* The image is generated and needs to be output.
2456 ** Also, if needed, print a line with information about the image.
2464 /* maybe prdata is not allocated yet ... lets do it now */
2465 if ((*prdata = calloc(2,sizeof(char *)))==NULL) {
2466 rrd_set_error("malloc imginfo");
2470 if(((*prdata)[0] = malloc((strlen(im.imginfo)+200+strlen(im.graphfile))*sizeof(char)))
2472 rrd_set_error("malloc imginfo");
2475 filename=im.graphfile+strlen(im.graphfile);
2476 while(filename > im.graphfile) {
2477 if (*(filename-1)=='/' || *(filename-1)=='\\' ) break;
2481 sprintf((*prdata)[0],im.imginfo,filename,(long)(im.canvas->zoom*im.ximg),(long)(im.canvas->zoom*im.yimg));
2488 rrd_graph_init(image_desc_t *im)
2492 im->xlab_user.minsec = -1;
2498 im->ylegend[0] = '\0';
2499 im->title[0] = '\0';
2502 im->unitsexponent= 9999;
2507 im->logarithmic = 0;
2508 im->ygridstep = DNAN;
2509 im->draw_x_grid = 1;
2510 im->draw_y_grid = 1;
2515 im->canvas = gfx_new_canvas();
2517 for(i=0;i<DIM(graph_col);i++)
2518 im->graph_col[i]=graph_col[i];
2520 for(i=0;i<DIM(text_prop);i++){
2521 im->text_prop[i].size = text_prop[i].size;
2522 im->text_prop[i].font = text_prop[i].font;
2527 rrd_graph_options(int argc, char *argv[],image_desc_t *im)
2530 char *parsetime_error = NULL;
2531 char scan_gtm[12],scan_mtm[12],scan_ltm[12],col_nam[12];
2532 time_t start_tmp=0,end_tmp=0;
2534 struct time_value start_tv, end_tv;
2537 parsetime("end-24h", &start_tv);
2538 parsetime("now", &end_tv);
2541 static struct option long_options[] =
2543 {"start", required_argument, 0, 's'},
2544 {"end", required_argument, 0, 'e'},
2545 {"x-grid", required_argument, 0, 'x'},
2546 {"y-grid", required_argument, 0, 'y'},
2547 {"vertical-label",required_argument,0,'v'},
2548 {"width", required_argument, 0, 'w'},
2549 {"height", required_argument, 0, 'h'},
2550 {"interlaced", no_argument, 0, 'i'},
2551 {"upper-limit",required_argument, 0, 'u'},
2552 {"lower-limit",required_argument, 0, 'l'},
2553 {"rigid", no_argument, 0, 'r'},
2554 {"base", required_argument, 0, 'b'},
2555 {"logarithmic",no_argument, 0, 'o'},
2556 {"color", required_argument, 0, 'c'},
2557 {"font", required_argument, 0, 'n'},
2558 {"title", required_argument, 0, 't'},
2559 {"imginfo", required_argument, 0, 'f'},
2560 {"imgformat", required_argument, 0, 'a'},
2561 {"lazy", no_argument, 0, 'z'},
2562 {"zoom", required_argument, 0, 'm'},
2563 {"no-legend", no_argument, 0, 'g'},
2564 {"alt-y-grid", no_argument, 0, 257 },
2565 {"alt-autoscale", no_argument, 0, 258 },
2566 {"alt-autoscale-max", no_argument, 0, 259 },
2567 {"units-exponent",required_argument, 0, 260},
2568 {"step", required_argument, 0, 261},
2570 int option_index = 0;
2574 opt = getopt_long(argc, argv,
2575 "s:e:x:y:v:w:h:iu:l:rb:oc:n:m:t:f:a:z:g",
2576 long_options, &option_index);
2583 im->extra_flags |= ALTYGRID;
2586 im->extra_flags |= ALTAUTOSCALE;
2589 im->extra_flags |= ALTAUTOSCALE_MAX;
2592 im->extra_flags |= NOLEGEND;
2595 im->unitsexponent = atoi(optarg);
2598 im->step = atoi(optarg);
2601 if ((parsetime_error = parsetime(optarg, &start_tv))) {
2602 rrd_set_error( "start time: %s", parsetime_error );
2607 if ((parsetime_error = parsetime(optarg, &end_tv))) {
2608 rrd_set_error( "end time: %s", parsetime_error );
2613 if(strcmp(optarg,"none") == 0){
2619 "%10[A-Z]:%ld:%10[A-Z]:%ld:%10[A-Z]:%ld:%ld:%n",
2621 &im->xlab_user.gridst,
2623 &im->xlab_user.mgridst,
2625 &im->xlab_user.labst,
2626 &im->xlab_user.precis,
2627 &stroff) == 7 && stroff != 0){
2628 strncpy(im->xlab_form, optarg+stroff, sizeof(im->xlab_form) - 1);
2629 if((im->xlab_user.gridtm = tmt_conv(scan_gtm)) == -1){
2630 rrd_set_error("unknown keyword %s",scan_gtm);
2632 } else if ((im->xlab_user.mgridtm = tmt_conv(scan_mtm)) == -1){
2633 rrd_set_error("unknown keyword %s",scan_mtm);
2635 } else if ((im->xlab_user.labtm = tmt_conv(scan_ltm)) == -1){
2636 rrd_set_error("unknown keyword %s",scan_ltm);
2639 im->xlab_user.minsec = 1;
2640 im->xlab_user.stst = im->xlab_form;
2642 rrd_set_error("invalid x-grid format");
2648 if(strcmp(optarg,"none") == 0){
2656 &im->ylabfact) == 2) {
2657 if(im->ygridstep<=0){
2658 rrd_set_error("grid step must be > 0");
2660 } else if (im->ylabfact < 1){
2661 rrd_set_error("label factor must be > 0");
2665 rrd_set_error("invalid y-grid format");
2670 strncpy(im->ylegend,optarg,150);
2671 im->ylegend[150]='\0';
2674 im->maxval = atof(optarg);
2677 im->minval = atof(optarg);
2680 im->base = atol(optarg);
2681 if(im->base != 1024 && im->base != 1000 ){
2682 rrd_set_error("the only sensible value for base apart from 1000 is 1024");
2687 long_tmp = atol(optarg);
2688 if (long_tmp < 10) {
2689 rrd_set_error("width below 10 pixels");
2692 im->xsize = long_tmp;
2695 long_tmp = atol(optarg);
2696 if (long_tmp < 10) {
2697 rrd_set_error("height below 10 pixels");
2700 im->ysize = long_tmp;
2703 im->canvas->interlaced = 1;
2709 im->imginfo = optarg;
2712 if((im->canvas->imgformat = if_conv(optarg)) == -1) {
2713 rrd_set_error("unsupported graphics format '%s'",optarg);
2721 im->logarithmic = 1;
2722 if (isnan(im->minval))
2728 col_nam,&color) == 2){
2730 if((ci=grc_conv(col_nam)) != -1){
2731 im->graph_col[ci]=color;
2733 rrd_set_error("invalid color name '%s'",col_nam);
2736 rrd_set_error("invalid color def format");
2741 /* originally this used char *prop = "" and
2742 ** char *font = "dummy" however this results
2743 ** in a SEG fault, at least on RH7.1
2745 ** The current implementation isn't proper
2746 ** either, font is never freed and prop uses
2747 ** a fixed width string
2756 prop,&size,font) == 3){
2758 if((sindex=text_prop_conv(prop)) != -1){
2759 im->text_prop[sindex].size=size;
2760 im->text_prop[sindex].font=font;
2761 if (sindex==0) { /* the default */
2762 im->text_prop[TEXT_PROP_TITLE].size=size;
2763 im->text_prop[TEXT_PROP_TITLE].font=font;
2764 im->text_prop[TEXT_PROP_AXIS].size=size;
2765 im->text_prop[TEXT_PROP_AXIS].font=font;
2766 im->text_prop[TEXT_PROP_UNIT].size=size;
2767 im->text_prop[TEXT_PROP_UNIT].font=font;
2768 im->text_prop[TEXT_PROP_LEGEND].size=size;
2769 im->text_prop[TEXT_PROP_LEGEND].font=font;
2772 rrd_set_error("invalid fonttag '%s'",prop);
2776 rrd_set_error("invalid text property format");
2782 im->canvas->zoom = atof(optarg);
2783 if (im->canvas->zoom <= 0.0) {
2784 rrd_set_error("zoom factor must be > 0");
2789 strncpy(im->title,optarg,150);
2790 im->title[150]='\0';
2795 rrd_set_error("unknown option '%c'", optopt);
2797 rrd_set_error("unknown option '%s'",argv[optind-1]);
2802 if (optind >= argc) {
2803 rrd_set_error("missing filename");
2807 if (im->logarithmic == 1 && (im->minval <= 0 || isnan(im->minval))){
2808 rrd_set_error("for a logarithmic yaxis you must specify a lower-limit > 0");
2812 if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
2813 /* error string is set in parsetime.c */
2817 if (start_tmp < 3600*24*365*10){
2818 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",start_tmp);
2822 if (end_tmp < start_tmp) {
2823 rrd_set_error("start (%ld) should be less than end (%ld)",
2824 start_tmp, end_tmp);
2828 im->start = start_tmp;
2833 rrd_graph_script(int argc, char *argv[], image_desc_t *im)
2837 int linepass = 0; /* stack must follow LINE*, AREA or STACK */
2839 for (i=optind+1;i<argc;i++) {
2844 char funcname[10],vname[MAX_VNAME_LEN+1],sep[1];
2849 /* Each command is one element from *argv[], we call this "line".
2851 ** Each command defines the most current gdes inside struct im.
2852 ** In stead of typing "im->gdes[im->gdes_c-1]" we use "gdp".
2855 gdp=&im->gdes[im->gdes_c-1];
2858 /* function:newvname=string[:ds-name:CF] for xDEF
2859 ** function:vname[#color[:string]] for LINEx,AREA,STACK
2860 ** function:vname#color[:num[:string]] for TICK
2861 ** function:vname-or-num#color[:string] for xRULE,PART
2862 ** function:vname:CF:string for xPRINT
2863 ** function:string for COMMENT
2867 sscanf(line, "%10[A-Z0-9]:%n", funcname,&argstart);
2869 rrd_set_error("Cannot parse function in line: %s",line);
2873 if(sscanf(funcname,"LINE%lf",&linewidth)){
2874 im->gdes[im->gdes_c-1].gf = GF_LINE;
2875 im->gdes[im->gdes_c-1].linewidth = linewidth;
2877 if ((gdp->gf=gf_conv(funcname))==-1) {
2878 rrd_set_error("'%s' is not a valid function name",funcname);
2884 /* If the error string is set, we exit at the end of the switch */
2887 if (rrd_graph_legend(gdp,&line[argstart])==0)
2888 rrd_set_error("Cannot parse comment in line: %s",line);
2894 sscanf(&line[argstart], "%lf%n#%n", &d, &j, &k);
2895 sscanf(&line[argstart], DEF_NAM_FMT "%n#%n", vname, &l, &m);
2897 rrd_set_error("Cannot parse name or num in line: %s",line);
2904 } else if (!rrd_graph_check_vname(im,vname,line)) {
2908 } else break; /* exit due to wrong vname */
2909 if ((j=rrd_graph_color(im,&line[argstart],line,0))==0) break;
2911 if (strlen(&line[argstart])!=0) {
2912 if (rrd_graph_legend(gdp,&line[++argstart])==0)
2913 rrd_set_error("Cannot parse comment in line: %s",line);
2918 rrd_set_error("STACK must follow another graphing element");
2926 sscanf(&line[argstart],DEF_NAM_FMT"%n%1[#:]%n",vname,&j,sep,&k);
2928 rrd_set_error("Cannot parse vname in line: %s",line);
2929 else if (rrd_graph_check_vname(im,vname,line))
2930 rrd_set_error("Undefined vname '%s' in line: %s",line);
2932 k=rrd_graph_color(im,&line[argstart],line,1);
2933 if (rrd_test_error()) break;
2934 argstart=argstart+j+k;
2935 if ((strlen(&line[argstart])!=0)&&(gdp->gf==GF_TICK)) {
2937 sscanf(&line[argstart], ":%lf%n", &gdp->yrule,&j);
2940 if (strlen(&line[argstart])!=0)
2941 if (rrd_graph_legend(gdp,&line[++argstart])==0)
2942 rrd_set_error("Cannot parse legend in line: %s",line);
2948 sscanf(&line[argstart], DEF_NAM_FMT ":%n",gdp->vname,&j);
2950 rrd_set_error("Cannot parse vname in line: '%s'",line);
2954 if (rrd_graph_check_vname(im,gdp->vname,line)) return;
2956 sscanf(&line[argstart], CF_NAM_FMT ":%n",symname,&j);
2958 k=(j!=0)?rrd_graph_check_CF(im,symname,line):1;
2959 #define VIDX im->gdes[gdp->vidx]
2961 case -1: /* looks CF but is not really CF */
2962 if (VIDX.gf == GF_VDEF) rrd_clear_error();
2964 case 0: /* CF present and correct */
2965 if (VIDX.gf == GF_VDEF)
2966 rrd_set_error("Don't use CF when printing VDEF");
2969 case 1: /* CF not present */
2970 if (VIDX.gf == GF_VDEF) rrd_clear_error();
2971 else rrd_set_error("Printing DEF or CDEF needs CF");
2974 rrd_set_error("Oops, bug in GPRINT scanning");
2977 if (rrd_test_error()) break;
2979 if (strlen(&line[argstart])!=0) {
2980 if (rrd_graph_legend(gdp,&line[argstart])==0)
2981 rrd_set_error("Cannot parse legend in line: %s",line);
2982 } else rrd_set_error("No legend in (G)PRINT line: %s",line);
2983 strcpy(gdp->format, gdp->legend);
2989 sscanf(&line[argstart], DEF_NAM_FMT "=%n",gdp->vname,&j);
2991 rrd_set_error("Could not parse line: %s",line);
2994 if (find_var(im,gdp->vname)!=-1) {
2995 rrd_set_error("Variable '%s' in line '%s' already in use\n",
3002 argstart+=scan_for_col(&line[argstart],MAXPATH,gdp->rrd);
3004 sscanf(&line[argstart],
3005 ":" DS_NAM_FMT ":" CF_NAM_FMT "%n%*s%n",
3006 gdp->ds_nam, symname, &j, &k);
3007 if ((j==0)||(k!=0)) {
3008 rrd_set_error("Cannot parse DS or CF in '%s'",line);
3011 rrd_graph_check_CF(im,symname,line);
3015 sscanf(&line[argstart],DEF_NAM_FMT ",%n",vname,&j);
3017 rrd_set_error("Cannot parse vname in line '%s'",line);
3021 if (rrd_graph_check_vname(im,vname,line)) return;
3022 if ( im->gdes[gdp->vidx].gf != GF_DEF
3023 && im->gdes[gdp->vidx].gf != GF_CDEF) {
3024 rrd_set_error("variable '%s' not DEF nor "
3025 "CDEF in VDEF '%s'", vname,gdp->vname);
3028 vdef_parse(gdp,&line[argstart+strstart]);
3031 if (strstr(&line[argstart],":")!=NULL) {
3032 rrd_set_error("Error in RPN, line: %s",line);
3035 if ((gdp->rpnp = rpn_parse(
3040 rrd_set_error("invalid rpn expression in: %s",line);
3045 default: rrd_set_error("Big oops");
3047 if (rrd_test_error()) {
3054 rrd_set_error("can't make a graph without contents");
3055 im_free(im); /* ??? is this set ??? */
3060 rrd_graph_check_vname(image_desc_t *im, char *varname, char *err)
3062 if ((im->gdes[im->gdes_c-1].vidx=find_var(im,varname))==-1) {
3063 rrd_set_error("Unknown variable '%s' in %s",varname,err);
3069 rrd_graph_color(image_desc_t *im, char *var, char *err, int optional)
3072 graph_desc_t *gdp=&im->gdes[im->gdes_c-1];
3074 color=strstr(var,"#");
3077 rrd_set_error("Found no color in %s",err);
3086 rest=strstr(color,":");
3094 sscanf(color,"#%6lx%n",&col,&n);
3095 col = (col << 8) + 0xff /* shift left by 8 */;
3096 if (n!=7) rrd_set_error("Color problem in %s",err);
3099 sscanf(color,"#%8lx%n",&col,&n);
3102 rrd_set_error("Color problem in %s",err);
3104 if (rrd_test_error()) return 0;
3110 rrd_graph_check_CF(image_desc_t *im, char *symname, char *err)
3112 if ((im->gdes[im->gdes_c-1].cf=cf_conv(symname))==-1) {
3113 rrd_set_error("Unknown CF '%s' in %s",symname,err);
3119 rrd_graph_legend(graph_desc_t *gdp, char *line)
3123 i=scan_for_col(line,FMT_LEG_LEN,gdp->legend);
3125 return (strlen(&line[i])==0);
3129 int bad_format(char *fmt) {
3134 while (*ptr != '\0') {
3135 if (*ptr == '%') {ptr++;
3136 if (*ptr == '\0') return 1;
3137 while ((*ptr >= '0' && *ptr <= '9') || *ptr == '.') {
3140 if (*ptr == '\0') return 1;
3144 if (*ptr == '\0') return 1;
3145 if (*ptr == 'e' || *ptr == 'f') {
3147 } else { return 1; }
3149 else if (*ptr == 's' || *ptr == 'S' || *ptr == '%') { ++ptr; }
3158 vdef_parse(gdes,str)
3159 struct graph_desc_t *gdes;
3162 /* A VDEF currently is either "func" or "param,func"
3163 * so the parsing is rather simple. Change if needed.
3170 sscanf(str,"%le,%29[A-Z]%n",¶m,func,&n);
3171 if (n==strlen(str)) { /* matched */
3175 sscanf(str,"%29[A-Z]%n",func,&n);
3176 if (n==strlen(str)) { /* matched */
3179 rrd_set_error("Unknown function string '%s' in VDEF '%s'"
3186 if (!strcmp("PERCENT",func)) gdes->vf.op = VDEF_PERCENT;
3187 else if (!strcmp("MAXIMUM",func)) gdes->vf.op = VDEF_MAXIMUM;
3188 else if (!strcmp("AVERAGE",func)) gdes->vf.op = VDEF_AVERAGE;
3189 else if (!strcmp("MINIMUM",func)) gdes->vf.op = VDEF_MINIMUM;
3190 else if (!strcmp("TOTAL", func)) gdes->vf.op = VDEF_TOTAL;
3191 else if (!strcmp("FIRST", func)) gdes->vf.op = VDEF_FIRST;
3192 else if (!strcmp("LAST", func)) gdes->vf.op = VDEF_LAST;
3194 rrd_set_error("Unknown function '%s' in VDEF '%s'\n"
3201 switch (gdes->vf.op) {
3203 if (isnan(param)) { /* no parameter given */
3204 rrd_set_error("Function '%s' needs parameter in VDEF '%s'\n"
3210 if (param>=0.0 && param<=100.0) {
3211 gdes->vf.param = param;
3212 gdes->vf.val = DNAN; /* undefined */
3213 gdes->vf.when = 0; /* undefined */
3215 rrd_set_error("Parameter '%f' out of range in VDEF '%s'\n"
3229 gdes->vf.param = DNAN;
3230 gdes->vf.val = DNAN;
3233 rrd_set_error("Function '%s' needs no parameter in VDEF '%s'\n"
3248 graph_desc_t *src,*dst;
3252 dst = &im->gdes[gdi];
3253 src = &im->gdes[dst->vidx];
3254 data = src->data + src->ds;
3255 steps = (src->end - src->start) / src->step;
3258 printf("DEBUG: start == %lu, end == %lu, %lu steps\n"
3265 switch (dst->vf.op) {
3266 case VDEF_PERCENT: {
3267 rrd_value_t * array;
3271 if ((array = malloc(steps*sizeof(double)))==NULL) {
3272 rrd_set_error("malloc VDEV_PERCENT");
3275 for (step=0;step < steps; step++) {
3276 array[step]=data[step*src->ds_cnt];
3278 qsort(array,step,sizeof(double),vdef_percent_compar);
3280 field = (steps-1)*dst->vf.param/100;
3281 dst->vf.val = array[field];
3282 dst->vf.when = 0; /* no time component */
3284 for(step=0;step<steps;step++)
3285 printf("DEBUG: %3li:%10.2f %c\n",step,array[step],step==field?'*':' ');
3291 while (step != steps && isnan(data[step*src->ds_cnt])) step++;
3292 if (step == steps) {
3296 dst->vf.val = data[step*src->ds_cnt];
3297 dst->vf.when = src->start + (step+1)*src->step;
3299 while (step != steps) {
3300 if (finite(data[step*src->ds_cnt])) {
3301 if (data[step*src->ds_cnt] > dst->vf.val) {
3302 dst->vf.val = data[step*src->ds_cnt];
3303 dst->vf.when = src->start + (step+1)*src->step;
3310 case VDEF_AVERAGE: {
3313 for (step=0;step<steps;step++) {
3314 if (finite(data[step*src->ds_cnt])) {
3315 sum += data[step*src->ds_cnt];
3320 if (dst->vf.op == VDEF_TOTAL) {
3321 dst->vf.val = sum*src->step;
3322 dst->vf.when = cnt*src->step; /* not really "when" */
3324 dst->vf.val = sum/cnt;
3325 dst->vf.when = 0; /* no time component */
3335 while (step != steps && isnan(data[step*src->ds_cnt])) step++;
3336 if (step == steps) {
3340 dst->vf.val = data[step*src->ds_cnt];
3341 dst->vf.when = src->start + (step+1)*src->step;
3343 while (step != steps) {
3344 if (finite(data[step*src->ds_cnt])) {
3345 if (data[step*src->ds_cnt] < dst->vf.val) {
3346 dst->vf.val = data[step*src->ds_cnt];
3347 dst->vf.when = src->start + (step+1)*src->step;
3354 /* The time value returned here is one step before the
3355 * actual time value. This is the start of the first
3359 while (step != steps && isnan(data[step*src->ds_cnt])) step++;
3360 if (step == steps) { /* all entries were NaN */
3364 dst->vf.val = data[step*src->ds_cnt];
3365 dst->vf.when = src->start + step*src->step;
3369 /* The time value returned here is the
3370 * actual time value. This is the end of the last
3374 while (step >= 0 && isnan(data[step*src->ds_cnt])) step--;
3375 if (step < 0) { /* all entries were NaN */
3379 dst->vf.val = data[step*src->ds_cnt];
3380 dst->vf.when = src->start + (step+1)*src->step;
3387 /* NaN < -INF < finite_values < INF */
3389 vdef_percent_compar(a,b)
3392 /* Equality is not returned; this doesn't hurt except
3393 * (maybe) for a little performance.
3396 /* First catch NaN values. They are smallest */
3397 if (isnan( *(double *)a )) return -1;
3398 if (isnan( *(double *)b )) return 1;
3400 /* NaN doesn't reach this part so INF and -INF are extremes.
3401 * The sign from isinf() is compatible with the sign we return
3403 if (isinf( *(double *)a )) return isinf( *(double *)a );
3404 if (isinf( *(double *)b )) return isinf( *(double *)b );
3406 /* If we reach this, both values must be finite */
3407 if ( *(double *)a < *(double *)b ) return -1; else return 1;