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