]> git.ipfire.org Git - thirdparty/sarg.git/blob - grepday.c
Explain why the graphs are not produced
[thirdparty/sarg.git] / grepday.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2011
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
24 *
25 */
26
27 #include "include/conf.h"
28 #include "include/defs.h"
29
30 #if defined(HAVE_GD)
31
32 #if defined(HAVE_ICONV_H) && defined(gdFTEX_Unicode)
33 #include <iconv.h>
34 #define USE_ICONV 1
35 #endif
36
37 struct GraphDataStruct
38 {
39 int lavender;
40 int darkblue;
41 int dimgray;
42 int goldenrod;
43 int goldenrod2;
44 int gray;
45 int silver;
46 int black;
47 //! The color of the top side of a graph bar.
48 int color1;
49 //! The color of the right side of a graph bar.
50 int color2;
51 //! The color of the front side of a graph bar.
52 int color3;
53 //! The libgd image we are drawing on.
54 gdImage *im;
55 //! An allocated buffer to convert the string into UTF-8.
56 char *string;
57 //! The number of bytes allocated for the string buffer.
58 size_t string_size;
59 //! The bottom border of the graph.
60 int BottomGraph;
61 //! The top border of the graph.
62 int TopGraph;
63 //! The left border of the graph.
64 int LeftGraph;
65 //! The right border of the graph.
66 int RightGraph;
67 //! The height at which the bottom depth border of the graph extends.
68 int BottomDepth;
69 //! The distance between two ticks on the horizontal axis.
70 double XScale;
71 //! The distance between two ticks on the vertical axis.
72 double YScale;
73 //! The exterior length of a tick on the scales.
74 int TickLength;
75 //! The distance, in pixels, between two ticks along the Y axis.
76 int YTickSpace;
77 };
78
79 enum PlotType
80 {
81 PTG_LinBin,
82 PTG_LogBin,
83 PTG_Time,
84 };
85
86 struct PlotStruct
87 {
88 //! The data points to plot.
89 long long int *datapoints;
90 //! The number of points to plot.
91 int npoints;
92 //! The minimum data to plot along the Y axis.
93 long long int ymin;
94 //! The maximum data to plot along the Y axis.
95 long long int ymax;
96 //! The type of Y axis to draw.
97 enum PlotType ytype;
98 //! The label to write on the X axis.
99 const char *XLabel;
100 //! The label to write on the Y axis.
101 const char *YLabel;
102 //! The name of the output PNG file.
103 const char *pngfile;
104 };
105
106 enum TextRefPos
107 {
108 TRP_TopLeft,
109 TRP_TopCenter,
110 TRP_TopRight,
111 TRP_BottomLeft,
112 TRP_BottomCenter,
113 TRP_BottomRight,
114 TRP_CenterLeft,
115 TRP_Center,
116 TRP_CenterRight,
117 };
118
119 #ifdef USE_ICONV
120 //! The iconv object to convert the text from the locale character set to UTF-8.
121 iconv_t localtoutf=(iconv_t)-1;
122 #endif
123
124 static void Sarg_gdImageStringFT (struct GraphDataStruct *gdata, int fg, char *fontlist,
125 double ptsize, double angle, int x, int y, const char *string,enum TextRefPos RefPos)
126 {
127 char *sstring;
128 char *retval;
129 int brect[8];
130 int minx,miny,maxx,maxy;
131 int i;
132
133 #ifdef USE_ICONV
134 if (localtoutf!=(iconv_t)-1) {
135 const char *str;
136 char *sstr;
137 size_t slen, sslen;
138
139 slen = strlen(string) + 1; // We must include string termination character
140 sslen = slen * 3; // We assume that the UTF8 string will not be bigger than 3 times the original size.
141 if (sslen>gdata->string_size) {
142 sstring = (char *)realloc(gdata->string,sslen);
143 if (!sstring) {
144 debuga(_("realloc error (%"PRIu64" bytes required)\n"),(uint64_t)sslen);
145 exit(EXIT_FAILURE);
146 }
147 gdata->string=(char *)sstring;
148 gdata->string_size=sslen;
149 } else {
150 sstring=gdata->string;
151 sslen=gdata->string_size;
152 }
153
154 str = string;
155 sstr = sstring;
156 if (iconv (localtoutf, (ICONV_CONST char **)&str, &slen, &sstr, &sslen)==-1) {
157 debuga(_("(grepday) iconv failed to convert string \"%s\" from %s to UTF-8 - %s\n"),string,CharSet,strerror(errno));
158 sstring=(char *)string; //show something sensible on the graph
159 }
160 } else {
161 sstring=(char *)string; //show something sensible on the graph
162 }
163 #else
164 sstring=(char *)string;
165 #endif
166
167 if (RefPos!=TRP_BottomLeft) {
168 retval = gdImageStringFTEx (NULL, brect, fg, fontlist, ptsize, angle, 0, 0, sstring, gdFTEX_Unicode);
169 if (retval) {
170 debuga(_("libgd failed to calculate the bounding box of the text \"%s\": %s\n"),sstring,retval);
171 exit(EXIT_FAILURE);
172 }
173 /*
174 From libgd documentation, brect contains this without taking into account the angle:
175 0 lower left corner, X position
176 1 lower left corner, Y position
177 2 lower right corner, X position
178 3 lower right corner, Y position
179 4 upper right corner, X position
180 5 upper right corner, Y position
181 6 upper left corner, X position
182 7 upper left corner, Y position
183 */
184 minx=maxx=brect[0];
185 miny=maxy=brect[1];
186 for (i=2 ; i<7 ; i+=2) {
187 if (minx>brect[i]) minx=brect[i];
188 if (maxx<brect[i]) maxx=brect[i];
189 if (miny>brect[i+1]) miny=brect[i+1];
190 if (maxy<brect[i+1]) maxy=brect[i+1];
191 }
192 }
193
194 switch (RefPos)
195 {
196 case TRP_TopLeft:
197 y-=miny;
198 break;
199
200 case TRP_TopCenter:
201 x-=(maxx-minx)/2;
202 y-=miny;
203 break;
204
205 case TRP_TopRight:
206 x-=maxx;
207 y-=miny;
208 break;
209
210 case TRP_BottomLeft:
211 break;
212
213 case TRP_BottomCenter:
214 x-=(maxx-minx)/2;
215 break;
216
217 case TRP_BottomRight:
218 x-=maxx;
219 break;
220
221 case TRP_Center:
222 x-=(maxx-minx)/2;
223 y+=(maxy-miny)/2;
224 break;
225
226 case TRP_CenterLeft:
227 y+=(maxy-miny)/2;
228 break;
229
230 case TRP_CenterRight:
231 x-=maxx;
232 y+=(maxy-miny)/2;
233 break;
234 }
235 retval = gdImageStringFTEx (gdata->im, brect, fg, fontlist, ptsize, angle, x, y, sstring, gdFTEX_Unicode);
236 if (retval) {
237 debuga(_("libgd failed to render the text \"%s\": %s\n"),sstring,retval);
238 exit(EXIT_FAILURE);
239 }
240 }
241
242 static void bar(struct GraphDataStruct *gdata,int x1,double y,const char *label)
243 {
244 gdPoint points[4];
245 int val=0;
246 int width2;
247
248 val=gdata->BottomGraph+5-(int)(y*gdata->YScale+0.5);
249 width2=(int)(gdata->XScale/4.);
250
251 // front side of the bar
252 if (val<gdata->BottomDepth)
253 gdImageFilledRectangle(gdata->im, x1-width2, val, x1+width2, gdata->BottomDepth, gdata->color3);
254
255 // top side of the bar
256 points[0].x = x1-width2+5;
257 points[0].y = val-5;
258 points[1].x = x1-width2;
259 points[1].y = val;
260 points[2].x = x1+width2;
261 points[2].y = val;
262 points[3].x = x1+width2+5;
263 points[3].y = val-5;
264 gdImageFilledPolygon(gdata->im, points, 4, gdata->color1);
265
266 gdImageLine(gdata->im, x1+2, val-2, x1+2, val-10, gdata->dimgray);
267 gdImageFilledRectangle(gdata->im, x1-8, val-20, x1+12, val-10, gdata->goldenrod);
268 gdImageRectangle(gdata->im, x1-8, val-20, x1+12, val-10, gdata->goldenrod2);
269
270 Sarg_gdImageStringFT(gdata,gdata->black,GraphFont,6,0.0,x1+2,val-12,label,TRP_BottomCenter);
271
272 // lateral side of the bar
273 if (val<gdata->BottomDepth) {
274 points[0].x = x1+width2+5;
275 points[0].y = val-5;
276 points[1].x = x1+width2;
277 points[1].y = val;
278 points[2].x = x1+width2;
279 points[2].y = gdata->BottomDepth;
280 points[3].x = x1+width2+5;
281 points[3].y = gdata->BottomGraph;
282 gdImageFilledPolygon(gdata->im, points, 4, gdata->color2);
283 }
284
285 return;
286 }
287
288 static int greport_compute_yaxis(struct PlotStruct *pdata,struct GraphDataStruct *gdata)
289 {
290 double symin,symax;
291 double range;
292 double yscale;
293 double ymag;
294
295 if (pdata->ymin<0.) {
296 debuga(_("Minimum for Y scale of the graph is out of range: %"PRId64"\n"),(int64_t)pdata->ymin);
297 return(-1);
298 }
299 if (pdata->ymax<=0.) {
300 debuga(_("Maximum for Y scale of the graph is out of range: %"PRId64"\n"),(int64_t)pdata->ymax);
301 return(-1);
302 }
303
304 switch(pdata->ytype)
305 {
306 case PTG_LinBin:
307 symin=(double)pdata->ymin;
308 symax=(double)pdata->ymax;
309 ymag=1000.;
310 break;
311
312 case PTG_LogBin:
313 if (pdata->ymin>0.)
314 symin=log(pdata->ymin);
315 else
316 symin=0.;
317 symax=log(pdata->ymax);
318 ymag=1000.;
319 break;
320
321 case PTG_Time:
322 symin=(double)pdata->ymin;
323 symax=(double)pdata->ymax;
324 ymag=1.;
325 break;
326
327 default:
328 debuga(_("Unknown type %d for Y axis scale\n"),pdata->ytype);
329 return(-1);
330 }
331 gdata->YTickSpace=10;
332
333 range=symax-symin;
334 yscale=(double)(gdata->BottomGraph-gdata->TopGraph)/range;
335 gdata->YScale=yscale;
336 return(0);
337 }
338
339 static void greport_formatbin(double yval,int maxdigits,char *string,int slen)
340 {
341 int len;
342 char schar[]={'\0','k','M','G','T','P'};
343 int scount;
344 int i;
345 int ndigits;
346
347 for (scount=0 ; scount<sizeof(schar)/sizeof(*schar) && yval>=1000. ; scount++)
348 yval/=1000.;
349 if (yval<2.)
350 ndigits=2;
351 else if (yval<3.)
352 ndigits=1;
353 else
354 ndigits=0;
355 if (ndigits>maxdigits) ndigits=maxdigits;
356 len=snprintf(string,slen,"%.*f",ndigits,(float)yval);
357 if (UseComma)
358 for (i=0 ; i<len ; i++)
359 if (string[i]=='.') string[i]=',';
360 if (schar[scount] && len<slen) {
361 string[len++]=schar[scount];
362 string[len]='\0';
363 }
364 }
365
366 static void greport_draw_yaxis(struct PlotStruct *pdata,struct GraphDataStruct *gdata)
367 {
368 double yval;
369 int y0;
370 int y;
371 int yt;
372 char YLabel[50];
373 int xexterior;
374 int xinterior;
375 int xtick;
376
377 y0=gdata->BottomGraph;
378 yt=gdata->BottomDepth-gdata->BottomGraph;
379 xexterior=gdata->LeftGraph-10;
380 xinterior=gdata->LeftGraph;
381 xtick=gdata->LeftGraph-10-gdata->TickLength;
382 for(y=y0-gdata->YTickSpace ; y>=gdata->TopGraph ; y-=gdata->YTickSpace) {
383 gdImageLine(gdata->im, xtick, y+yt, xexterior, y+yt, gdata->dimgray);
384 gdImageLine(gdata->im, xexterior, y+yt, xinterior, y, gdata->dimgray);
385 gdImageLine(gdata->im, xinterior, y, gdata->RightGraph, y, gdata->dimgray);
386 switch (pdata->ytype)
387 {
388 case PTG_LinBin:
389 yval=(double)(y0-y)/gdata->YScale+(double)pdata->ymin;
390 greport_formatbin(yval,2,YLabel,sizeof(YLabel));
391 break;
392
393 case PTG_LogBin:
394 yval=exp((double)(y0-y)/gdata->YScale+log(pdata->ymin));
395 greport_formatbin(yval,2,YLabel,sizeof(YLabel));
396 break;
397
398 case PTG_Time:
399 {
400 int t;
401
402 yval=(double)(y0-y)/gdata->YScale+(double)pdata->ymin;
403 t=(int)(yval/60000.+0.5);
404 snprintf(YLabel,sizeof(YLabel),"%02d:%02d",t/60,t%60);
405 break;
406 }
407 }
408 Sarg_gdImageStringFT(gdata,gdata->dimgray,GraphFont,7,0.0,xtick,y+yt,YLabel,TRP_CenterRight);
409 }
410 }
411
412 static void greport_plot(const struct userinfostruct *uinfo,struct PlotStruct *pdata)
413 {
414 FILE *pngout;
415 int x, y;
416 int day;
417 int x1;
418 char graph[MAXLEN];
419 char s[15];
420 char ftime[128];
421 time_t t;
422 struct tm *local;
423 gdPoint points[4];
424 struct GraphDataStruct gdata;
425 const int ImgXSize=720;
426 const int ImgYSize=480;
427 const int LeftMargin=60;
428 const int RightMargin=20;
429 const int TopMargin=60;
430 const int BottomMargin=60;
431 const int TickLength=3;
432 const int ZTickLength=5;
433 double yval;
434 char blabel[50];
435 double logpmin;
436
437 memset(&gdata,0,sizeof(gdata));
438
439 gdata.im = gdImageCreate(ImgXSize, ImgYSize);
440 gdata.BottomGraph=ImgYSize-BottomMargin;
441 gdata.LeftGraph=LeftMargin;
442 gdata.RightGraph=ImgXSize-RightMargin;
443 gdata.TopGraph=TopMargin;
444 gdata.BottomDepth=gdata.BottomGraph+5;
445 gdata.XScale=(double)(gdata.RightGraph-gdata.LeftGraph)/(pdata->npoints+1);
446 if (greport_compute_yaxis(pdata,&gdata)<0) return;
447 gdata.TickLength=TickLength;
448
449 // first allocated color is the background
450 gdata.lavender = gdImageColorAllocate(gdata.im, 230, 230, 250);
451 gdata.gray = gdImageColorAllocate(gdata.im, 192, 192, 192);
452 gdata.silver = gdImageColorAllocate(gdata.im, 211, 211, 211);
453 gdata.black = gdImageColorAllocate(gdata.im, 0, 0, 0);
454 gdata.dimgray = gdImageColorAllocate(gdata.im, 105, 105, 105);
455 gdata.darkblue = gdImageColorAllocate(gdata.im, 0, 0, 139);
456 gdata.goldenrod = gdImageColorAllocate(gdata.im, 234, 234, 174);
457 gdata.goldenrod2 = gdImageColorAllocate(gdata.im, 207, 181, 59);
458
459 if(strcmp(GraphDaysBytesBarColor,"orange") == 0) {
460 gdata.color1 = gdImageColorAllocate(gdata.im, 255, 233, 142);
461 gdata.color2 = gdImageColorAllocate(gdata.im, 220, 163, 72);
462 gdata.color3 = gdImageColorAllocate(gdata.im, 255, 198, 107);
463 }
464 else if(strcmp(GraphDaysBytesBarColor,"blue") == 0) {
465 gdata.color1 = gdImageColorAllocate(gdata.im, 62, 80, 167);
466 gdata.color2 = gdImageColorAllocate(gdata.im, 40, 51, 101);
467 gdata.color3 = gdImageColorAllocate(gdata.im, 57, 73, 150);
468 }
469 else if(strcmp(GraphDaysBytesBarColor,"green") == 0) {
470 gdata.color1 = gdImageColorAllocate(gdata.im,120,166,129);
471 gdata.color2 = gdImageColorAllocate(gdata.im,84,113,82);
472 gdata.color3 = gdImageColorAllocate(gdata.im,158,223,167);
473 }
474 else if(strcmp(GraphDaysBytesBarColor,"yellow") == 0) {
475 gdata.color1 = gdImageColorAllocate(gdata.im,185,185,10);
476 gdata.color2 = gdImageColorAllocate(gdata.im,111,111,10);
477 gdata.color3 = gdImageColorAllocate(gdata.im,166,166,10);
478 }
479 else if(strcmp(GraphDaysBytesBarColor,"brown") == 0) {
480 gdata.color1 = gdImageColorAllocate(gdata.im,97,45,27);
481 gdata.color2 = gdImageColorAllocate(gdata.im,60,30,20);
482 gdata.color3 = gdImageColorAllocate(gdata.im,88,41,26);
483 }
484 else if(strcmp(GraphDaysBytesBarColor,"red") == 0){
485 gdata.color1 = gdImageColorAllocate(gdata.im,185,10,10);
486 gdata.color2 = gdImageColorAllocate(gdata.im,111,10,10);
487 gdata.color3 = gdImageColorAllocate(gdata.im,166,10,10);
488 } else {
489 debuga(_("Unknown color \"%s\" requested for the graph. Using orange instead\n"),GraphDaysBytesBarColor);
490 gdata.color1 = gdImageColorAllocate(gdata.im, 255, 233, 142);
491 gdata.color2 = gdImageColorAllocate(gdata.im, 220, 163, 72);
492 gdata.color3 = gdImageColorAllocate(gdata.im, 255, 198, 107);
493 }
494
495 // rectangle around the image
496 gdImageRectangle(gdata.im, 0, 0, ImgXSize-1, ImgYSize-1, gdata.dimgray);
497 // backtround of the graph
498 gdImageFilledRectangle(gdata.im, LeftMargin, gdata.TopGraph, gdata.RightGraph, gdata.BottomGraph, gdata.silver);
499
500 // depth of the left Y axis
501 points[0].x = gdata.LeftGraph-10;
502 points[0].y = gdata.TopGraph+5;
503 points[1].x = gdata.LeftGraph-10;
504 points[1].y = gdata.BottomDepth;
505 points[2].x = gdata.LeftGraph;
506 points[2].y = gdata.BottomGraph;
507 points[3].x = gdata.LeftGraph;
508 points[3].y = gdata.TopGraph;
509 gdImageFilledPolygon(gdata.im, points, 4, gdata.gray);
510
511 // depth of the bottom X axis
512 points[0].x = gdata.LeftGraph;
513 points[0].y = gdata.BottomGraph;
514 points[1].x = gdata.LeftGraph-10;
515 points[1].y = gdata.BottomDepth;
516 points[2].x = gdata.RightGraph-10;
517 points[2].y = gdata.BottomDepth;
518 points[3].x = gdata.RightGraph;
519 points[3].y = gdata.BottomGraph;
520 gdImageFilledPolygon(gdata.im, points, 4, gdata.gray);
521
522 // vertical exterior line of the depth
523 gdImageLine(gdata.im, LeftMargin-10, TopMargin+5, LeftMargin-10, gdata.BottomDepth+ZTickLength, gdata.black);
524 // horizontal exterior line of the depth
525 gdImageLine(gdata.im, LeftMargin-10-ZTickLength, gdata.BottomDepth, gdata.RightGraph-10, gdata.BottomDepth, gdata.black);
526 // diagonal line between the two depths
527 gdImageLine(gdata.im, LeftMargin-10, gdata.BottomDepth, LeftMargin, gdata.BottomGraph, gdata.black);
528 // vertical left line of the graph
529 gdImageLine(gdata.im, LeftMargin, gdata.BottomGraph, LeftMargin, gdata.TopGraph, gdata.black);
530 // horizontal bottom line of the graph
531 gdImageLine(gdata.im, LeftMargin, gdata.BottomGraph, gdata.RightGraph, gdata.BottomGraph, gdata.black);
532 // vertical right line of the graph
533 gdImageLine(gdata.im, gdata.RightGraph, gdata.TopGraph, gdata.RightGraph, gdata.BottomGraph, gdata.black);
534 // diagonal line to close the right of the bottom depth
535 gdImageLine(gdata.im, gdata.RightGraph-10, gdata.BottomDepth, gdata.RightGraph, gdata.BottomGraph, gdata.black);
536
537 // Y axis ticks
538 greport_draw_yaxis(pdata,&gdata);
539
540 // X axis ticks and labels
541 for(y=1; y<=pdata->npoints; y++) {
542 x=gdata.LeftGraph-10+(int)((double)y*gdata.XScale+0.5);
543 gdImageLine(gdata.im, x, gdata.BottomDepth, x, gdata.BottomDepth+TickLength, gdata.dimgray);
544 sprintf(s,"%02d",y);
545 Sarg_gdImageStringFT(&gdata,gdata.dimgray,GraphFont,7,0.0,x,gdata.BottomDepth+TickLength+1,s,TRP_TopCenter);
546 }
547
548 t = time(NULL);
549 local = localtime(&t);
550 if(DateFormat[0]=='u')
551 strftime(ftime, sizeof(ftime), "%b/%d/%Y %H:%M", local);
552 if(DateFormat[0]=='e')
553 strftime(ftime, sizeof(ftime), "%d/%b/%Y-%H:%M", local);
554
555 x=ImgXSize*5/12;
556 Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,7,0.0,ImgXSize-10,ImgYSize-10,ftime,TRP_BottomRight);
557 if(ShowSargInfo) Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,10,0.0,x,15,_("SARG, "),TRP_BottomRight);
558 Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,10,0.0,x,15,Title,TRP_BottomLeft);
559 sprintf(warea,_("Period: %s"),period.text);
560 Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,9,0.0,x,27,warea,TRP_BottomLeft);
561 sprintf(warea,_("User: %s"),uinfo->label);
562 Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,9,0.0,x,38,warea,TRP_BottomLeft);
563
564 Sarg_gdImageStringFT(&gdata,gdata.black,GraphFont,10,3.141592/2,15,ImgYSize/2,pdata->YLabel,TRP_CenterLeft);
565 Sarg_gdImageStringFT(&gdata,gdata.black,GraphFont,10,0.0,ImgXSize/2,ImgYSize-20,pdata->XLabel,TRP_BottomCenter);
566
567 logpmin=(pdata->ytype==PTG_LogBin && pdata->ymin>0.) ? log(pdata->ymin) : 0.;
568 for (day=0 ; day<pdata->npoints ; day++) {
569 if (pdata->datapoints[day]>0) {
570 x1=gdata.LeftGraph-10+(int)((double)(day+1)*gdata.XScale+0.5);
571 switch (pdata->ytype)
572 {
573 case PTG_LinBin:
574 yval=(double)pdata->datapoints[day];
575 if (yval<pdata->ymin)
576 yval=0.;
577 else if (yval>pdata->ymax)
578 yval=pdata->ymax;
579 else
580 yval-=pdata->ymin;
581 greport_formatbin(pdata->datapoints[day],1,blabel,sizeof(blabel));
582 break;
583 case PTG_LogBin:
584 yval=(double)pdata->datapoints[day];
585 if (yval<=pdata->ymin)
586 yval=0.;
587 else if (yval>pdata->ymax)
588 yval=log(pdata->ymax)-logpmin;
589 else
590 yval=log(yval)-logpmin;
591 greport_formatbin(pdata->datapoints[day],1,blabel,sizeof(blabel));
592 break;
593 case PTG_Time:
594 {
595 int t;
596
597 yval=(double)pdata->datapoints[day];
598 if (yval<pdata->ymin)
599 yval=0.;
600 else if (yval>pdata->ymax)
601 yval=pdata->ymax;
602 else
603 yval-=pdata->ymin;
604 t=(int)(pdata->datapoints[day]/60000.);
605 snprintf(blabel,sizeof(blabel),"%d:%02d",t/60,t%60);
606 break;
607 }
608 default:
609 yval=-1.;
610 break;
611 }
612 if (yval>=0.) bar(&gdata,x1,yval,blabel);
613 }
614 }
615
616 if (snprintf(graph,sizeof(graph),"%s/%s/%s",outdirname,uinfo->filename,pdata->pngfile)>=sizeof(graph)) {
617 debuga(_("user name too long for %s/%s/%s\n"),outdirname,uinfo->filename,pdata->pngfile);
618 exit(EXIT_FAILURE);
619 }
620 if((pngout=fopen(graph,"wb"))==NULL) {
621 debuga(_("(grepday) Cannot open log file %s\n"),graph);
622 exit(EXIT_FAILURE);
623 }
624 gdImagePng(gdata.im, pngout);
625 fclose(pngout);
626 gdImageDestroy(gdata.im);
627
628 if (gdata.string) free(gdata.string);
629 }
630
631 #endif //HAVE_GD
632
633 void greport_prepare(void)
634 {
635 #ifdef HAVE_GD
636 if (!Graphs) {
637 if (debugz)
638 debugaz(_("Graphs disabled as requested in %s\n"),ConfigFile);
639 return;
640 }
641 if (GraphFont[0]=='\0') {
642 if (debugz)
643 debugaz(_("Graphs disabled as no font names were provided in %s\n"),ConfigFile);
644 return;
645 }
646
647 if(access(GraphFont, R_OK) != 0) {
648 debuga(_("(grepday) Fontname %s not found\n"),GraphFont);
649 exit(EXIT_FAILURE);
650 }
651
652 #ifdef USE_ICONV
653 localtoutf = iconv_open ("UTF-8", CharSet);
654 if (localtoutf==(iconv_t)-1) {
655 debuga(_("(grepday) iconv cannot convert from %s to UTF-8 - %s\n"),CharSet,strerror(errno));
656 }
657 #endif
658
659 #endif //HAVE_GD
660 }
661
662 void greport_day(const struct userinfostruct *uinfo)
663 {
664 #ifdef HAVE_GD
665 FILE *fp_in, *fp_ou;
666 char wdirname[MAXLEN];
667 char buf[MAXLEN];
668 char colsep;
669 int day;
670 long long int llday;
671 long long int bytes;
672 long long int elap;
673 long long int bytespoints[31];
674 long long int elappoints[31];
675 struct getwordstruct gwarea;
676 struct PlotStruct pdata;
677
678 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.day",tmp,uinfo->filename)>=sizeof(wdirname)) {
679 debuga(_("user name too long for %s/%s.day\n"),tmp,uinfo->filename);
680 exit(EXIT_FAILURE);
681 }
682 if(access(wdirname, R_OK) != 0) {
683 return;
684 }
685 if(!Graphs || GraphFont[0]=='\0') {
686 unlink(wdirname);
687 return;
688 }
689
690 if((fp_in=fopen(wdirname,"r"))==NULL) {
691 debuga(_("(grepday) Cannot open log file %s\n"),wdirname);
692 exit(EXIT_FAILURE);
693 }
694
695 memset(bytespoints,0,sizeof(bytespoints));
696 memset(elappoints,0,sizeof(elappoints));
697 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
698 fixendofline(buf);
699 getword_start(&gwarea,buf);
700 if (getword_atoll(&llday,&gwarea,'/')<0) {
701 debuga(_("Invalid date in file %s\n"),wdirname);
702 exit(EXIT_FAILURE);
703 }
704 day=(int)llday;
705 if (day<1 || day>31) continue;
706 if (getword_skip(20,&gwarea,'\t')<0 || getword_skip(20,&gwarea,'\t')<0) {
707 debuga(_("Invalid entry in file %s\n"),wdirname);
708 exit(EXIT_FAILURE);
709 }
710 if ((datetimeby & DATETIME_BYTE)!=0) {
711 colsep=((datetimeby & DATETIME_ELAP)!=0) ? '\t' : '\0';
712 if (getword_atoll(&bytes,&gwarea,'\t')<0) {
713 debuga(_("Invalid number of bytes in file %s\n"),wdirname);
714 exit(EXIT_FAILURE);
715 }
716 bytespoints[day-1]+=bytes;
717 }
718 if ((datetimeby & DATETIME_ELAP)!=0) {
719 if (getword_atoll(&elap,&gwarea,'\0')<0) {
720 debuga(_("Invalid elapsed time in file %s\n"),wdirname);
721 exit(EXIT_FAILURE);
722 }
723 elappoints[day-1]+=elap;
724 }
725 }
726 fclose(fp_in);
727 if (unlink(wdirname)) {
728 debuga(_("Cannot delete %s - %s\n"),wdirname,strerror(errno));
729 exit(EXIT_FAILURE);
730 }
731
732 if (snprintf(wdirname,sizeof(wdirname),"%s/%s/graph.html",outdirname,uinfo->filename)>=sizeof(wdirname)) {
733 debuga(_("user name too long for %s/%s/%s\n"),outdirname,uinfo->filename,"graph.html");
734 exit(EXIT_FAILURE);
735 }
736 if ((fp_ou=fopen(wdirname,"wt"))==NULL) {
737 debuga(_("(grepday) Cannot open output file %s\n"),wdirname);
738 exit(EXIT_FAILURE);
739 }
740 write_html_head(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Graph report"),HTML_JS_NONE);
741
742 fputs("<table class=\"report\" cellpadding=\"0\" cellspacing=\"2\">\n", fp_ou);
743 if((datetimeby & DATETIME_BYTE)!=0) {
744 memset(&pdata,0,sizeof(pdata));
745 pdata.datapoints=bytespoints;
746 pdata.npoints=31;
747 pdata.XLabel=_("DAYS");
748 pdata.ymin=50LL*1000LL;
749 pdata.ymax=5LL*1000LL*1000LL*1000LL;
750 pdata.ytype=PTG_LogBin;
751 pdata.YLabel=_("BYTES");
752 pdata.pngfile="graph_day_byte.png";
753 greport_plot(uinfo,&pdata);
754 fprintf(fp_ou,"<tr><td><img src=\"%s\" alt=\"B\"></td></tr>\n",pdata.pngfile);
755 }
756 if((datetimeby & DATETIME_ELAP)!=0) {
757 memset(&pdata,0,sizeof(pdata));
758 pdata.datapoints=elappoints;
759 pdata.npoints=31;
760 pdata.XLabel=_("DAYS");
761 pdata.ymin=0;
762 pdata.ymax=86400000;
763 pdata.ytype=PTG_Time;
764 pdata.YLabel=_("ELAPSED TIME");
765 pdata.pngfile="graph_day_elap.png";
766 greport_plot(uinfo,&pdata);
767 fprintf(fp_ou,"<tr><td><img src=\"%s\" alt=\"E\"></td></tr>\n",pdata.pngfile);
768 }
769 fputs("</table>\n",fp_ou);
770
771 if (write_html_trailer(fp_ou)<0)
772 debuga(_("Write error in file %s\n"),wdirname);
773 if (fclose(fp_ou)==EOF)
774 debuga(_("Failed to close file %s - %s\n"),wdirname,strerror(errno));
775 #endif //HAVE_GD
776
777 return;
778 }
779
780 void greport_cleanup(void)
781 {
782 #ifdef HAVE_GD
783 gdFontCacheShutdown();
784
785 #ifdef USE_ICONV
786 if (localtoutf!=(iconv_t)-1)
787 iconv_close (localtoutf);
788 #endif
789 #endif //HAVE_GD
790 }