]> git.ipfire.org Git - thirdparty/sarg.git/blob - grepday.c
Ported r208 from branches/v2_2_7
[thirdparty/sarg.git] / grepday.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2010
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26 #include "include/conf.h"
27 #include "include/defs.h"
28
29 #if defined(HAVE_GD)
30
31 static int blue;
32 static int white;
33 static int lavender;
34 static int darkblue;
35 static int dimgray;
36 static int goldenrod;
37 static int goldenrod2;
38 static int gray;
39 static int silver;
40 static int black;
41 //static char *font1 = FONTDIR"/FreeSans.ttf";
42 static char *font1 = FONTDIR"/DejaVuSans.ttf";
43
44 #if defined(HAVE_ICONV_H) && defined(gdFTEX_Unicode)
45 #include <iconv.h>
46 #define SARGgdImageStringFT I18NgdImageStringFT
47
48 static char * I18NgdImageStringFT (gdImage * im, int *brect, int fg, char *fontlist,
49 double ptsize, double angle, int x, int y, char *string)
50 {
51 iconv_t localtoutf;
52 char *sstring, *str, *sstr, *retval;
53 size_t slen, sslen;
54
55 slen = strlen(string) + 1; // We must include string termination character
56 sslen = slen * 2; // We assume that UTF8 maximum 2 times large than local
57 sstring = (char *)malloc(sslen);
58 if (!sstring) {
59 fprintf(stderr, "SARG: %s (%zu):\n",text[59],sslen);
60 exit(1);
61 }
62
63 str = (char *) string;
64 sstr = (char *) sstring;
65
66 localtoutf = iconv_open ("UTF-8", CharSet);
67 if (localtoutf==(iconv_t)-1) {
68 fprintf(stderr,"SARG(grepday) iconv cannot convert from %s to UTF-8 - %s\n",CharSet,strerror(errno));
69 strcpy(sstring,string); //show something sensible on the graph
70 }
71 else {
72 if (iconv (localtoutf, (ICONV_CONST char **)&str, &slen, &sstr, &sslen)==-1) {
73 fprintf(stderr,"SARG(grepday) iconv failed to convert string \"%s\" from %s to UTF-8 - %s\n",string,CharSet,strerror(errno));
74 strcpy(sstring,string); //show something sensible on the graph
75 }
76 iconv_close (localtoutf);
77 }
78
79 retval = gdImageStringFTEx (im, brect, fg, fontlist, ptsize, angle, x, y, sstring, gdFTEX_Unicode);
80 free(sstring);
81
82 return retval;
83 }
84 #else
85 #define SARGgdImageStringFT gdImageStringFT
86 #endif
87
88 static void bar(int x1,long long int n)
89 {
90 gdPoint points[4];
91 int brect[8];
92 int val=0, x;
93 long long int num;
94 char v[15];
95
96 static const long long int limits[400]={0,500,1000,2000,3000,4000,
97 5000,6000,7000,8000,9000,10000,
98 20000,30000,40000,50000,70000,90000,
99 110000,130000,150000,170000,190000,210000,
100 230000,250000,280000,310000,340000,370000,
101 400000,430000,450000,460000,490000,500000,
102 550000,600000,650000,700000,750000,800000,
103 850000,900000,950000,1000000,1100000,1200000,
104 1300000,1400000,1500000,1600000,1700000,1800000,
105 1900000,2000000,2100000,2200000,2300000,2400000,
106 2500000,2600000,2700000,2800000,2900000,3000000,
107 3100000,3200000,3300000,3400000,3500000,3600000,
108 3700000,3800000,3900000,4000000,4100000,4200000,
109 4300000,4400000,4500000,4600000,4700000,4800000,
110 4900000,5000000,5100000,5200000,5300000,5400000,
111 5500000,5600000,5700000,5800000,5900000,6000000,
112 6100000,6200000,6300000,6400000,6500000,6600000,
113 6700000,6800000,6900000,7000000,7100000,7200000,
114 7300000,7400000,7500000,7600000,7700000,7800000,
115 7900000,8000000,8100000,8200000,8300000,8400000,
116 8500000,8600000,8700000,8800000,8900000,9000000,
117 9100000,9200000,9300000,9400000,9500000,9600000,
118 9700000,9800000,9900000,10000000,10500000,11000000,
119 11500000,12000000,12500000,13000000,13500000,14000000,
120 14500000,15000000,15500000,16000000,16500000,17000000,
121 17500000,18000000,18500000,19000000,19500000,20000000,
122 21000000,22000000,23000000,24000000,25000000,26000000,
123 27000000,28000000,29000000,30000000,31000000,32000000,
124 33000000,34000000,35000000,36000000,37000000,38000000,
125 39000000,40000000,41000000,42000000,43000000,44000000,
126 45000000,46000000,47000000,48000000,49000000,50000000,
127 51000000,52000000,53000000,54000000,55000000,56000000,
128 57000000,58000000,59000000,60000000,61000000,62000000,
129 63000000,64000000,65000000,66000000,67000000,68000000,
130 69000000,70000000,71000000,72000000,73000000,74000000,
131 75000000,76000000,77000000,78000000,79000000,80000000,
132 81000000,82000000,83000000,84000000,85000000,86000000,
133 87000000,88000000,89000000,90000000,91000000,92000000,
134 93000000,94000000,95000000,96000000,97000000,98000000,
135 99000000,100000000,110000000,120000000,130000000,140000000,
136 150000000,160000000,170000000,180000000,190000000,200000000,
137 210000000,220000000,230000000,240000000,250000000,260000000,
138 270000000,280000000,290000000,300000000,310000000,320000000,
139 330000000,340000000,350000000,360000000,370000000,380000000,
140 390000000,400000000,410000000,420000000,430000000,440000000,
141 450000000,460000000,470000000,480000000,490000000,500000000,
142 510000000,520000000,530000000,540000000,550000000,560000000,
143 570000000,580000000,590000000,600000000,610000000,620000000,
144 630000000,640000000,650000000,660000000,670000000,680000000,
145 690000000,700000000,710000000,720000000,730000000,740000000,
146 750000000,760000000,770000000,780000000,790000000,800000000,
147 810000000,820000000,830000000,840000000,850000000,860000000,
148 870000000,880000000,890000000,900000000,910000000,920000000,
149 930000000,940000000,950000000,960000000,970000000,980000000,
150 990000000LL,1000000000LL,1100000000LL,1200000000LL,1300000000LL,1400000000LL,
151 1500000000LL,1600000000LL,1700000000LL,1800000000LL,1900000000LL,2000000000LL,
152 2100000000LL,2200000000LL,2300000000LL,2400000000LL,2500000000LL,2600000000LL,
153 2700000000LL,2800000000LL,2900000000LL,3000000000LL,3100000000LL,3200000000LL,
154 3300000000LL,3400000000LL,3500000000LL,3600000000LL,3700000000LL,3800000000LL,
155 3900000000LL,4000000000LL,4100000000LL,4200000000LL,4300000000LL,4400000000LL,
156 4500000000LL,4600000000LL,4700000000LL,4800000000LL,4900000000LL,5000000000LL};
157
158 if(access(font1, R_OK) != 0) {
159 fprintf(stderr, "SARG: (grepday) Fontname: %s not found.\n",font1);
160 exit(1);
161 }
162
163 if(strcmp(GraphDaysBytesBarColor,"orange") == 0) {
164 color1 = gdImageColorAllocate(im, 255, 233, 142);
165 color2 = gdImageColorAllocate(im, 220, 163, 72);
166 color3 = gdImageColorAllocate(im, 255, 198, 107);
167 }
168 if(strcmp(GraphDaysBytesBarColor,"blue") == 0) {
169 color1 = gdImageColorAllocate(im, 62, 80, 167);
170 color2 = gdImageColorAllocate(im, 40, 51, 101);
171 color3 = gdImageColorAllocate(im, 57, 73, 150);
172 }
173 if(strcmp(GraphDaysBytesBarColor,"green") == 0) {
174 color1 = gdImageColorAllocate(im,120,166,129);
175 color2 = gdImageColorAllocate(im,84,113,82);
176 color3 = gdImageColorAllocate(im,158,223,167);
177 }
178 if(strcmp(GraphDaysBytesBarColor,"yellow") == 0) {
179 color1 = gdImageColorAllocate(im,185,185,10);
180 color2 = gdImageColorAllocate(im,111,111,10);
181 color3 = gdImageColorAllocate(im,166,166,10);
182 }
183 if(strcmp(GraphDaysBytesBarColor,"brown") == 0) {
184 color1 = gdImageColorAllocate(im,97,45,27);
185 color2 = gdImageColorAllocate(im,60,30,20);
186 color3 = gdImageColorAllocate(im,88,41,26);
187 }
188 if(strcmp(GraphDaysBytesBarColor,"red") == 0){
189 color1 = gdImageColorAllocate(im,185,10,10);
190 color2 = gdImageColorAllocate(im,111,10,10);
191 color3 = gdImageColorAllocate(im,166,10,10);
192 }
193
194 blue = gdImageColorAllocate(im, 0, 0, 255);
195 white = gdImageColorAllocate(im, 255, 255, 255);
196 dimgray = gdImageColorAllocate(im, 105, 105, 105);
197 goldenrod = gdImageColorAllocate(im, 234, 234, 174);
198 goldenrod2 = gdImageColorAllocate(im, 207, 181, 59);
199
200 num = n;
201 val = 55;
202 for(x=0; x<=366; x++) {
203 if(limits[x] >= num) {
204 val = 425 - x;
205 break;
206 }
207 }
208
209 gdImageFilledRectangle(im, x1, val, x1+11, 425, color3);
210
211 points[0].x = x1+7;
212 points[0].y = val-5;
213 points[1].x = x1;
214 points[1].y = val;
215 points[2].x = x1+11;
216 points[2].y = val;
217 points[3].x = x1+17;
218 points[3].y = val-5;
219 gdImageFilledPolygon(im, points, 4, color1);
220
221 gdImageLine(im, x1+8, val-2, x1+8, val-10, dimgray);
222 gdImageFilledRectangle(im, x1-2, val-20, x1+18, val-10, goldenrod);
223 gdImageRectangle(im, x1-2, val-20, x1+18, val-10, goldenrod2);
224
225 snprintf(v,6,"%s",fixnum(num,0));
226
227 SARGgdImageStringFT(im,&brect[0],black,font1,6,0.0,x1-1,val-12,v);
228
229 points[0].x = x1+17;
230 points[0].y = val-5;
231 points[1].x = x1+11;
232 points[1].y = val;
233 points[2].x = x1+11;
234 points[2].y = 426;
235 points[3].x = x1+17;
236 points[3].y = 420;
237 gdImageFilledPolygon(im, points, 4, color2);
238
239 return;
240 }
241 #endif //HAVE_GD
242
243 void greport_day(const char *user)
244 {
245 #ifdef HAVE_GD
246 FILE *fp_in, *pngout;
247 int x, y;
248 int x1;
249 int brect[8];
250 char wdirname[MAXLEN];
251 char graph[MAXLEN];
252 char buf[MAXLEN];
253 char wuser[255];
254 char csort[255];
255 char data[20];
256 int count=0;
257 int oday=0;
258 int day=0;
259 long long int llday;
260 long long int bytes;
261 char ftime[128];
262 long long int tot=0;
263 time_t t;
264 struct tm *local;
265 int cstatus;
266 char s[15];
267 struct getwordstruct gwarea;
268 struct getwordstruct gwarea1;
269
270 if(!Graphs) {
271 unlink(wdirname);
272 return;
273 }
274
275 im = gdImageCreate(720, 480);
276
277 lavender = gdImageColorAllocate(im, 230, 230, 250);
278 white = gdImageColorAllocate(im, 255, 255, 255);
279 gray = gdImageColorAllocate(im, 192, 192, 192);
280 silver = gdImageColorAllocate(im, 211, 211, 211);
281 black = gdImageColorAllocate(im, 0, 0, 0);
282 blue = gdImageColorAllocate(im, 35, 35, 227);
283 dimgray = gdImageColorAllocate(im, 105, 105, 105);
284 darkblue = gdImageColorAllocate(im, 0, 0, 139);
285
286 gdImageRectangle(im, 0, 0, 719, 479, dimgray);
287 gdImageFilledRectangle(im, 60, 60, 700, 420, silver);
288
289 points[0].x = 50;
290 points[0].y = 65;
291 points[1].x = 50;
292 points[1].y = 425;
293 points[2].x = 60;
294 points[2].y = 420;
295 points[3].x = 60;
296 points[3].y = 60;
297 gdImageFilledPolygon(im, points, 4, gray);
298
299 points[0].x = 60;
300 points[0].y = 420;
301 points[1].x = 50;
302 points[1].y = 425;
303 points[2].x = 690;
304 points[2].y = 425;
305 points[3].x = 700;
306 points[3].y = 420;
307 gdImageFilledPolygon(im, points, 4, gray);
308
309 gdImageLine(im, 50, 65, 50, 430, black);
310 gdImageLine(im, 45, 425, 690, 425, black);
311 gdImageLine(im, 50, 425, 60, 420, black);
312 gdImageLine(im, 60, 420, 60, 60, black);
313 gdImageLine(im, 700, 60, 700, 420, black);
314 gdImageLine(im, 690, 425, 700, 420, black);
315
316 for(x=415; x>=65; x=x-10) {
317 gdImageLine(im, 50, x, 60, x-5, dimgray);
318 gdImageLine(im, 47, x, 50, x, dimgray);
319 }
320
321 for(x=60; x<=420; x=x+10)
322 gdImageLine(im, 60, x, 700, x, dimgray);
323
324 gdImageLine(im, 60, 420, 700, 420, black);
325
326 for(x=70; x<=680; x=x+20)
327 gdImageLine(im, x, 425, x, 428, dimgray);
328
329 y=65;
330 for(x=1; x<=31; x++) {
331 sprintf(s,"%02d",x);
332 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,y,437,s);
333 y=y+20;
334 }
335
336 t = time(NULL);
337 local = localtime(&t);
338 if(strcmp(DateFormat,"u") == 0)
339 strftime(ftime, sizeof(ftime), "%b/%d/%Y %H:%M", local);
340 if(strcmp(DateFormat,"e") == 0)
341 strftime(ftime, sizeof(ftime), "%d/%b/%Y-%H:%M", local);
342
343 if(dotinuser && strchr(name,'_')) {
344 subs(name,sizeof(name),"_",".");
345 }
346
347 SARGgdImageStringFT(im,&brect[0],darkblue,font1,7,0.0,620,470,ftime);
348 if(ShowSargInfo) SARGgdImageStringFT(im,&brect[0],darkblue,font1,10,0.0,257,15,"SARG, ");
349 SARGgdImageStringFT(im,&brect[0],darkblue,font1,10,0.0,300,15,Title);
350 sprintf(warea,"%s: %s",text[89],period);
351 SARGgdImageStringFT(im,&brect[0],darkblue,font1,9,0.0,300,27,warea);
352 sprintf(warea,"%s: %s",text[90],name);
353 SARGgdImageStringFT(im,&brect[0],darkblue,font1,9,0.0,300,38,warea);
354 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,418," 50K");
355 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,408,"250K");
356 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,398,"500K");
357 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,388," 1M");
358 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,378," 2M");
359 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,368," 3M");
360 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,358," 4M");
361 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,348," 5M");
362 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,338," 6M");
363 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,328," 7M");
364 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,318," 8M");
365 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,308," 9M");
366 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,298," 10M");
367 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,288," 15M");
368 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,278," 20M");
369 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,268," 30M");
370 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,258," 40M");
371 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,248," 50M");
372 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,238," 60M");
373 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,228," 70M");
374 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,218," 80M");
375 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,208," 90M");
376 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,198,"100M");
377 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,188,"200M");
378 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,178,"300M");
379 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,168,"400M");
380 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,158,"500M");
381 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,148,"600M");
382 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,138,"700M");
383 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,128,"800M");
384 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,118,"900M");
385 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23,108," 1G");
386 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23, 98," 2G");
387 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23, 88," 3G");
388 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23, 78," 4G");
389 SARGgdImageStringFT(im,&brect[0],dimgray,font1,7,0.0,23, 68," 5G");
390 if(strcmp(datetimeby,"bytes") == 0)
391 SARGgdImageStringFT(im,&brect[0],black,font1,10,3.14/2,20,248,text[93]);
392 else
393 SARGgdImageStringFT(im,&brect[0],black,font1,10,3.14/2,20,248,text[94]);
394 SARGgdImageStringFT(im,&brect[0],black,font1,10,0.0,330,460,text[127]);
395
396 if (snprintf(graph,sizeof(graph),"%s/%s/graph_day.png",dirname,user)>=sizeof(graph)) {
397 fprintf(stderr, "SARG: user name too long for: %s/%s/graph_day.png\n",dirname,user);
398 exit(1);
399 }
400 if (snprintf(wdirname,sizeof(wdirname),"%s/%s.day",tmp,user)>=sizeof(wdirname)) {
401 fprintf(stderr, "SARG: user name too long for: %s/%s.day\n",tmp,user);
402 exit(1);
403 }
404 if (snprintf(tmp5,sizeof(tmp5),"%s/%s.graph",tmp,user)>=sizeof(tmp5)) {
405 fprintf(stderr, "SARG: user name too long for: %s/%s.graph\n",tmp,user);
406 exit(1);
407 }
408
409 if(access(wdirname, R_OK) != 0) {
410 gdImageDestroy(im);
411 return;
412 }
413
414 if(strcmp(DateFormat,"e") == 0)
415 sprintf(csort,"sort -t\"/\" -k 1,1 -o \"%s\" \"%s\"",tmp5,wdirname);
416 else
417 sprintf(csort,"sort -t\"/\" -k 2,2 -o \"%s\" \"%s\"",tmp5,wdirname);
418
419 cstatus=system(csort);
420 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
421 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
422 fprintf(stderr, "SARG: sort command: %s\n",csort);
423 exit(1);
424 }
425
426 if((fp_in=fopen(tmp5,"r"))==NULL) {
427 fprintf(stderr, "SARG: (grepday) %s: %s\n",text[8],tmp5);
428 exit(1);
429 }
430
431 if((pngout=fopen(graph,"wb"))==NULL) {
432 fprintf(stderr, "SARG: (grepday) %s: %s\n",text[8],graph);
433 exit(1);
434 }
435
436 strcpy(wuser,user);
437 if(strstr(wuser,"_") != 0)
438 fixip(wuser);
439
440 if(Ip2Name)
441 if((str=(char *) strstr(name, ".")) != (char *) NULL) {
442 if((str=(char *) strstr(str+1, ".")) != (char *) NULL)
443 ip2name(wuser,sizeof(wuser));
444 }
445
446 // get_usertab_name(wuser,name,sizeof(name));
447 user_find(name, wuser);
448
449 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
450 fixendofline(buf);
451 getword_start(&gwarea,buf);
452 if (getword(data,sizeof(data),&gwarea,'\t')<0) {
453 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp5);
454 exit(1);
455 }
456 getword_start(&gwarea1,data);
457 if (getword_atoll(&llday,&gwarea1,'/')<0) {
458 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp5);
459 exit(1);
460 }
461 if(strcmp(DateFormat,"u") == 0) {
462 if (getword_atoll(&llday,&gwarea1,'/')<0) {
463 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp5);
464 exit(1);
465 }
466 }
467 day=(int)llday;
468 if(!count) {
469 oday=day;
470 count++;
471 }
472 if (getword_skip(20,&gwarea,'\t')<0 || getword_atoll(&bytes,&gwarea,'\t')<0) {
473 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp5);
474 exit(1);
475 }
476
477 if(oday!=day) {
478 x1 = 44 +(oday * 20);
479 bar(x1,tot);
480 oday=day;
481 tot=0;
482 }
483 tot+=bytes;
484 }
485
486 if(tot) {
487 x1 = 44 +(day * 20);
488 bar(x1,tot);
489 }
490
491 gdImagePng(im, pngout);
492 fclose(pngout);
493 gdImageDestroy(im);
494
495 fclose(fp_in);
496 unlink(wdirname);
497 unlink(tmp5);
498
499 #endif //HAVE_GD
500
501 return;
502 }
503
504 void greport_cleanup(void)
505 {
506 #ifdef HAVE_GD
507 gdFontCacheShutdown();
508 #endif
509 }