1 /****************************************************************************
2 * RRDtool 1.1.x Copyright Tobias Oetiker, 1997 - 2002
3 ****************************************************************************
4 * rrd__graph.c make creates ne rrds
5 ****************************************************************************/
15 #define RRD_DEFAULT_FONT "c:/winnt/fonts/COUR.TTF"
18 #include "rrd_graph.h"
19 #include "rrd_graph_helper.h"
21 /* some constant definitions */
24 #ifndef RRD_DEFAULT_FONT
25 #define RRD_DEFAULT_FONT "/usr/share/fonts/truetype/openoffice/ariosor.ttf"
26 /* #define RRD_DEFAULT_FONT "/usr/share/fonts/truetype/Arial.ttf" */
30 text_prop_t text_prop[] = {
31 { 10.0, RRD_DEFAULT_FONT }, /* default */
32 { 12.0, RRD_DEFAULT_FONT }, /* title */
33 { 8.0, RRD_DEFAULT_FONT }, /* axis */
34 { 10.0, RRD_DEFAULT_FONT }, /* unit */
35 { 10.0, RRD_DEFAULT_FONT } /* legend */
39 {0, TMT_SECOND,30, TMT_MINUTE,5, TMT_MINUTE,5, 0,"%H:%M"},
40 {2, TMT_MINUTE,1, TMT_MINUTE,5, TMT_MINUTE,5, 0,"%H:%M"},
41 {5, TMT_MINUTE,2, TMT_MINUTE,10, TMT_MINUTE,10, 0,"%H:%M"},
42 {10, TMT_MINUTE,5, TMT_MINUTE,20, TMT_MINUTE,20, 0,"%H:%M"},
43 {30, TMT_MINUTE,10, TMT_HOUR,1, TMT_HOUR,1, 0,"%H:%M"},
44 {60, TMT_MINUTE,30, TMT_HOUR,2, TMT_HOUR,2, 0,"%H:%M"},
45 {180, TMT_HOUR,1, TMT_HOUR,6, TMT_HOUR,6, 0,"%H:%M"},
46 /*{300, TMT_HOUR,3, TMT_HOUR,12, TMT_HOUR,12, 12*3600,"%a %p"}, this looks silly*/
47 {600, TMT_HOUR,6, TMT_DAY,1, TMT_DAY,1, 24*3600,"%a"},
48 {1800, TMT_HOUR,12, TMT_DAY,1, TMT_DAY,2, 24*3600,"%a"},
49 {3600, TMT_DAY,1, TMT_WEEK,1, TMT_WEEK,1, 7*24*3600,"Week %V"},
50 {3*3600, TMT_WEEK,1, TMT_MONTH,1, TMT_WEEK,2, 7*24*3600,"Week %V"},
51 {6*3600, TMT_MONTH,1, TMT_MONTH,1, TMT_MONTH,1, 30*24*3600,"%b"},
52 {48*3600, TMT_MONTH,1, TMT_MONTH,3, TMT_MONTH,3, 30*24*3600,"%b"},
53 {10*24*3600, TMT_YEAR,1, TMT_YEAR,1, TMT_YEAR,1, 365*24*3600,"%y"},
54 {-1,TMT_MONTH,0,TMT_MONTH,0,TMT_MONTH,0,0,""}
57 /* sensible logarithmic y label intervals ...
58 the first element of each row defines the possible starting points on the
59 y axis ... the other specify the */
61 double yloglab[][12]= {{ 1e9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
62 { 1e3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
63 { 1e1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
64 /* { 1e1, 1, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, */
65 { 1e1, 1, 2.5, 5, 7.5, 0, 0, 0, 0, 0, 0, 0 },
66 { 1e1, 1, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0 },
67 { 1e1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0 },
68 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }};
70 /* sensible y label intervals ...*/
88 gfx_color_t graph_col[] = /* default colors */
89 { 0xFFFFFFFF, /* canvas */
90 0xF0F0F0FF, /* background */
91 0xD0D0D0FF, /* shade A */
92 0xA0A0A0FF, /* shade B */
93 0x909090FF, /* grid */
94 0xE05050FF, /* major grid */
95 0x000000FF, /* font */
96 0x000000FF, /* frame */
97 0xFF0000FF /* arrow */
104 # define DPRINT(x) (void)(printf x, printf("\n"))
110 /* initialize with xtr(im,0); */
112 xtr(image_desc_t *im,time_t mytime){
115 pixie = (double) im->xsize / (double)(im->end - im->start);
118 return (int)((double)im->xorigin
119 + pixie * ( mytime - im->start ) );
122 /* translate data values into y coordinates */
124 ytr(image_desc_t *im, double value){
129 pixie = (double) im->ysize / (im->maxval - im->minval);
131 pixie = (double) im->ysize / (log10(im->maxval) - log10(im->minval));
133 } else if(!im->logarithmic) {
134 yval = im->yorigin - pixie * (value - im->minval) + 0.5;
136 if (value < im->minval) {
139 yval = im->yorigin - pixie * (log10(value) - log10(im->minval)) + 0.5;
142 /* make sure we don't return anything too unreasonable. GD lib can
143 get terribly slow when drawing lines outside its scope. This is
144 especially problematic in connection with the rigid option */
147 } else if ((int)yval > im->yorigin) {
148 return im->yorigin+2;
149 } else if ((int) yval < im->yorigin - im->ysize){
150 return im->yorigin - im->ysize - 2;
158 /* conversion function for symbolic entry names */
161 #define conv_if(VV,VVV) \
162 if (strcmp(#VV, string) == 0) return VVV ;
164 enum gf_en gf_conv(char *string){
166 conv_if(PRINT,GF_PRINT)
167 conv_if(GPRINT,GF_GPRINT)
168 conv_if(COMMENT,GF_COMMENT)
169 conv_if(HRULE,GF_HRULE)
170 conv_if(VRULE,GF_VRULE)
171 conv_if(LINE,GF_LINE)
172 conv_if(AREA,GF_AREA)
173 conv_if(STACK,GF_STACK)
174 conv_if(TICK,GF_TICK)
176 conv_if(CDEF,GF_CDEF)
177 conv_if(VDEF,GF_VDEF)
178 conv_if(PART,GF_PART)
183 enum gfx_if_en if_conv(char *string){
191 enum tmt_en tmt_conv(char *string){
193 conv_if(SECOND,TMT_SECOND)
194 conv_if(MINUTE,TMT_MINUTE)
195 conv_if(HOUR,TMT_HOUR)
197 conv_if(WEEK,TMT_WEEK)
198 conv_if(MONTH,TMT_MONTH)
199 conv_if(YEAR,TMT_YEAR)
203 enum grc_en grc_conv(char *string){
205 conv_if(BACK,GRC_BACK)
206 conv_if(CANVAS,GRC_CANVAS)
207 conv_if(SHADEA,GRC_SHADEA)
208 conv_if(SHADEB,GRC_SHADEB)
209 conv_if(GRID,GRC_GRID)
210 conv_if(MGRID,GRC_MGRID)
211 conv_if(FONT,GRC_FONT)
212 conv_if(FRAME,GRC_FRAME)
213 conv_if(ARROW,GRC_ARROW)
218 enum text_prop_en text_prop_conv(char *string){
220 conv_if(DEFAULT,TEXT_PROP_DEFAULT)
221 conv_if(TITLE,TEXT_PROP_TITLE)
222 conv_if(AXIS,TEXT_PROP_AXIS)
223 conv_if(UNIT,TEXT_PROP_UNIT)
224 conv_if(LEGEND,TEXT_PROP_LEGEND)
234 im_free(image_desc_t *im)
237 if (im == NULL) return 0;
238 for(i=0;i<im->gdes_c;i++){
239 if (im->gdes[i].data_first){
240 /* careful here, because a single pointer can occur several times */
241 free (im->gdes[i].data);
242 if (im->gdes[i].ds_namv){
243 for (ii=0;ii<im->gdes[i].ds_cnt;ii++)
244 free(im->gdes[i].ds_namv[ii]);
245 free(im->gdes[i].ds_namv);
248 free (im->gdes[i].p_data);
249 free (im->gdes[i].rpnp);
252 gfx_destroy(im->canvas);
256 /* find SI magnitude symbol for the given number*/
259 image_desc_t *im, /* image description */
266 char *symbol[] = {"a", /* 10e-18 Atto */
267 "f", /* 10e-15 Femto */
268 "p", /* 10e-12 Pico */
269 "n", /* 10e-9 Nano */
270 "u", /* 10e-6 Micro */
271 "m", /* 10e-3 Milli */
276 "T", /* 10e12 Tera */
277 "P", /* 10e15 Peta */
283 if (*value == 0.0 || isnan(*value) ) {
287 sindex = floor(log(fabs(*value))/log((double)im->base));
288 *magfact = pow((double)im->base, (double)sindex);
289 (*value) /= (*magfact);
291 if ( sindex <= symbcenter && sindex >= -symbcenter) {
292 (*symb_ptr) = symbol[sindex+symbcenter];
300 /* find SI magnitude symbol for the numbers on the y-axis*/
303 image_desc_t *im /* image description */
307 char symbol[] = {'a', /* 10e-18 Atto */
308 'f', /* 10e-15 Femto */
309 'p', /* 10e-12 Pico */
310 'n', /* 10e-9 Nano */
311 'u', /* 10e-6 Micro */
312 'm', /* 10e-3 Milli */
317 'T', /* 10e12 Tera */
318 'P', /* 10e15 Peta */
324 if (im->unitsexponent != 9999) {
325 /* unitsexponent = 9, 6, 3, 0, -3, -6, -9, etc */
326 digits = floor(im->unitsexponent / 3);
328 digits = floor( log( max( fabs(im->minval),fabs(im->maxval)))/log((double)im->base));
330 im->magfact = pow((double)im->base , digits);
333 printf("digits %6.3f im->magfact %6.3f\n",digits,im->magfact);
336 if ( ((digits+symbcenter) < sizeof(symbol)) &&
337 ((digits+symbcenter) >= 0) )
338 im->symbol = symbol[(int)digits+symbcenter];
343 /* move min and max values around to become sensible */
346 expand_range(image_desc_t *im)
348 double sensiblevalues[] ={1000.0,900.0,800.0,750.0,700.0,
349 600.0,500.0,400.0,300.0,250.0,
350 200.0,125.0,100.0,90.0,80.0,
351 75.0,70.0,60.0,50.0,40.0,30.0,
352 25.0,20.0,10.0,9.0,8.0,
353 7.0,6.0,5.0,4.0,3.5,3.0,
354 2.5,2.0,1.8,1.5,1.2,1.0,
355 0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0.0,-1};
357 double scaled_min,scaled_max;
364 printf("Min: %6.2f Max: %6.2f MagFactor: %6.2f\n",
365 im->minval,im->maxval,im->magfact);
368 if (isnan(im->ygridstep)){
369 if(im->extra_flags & ALTAUTOSCALE) {
370 /* measure the amplitude of the function. Make sure that
371 graph boundaries are slightly higher then max/min vals
372 so we can see amplitude on the graph */
375 delt = im->maxval - im->minval;
377 fact = 2.0 * pow(10.0,
378 floor(log10(max(fabs(im->minval), fabs(im->maxval)))) - 2);
380 adj = (fact - delt) * 0.55;
382 printf("Min: %6.2f Max: %6.2f delt: %6.2f fact: %6.2f adj: %6.2f\n", im->minval, im->maxval, delt, fact, adj);
388 else if(im->extra_flags & ALTAUTOSCALE_MAX) {
389 /* measure the amplitude of the function. Make sure that
390 graph boundaries are slightly higher than max vals
391 so we can see amplitude on the graph */
392 adj = (im->maxval - im->minval) * 0.1;
396 scaled_min = im->minval / im->magfact;
397 scaled_max = im->maxval / im->magfact;
399 for (i=1; sensiblevalues[i] > 0; i++){
400 if (sensiblevalues[i-1]>=scaled_min &&
401 sensiblevalues[i]<=scaled_min)
402 im->minval = sensiblevalues[i]*(im->magfact);
404 if (-sensiblevalues[i-1]<=scaled_min &&
405 -sensiblevalues[i]>=scaled_min)
406 im->minval = -sensiblevalues[i-1]*(im->magfact);
408 if (sensiblevalues[i-1] >= scaled_max &&
409 sensiblevalues[i] <= scaled_max)
410 im->maxval = sensiblevalues[i-1]*(im->magfact);
412 if (-sensiblevalues[i-1]<=scaled_max &&
413 -sensiblevalues[i] >=scaled_max)
414 im->maxval = -sensiblevalues[i]*(im->magfact);
418 /* adjust min and max to the grid definition if there is one */
419 im->minval = (double)im->ylabfact * im->ygridstep *
420 floor(im->minval / ((double)im->ylabfact * im->ygridstep));
421 im->maxval = (double)im->ylabfact * im->ygridstep *
422 ceil(im->maxval /( (double)im->ylabfact * im->ygridstep));
426 fprintf(stderr,"SCALED Min: %6.2f Max: %6.2f Factor: %6.2f\n",
427 im->minval,im->maxval,im->magfact);
432 /* reduce data reimplementation by Alex */
436 enum cf_en cf, /* which consolidation function ?*/
437 unsigned long cur_step, /* step the data currently is in */
438 time_t *start, /* start, end and step as requested ... */
439 time_t *end, /* ... by the application will be ... */
440 unsigned long *step, /* ... adjusted to represent reality */
441 unsigned long *ds_cnt, /* number of data sources in file */
442 rrd_value_t **data) /* two dimensional array containing the data */
444 int i,reduce_factor = ceil((double)(*step) / (double)cur_step);
445 unsigned long col,dst_row,row_cnt,start_offset,end_offset,skiprows=0;
446 rrd_value_t *srcptr,*dstptr;
448 (*step) = cur_step*reduce_factor; /* set new step size for reduced data */
451 row_cnt = ((*end)-(*start))/cur_step;
457 printf("Reducing %lu rows with factor %i time %lu to %lu, step %lu\n",
458 row_cnt,reduce_factor,*start,*end,cur_step);
459 for (col=0;col<row_cnt;col++) {
460 printf("time %10lu: ",*start+(col+1)*cur_step);
461 for (i=0;i<*ds_cnt;i++)
462 printf(" %8.2e",srcptr[*ds_cnt*col+i]);
467 /* We have to combine [reduce_factor] rows of the source
468 ** into one row for the destination. Doing this we also
469 ** need to take care to combine the correct rows. First
470 ** alter the start and end time so that they are multiples
471 ** of the new step time. We cannot reduce the amount of
472 ** time so we have to move the end towards the future and
473 ** the start towards the past.
475 end_offset = (*end) % (*step);
476 start_offset = (*start) % (*step);
478 /* If there is a start offset (which cannot be more than
479 ** one destination row), skip the appropriate number of
480 ** source rows and one destination row. The appropriate
481 ** number is what we do know (start_offset/cur_step) of
482 ** the new interval (*step/cur_step aka reduce_factor).
485 printf("start_offset: %lu end_offset: %lu\n",start_offset,end_offset);
486 printf("row_cnt before: %lu\n",row_cnt);
489 (*start) = (*start)-start_offset;
490 skiprows=reduce_factor-start_offset/cur_step;
491 srcptr+=skiprows* *ds_cnt;
492 for (col=0;col<(*ds_cnt);col++) *dstptr++ = DNAN;
496 printf("row_cnt between: %lu\n",row_cnt);
499 /* At the end we have some rows that are not going to be
500 ** used, the amount is end_offset/cur_step
503 (*end) = (*end)-end_offset+(*step);
504 skiprows = end_offset/cur_step;
508 printf("row_cnt after: %lu\n",row_cnt);
511 /* Sanity check: row_cnt should be multiple of reduce_factor */
512 /* if this gets triggered, something is REALLY WRONG ... we die immediately */
514 if (row_cnt%reduce_factor) {
515 printf("SANITY CHECK: %lu rows cannot be reduced by %i \n",
516 row_cnt,reduce_factor);
517 printf("BUG in reduce_data()\n");
521 /* Now combine reduce_factor intervals at a time
522 ** into one interval for the destination.
525 for (dst_row=0;row_cnt>=reduce_factor;dst_row++) {
526 for (col=0;col<(*ds_cnt);col++) {
527 rrd_value_t newval=DNAN;
528 unsigned long validval=0;
530 for (i=0;i<reduce_factor;i++) {
531 if (isnan(srcptr[i*(*ds_cnt)+col])) {
535 if (isnan(newval)) newval = srcptr[i*(*ds_cnt)+col];
543 newval += srcptr[i*(*ds_cnt)+col];
546 newval = min (newval,srcptr[i*(*ds_cnt)+col]);
549 /* an interval contains a failure if any subintervals contained a failure */
551 newval = max (newval,srcptr[i*(*ds_cnt)+col]);
554 newval = srcptr[i*(*ds_cnt)+col];
559 if (validval == 0){newval = DNAN;} else{
577 srcptr+=(*ds_cnt)*reduce_factor;
578 row_cnt-=reduce_factor;
580 /* If we had to alter the endtime, we didn't have enough
581 ** source rows to fill the last row. Fill it with NaN.
583 if (end_offset) for (col=0;col<(*ds_cnt);col++) *dstptr++ = DNAN;
585 row_cnt = ((*end)-(*start))/ *step;
587 printf("Done reducing. Currently %lu rows, time %lu to %lu, step %lu\n",
588 row_cnt,*start,*end,*step);
589 for (col=0;col<row_cnt;col++) {
590 printf("time %10lu: ",*start+(col+1)*(*step));
591 for (i=0;i<*ds_cnt;i++)
592 printf(" %8.2e",srcptr[*ds_cnt*col+i]);
599 /* get the data required for the graphs from the
603 data_fetch( image_desc_t *im )
607 /* pull the data from the log files ... */
608 for (i=0;i<im->gdes_c;i++){
609 /* only GF_DEF elements fetch data */
610 if (im->gdes[i].gf != GF_DEF)
614 /* do we have it already ?*/
615 for (ii=0;ii<i;ii++){
616 if (im->gdes[ii].gf != GF_DEF)
618 if((strcmp(im->gdes[i].rrd,im->gdes[ii].rrd) == 0)
619 && (im->gdes[i].cf == im->gdes[ii].cf)){
620 /* OK the data it is here already ...
621 * we just copy the header portion */
622 im->gdes[i].start = im->gdes[ii].start;
623 im->gdes[i].end = im->gdes[ii].end;
624 im->gdes[i].step = im->gdes[ii].step;
625 im->gdes[i].ds_cnt = im->gdes[ii].ds_cnt;
626 im->gdes[i].ds_namv = im->gdes[ii].ds_namv;
627 im->gdes[i].data = im->gdes[ii].data;
628 im->gdes[i].data_first = 0;
635 unsigned long ft_step = im->gdes[i].step ;
637 if((rrd_fetch_fn(im->gdes[i].rrd,
643 &im->gdes[i].ds_namv,
644 &im->gdes[i].data)) == -1){
647 im->gdes[i].data_first = 1;
649 if (ft_step < im->gdes[i].step) {
650 reduce_data(im->gdes[i].cf,
658 im->gdes[i].step = ft_step;
662 /* lets see if the required data source is realy there */
663 for(ii=0;ii<im->gdes[i].ds_cnt;ii++){
664 if(strcmp(im->gdes[i].ds_namv[ii],im->gdes[i].ds_nam) == 0){
667 if (im->gdes[i].ds== -1){
668 rrd_set_error("No DS called '%s' in '%s'",
669 im->gdes[i].ds_nam,im->gdes[i].rrd);
677 /* evaluate the expressions in the CDEF functions */
679 /*************************************************************
681 *************************************************************/
684 find_var_wrapper(void *arg1, char *key)
686 return find_var((image_desc_t *) arg1, key);
689 /* find gdes containing var*/
691 find_var(image_desc_t *im, char *key){
693 for(ii=0;ii<im->gdes_c-1;ii++){
694 if((im->gdes[ii].gf == GF_DEF
695 || im->gdes[ii].gf == GF_VDEF
696 || im->gdes[ii].gf == GF_CDEF)
697 && (strcmp(im->gdes[ii].vname,key) == 0)){
704 /* find the largest common denominator for all the numbers
705 in the 0 terminated num array */
710 for (i=0;num[i+1]!=0;i++){
712 rest=num[i] % num[i+1];
713 num[i]=num[i+1]; num[i+1]=rest;
717 /* return i==0?num[i]:num[i-1]; */
721 /* run the rpn calculator on all the VDEF and CDEF arguments */
723 data_calc( image_desc_t *im){
727 long *steparray, rpi;
732 rpnstack_init(&rpnstack);
734 for (gdi=0;gdi<im->gdes_c;gdi++){
735 /* Look for GF_VDEF and GF_CDEF in the same loop,
736 * so CDEFs can use VDEFs and vice versa
738 switch (im->gdes[gdi].gf) {
740 /* A VDEF has no DS. This also signals other parts
741 * of rrdtool that this is a VDEF value, not a CDEF.
743 im->gdes[gdi].ds_cnt = 0;
744 if (vdef_calc(im,gdi)) {
745 rrd_set_error("Error processing VDEF '%s'"
748 rpnstack_free(&rpnstack);
753 im->gdes[gdi].ds_cnt = 1;
754 im->gdes[gdi].ds = 0;
755 im->gdes[gdi].data_first = 1;
756 im->gdes[gdi].start = 0;
757 im->gdes[gdi].end = 0;
762 /* Find the variables in the expression.
763 * - VDEF variables are substituted by their values
764 * and the opcode is changed into OP_NUMBER.
765 * - CDEF variables are analized for their step size,
766 * the lowest common denominator of all the step
767 * sizes of the data sources involved is calculated
768 * and the resulting number is the step size for the
769 * resulting data source.
771 for(rpi=0;im->gdes[gdi].rpnp[rpi].op != OP_END;rpi++){
772 if(im->gdes[gdi].rpnp[rpi].op == OP_VARIABLE){
773 long ptr = im->gdes[gdi].rpnp[rpi].ptr;
774 if (im->gdes[ptr].ds_cnt == 0) {
776 printf("DEBUG: inside CDEF '%s' processing VDEF '%s'\n",
778 im->gdes[ptr].vname);
779 printf("DEBUG: value from vdef is %f\n",im->gdes[ptr].vf.val);
781 im->gdes[gdi].rpnp[rpi].val = im->gdes[ptr].vf.val;
782 im->gdes[gdi].rpnp[rpi].op = OP_NUMBER;
784 if ((steparray = rrd_realloc(steparray, (++stepcnt+1)*sizeof(*steparray)))==NULL){
785 rrd_set_error("realloc steparray");
786 rpnstack_free(&rpnstack);
790 steparray[stepcnt-1] = im->gdes[ptr].step;
792 /* adjust start and end of cdef (gdi) so
793 * that it runs from the latest start point
794 * to the earliest endpoint of any of the
795 * rras involved (ptr)
797 if(im->gdes[gdi].start < im->gdes[ptr].start)
798 im->gdes[gdi].start = im->gdes[ptr].start;
800 if(im->gdes[gdi].end == 0 ||
801 im->gdes[gdi].end > im->gdes[ptr].end)
802 im->gdes[gdi].end = im->gdes[ptr].end;
804 /* store pointer to the first element of
805 * the rra providing data for variable,
806 * further save step size and data source
809 im->gdes[gdi].rpnp[rpi].data = im->gdes[ptr].data + im->gdes[ptr].ds;
810 im->gdes[gdi].rpnp[rpi].step = im->gdes[ptr].step;
811 im->gdes[gdi].rpnp[rpi].ds_cnt = im->gdes[ptr].ds_cnt;
813 /* backoff the *.data ptr; this is done so
814 * rpncalc() function doesn't have to treat
815 * the first case differently
817 } /* if ds_cnt != 0 */
818 } /* if OP_VARIABLE */
819 } /* loop through all rpi */
821 /* move the data pointers to the correct period */
822 for(rpi=0;im->gdes[gdi].rpnp[rpi].op != OP_END;rpi++){
823 if(im->gdes[gdi].rpnp[rpi].op == OP_VARIABLE){
824 long ptr = im->gdes[gdi].rpnp[rpi].ptr;
825 if(im->gdes[gdi].start > im->gdes[ptr].start) {
826 im->gdes[gdi].rpnp[rpi].data += im->gdes[gdi].rpnp[rpi].ds_cnt;
832 if(steparray == NULL){
833 rrd_set_error("rpn expressions without DEF"
834 " or CDEF variables are not supported");
835 rpnstack_free(&rpnstack);
838 steparray[stepcnt]=0;
839 /* Now find the resulting step. All steps in all
840 * used RRAs have to be visited
842 im->gdes[gdi].step = lcd(steparray);
844 if((im->gdes[gdi].data = malloc((
845 (im->gdes[gdi].end-im->gdes[gdi].start)
846 / im->gdes[gdi].step)
847 * sizeof(double)))==NULL){
848 rrd_set_error("malloc im->gdes[gdi].data");
849 rpnstack_free(&rpnstack);
853 /* Step through the new cdef results array and
854 * calculate the values
856 for (now = im->gdes[gdi].start + im->gdes[gdi].step;
857 now<=im->gdes[gdi].end;
858 now += im->gdes[gdi].step)
860 rpnp_t *rpnp = im -> gdes[gdi].rpnp;
862 /* 3rd arg of rpn_calc is for OP_VARIABLE lookups;
863 * in this case we are advancing by timesteps;
864 * we use the fact that time_t is a synonym for long
866 if (rpn_calc(rpnp,&rpnstack,(long) now,
867 im->gdes[gdi].data,++dataidx) == -1) {
868 /* rpn_calc sets the error string */
869 rpnstack_free(&rpnstack);
872 } /* enumerate over time steps within a CDEF */
877 } /* enumerate over CDEFs */
878 rpnstack_free(&rpnstack);
882 /* massage data so, that we get one value for each x coordinate in the graph */
884 data_proc( image_desc_t *im ){
886 double pixstep = (double)(im->end-im->start)
887 /(double)im->xsize; /* how much time
888 passes in one pixel */
890 double minval=DNAN,maxval=DNAN;
892 unsigned long gr_time;
894 /* memory for the processed data */
895 for(i=0;i<im->gdes_c;i++){
896 if((im->gdes[i].gf==GF_LINE) ||
897 (im->gdes[i].gf==GF_AREA) ||
898 (im->gdes[i].gf==GF_TICK) ||
899 (im->gdes[i].gf==GF_STACK)){
900 if((im->gdes[i].p_data = malloc((im->xsize +1)
901 * sizeof(rrd_value_t)))==NULL){
902 rrd_set_error("malloc data_proc");
908 for(i=0;i<im->xsize;i++){
910 gr_time = im->start+pixstep*i; /* time of the
914 for(ii=0;ii<im->gdes_c;ii++){
916 switch(im->gdes[ii].gf){
922 vidx = im->gdes[ii].vidx;
926 ((unsigned long)floor(
927 (double)(gr_time-im->gdes[vidx].start) / im->gdes[vidx].step
929 ) *im->gdes[vidx].ds_cnt
932 if (! isnan(value)) {
934 im->gdes[ii].p_data[i] = paintval;
935 /* GF_TICK: the data values are not relevant for min and max */
936 if (finite(paintval) && im->gdes[ii].gf != GF_TICK ){
937 if (isnan(minval) || paintval < minval)
939 if (isnan(maxval) || paintval > maxval)
943 im->gdes[ii].p_data[i] = DNAN;
960 /* if min or max have not been asigned a value this is because
961 there was no data in the graph ... this is not good ...
962 lets set these to dummy values then ... */
964 if (isnan(minval)) minval = 0.0;
965 if (isnan(maxval)) maxval = 1.0;
967 /* adjust min and max values */
968 if (isnan(im->minval)
969 || ((!im->logarithmic && !im->rigid) /* don't adjust low-end with log scale */
970 && im->minval > minval))
972 if (isnan(im->maxval)
974 && im->maxval < maxval)){
976 im->maxval = maxval * 1.1;
980 /* make sure min and max are not equal */
981 if (im->minval == im->maxval) {
983 if (! im->logarithmic) {
987 /* make sure min and max are not both zero */
988 if (im->maxval == 0.0) {
998 /* identify the point where the first gridline, label ... gets placed */
1002 time_t start, /* what is the initial time */
1003 enum tmt_en baseint, /* what is the basic interval */
1004 long basestep /* how many if these do we jump a time */
1008 tm = *localtime(&start);
1011 tm.tm_sec -= tm.tm_sec % basestep; break;
1014 tm.tm_min -= tm.tm_min % basestep;
1019 tm.tm_hour -= tm.tm_hour % basestep; break;
1021 /* we do NOT look at the basestep for this ... */
1024 tm.tm_hour = 0; break;
1026 /* we do NOT look at the basestep for this ... */
1030 tm.tm_mday -= tm.tm_wday -1; /* -1 because we want the monday */
1031 if (tm.tm_wday==0) tm.tm_mday -= 7; /* we want the *previous* monday */
1038 tm.tm_mon -= tm.tm_mon % basestep; break;
1046 tm.tm_year -= (tm.tm_year+1900) % basestep;
1051 /* identify the point where the next gridline, label ... gets placed */
1054 time_t current, /* what is the initial time */
1055 enum tmt_en baseint, /* what is the basic interval */
1056 long basestep /* how many if these do we jump a time */
1061 tm = *localtime(¤t);
1065 tm.tm_sec += basestep; break;
1067 tm.tm_min += basestep; break;
1069 tm.tm_hour += basestep; break;
1071 tm.tm_mday += basestep; break;
1073 tm.tm_mday += 7*basestep; break;
1075 tm.tm_mon += basestep; break;
1077 tm.tm_year += basestep;
1079 madetime = mktime(&tm);
1080 } while (madetime == -1); /* this is necessary to skip impssible times
1081 like the daylight saving time skips */
1087 /* calculate values required for PRINT and GPRINT functions */
1090 print_calc(image_desc_t *im, char ***prdata)
1092 long i,ii,validsteps;
1095 int graphelement = 0;
1098 double magfact = -1;
1102 if (im->imginfo) prlines++;
1103 for(i=0;i<im->gdes_c;i++){
1104 switch(im->gdes[i].gf){
1107 if(((*prdata) = rrd_realloc((*prdata),prlines*sizeof(char *)))==NULL){
1108 rrd_set_error("realloc prdata");
1112 /* PRINT and GPRINT can now print VDEF generated values.
1113 * There's no need to do any calculations on them as these
1114 * calculations were already made.
1116 vidx = im->gdes[i].vidx;
1117 if (im->gdes[vidx].gf==GF_VDEF) { /* simply use vals */
1118 printval = im->gdes[vidx].vf.val;
1119 printtime = im->gdes[vidx].vf.when;
1120 } else { /* need to calculate max,min,avg etcetera */
1121 max_ii =((im->gdes[vidx].end
1122 - im->gdes[vidx].start)
1123 / im->gdes[vidx].step
1124 * im->gdes[vidx].ds_cnt);
1127 for( ii=im->gdes[vidx].ds;
1129 ii+=im->gdes[vidx].ds_cnt){
1130 if (! finite(im->gdes[vidx].data[ii]))
1132 if (isnan(printval)){
1133 printval = im->gdes[vidx].data[ii];
1138 switch (im->gdes[i].cf){
1141 case CF_DEVSEASONAL:
1145 printval += im->gdes[vidx].data[ii];
1148 printval = min( printval, im->gdes[vidx].data[ii]);
1152 printval = max( printval, im->gdes[vidx].data[ii]);
1155 printval = im->gdes[vidx].data[ii];
1158 if (im->gdes[i].cf==CF_AVERAGE || im->gdes[i].cf > CF_LAST) {
1159 if (validsteps > 1) {
1160 printval = (printval / validsteps);
1163 } /* prepare printval */
1165 if (!strcmp(im->gdes[i].format,"%c")) { /* VDEF time print */
1166 if (im->gdes[i].gf == GF_PRINT){
1167 (*prdata)[prlines-2] = malloc((FMT_LEG_LEN+2)*sizeof(char));
1168 sprintf((*prdata)[prlines-2],"%s (%lu)",
1169 ctime(&printtime),printtime);
1170 (*prdata)[prlines-1] = NULL;
1172 sprintf(im->gdes[i].legend,"%s (%lu)",
1173 ctime(&printtime),printtime);
1177 if ((percent_s = strstr(im->gdes[i].format,"%S")) != NULL) {
1178 /* Magfact is set to -1 upon entry to print_calc. If it
1179 * is still less than 0, then we need to run auto_scale.
1180 * Otherwise, put the value into the correct units. If
1181 * the value is 0, then do not set the symbol or magnification
1182 * so next the calculation will be performed again. */
1183 if (magfact < 0.0) {
1184 auto_scale(im,&printval,&si_symb,&magfact);
1185 if (printval == 0.0)
1188 printval /= magfact;
1190 *(++percent_s) = 's';
1191 } else if (strstr(im->gdes[i].format,"%s") != NULL) {
1192 auto_scale(im,&printval,&si_symb,&magfact);
1195 if (im->gdes[i].gf == GF_PRINT){
1196 (*prdata)[prlines-2] = malloc((FMT_LEG_LEN+2)*sizeof(char));
1197 if (bad_format(im->gdes[i].format)) {
1198 rrd_set_error("bad format for [G]PRINT in '%s'", im->gdes[i].format);
1201 #ifdef HAVE_SNPRINTF
1202 snprintf((*prdata)[prlines-2],FMT_LEG_LEN,im->gdes[i].format,printval,si_symb);
1204 sprintf((*prdata)[prlines-2],im->gdes[i].format,printval,si_symb);
1206 (*prdata)[prlines-1] = NULL;
1210 if (bad_format(im->gdes[i].format)) {
1211 rrd_set_error("bad format for [G]PRINT in '%s'", im->gdes[i].format);
1214 #ifdef HAVE_SNPRINTF
1215 snprintf(im->gdes[i].legend,FMT_LEG_LEN-2,im->gdes[i].format,printval,si_symb);
1217 sprintf(im->gdes[i].legend,im->gdes[i].format,printval,si_symb);
1239 return graphelement;
1243 /* place legends with color spots */
1245 leg_place(image_desc_t *im)
1248 int interleg = im->text_prop[TEXT_PROP_LEGEND].size*2.0;
1249 int box =im->text_prop[TEXT_PROP_LEGEND].size*1.5;
1250 int border = im->text_prop[TEXT_PROP_LEGEND].size*2.0;
1251 int fill=0, fill_last;
1253 int leg_x = border, leg_y = im->ygif;
1257 char prt_fctn; /*special printfunctions */
1260 if( !(im->extra_flags & NOLEGEND) ) {
1261 if ((legspace = malloc(im->gdes_c*sizeof(int)))==NULL){
1262 rrd_set_error("malloc for legspace");
1266 for(i=0;i<im->gdes_c;i++){
1269 leg_cc = strlen(im->gdes[i].legend);
1271 /* is there a controle code ant the end of the legend string ? */
1272 if (leg_cc >= 2 && im->gdes[i].legend[leg_cc-2] == '\\') {
1273 prt_fctn = im->gdes[i].legend[leg_cc-1];
1275 im->gdes[i].legend[leg_cc] = '\0';
1279 /* remove exess space */
1280 while (prt_fctn=='g' &&
1282 im->gdes[i].legend[leg_cc-1]==' '){
1284 im->gdes[i].legend[leg_cc]='\0';
1287 legspace[i]=(prt_fctn=='g' ? 0 : interleg);
1290 /* no interleg space if string ends in \g */
1291 fill += legspace[i];
1293 if (im->gdes[i].gf != GF_GPRINT &&
1294 im->gdes[i].gf != GF_COMMENT) {
1297 fill += gfx_get_text_width(im->canvas, fill+border,
1298 im->text_prop[TEXT_PROP_LEGEND].font,
1299 im->text_prop[TEXT_PROP_LEGEND].size,
1301 im->gdes[i].legend);
1306 /* who said there was a special tag ... ?*/
1307 if (prt_fctn=='g') {
1310 if (prt_fctn == '\0') {
1311 if (i == im->gdes_c -1 ) prt_fctn ='l';
1313 /* is it time to place the legends ? */
1314 if (fill > im->xgif - 2*border){
1329 if (prt_fctn != '\0'){
1331 if (leg_c >= 2 && prt_fctn == 'j') {
1332 glue = (im->xgif - fill - 2* border) / (leg_c-1);
1336 if (prt_fctn =='c') leg_x = (im->xgif - fill) / 2.0;
1337 if (prt_fctn =='r') leg_x = im->xgif - fill - border;
1339 for(ii=mark;ii<=i;ii++){
1340 if(im->gdes[ii].legend[0]=='\0')
1342 im->gdes[ii].leg_x = leg_x;
1343 im->gdes[ii].leg_y = leg_y;
1345 gfx_get_text_width(im->canvas, leg_x,
1346 im->text_prop[TEXT_PROP_LEGEND].font,
1347 im->text_prop[TEXT_PROP_LEGEND].size,
1349 im->gdes[ii].legend)
1352 if (im->gdes[ii].gf != GF_GPRINT &&
1353 im->gdes[ii].gf != GF_COMMENT)
1356 leg_y = leg_y + im->text_prop[TEXT_PROP_LEGEND].size*1.2;
1357 if (prt_fctn == 's') leg_y -= im->text_prop[TEXT_PROP_LEGEND].size*1.2;
1369 /* create a grid on the graph. it determines what to do
1370 from the values of xsize, start and end */
1372 /* the xaxis labels are determined from the number of seconds per pixel
1373 in the requested graph */
1378 horizontal_grid(image_desc_t *im)
1386 char graph_label[100];
1388 int labfact,gridind;
1389 int decimals, fractionals;
1394 range = im->maxval - im->minval;
1395 scaledrange = range / im->magfact;
1397 /* does the scale of this graph make it impossible to put lines
1398 on it? If so, give up. */
1399 if (isnan(scaledrange)) {
1403 /* find grid spaceing */
1405 if(isnan(im->ygridstep)){
1406 if(im->extra_flags & ALTYGRID) {
1407 /* find the value with max number of digits. Get number of digits */
1408 decimals = ceil(log10(max(fabs(im->maxval), fabs(im->minval))));
1409 if(decimals <= 0) /* everything is small. make place for zero */
1412 fractionals = floor(log10(range));
1413 if(fractionals < 0) /* small amplitude. */
1414 sprintf(labfmt, "%%%d.%df", decimals - fractionals + 1, -fractionals + 1);
1416 sprintf(labfmt, "%%%d.1f", decimals + 1);
1417 gridstep = pow((double)10, (double)fractionals);
1418 if(gridstep == 0) /* range is one -> 0.1 is reasonable scale */
1420 /* should have at least 5 lines but no more then 15 */
1421 if(range/gridstep < 5)
1423 if(range/gridstep > 15)
1425 if(range/gridstep > 5) {
1427 if(range/gridstep > 8)
1436 for(i=0;ylab[i].grid > 0;i++){
1437 pixel = im->ysize / (scaledrange / ylab[i].grid);
1438 if (gridind == -1 && pixel > 5) {
1445 if (pixel * ylab[gridind].lfac[i] >= 2 * im->text_prop[TEXT_PROP_AXIS].size) {
1446 labfact = ylab[gridind].lfac[i];
1451 gridstep = ylab[gridind].grid * im->magfact;
1454 gridstep = im->ygridstep;
1455 labfact = im->ylabfact;
1459 x1=im->xorigin+im->xsize;
1461 sgrid = (int)( im->minval / gridstep - 1);
1462 egrid = (int)( im->maxval / gridstep + 1);
1463 scaledstep = gridstep/im->magfact;
1464 for (i = sgrid; i <= egrid; i++){
1465 y0=ytr(im,gridstep*i);
1466 if ( y0 >= im->yorigin-im->ysize
1467 && y0 <= im->yorigin){
1468 if(i % labfact == 0){
1469 if (i==0 || im->symbol == ' ') {
1471 if(im->extra_flags & ALTYGRID) {
1472 sprintf(graph_label,labfmt,scaledstep*i);
1475 sprintf(graph_label,"%4.1f",scaledstep*i);
1478 sprintf(graph_label,"%4.0f",scaledstep*i);
1482 sprintf(graph_label,"%4.1f %c",scaledstep*i, im->symbol);
1484 sprintf(graph_label,"%4.0f %c",scaledstep*i, im->symbol);
1488 gfx_new_text ( im->canvas,
1489 x0-im->text_prop[TEXT_PROP_AXIS].size/1.5, y0,
1490 im->graph_col[GRC_FONT],
1491 im->text_prop[TEXT_PROP_AXIS].font,
1492 im->text_prop[TEXT_PROP_AXIS].size,
1493 im->tabwidth, 0.0, GFX_H_RIGHT, GFX_V_CENTER,
1495 gfx_new_line ( im->canvas,
1498 MGRIDWIDTH, im->graph_col[GRC_MGRID] );
1501 gfx_new_line ( im->canvas,
1504 GRIDWIDTH, im->graph_col[GRC_GRID] );
1512 /* logaritmic horizontal grid */
1514 horizontal_log_grid(image_desc_t *im)
1518 int minoridx=0, majoridx=0;
1519 char graph_label[100];
1521 double value, pixperstep, minstep;
1523 /* find grid spaceing */
1524 pixpex= (double)im->ysize / (log10(im->maxval) - log10(im->minval));
1526 if (isnan(pixpex)) {
1530 for(i=0;yloglab[i][0] > 0;i++){
1531 minstep = log10(yloglab[i][0]);
1532 for(ii=1;yloglab[i][ii+1] > 0;ii++){
1533 if(yloglab[i][ii+2]==0){
1534 minstep = log10(yloglab[i][ii+1])-log10(yloglab[i][ii]);
1538 pixperstep = pixpex * minstep;
1539 if(pixperstep > 5){minoridx = i;}
1540 if(pixperstep > 2 * im->text_prop[TEXT_PROP_LEGEND].size){majoridx = i;}
1544 x1=im->xorigin+im->xsize;
1545 /* paint minor grid */
1546 for (value = pow((double)10, log10(im->minval)
1547 - fmod(log10(im->minval),log10(yloglab[minoridx][0])));
1548 value <= im->maxval;
1549 value *= yloglab[minoridx][0]){
1550 if (value < im->minval) continue;
1552 while(yloglab[minoridx][++i] > 0){
1553 y0 = ytr(im,value * yloglab[minoridx][i]);
1554 if (y0 <= im->yorigin - im->ysize) break;
1555 gfx_new_line ( im->canvas,
1558 GRIDWIDTH, im->graph_col[GRC_GRID] );
1562 /* paint major grid and labels*/
1563 for (value = pow((double)10, log10(im->minval)
1564 - fmod(log10(im->minval),log10(yloglab[majoridx][0])));
1565 value <= im->maxval;
1566 value *= yloglab[majoridx][0]){
1567 if (value < im->minval) continue;
1569 while(yloglab[majoridx][++i] > 0){
1570 y0 = ytr(im,value * yloglab[majoridx][i]);
1571 if (y0 <= im->yorigin - im->ysize) break;
1572 gfx_new_line ( im->canvas,
1575 MGRIDWIDTH, im->graph_col[GRC_MGRID] );
1577 sprintf(graph_label,"%3.0e",value * yloglab[majoridx][i]);
1578 gfx_new_text ( im->canvas,
1579 x0-im->text_prop[TEXT_PROP_AXIS].size/1.5, y0,
1580 im->graph_col[GRC_FONT],
1581 im->text_prop[TEXT_PROP_AXIS].font,
1582 im->text_prop[TEXT_PROP_AXIS].size,
1583 im->tabwidth,0.0, GFX_H_RIGHT, GFX_V_CENTER,
1595 int xlab_sel; /* which sort of label and grid ? */
1598 char graph_label[100];
1599 double x0,y0,y1; /* points for filled graph and more*/
1602 /* the type of time grid is determined by finding
1603 the number of seconds per pixel in the graph */
1606 if(im->xlab_user.minsec == -1){
1607 factor=(im->end - im->start)/im->xsize;
1609 while ( xlab[xlab_sel+1].minsec != -1
1610 && xlab[xlab_sel+1].minsec <= factor){ xlab_sel++; }
1611 im->xlab_user.gridtm = xlab[xlab_sel].gridtm;
1612 im->xlab_user.gridst = xlab[xlab_sel].gridst;
1613 im->xlab_user.mgridtm = xlab[xlab_sel].mgridtm;
1614 im->xlab_user.mgridst = xlab[xlab_sel].mgridst;
1615 im->xlab_user.labtm = xlab[xlab_sel].labtm;
1616 im->xlab_user.labst = xlab[xlab_sel].labst;
1617 im->xlab_user.precis = xlab[xlab_sel].precis;
1618 im->xlab_user.stst = xlab[xlab_sel].stst;
1621 /* y coords are the same for every line ... */
1623 y1 = im->yorigin-im->ysize;
1626 /* paint the minor grid */
1627 for(ti = find_first_time(im->start,
1628 im->xlab_user.gridtm,
1629 im->xlab_user.gridst);
1631 ti = find_next_time(ti,im->xlab_user.gridtm,im->xlab_user.gridst)
1633 /* are we inside the graph ? */
1634 if (ti < im->start || ti > im->end) continue;
1636 gfx_new_line(im->canvas,x0,y0+1, x0,y1-1,GRIDWIDTH, im->graph_col[GRC_GRID]);
1640 /* paint the major grid */
1641 for(ti = find_first_time(im->start,
1642 im->xlab_user.mgridtm,
1643 im->xlab_user.mgridst);
1645 ti = find_next_time(ti,im->xlab_user.mgridtm,im->xlab_user.mgridst)
1647 /* are we inside the graph ? */
1648 if (ti < im->start || ti > im->end) continue;
1650 gfx_new_line(im->canvas,x0,y0+2, x0,y1-2,MGRIDWIDTH, im->graph_col[GRC_MGRID]);
1653 /* paint the labels below the graph */
1654 for(ti = find_first_time(im->start,
1655 im->xlab_user.labtm,
1656 im->xlab_user.labst);
1658 ti = find_next_time(ti,im->xlab_user.labtm,im->xlab_user.labst)
1660 tilab= ti + im->xlab_user.precis/2; /* correct time for the label */
1661 /* are we inside the graph ? */
1662 if (ti < im->start || ti > im->end) continue;
1665 strftime(graph_label,99,im->xlab_user.stst,localtime(&tilab));
1667 # error "your libc has no strftime I guess we'll abort the exercise here."
1669 gfx_new_text ( im->canvas,
1670 xtr(im,tilab), y0+im->text_prop[TEXT_PROP_AXIS].size/1.5,
1671 im->graph_col[GRC_FONT],
1672 im->text_prop[TEXT_PROP_AXIS].font,
1673 im->text_prop[TEXT_PROP_AXIS].size,
1674 im->tabwidth, 0.0, GFX_H_CENTER, GFX_V_TOP,
1687 /* draw x and y axis */
1688 gfx_new_line ( im->canvas, im->xorigin+im->xsize,im->yorigin,
1689 im->xorigin+im->xsize,im->yorigin-im->ysize,
1690 GRIDWIDTH, im->graph_col[GRC_GRID]);
1692 gfx_new_line ( im->canvas, im->xorigin,im->yorigin-im->ysize,
1693 im->xorigin+im->xsize,im->yorigin-im->ysize,
1694 GRIDWIDTH, im->graph_col[GRC_GRID]);
1696 gfx_new_line ( im->canvas, im->xorigin-4,im->yorigin,
1697 im->xorigin+im->xsize+4,im->yorigin,
1698 MGRIDWIDTH, im->graph_col[GRC_GRID]);
1700 gfx_new_line ( im->canvas, im->xorigin,im->yorigin+4,
1701 im->xorigin,im->yorigin-im->ysize-4,
1702 MGRIDWIDTH, im->graph_col[GRC_GRID]);
1705 /* arrow for X axis direction */
1706 gfx_new_area ( im->canvas,
1707 im->xorigin+im->xsize+3, im->yorigin-3,
1708 im->xorigin+im->xsize+3, im->yorigin+4,
1709 im->xorigin+im->xsize+8, im->yorigin+0.5, // LINEOFFSET
1710 im->graph_col[GRC_ARROW]);
1717 grid_paint(image_desc_t *im)
1721 double x0,y0; /* points for filled graph and more*/
1724 /* draw 3d border */
1725 node = gfx_new_area (im->canvas, 0,im->ygif,
1727 2,2,im->graph_col[GRC_SHADEA]);
1728 gfx_add_point( node , im->xgif - 2, 2 );
1729 gfx_add_point( node , im->xgif, 0 );
1730 gfx_add_point( node , 0,0 );
1731 /* gfx_add_point( node , 0,im->ygif ); */
1733 node = gfx_new_area (im->canvas, 2,im->ygif-2,
1734 im->xgif-2,im->ygif-2,
1736 im->graph_col[GRC_SHADEB]);
1737 gfx_add_point( node , im->xgif,0);
1738 gfx_add_point( node , im->xgif,im->ygif);
1739 gfx_add_point( node , 0,im->ygif);
1740 /* gfx_add_point( node , 0,im->ygif ); */
1743 if (im->draw_x_grid == 1 )
1746 if (im->draw_y_grid == 1){
1747 if(im->logarithmic){
1748 res = horizontal_log_grid(im);
1750 res = horizontal_grid(im);
1753 /* dont draw horizontal grid if there is no min and max val */
1755 char *nodata = "No Data found";
1756 gfx_new_text(im->canvas,im->xgif/2, (2*im->yorigin-im->ysize) / 2,
1757 im->graph_col[GRC_FONT],
1758 im->text_prop[TEXT_PROP_AXIS].font,
1759 im->text_prop[TEXT_PROP_AXIS].size,
1760 im->tabwidth, 0.0, GFX_H_CENTER, GFX_V_CENTER,
1765 /* yaxis description */
1766 if (im->canvas->imgformat != IF_PNG) {
1767 gfx_new_text( im->canvas,
1768 7, (im->yorigin - im->ysize/2),
1769 im->graph_col[GRC_FONT],
1770 im->text_prop[TEXT_PROP_AXIS].font,
1771 im->text_prop[TEXT_PROP_AXIS].size, im->tabwidth, 270.0,
1772 GFX_H_CENTER, GFX_V_CENTER,
1775 /* horrible hack until we can actually print vertically */
1778 int l=strlen(im->ylegend);
1780 for (n=0;n<strlen(im->ylegend);n++) {
1781 s[0]=im->ylegend[n];
1783 gfx_new_text(im->canvas,7,im->text_prop[TEXT_PROP_AXIS].size*(l-n),
1784 im->graph_col[GRC_FONT],
1785 im->text_prop[TEXT_PROP_AXIS].font,
1786 im->text_prop[TEXT_PROP_AXIS].size, im->tabwidth, 270.0,
1787 GFX_H_CENTER, GFX_V_CENTER,
1794 gfx_new_text( im->canvas,
1795 im->xgif/2, im->text_prop[TEXT_PROP_TITLE].size,
1796 im->graph_col[GRC_FONT],
1797 im->text_prop[TEXT_PROP_TITLE].font,
1798 im->text_prop[TEXT_PROP_TITLE].size, im->tabwidth, 0.0,
1799 GFX_H_CENTER, GFX_V_CENTER,
1803 if( !(im->extra_flags & NOLEGEND) ) {
1804 for(i=0;i<im->gdes_c;i++){
1805 if(im->gdes[i].legend[0] =='\0')
1808 /* im->gdes[i].leg_y is the bottom of the legend */
1809 x0 = im->gdes[i].leg_x;
1810 y0 = im->gdes[i].leg_y;
1812 if ( im->gdes[i].gf != GF_GPRINT
1813 && im->gdes[i].gf != GF_COMMENT) {
1816 boxH = gfx_get_text_width(im->canvas, 0,
1817 im->text_prop[TEXT_PROP_AXIS].font,
1818 im->text_prop[TEXT_PROP_AXIS].size,
1819 im->tabwidth,"M") * 1.25;
1822 node = gfx_new_area(im->canvas,
1827 gfx_add_point ( node, x0+boxH, y0-boxV );
1828 node = gfx_new_line(im->canvas,
1831 gfx_add_point(node,x0+boxH,y0);
1832 gfx_add_point(node,x0+boxH,y0-boxV);
1833 gfx_close_path(node);
1834 x0 += boxH / 1.25 * 2;
1836 gfx_new_text ( im->canvas, x0, y0,
1837 im->graph_col[GRC_FONT],
1838 im->text_prop[TEXT_PROP_AXIS].font,
1839 im->text_prop[TEXT_PROP_AXIS].size,
1840 im->tabwidth,0.0, GFX_H_LEFT, GFX_V_BOTTOM,
1841 im->gdes[i].legend );
1847 /*****************************************************
1848 * lazy check make sure we rely need to create this graph
1849 *****************************************************/
1851 int lazy_check(image_desc_t *im){
1854 struct stat gifstat;
1856 if (im->lazy == 0) return 0; /* no lazy option */
1857 if (stat(im->graphfile,&gifstat) != 0)
1858 return 0; /* can't stat */
1859 /* one pixel in the existing graph is more then what we would
1861 if (time(NULL) - gifstat.st_mtime >
1862 (im->end - im->start) / im->xsize)
1864 if ((fd = fopen(im->graphfile,"rb")) == NULL)
1865 return 0; /* the file does not exist */
1866 switch (im->canvas->imgformat) {
1868 size = PngSize(fd,&(im->xgif),&(im->ygif));
1878 pie_part(image_desc_t *im, gfx_color_t color,
1879 double PieCenterX, double PieCenterY, double Radius,
1880 double startangle, double endangle)
1884 double step=M_PI/50; /* Number of iterations for the circle;
1885 ** 10 is definitely too low, more than
1886 ** 50 seems to be overkill
1889 /* Strange but true: we have to work clockwise or else
1890 ** anti aliasing nor transparency don't work.
1892 ** This test is here to make sure we do it right, also
1893 ** this makes the for...next loop more easy to implement.
1894 ** The return will occur if the user enters a negative number
1895 ** (which shouldn't be done according to the specs) or if the
1896 ** programmers do something wrong (which, as we all know, never
1897 ** happens anyway :)
1899 if (endangle<startangle) return;
1901 /* Hidden feature: Radius decreases each full circle */
1903 while (angle>=2*M_PI) {
1908 node=gfx_new_area(im->canvas,
1909 PieCenterX+sin(startangle)*Radius,
1910 PieCenterY-cos(startangle)*Radius,
1913 PieCenterX+sin(endangle)*Radius,
1914 PieCenterY-cos(endangle)*Radius,
1916 for (angle=endangle;angle-startangle>=step;angle-=step) {
1918 PieCenterX+sin(angle)*Radius,
1919 PieCenterY-cos(angle)*Radius );
1924 graph_size_location(image_desc_t *im, int elements, int piechart )
1926 /* The actual size of the image to draw is determined from
1927 ** several sources. The size given on the command line is
1928 ** the graph area but we need more as we have to draw labels
1929 ** and other things outside the graph area
1932 /* +-+-------------------------------------------+
1933 ** |l|.................title.....................|
1934 ** |e+--+-------------------------------+--------+
1937 ** |l| l| main graph area | chart |
1940 ** |r+--+-------------------------------+--------+
1941 ** |e| | x-axis labels | |
1942 ** |v+--+-------------------------------+--------+
1943 ** | |..............legends......................|
1944 ** +-+-------------------------------------------+
1946 int Xvertical=0, Yvertical=0,
1947 Xtitle =0, Ytitle =0,
1948 Xylabel =0, Yylabel =0,
1951 Xxlabel =0, Yxlabel =0,
1952 Xlegend =0, Ylegend =0,
1953 Xspacing =10, Yspacing =10;
1955 if (im->ylegend[0] != '\0') {
1956 Xvertical = im->text_prop[TEXT_PROP_LEGEND].size *2;
1957 Yvertical = im->text_prop[TEXT_PROP_LEGEND].size * (strlen(im->ylegend)+1);
1960 if (im->title[0] != '\0') {
1961 /* The title is placed "inbetween" two text lines so it
1962 ** automatically has some vertical spacing. The horizontal
1963 ** spacing is added here, on each side.
1965 Xtitle = gfx_get_text_width(im->canvas, 0,
1966 im->text_prop[TEXT_PROP_TITLE].font,
1967 im->text_prop[TEXT_PROP_TITLE].size,
1969 im->title) + 2*Xspacing;
1970 Ytitle = im->text_prop[TEXT_PROP_TITLE].size*2;
1976 if (im->draw_x_grid) {
1978 Yxlabel=im->text_prop[TEXT_PROP_LEGEND].size *2;
1980 if (im->draw_y_grid) {
1981 Xylabel=im->text_prop[TEXT_PROP_LEGEND].size *6;
1987 im->piesize=im->xsize<im->ysize?im->xsize:im->ysize;
1992 /* Now calculate the total size. Insert some spacing where
1993 desired. im->xorigin and im->yorigin need to correspond
1994 with the lower left corner of the main graph area or, if
1995 this one is not set, the imaginary box surrounding the
1998 /* The legend width cannot yet be determined, as a result we
1999 ** have problems adjusting the image to it. For now, we just
2000 ** forget about it at all; the legend will have to fit in the
2001 ** size already allocated.
2003 im->xgif = Xylabel + Xmain + Xpie + Xspacing;
2004 if (Xmain) im->xgif += Xspacing;
2005 if (Xpie) im->xgif += Xspacing;
2006 im->xorigin = Xspacing + Xylabel;
2007 if (Xtitle > im->xgif) im->xgif = Xtitle;
2009 im->xgif += Xvertical;
2010 im->xorigin += Xvertical;
2014 /* The vertical size is interesting... we need to compare
2015 ** the sum of {Ytitle, Ymain, Yxlabel, Ylegend} with Yvertical
2016 ** however we need to know {Ytitle+Ymain+Yxlabel} in order to
2017 ** start even thinking about Ylegend.
2019 ** Do it in three portions: First calculate the inner part,
2020 ** then do the legend, then adjust the total height of the img.
2023 /* reserve space for main and/or pie */
2024 im->ygif = Ymain + Yxlabel;
2025 if (im->ygif < Ypie) im->ygif = Ypie;
2026 im->yorigin = im->ygif - Yxlabel;
2027 /* reserve space for the title *or* some padding above the graph */
2030 im->yorigin += Ytitle;
2032 im->ygif += Yspacing;
2033 im->yorigin += Yspacing;
2035 /* reserve space for padding below the graph */
2036 im->ygif += Yspacing;
2039 /* Determine where to place the legends onto the image.
2040 ** Adjust im->ygif to match the space requirements.
2042 if(leg_place(im)==-1)
2045 /* last of three steps: check total height of image */
2046 if (im->ygif < Yvertical) im->ygif = Yvertical;
2049 if (Xlegend > im->xgif) {
2051 /* reposition Pie */
2054 /* The pie is placed in the upper right hand corner,
2055 ** just below the title (if any) and with sufficient
2059 im->pie_x = im->xgif - Xspacing - Xpie/2;
2060 im->pie_y = im->yorigin-Ymain+Ypie/2;
2062 im->pie_x = im->xgif/2;
2063 im->pie_y = im->yorigin-Ypie/2;
2069 /* draw that picture thing ... */
2071 graph_paint(image_desc_t *im, char ***calcpr)
2074 int lazy = lazy_check(im);
2076 double PieStart=0.0;
2080 double areazero = 0.0;
2081 enum gf_en stack_gf = GF_PRINT;
2082 graph_desc_t *lastgdes = NULL;
2084 /* if we are lazy and there is nothing to PRINT ... quit now */
2085 if (lazy && im->prt_c==0) return 0;
2087 /* pull the data from the rrd files ... */
2089 if(data_fetch(im)==-1)
2092 /* evaluate VDEF and CDEF operations ... */
2093 if(data_calc(im)==-1)
2096 /* check if we need to draw a piechart */
2097 for(i=0;i<im->gdes_c;i++){
2098 if (im->gdes[i].gf == GF_PART) {
2104 /* calculate and PRINT and GPRINT definitions. We have to do it at
2105 * this point because it will affect the length of the legends
2106 * if there are no graph elements we stop here ...
2107 * if we are lazy, try to quit ...
2109 i=print_calc(im,calcpr);
2111 if(((i==0)&&(piechart==0)) || lazy) return 0;
2113 /* If there's only the pie chart to draw, signal this */
2114 if (i==0) piechart=2;
2116 /* get actual drawing data and find min and max values*/
2117 if(data_proc(im)==-1)
2120 if(!im->logarithmic){si_unit(im);} /* identify si magnitude Kilo, Mega Giga ? */
2122 if(!im->rigid && ! im->logarithmic)
2123 expand_range(im); /* make sure the upper and lower limit are
2126 /**************************************************************
2127 *** Calculating sizes and locations became a bit confusing ***
2128 *** so I moved this into a separate function. ***
2129 **************************************************************/
2130 if(graph_size_location(im,i,piechart)==-1)
2133 /* the actual graph is created by going through the individual
2134 graph elements and then drawing them */
2136 node=gfx_new_area ( im->canvas,
2140 im->graph_col[GRC_BACK]);
2142 gfx_add_point(node,0, im->ygif);
2144 if (piechart != 2) {
2145 node=gfx_new_area ( im->canvas,
2146 im->xorigin, im->yorigin,
2147 im->xorigin + im->xsize, im->yorigin,
2148 im->xorigin + im->xsize, im->yorigin-im->ysize,
2149 im->graph_col[GRC_CANVAS]);
2151 gfx_add_point(node,im->xorigin, im->yorigin - im->ysize);
2153 if (im->minval > 0.0)
2154 areazero = im->minval;
2155 if (im->maxval < 0.0)
2156 areazero = im->maxval;
2162 pie_part(im,im->graph_col[GRC_CANVAS],im->pie_x,im->pie_y,im->piesize*0.5,0,2*M_PI);
2165 for(i=0;i<im->gdes_c;i++){
2166 switch(im->gdes[i].gf){
2177 for (ii = 0; ii < im->xsize; ii++)
2179 if (!isnan(im->gdes[i].p_data[ii]) &&
2180 im->gdes[i].p_data[ii] > 0.0)
2182 /* generate a tick */
2183 gfx_new_line(im->canvas, im -> xorigin + ii,
2184 im -> yorigin - (im -> gdes[i].yrule * im -> ysize),
2188 im -> gdes[i].col );
2194 stack_gf = im->gdes[i].gf;
2196 /* fix data points at oo and -oo */
2197 for(ii=0;ii<im->xsize;ii++){
2198 if (isinf(im->gdes[i].p_data[ii])){
2199 if (im->gdes[i].p_data[ii] > 0) {
2200 im->gdes[i].p_data[ii] = im->maxval ;
2202 im->gdes[i].p_data[ii] = im->minval ;
2208 if (im->gdes[i].col != 0x0){
2209 /* GF_LINE and friend */
2210 if(stack_gf == GF_LINE ){
2212 for(ii=1;ii<im->xsize;ii++){
2213 if ( ! isnan(im->gdes[i].p_data[ii-1])
2214 && ! isnan(im->gdes[i].p_data[ii])){
2216 node = gfx_new_line(im->canvas,
2217 ii-1+im->xorigin,ytr(im,im->gdes[i].p_data[ii-1]),
2218 ii+im->xorigin,ytr(im,im->gdes[i].p_data[ii]),
2219 im->gdes[i].linewidth,
2222 gfx_add_point(node,ii+im->xorigin,ytr(im,im->gdes[i].p_data[ii]));
2231 for(ii=1;ii<im->xsize;ii++){
2233 if ( ! isnan(im->gdes[i].p_data[ii-1])
2234 && ! isnan(im->gdes[i].p_data[ii])){
2237 if (im->gdes[i].gf == GF_STACK) {
2238 ybase = ytr(im,lastgdes->p_data[ii-1]);
2240 ybase = ytr(im,areazero);
2243 node = gfx_new_area(im->canvas,
2244 ii-1+im->xorigin,ybase,
2245 ii-1+im->xorigin,ytr(im,im->gdes[i].p_data[ii-1]),
2246 ii+im->xorigin,ytr(im,im->gdes[i].p_data[ii]),
2250 gfx_add_point(node,ii+im->xorigin,ytr(im,im->gdes[i].p_data[ii]));
2254 if ( node != NULL && (ii+1==im->xsize || isnan(im->gdes[i].p_data[ii]) )){
2255 /* GF_AREA STACK type*/
2256 if (im->gdes[i].gf == GF_STACK ) {
2258 for (iii=ii-1;iii>area_start;iii--){
2259 gfx_add_point(node,iii+im->xorigin,ytr(im,lastgdes->p_data[iii]));
2262 gfx_add_point(node,ii+im->xorigin,ytr(im,areazero));
2267 } /* else GF_LINE */
2268 } /* if color != 0x0 */
2269 /* make sure we do not run into trouble when stacking on NaN */
2270 for(ii=0;ii<im->xsize;ii++){
2271 if (isnan(im->gdes[i].p_data[ii])) {
2274 ybase = ytr(im,lastgdes->p_data[ii-1]);
2276 if (isnan(ybase) || !lastgdes ){
2277 ybase = ytr(im,areazero);
2279 im->gdes[i].p_data[ii] = ybase;
2282 lastgdes = &(im->gdes[i]);
2285 if(isnan(im->gdes[i].yrule)) /* fetch variable */
2286 im->gdes[i].yrule = im->gdes[im->gdes[i].vidx].vf.val;
2288 if (finite(im->gdes[i].yrule)) { /* even the fetched var can be NaN */
2289 pie_part(im,im->gdes[i].col,
2290 im->pie_x,im->pie_y,im->piesize*0.4,
2291 M_PI*2.0*PieStart/100.0,
2292 M_PI*2.0*(PieStart+im->gdes[i].yrule)/100.0);
2293 PieStart += im->gdes[i].yrule;
2302 /* grid_paint also does the text */
2305 /* the RULES are the last thing to paint ... */
2306 for(i=0;i<im->gdes_c;i++){
2308 switch(im->gdes[i].gf){
2310 if(isnan(im->gdes[i].yrule)) { /* fetch variable */
2311 im->gdes[i].yrule = im->gdes[im->gdes[i].vidx].vf.val;
2313 if(im->gdes[i].yrule >= im->minval
2314 && im->gdes[i].yrule <= im->maxval)
2315 gfx_new_line(im->canvas,
2316 im->xorigin,ytr(im,im->gdes[i].yrule),
2317 im->xorigin+im->xsize,ytr(im,im->gdes[i].yrule),
2318 1.0,im->gdes[i].col);
2321 if(im->gdes[i].xrule == 0) { /* fetch variable */
2322 im->gdes[i].xrule = im->gdes[im->gdes[i].vidx].vf.when;
2324 if(im->gdes[i].xrule >= im->start
2325 && im->gdes[i].xrule <= im->end)
2326 gfx_new_line(im->canvas,
2327 xtr(im,im->gdes[i].xrule),im->yorigin,
2328 xtr(im,im->gdes[i].xrule),im->yorigin-im->ysize,
2329 1.0,im->gdes[i].col);
2337 if (strcmp(im->graphfile,"-")==0) {
2339 /* Change translation mode for stdout to BINARY */
2340 _setmode( _fileno( stdout ), O_BINARY );
2344 if ((fo = fopen(im->graphfile,"wb")) == NULL) {
2345 rrd_set_error("Opening '%s' for write: %s",im->graphfile,
2350 gfx_render (im->canvas,im->xgif,im->ygif,0x0,fo);
2351 if (strcmp(im->graphfile,"-") != 0)
2357 /*****************************************************
2359 *****************************************************/
2362 gdes_alloc(image_desc_t *im){
2364 long def_step = (im->end-im->start)/im->xsize;
2366 if (im->step > def_step) /* step can be increassed ... no decreassed */
2367 def_step = im->step;
2371 if ((im->gdes = (graph_desc_t *) rrd_realloc(im->gdes, (im->gdes_c)
2372 * sizeof(graph_desc_t)))==NULL){
2373 rrd_set_error("realloc graph_descs");
2378 im->gdes[im->gdes_c-1].step=def_step;
2379 im->gdes[im->gdes_c-1].start=im->start;
2380 im->gdes[im->gdes_c-1].end=im->end;
2381 im->gdes[im->gdes_c-1].vname[0]='\0';
2382 im->gdes[im->gdes_c-1].data=NULL;
2383 im->gdes[im->gdes_c-1].ds_namv=NULL;
2384 im->gdes[im->gdes_c-1].data_first=0;
2385 im->gdes[im->gdes_c-1].p_data=NULL;
2386 im->gdes[im->gdes_c-1].rpnp=NULL;
2387 im->gdes[im->gdes_c-1].col = 0x0;
2388 im->gdes[im->gdes_c-1].legend[0]='\0';
2389 im->gdes[im->gdes_c-1].rrd[0]='\0';
2390 im->gdes[im->gdes_c-1].ds=-1;
2391 im->gdes[im->gdes_c-1].p_data=NULL;
2395 /* copies input untill the first unescaped colon is found
2396 or until input ends. backslashes have to be escaped as well */
2398 scan_for_col(char *input, int len, char *output)
2403 input[inp] != ':' &&
2406 if (input[inp] == '\\' &&
2407 input[inp+1] != '\0' &&
2408 (input[inp+1] == '\\' ||
2409 input[inp+1] == ':')){
2410 output[outp++] = input[++inp];
2413 output[outp++] = input[inp];
2416 output[outp] = '\0';
2420 /* Some surgery done on this function, it became ridiculously big.
2422 ** - initializing now in rrd_graph_init()
2423 ** - options parsing now in rrd_graph_options()
2424 ** - script parsing now in rrd_graph_script()
2427 rrd_graph(int argc, char **argv, char ***prdata, int *xsize, int *ysize)
2431 rrd_graph_init(&im);
2433 rrd_graph_options(argc,argv,&im);
2434 if (rrd_test_error()) return -1;
2436 if (strlen(argv[optind])>=MAXPATH) {
2437 rrd_set_error("filename (including path) too long");
2440 strncpy(im.graphfile,argv[optind],MAXPATH-1);
2441 im.graphfile[MAXPATH-1]='\0';
2443 rrd_graph_script(argc,argv,&im);
2444 if (rrd_test_error()) return -1;
2446 /* Everything is now read and the actual work can start */
2449 if (graph_paint(&im,prdata)==-1){
2454 /* The image is generated and needs to be output.
2455 ** Also, if needed, print a line with information about the image.
2463 /* maybe prdata is not allocated yet ... lets do it now */
2464 if ((*prdata = calloc(2,sizeof(char *)))==NULL) {
2465 rrd_set_error("malloc imginfo");
2469 if(((*prdata)[0] = malloc((strlen(im.imginfo)+200+strlen(im.graphfile))*sizeof(char)))
2471 rrd_set_error("malloc imginfo");
2474 filename=im.graphfile+strlen(im.graphfile);
2475 while(filename > im.graphfile) {
2476 if (*(filename-1)=='/' || *(filename-1)=='\\' ) break;
2480 sprintf((*prdata)[0],im.imginfo,filename,(long)(im.canvas->zoom*im.xgif),(long)(im.canvas->zoom*im.ygif));
2487 rrd_graph_init(image_desc_t *im)
2491 im->xlab_user.minsec = -1;
2497 im->ylegend[0] = '\0';
2498 im->title[0] = '\0';
2501 im->unitsexponent= 9999;
2506 im->logarithmic = 0;
2507 im->ygridstep = DNAN;
2508 im->draw_x_grid = 1;
2509 im->draw_y_grid = 1;
2514 im->canvas = gfx_new_canvas();
2516 for(i=0;i<DIM(graph_col);i++)
2517 im->graph_col[i]=graph_col[i];
2519 for(i=0;i<DIM(text_prop);i++){
2520 im->text_prop[i].size = text_prop[i].size;
2521 im->text_prop[i].font = text_prop[i].font;
2526 rrd_graph_options(int argc, char *argv[],image_desc_t *im)
2529 char *parsetime_error = NULL;
2530 char scan_gtm[12],scan_mtm[12],scan_ltm[12],col_nam[12];
2531 time_t start_tmp=0,end_tmp=0;
2533 struct time_value start_tv, end_tv;
2536 parsetime("end-24h", &start_tv);
2537 parsetime("now", &end_tv);
2540 static struct option long_options[] =
2542 {"start", required_argument, 0, 's'},
2543 {"end", required_argument, 0, 'e'},
2544 {"x-grid", required_argument, 0, 'x'},
2545 {"y-grid", required_argument, 0, 'y'},
2546 {"vertical-label",required_argument,0,'v'},
2547 {"width", required_argument, 0, 'w'},
2548 {"height", required_argument, 0, 'h'},
2549 {"interlaced", no_argument, 0, 'i'},
2550 {"upper-limit",required_argument, 0, 'u'},
2551 {"lower-limit",required_argument, 0, 'l'},
2552 {"rigid", no_argument, 0, 'r'},
2553 {"base", required_argument, 0, 'b'},
2554 {"logarithmic",no_argument, 0, 'o'},
2555 {"color", required_argument, 0, 'c'},
2556 {"font", required_argument, 0, 'n'},
2557 {"title", required_argument, 0, 't'},
2558 {"imginfo", required_argument, 0, 'f'},
2559 {"imgformat", required_argument, 0, 'a'},
2560 {"lazy", no_argument, 0, 'z'},
2561 {"zoom", required_argument, 0, 'm'},
2562 {"no-legend", no_argument, 0, 'g'},
2563 {"alt-y-grid", no_argument, 0, 257 },
2564 {"alt-autoscale", no_argument, 0, 258 },
2565 {"alt-autoscale-max", no_argument, 0, 259 },
2566 {"units-exponent",required_argument, 0, 260},
2567 {"step", required_argument, 0, 261},
2569 int option_index = 0;
2573 opt = getopt_long(argc, argv,
2574 "s:e:x:y:v:w:h:iu:l:rb:oc:n:m:t:f:a:z:g",
2575 long_options, &option_index);
2582 im->extra_flags |= ALTYGRID;
2585 im->extra_flags |= ALTAUTOSCALE;
2588 im->extra_flags |= ALTAUTOSCALE_MAX;
2591 im->extra_flags |= NOLEGEND;
2594 im->unitsexponent = atoi(optarg);
2597 im->step = atoi(optarg);
2600 if ((parsetime_error = parsetime(optarg, &start_tv))) {
2601 rrd_set_error( "start time: %s", parsetime_error );
2606 if ((parsetime_error = parsetime(optarg, &end_tv))) {
2607 rrd_set_error( "end time: %s", parsetime_error );
2612 if(strcmp(optarg,"none") == 0){
2618 "%10[A-Z]:%ld:%10[A-Z]:%ld:%10[A-Z]:%ld:%ld:%n",
2620 &im->xlab_user.gridst,
2622 &im->xlab_user.mgridst,
2624 &im->xlab_user.labst,
2625 &im->xlab_user.precis,
2626 &stroff) == 7 && stroff != 0){
2627 strncpy(im->xlab_form, optarg+stroff, sizeof(im->xlab_form) - 1);
2628 if((im->xlab_user.gridtm = tmt_conv(scan_gtm)) == -1){
2629 rrd_set_error("unknown keyword %s",scan_gtm);
2631 } else if ((im->xlab_user.mgridtm = tmt_conv(scan_mtm)) == -1){
2632 rrd_set_error("unknown keyword %s",scan_mtm);
2634 } else if ((im->xlab_user.labtm = tmt_conv(scan_ltm)) == -1){
2635 rrd_set_error("unknown keyword %s",scan_ltm);
2638 im->xlab_user.minsec = 1;
2639 im->xlab_user.stst = im->xlab_form;
2641 rrd_set_error("invalid x-grid format");
2647 if(strcmp(optarg,"none") == 0){
2655 &im->ylabfact) == 2) {
2656 if(im->ygridstep<=0){
2657 rrd_set_error("grid step must be > 0");
2659 } else if (im->ylabfact < 1){
2660 rrd_set_error("label factor must be > 0");
2664 rrd_set_error("invalid y-grid format");
2669 strncpy(im->ylegend,optarg,150);
2670 im->ylegend[150]='\0';
2673 im->maxval = atof(optarg);
2676 im->minval = atof(optarg);
2679 im->base = atol(optarg);
2680 if(im->base != 1024 && im->base != 1000 ){
2681 rrd_set_error("the only sensible value for base apart from 1000 is 1024");
2686 long_tmp = atol(optarg);
2687 if (long_tmp < 10) {
2688 rrd_set_error("width below 10 pixels");
2691 im->xsize = long_tmp;
2694 long_tmp = atol(optarg);
2695 if (long_tmp < 10) {
2696 rrd_set_error("height below 10 pixels");
2699 im->ysize = long_tmp;
2702 im->canvas->interlaced = 1;
2708 im->imginfo = optarg;
2711 if((im->canvas->imgformat = if_conv(optarg)) == -1) {
2712 rrd_set_error("unsupported graphics format '%s'",optarg);
2720 im->logarithmic = 1;
2721 if (isnan(im->minval))
2727 col_nam,&color) == 2){
2729 if((ci=grc_conv(col_nam)) != -1){
2730 im->graph_col[ci]=color;
2732 rrd_set_error("invalid color name '%s'",col_nam);
2735 rrd_set_error("invalid color def format");
2740 /* originally this used char *prop = "" and
2741 ** char *font = "dummy" however this results
2742 ** in a SEG fault, at least on RH7.1
2744 ** The current implementation isn't proper
2745 ** either, font is never freed and prop uses
2746 ** a fixed width string
2755 prop,&size,font) == 3){
2757 if((sindex=text_prop_conv(prop)) != -1){
2758 im->text_prop[sindex].size=size;
2759 im->text_prop[sindex].font=font;
2760 if (sindex==0) { /* the default */
2761 im->text_prop[TEXT_PROP_TITLE].size=size;
2762 im->text_prop[TEXT_PROP_TITLE].font=font;
2763 im->text_prop[TEXT_PROP_AXIS].size=size;
2764 im->text_prop[TEXT_PROP_AXIS].font=font;
2765 im->text_prop[TEXT_PROP_UNIT].size=size;
2766 im->text_prop[TEXT_PROP_UNIT].font=font;
2767 im->text_prop[TEXT_PROP_LEGEND].size=size;
2768 im->text_prop[TEXT_PROP_LEGEND].font=font;
2771 rrd_set_error("invalid fonttag '%s'",prop);
2775 rrd_set_error("invalid text property format");
2781 im->canvas->zoom = atof(optarg);
2782 if (im->canvas->zoom <= 0.0) {
2783 rrd_set_error("zoom factor must be > 0");
2788 strncpy(im->title,optarg,150);
2789 im->title[150]='\0';
2794 rrd_set_error("unknown option '%c'", optopt);
2796 rrd_set_error("unknown option '%s'",argv[optind-1]);
2801 if (optind >= argc) {
2802 rrd_set_error("missing filename");
2806 if (im->logarithmic == 1 && (im->minval <= 0 || isnan(im->minval))){
2807 rrd_set_error("for a logarithmic yaxis you must specify a lower-limit > 0");
2811 if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
2812 /* error string is set in parsetime.c */
2816 if (start_tmp < 3600*24*365*10){
2817 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",start_tmp);
2821 if (end_tmp < start_tmp) {
2822 rrd_set_error("start (%ld) should be less than end (%ld)",
2823 start_tmp, end_tmp);
2827 im->start = start_tmp;
2832 rrd_graph_script(int argc, char *argv[], image_desc_t *im)
2836 int linepass = 0; /* stack must follow LINE*, AREA or STACK */
2838 for (i=optind+1;i<argc;i++) {
2843 char funcname[10],vname[MAX_VNAME_LEN+1],sep[1];
2848 /* Each command is one element from *argv[], we call this "line".
2850 ** Each command defines the most current gdes inside struct im.
2851 ** In stead of typing "im->gdes[im->gdes_c-1]" we use "gdp".
2854 gdp=&im->gdes[im->gdes_c-1];
2857 /* function:newvname=string[:ds-name:CF] for xDEF
2858 ** function:vname[#color[:string]] for LINEx,AREA,STACK
2859 ** function:vname#color[:num[:string]] for TICK
2860 ** function:vname-or-num#color[:string] for xRULE,PART
2861 ** function:vname:CF:string for xPRINT
2862 ** function:string for COMMENT
2866 sscanf(line, "%10[A-Z0-9]:%n", funcname,&argstart);
2868 rrd_set_error("Cannot parse function in line: %s",line);
2872 if(sscanf(funcname,"LINE%lf",&linewidth)){
2873 im->gdes[im->gdes_c-1].gf = GF_LINE;
2874 im->gdes[im->gdes_c-1].linewidth = linewidth;
2876 if ((gdp->gf=gf_conv(funcname))==-1) {
2877 rrd_set_error("'%s' is not a valid function name",funcname);
2883 /* If the error string is set, we exit at the end of the switch */
2886 if (rrd_graph_legend(gdp,&line[argstart])==0)
2887 rrd_set_error("Cannot parse comment in line: %s",line);
2893 sscanf(&line[argstart], "%lf%n#%n", &d, &j, &k);
2894 sscanf(&line[argstart], DEF_NAM_FMT "%n#%n", vname, &l, &m);
2896 rrd_set_error("Cannot parse name or num in line: %s",line);
2903 } else if (!rrd_graph_check_vname(im,vname,line)) {
2907 } else break; /* exit due to wrong vname */
2908 if ((j=rrd_graph_color(im,&line[argstart],line,0))==0) break;
2910 if (strlen(&line[argstart])!=0) {
2911 if (rrd_graph_legend(gdp,&line[++argstart])==0)
2912 rrd_set_error("Cannot parse comment in line: %s",line);
2917 rrd_set_error("STACK must follow another graphing element");
2925 sscanf(&line[argstart],DEF_NAM_FMT"%n%1[#:]%n",vname,&j,sep,&k);
2927 rrd_set_error("Cannot parse vname in line: %s",line);
2928 else if (rrd_graph_check_vname(im,vname,line))
2929 rrd_set_error("Undefined vname '%s' in line: %s",line);
2931 k=rrd_graph_color(im,&line[argstart],line,1);
2932 if (rrd_test_error()) break;
2933 argstart=argstart+j+k;
2934 if ((strlen(&line[argstart])!=0)&&(gdp->gf==GF_TICK)) {
2936 sscanf(&line[argstart], ":%lf%n", &gdp->yrule,&j);
2939 if (strlen(&line[argstart])!=0)
2940 if (rrd_graph_legend(gdp,&line[++argstart])==0)
2941 rrd_set_error("Cannot parse legend in line: %s",line);
2947 sscanf(&line[argstart], DEF_NAM_FMT ":%n",gdp->vname,&j);
2949 rrd_set_error("Cannot parse vname in line: '%s'",line);
2953 if (rrd_graph_check_vname(im,gdp->vname,line)) return;
2955 sscanf(&line[argstart], CF_NAM_FMT ":%n",symname,&j);
2957 k=(j!=0)?rrd_graph_check_CF(im,symname,line):1;
2958 #define VIDX im->gdes[gdp->vidx]
2960 case -1: /* looks CF but is not really CF */
2961 if (VIDX.gf == GF_VDEF) rrd_clear_error();
2963 case 0: /* CF present and correct */
2964 if (VIDX.gf == GF_VDEF)
2965 rrd_set_error("Don't use CF when printing VDEF");
2968 case 1: /* CF not present */
2969 if (VIDX.gf == GF_VDEF) rrd_clear_error();
2970 else rrd_set_error("Printing DEF or CDEF needs CF");
2973 rrd_set_error("Oops, bug in GPRINT scanning");
2976 if (rrd_test_error()) break;
2978 if (strlen(&line[argstart])!=0) {
2979 if (rrd_graph_legend(gdp,&line[argstart])==0)
2980 rrd_set_error("Cannot parse legend in line: %s",line);
2981 } else rrd_set_error("No legend in (G)PRINT line: %s",line);
2982 strcpy(gdp->format, gdp->legend);
2988 sscanf(&line[argstart], DEF_NAM_FMT "=%n",gdp->vname,&j);
2990 rrd_set_error("Could not parse line: %s",line);
2993 if (find_var(im,gdp->vname)!=-1) {
2994 rrd_set_error("Variable '%s' in line '%s' already in use\n",
3001 argstart+=scan_for_col(&line[argstart],MAXPATH,gdp->rrd);
3003 sscanf(&line[argstart],
3004 ":" DS_NAM_FMT ":" CF_NAM_FMT "%n%*s%n",
3005 gdp->ds_nam, symname, &j, &k);
3006 if ((j==0)||(k!=0)) {
3007 rrd_set_error("Cannot parse DS or CF in '%s'",line);
3010 rrd_graph_check_CF(im,symname,line);
3014 sscanf(&line[argstart],DEF_NAM_FMT ",%n",vname,&j);
3016 rrd_set_error("Cannot parse vname in line '%s'",line);
3020 if (rrd_graph_check_vname(im,vname,line)) return;
3021 if ( im->gdes[gdp->vidx].gf != GF_DEF
3022 && im->gdes[gdp->vidx].gf != GF_CDEF) {
3023 rrd_set_error("variable '%s' not DEF nor "
3024 "CDEF in VDEF '%s'", vname,gdp->vname);
3027 vdef_parse(gdp,&line[argstart+strstart]);
3030 if (strstr(&line[argstart],":")!=NULL) {
3031 rrd_set_error("Error in RPN, line: %s",line);
3034 if ((gdp->rpnp = rpn_parse(
3039 rrd_set_error("invalid rpn expression in: %s",line);
3044 default: rrd_set_error("Big oops");
3046 if (rrd_test_error()) {
3053 rrd_set_error("can't make a graph without contents");
3054 im_free(im); /* ??? is this set ??? */
3059 rrd_graph_check_vname(image_desc_t *im, char *varname, char *err)
3061 if ((im->gdes[im->gdes_c-1].vidx=find_var(im,varname))==-1) {
3062 rrd_set_error("Unknown variable '%s' in %s",varname,err);
3068 rrd_graph_color(image_desc_t *im, char *var, char *err, int optional)
3071 graph_desc_t *gdp=&im->gdes[im->gdes_c-1];
3073 color=strstr(var,"#");
3076 rrd_set_error("Found no color in %s",err);
3085 rest=strstr(color,":");
3093 sscanf(color,"#%6lx%n",&col,&n);
3094 col = (col << 8) + 0xff /* shift left by 8 */;
3095 if (n!=7) rrd_set_error("Color problem in %s",err);
3098 sscanf(color,"#%8lx%n",&col,&n);
3101 rrd_set_error("Color problem in %s",err);
3103 if (rrd_test_error()) return 0;
3109 rrd_graph_check_CF(image_desc_t *im, char *symname, char *err)
3111 if ((im->gdes[im->gdes_c-1].cf=cf_conv(symname))==-1) {
3112 rrd_set_error("Unknown CF '%s' in %s",symname,err);
3118 rrd_graph_legend(graph_desc_t *gdp, char *line)
3122 i=scan_for_col(line,FMT_LEG_LEN,gdp->legend);
3124 return (strlen(&line[i])==0);
3128 int bad_format(char *fmt) {
3133 while (*ptr != '\0') {
3134 if (*ptr == '%') {ptr++;
3135 if (*ptr == '\0') return 1;
3136 while ((*ptr >= '0' && *ptr <= '9') || *ptr == '.') {
3139 if (*ptr == '\0') return 1;
3143 if (*ptr == '\0') return 1;
3144 if (*ptr == 'e' || *ptr == 'f') {
3146 } else { return 1; }
3148 else if (*ptr == 's' || *ptr == 'S' || *ptr == '%') { ++ptr; }
3157 vdef_parse(gdes,str)
3158 struct graph_desc_t *gdes;
3161 /* A VDEF currently is either "func" or "param,func"
3162 * so the parsing is rather simple. Change if needed.
3169 sscanf(str,"%le,%29[A-Z]%n",¶m,func,&n);
3170 if (n==strlen(str)) { /* matched */
3174 sscanf(str,"%29[A-Z]%n",func,&n);
3175 if (n==strlen(str)) { /* matched */
3178 rrd_set_error("Unknown function string '%s' in VDEF '%s'"
3185 if (!strcmp("PERCENT",func)) gdes->vf.op = VDEF_PERCENT;
3186 else if (!strcmp("MAXIMUM",func)) gdes->vf.op = VDEF_MAXIMUM;
3187 else if (!strcmp("AVERAGE",func)) gdes->vf.op = VDEF_AVERAGE;
3188 else if (!strcmp("MINIMUM",func)) gdes->vf.op = VDEF_MINIMUM;
3189 else if (!strcmp("TOTAL", func)) gdes->vf.op = VDEF_TOTAL;
3190 else if (!strcmp("FIRST", func)) gdes->vf.op = VDEF_FIRST;
3191 else if (!strcmp("LAST", func)) gdes->vf.op = VDEF_LAST;
3193 rrd_set_error("Unknown function '%s' in VDEF '%s'\n"
3200 switch (gdes->vf.op) {
3202 if (isnan(param)) { /* no parameter given */
3203 rrd_set_error("Function '%s' needs parameter in VDEF '%s'\n"
3209 if (param>=0.0 && param<=100.0) {
3210 gdes->vf.param = param;
3211 gdes->vf.val = DNAN; /* undefined */
3212 gdes->vf.when = 0; /* undefined */
3214 rrd_set_error("Parameter '%f' out of range in VDEF '%s'\n"
3228 gdes->vf.param = DNAN;
3229 gdes->vf.val = DNAN;
3232 rrd_set_error("Function '%s' needs no parameter in VDEF '%s'\n"
3247 graph_desc_t *src,*dst;
3251 dst = &im->gdes[gdi];
3252 src = &im->gdes[dst->vidx];
3253 data = src->data + src->ds;
3254 steps = (src->end - src->start) / src->step;
3257 printf("DEBUG: start == %lu, end == %lu, %lu steps\n"
3264 switch (dst->vf.op) {
3265 case VDEF_PERCENT: {
3266 rrd_value_t * array;
3270 if ((array = malloc(steps*sizeof(double)))==NULL) {
3271 rrd_set_error("malloc VDEV_PERCENT");
3274 for (step=0;step < steps; step++) {
3275 array[step]=data[step*src->ds_cnt];
3277 qsort(array,step,sizeof(double),vdef_percent_compar);
3279 field = (steps-1)*dst->vf.param/100;
3280 dst->vf.val = array[field];
3281 dst->vf.when = 0; /* no time component */
3283 for(step=0;step<steps;step++)
3284 printf("DEBUG: %3li:%10.2f %c\n",step,array[step],step==field?'*':' ');
3290 while (step != steps && isnan(data[step*src->ds_cnt])) step++;
3291 if (step == steps) {
3295 dst->vf.val = data[step*src->ds_cnt];
3296 dst->vf.when = src->start + (step+1)*src->step;
3298 while (step != steps) {
3299 if (finite(data[step*src->ds_cnt])) {
3300 if (data[step*src->ds_cnt] > dst->vf.val) {
3301 dst->vf.val = data[step*src->ds_cnt];
3302 dst->vf.when = src->start + (step+1)*src->step;
3309 case VDEF_AVERAGE: {
3312 for (step=0;step<steps;step++) {
3313 if (finite(data[step*src->ds_cnt])) {
3314 sum += data[step*src->ds_cnt];
3319 if (dst->vf.op == VDEF_TOTAL) {
3320 dst->vf.val = sum*src->step;
3321 dst->vf.when = cnt*src->step; /* not really "when" */
3323 dst->vf.val = sum/cnt;
3324 dst->vf.when = 0; /* no time component */
3334 while (step != steps && isnan(data[step*src->ds_cnt])) step++;
3335 if (step == steps) {
3339 dst->vf.val = data[step*src->ds_cnt];
3340 dst->vf.when = src->start + (step+1)*src->step;
3342 while (step != steps) {
3343 if (finite(data[step*src->ds_cnt])) {
3344 if (data[step*src->ds_cnt] < dst->vf.val) {
3345 dst->vf.val = data[step*src->ds_cnt];
3346 dst->vf.when = src->start + (step+1)*src->step;
3353 /* The time value returned here is one step before the
3354 * actual time value. This is the start of the first
3358 while (step != steps && isnan(data[step*src->ds_cnt])) step++;
3359 if (step == steps) { /* all entries were NaN */
3363 dst->vf.val = data[step*src->ds_cnt];
3364 dst->vf.when = src->start + step*src->step;
3368 /* The time value returned here is the
3369 * actual time value. This is the end of the last
3373 while (step >= 0 && isnan(data[step*src->ds_cnt])) step--;
3374 if (step < 0) { /* all entries were NaN */
3378 dst->vf.val = data[step*src->ds_cnt];
3379 dst->vf.when = src->start + (step+1)*src->step;
3386 /* NaN < -INF < finite_values < INF */
3388 vdef_percent_compar(a,b)
3391 /* Equality is not returned; this doesn't hurt except
3392 * (maybe) for a little performance.
3395 /* First catch NaN values. They are smallest */
3396 if (isnan( *(double *)a )) return -1;
3397 if (isnan( *(double *)b )) return 1;
3399 /* NaN doesn't reach this part so INF and -INF are extremes.
3400 * The sign from isinf() is compatible with the sign we return
3402 if (isinf( *(double *)a )) return isinf( *(double *)a );
3403 if (isinf( *(double *)b )) return isinf( *(double *)b );
3405 /* If we reach this, both values must be finite */
3406 if ( *(double *)a < *(double *)b ) return -1; else return 1;