]> git.ipfire.org Git - thirdparty/sarg.git/blob - log.c
Display locale information for debugging purpose
[thirdparty/sarg.git] / log.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2013
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 #include "include/readlog.h"
30 #include "include/filelist.h"
31
32 #ifdef HAVE_GETOPT_H
33 #include <getopt.h>
34 #endif
35
36 //! The log file filtering.
37 struct ReadLogDataStruct ReadFilter;
38
39 //! The list of the system users.
40 /*@null@*/char *userfile=NULL;
41
42 //! List of the input log files to process.
43 FileListObject AccessLog=NULL;
44
45
46 //! Selected locale set through the environment variable.
47 char *CurrentLocale=NULL;
48
49 static void getusers(const char *pwdfile, int debug);
50
51 int main(int argc,char *argv[])
52 {
53 extern int optind;
54 extern int optopt;
55 extern char *optarg;
56
57 char hm_str[15];
58 char uagent[MAXLEN];
59 char hexclude[MAXLEN];
60 char splitprefix[MAXLEN];
61 int ch;
62 int errflg=0;
63 bool dns=false;
64 int iarq=0;
65 int lastlog=-1;
66 int LogStatus;
67 bool realt;
68 bool userip;
69 time_t start_time;
70 time_t end_time;
71 time_t read_start_time;
72 time_t read_end_time;
73 time_t process_start_time;
74 time_t process_end_time;
75 double read_elapsed;
76 double process_elapsed;
77 FileListIterator FIter;
78 static int split=0;
79 static int convert=0;
80 static int output_css=0;
81 static int show_statis=0;
82 int option_index;
83 static struct option long_options[]=
84 {
85 {"convert",no_argument,&convert,1},
86 {"css",no_argument,&output_css,1},
87 {"help",no_argument,NULL,'h'},
88 {"lastlog",required_argument,NULL,2},
89 {"keeplogs",no_argument,NULL,3},
90 {"split",no_argument,&split,1},
91 {"splitprefix",required_argument,NULL,'P'},
92 {"statistics",no_argument,&show_statis,1},
93 {0,0,0,0}
94 };
95
96 start_time=time(NULL);
97
98 #ifdef HAVE_LOCALE_H
99 setlocale(LC_TIME,"");
100 #endif
101
102 #if defined(ENABLE_NLS) && defined(HAVE_LOCALE_H)
103 CurrentLocale=setlocale (LC_ALL, "");
104 if (!CurrentLocale) {
105 fprintf(stderr,"SARG: Cannot set the locale LC_ALL to the environment variable\n");
106 exit(EXIT_FAILURE);
107 }
108 if (!bindtextdomain (PACKAGE_NAME, LOCALEDIR)) {
109 fprintf(stderr,"SARG: Cannot bind to text domain %s in directory %s (%s)\n",PACKAGE_NAME,LOCALEDIR,strerror(errno));
110 exit(EXIT_FAILURE);
111 }
112 if (!textdomain (PACKAGE_NAME)) {
113 fprintf(stderr,"SARG: Cannot set gettext domain for %s PACKAGE_NAME (%s)\n",PACKAGE_NAME,strerror(errno));
114 exit(EXIT_FAILURE);
115 }
116 #endif //ENABLE_NLS
117
118 BgImage[0]='\0';
119 LogoImage[0]='\0';
120 LogoText[0]='\0';
121 PasswdFile[0]='\0';
122 OutputEmail[0]='\0';
123 UserAgentLog[0]='\0';
124 ExcludeHosts[0]='\0';
125 ExcludeUsers[0]='\0';
126 ConfigFile[0]='\0';
127 code[0]='\0';
128 LastLog=0;
129 ReportType=0UL;
130 UserTabFile[0]='\0';
131 BlockIt[0]='\0';
132 ExternalCSSFile[0]='\0';
133 RedirectorLogFormat[0]='\0';
134 NRedirectorLogs=0;
135
136 snprintf(ExcludeCodes,sizeof(ExcludeCodes),"%s/exclude_codes",SYSCONFDIR);
137 strcpy(GraphDaysBytesBarColor,"orange");
138 strcpy(BgColor,"#ffffff");
139 strcpy(TxColor,"#000000");
140 strcpy(TxBgColor,"lavender");
141 strcpy(TiColor,"darkblue");
142 strcpy(Width,"80");
143 strcpy(Height,"45");
144 strcpy(LogoTextColor,"#000000");
145 strcpy(HeaderColor,"darkblue");
146 strcpy(HeaderBgColor,"#dddddd");
147 strcpy(LogoTextColor,"#006699");
148 strcpy(FontSize,"9px");
149 strcpy(TempDir,"/tmp");
150 strcpy(OutputDir,"/var/www/html/squid-reports");
151 AnonymousOutputFiles=false;
152 Ip2Name=false;
153 DateFormat='u';
154 OverwriteReport=false;
155 RemoveTempFiles=true;
156 strcpy(ReplaceIndex,INDEX_HTML_FILE);
157 Index=INDEX_YES;
158 RecordsWithoutUser=RECORDWITHOUTUSER_IP;
159 UseComma=0;
160 strcpy(MailUtility,"mailx");
161 TopSitesNum=100;
162 TopUsersNum=0;
163 UserIp=0;
164 TopuserSort=TOPUSER_SORT_BYTES | TOPUSER_SORT_REVERSE;
165 UserSort=USER_SORT_BYTES | USER_SORT_REVERSE;
166 TopsitesSort=TOPSITE_SORT_CONNECT | TOPSITE_SORT_REVERSE;
167 LongUrl=0;
168 strcpy(FontFace,"Verdana,Tahoma,Arial");
169 datetimeby=DATETIME_BYTE;
170 strcpy(CharSet,"ISO-8859-1");
171 Privacy=0;
172 strcpy(PrivacyString,"***.***.***.***");
173 strcpy(PrivacyStringColor,"blue");
174 SuccessfulMsg=true;
175 TopUserFields=TOPUSERFIELDS_NUM | TOPUSERFIELDS_DATE_TIME | TOPUSERFIELDS_USERID | TOPUSERFIELDS_CONNECT |
176 TOPUSERFIELDS_BYTES | TOPUSERFIELDS_SETYB | TOPUSERFIELDS_IN_CACHE_OUT |
177 TOPUSERFIELDS_USED_TIME | TOPUSERFIELDS_MILISEC | TOPUSERFIELDS_PTIME |
178 TOPUSERFIELDS_TOTAL | TOPUSERFIELDS_AVERAGE;
179 UserReportFields=USERREPORTFIELDS_CONNECT | USERREPORTFIELDS_BYTES | USERREPORTFIELDS_SETYB |
180 USERREPORTFIELDS_IN_CACHE_OUT | USERREPORTFIELDS_USED_TIME | USERREPORTFIELDS_MILISEC |
181 USERREPORTFIELDS_PTIME | USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_AVERAGE;
182 strcpy(DataFileDelimiter,";");
183 DataFileFields=DATA_FIELD_USER | DATA_FIELD_DATE | DATA_FIELD_TIME | DATA_FIELD_URL | DATA_FIELD_CONNECT |
184 DATA_FIELD_BYTES | DATA_FIELD_IN_CACHE | DATA_FIELD_OUT_CACHE | DATA_FIELD_ELAPSED;
185 ShowReadStatistics=true;
186 ShowReadPercent=false;
187 strcpy(IndexSortOrder,"D");
188 ShowSargInfo=true;
189 ShowSargLogo=true;
190 ParsedOutputLog[0]='\0';
191 strcpy(ParsedOutputLogCompress,"/bin/gzip -f");
192 DisplayedValues=DISPLAY_ABBREV;
193 strcpy(HeaderFontSize,"9px");
194 strcpy(TitleFontSize,"11px");
195 strcpy(AuthUserTemplateFile,"sarg_htaccess");
196 set_download_suffix("7z,ace,arj,avi,bat,bin,bz2,bzip,cab,com,cpio,dll,doc,dot,exe,gz,iso,lha,lzh,mdb,mov,mp3,mpeg,mpg,mso,nrg,ogg,ppt,rar,rtf,shs,src,sys,tar,tgz,vcd,vob,wma,wmv,zip");
197 Graphs=true;
198 #if defined(FONTDIR)
199 strcpy(GraphFont,FONTDIR"/DejaVuSans.ttf");
200 #else
201 GraphFont[0]='\0';
202 #endif
203 strcpy(Ulimit,"20000");
204 NtlmUserFormat=NTLMUSERFORMAT_DOMAINUSER;
205 IndexTree=INDEX_TREE_FILE;
206 IndexFields=INDEXFIELDS_DIRSIZE;
207 strcpy(RealtimeTypes,"GET,PUT,CONNECT");
208 RealtimeUnauthRec=REALTIME_UNAUTH_REC_SHOW;
209 RedirectorFilterOutDate=true;
210 DansguardianFilterOutDate=true;
211 DataFileUrl=DATAFILEURL_IP;
212 strcpy(MaxElapsed,"28800000");
213 BytesInSitesUsersReport=0;
214 UserAuthentication=0;
215 strcpy(LDAPHost,"127.0.0.1");
216 LDAPPort=389;
217 LDAPProtocolVersion=3;
218 LDAPBindDN[0]='\0';
219 LDAPBindPW[0]='\0';
220 LDAPBaseSearch[0]='\0';
221 strcpy(LDAPFilterSearch, "(uid=%s)");
222 strcpy(LDAPTargetAttr, "cn");
223 LDAPNativeCharset[0]='\0';
224 SortTableJs[0]='\0';
225
226 tmp[0]='\0';
227 us[0]='\0';
228 ReadFilter.DateRange[0]='\0';
229 df='\0';
230 uagent[0]='\0';
231 hexclude[0]='\0';
232 addr[0]='\0';
233 ReadFilter.StartTime=-1;
234 ReadFilter.EndTime=-1;
235 site[0]='\0';
236 outdir[0]='\0';
237 splitprefix[0]='\0';
238 email[0]='\0';
239 UserInvalidChar[0]='\0';
240 DataFile[0]='\0';
241 SquidGuardConf[0]='\0';
242 DansGuardianConf[0]='\0';
243 hm_str[0]='\0';
244 HostAliasFile[0]='\0';
245 UserAliasFile[0]='\0';
246
247 dansguardian_count=0;
248 redirector_count=0;
249 useragent_count=0;
250 DeniedReportLimit=10;
251 AuthfailReportLimit=10;
252 DansGuardianReportLimit=10;
253 SquidGuardReportLimit=10;
254 DownloadReportLimit=50;
255 UserReportLimit=0;
256 debug=0;
257 debugz=0;
258 debugm=0;
259 iprel=false;
260 userip=false;
261 realt=false;
262 realtime_refresh=3;
263 realtime_access_log_lines=1000;
264 cost=0.01;
265 nocost=50000000;
266 squid24=false;
267 dfrom=0;
268 duntil=0;
269 KeepTempLog=false;
270 NumLogSuccessiveErrors=3;
271 NumLogTotalErrors=50;
272 lines_read=0UL;
273 records_kept=0UL;
274 nusers=0UL;
275
276 bzero(IncludeUsers, sizeof(IncludeUsers));
277 bzero(ExcludeString, sizeof(ExcludeString));
278 memset(&period,0,sizeof(period));
279
280 AccessLogFromCmdLine=0;
281 RedirectorLogFromCmdLine=0;
282
283 strcpy(Title,_("Squid User Access Report"));
284
285 while((ch = getopt_long(argc, argv, "a:b:c:d:e:f:g:hikl:L:mno:P:prs:t:u:vw:xyz",long_options,&option_index)) != -1){
286 switch(ch)
287 {
288 case 0:
289 break;
290 case 2:
291 lastlog=atoi(optarg);
292 break;
293 case 3:
294 lastlog=0;
295 break;
296 case 'a':
297 safe_strcpy(addr,optarg,sizeof(addr));
298 break;
299 case 'b': //unused option
300 safe_strcpy(uagent,optarg,sizeof(uagent));
301 break;
302 case 'c':
303 safe_strcpy(hexclude,optarg,sizeof(hexclude));
304 break;
305 case 'd':
306 safe_strcpy(ReadFilter.DateRange,optarg,sizeof(ReadFilter.DateRange));
307 date_from(ReadFilter.DateRange,sizeof(ReadFilter.DateRange), &dfrom, &duntil);
308 break;
309 case 'e':
310 safe_strcpy(email,optarg,sizeof(email));
311 break;
312 case 'f':
313 safe_strcpy(ConfigFile,optarg,sizeof(ConfigFile));
314 break;
315 case 'g':
316 df=*optarg;
317 break;
318 case 'h':
319 usage(argv[0]);
320 exit(EXIT_SUCCESS);
321 case 'i':
322 iprel=true;
323 break;
324 case 'k':
325 KeepTempLog=true;
326 break;
327 case 'l':
328 if (!AccessLog)
329 AccessLog=FileList_Create();
330 if (!FileList_AddFile(AccessLog,optarg)) {
331 debuga(_("Not enough memory to store the input log file names\n"));
332 exit(EXIT_FAILURE);
333 }
334 AccessLogFromCmdLine++;
335 break;
336 case 'L':
337 if (NRedirectorLogs>MAX_REDIRECTOR_LOGS) {
338 debuga(_("Too many redirector logs passed on command line with option -L.\n"));
339 exit(EXIT_FAILURE);
340 }
341 if (strlen(optarg)>=MAX_REDIRECTOR_FILELEN) {
342 debuga(_("Redirector log file name too long passed on command line with opton -L: %s\n"),optarg);
343 exit(EXIT_FAILURE);
344 }
345 strcpy(RedirectorLogs[NRedirectorLogs],optarg);
346 NRedirectorLogs++;
347 RedirectorLogFromCmdLine++;
348 break;
349 case 'm':
350 debugm++;
351 break;
352 case 'n':
353 dns=true;
354 break;
355 case 'o':
356 safe_strcpy(outdir,optarg,sizeof(outdir));
357 break;
358 case 'p':
359 userip=true;
360 break;
361 case 'P':
362 safe_strcpy(splitprefix,optarg,sizeof(splitprefix));
363 break;
364 case 'r':
365 realt=true;
366 break;
367 case 's':
368 safe_strcpy(site,optarg,sizeof(site));
369 break;
370 case 't':
371 {
372 int h1,m1,h2,m2;
373
374 if(strstr(optarg,"-") == 0) {
375 if(sscanf(optarg,"%d:%d",&h1,&m1)!=2) {
376 debuga(_("Time period passed on the command line with option -t must be HH:MM\n"));
377 exit(EXIT_FAILURE);
378 }
379 ReadFilter.StartTime=h1*100+m1;
380 ReadFilter.EndTime=ReadFilter.StartTime;
381 snprintf(hm_str,sizeof(hm_str),"%02d:%02d",h1,m1);
382 } else {
383 if(sscanf(optarg,"%d:%d-%d:%d",&h1,&m1,&h2,&m2)!=4) {
384 debuga(_("Time range passed on the command line with option -t must be HH:MM-HH:MM\n"));
385 exit(EXIT_FAILURE);
386 }
387 ReadFilter.StartTime=h1*100+m1;
388 ReadFilter.EndTime=h2*100+m2;
389 snprintf(hm_str,sizeof(hm_str),"%02d:%02d-%02d:%02d",h1,m1,h2,m2);
390 }
391 break;
392 }
393 case 'u':
394 safe_strcpy(us,optarg,sizeof(us));
395 break;
396 case 'v':
397 version();
398 break;
399 case 'w':
400 safe_strcpy(tmp,optarg,sizeof(tmp));
401 break;
402 case 'x':
403 debug++;
404 break;
405 case 'y': //unused option
406 langcode++;
407 break;
408 case 'z':
409 debugz++;
410 break;
411 case ':':
412 debuga(_("Option -%c requires an argument\n"),optopt);
413 exit(EXIT_FAILURE);
414 case '?':
415 usage(argv[0]);
416 exit(EXIT_FAILURE);
417 default:
418 abort();
419 }
420 }
421
422 if (errflg>0) {
423 usage(argv[0]);
424 exit(2);
425 }
426
427 if(output_css) {
428 css_content(stdout);
429 exit(EXIT_SUCCESS);
430 }
431
432 if (optind<argc) {
433 if (!AccessLog)
434 AccessLog=FileList_Create();
435 for (iarq=optind ; iarq<argc ; iarq++) {
436 if (!FileList_AddFile(AccessLog,argv[iarq])) {
437 debuga(_("Not enough memory to store the input log file names\n"));
438 exit(EXIT_FAILURE);
439 }
440 AccessLogFromCmdLine++;
441 }
442 }
443
444 if(debug) debuga(_("Init\n"));
445
446 if(ConfigFile[0] == '\0') snprintf(ConfigFile,sizeof(ConfigFile),"%s/sarg.conf",SYSCONFDIR);
447 if(access(ConfigFile, R_OK) != 0) {
448 debuga(_("Cannot open config file: %s - %s\n"),ConfigFile,strerror(errno));
449 exit(EXIT_FAILURE);
450 }
451
452 if(access(ConfigFile, R_OK) == 0)
453 getconf();
454
455 if(userip) UserIp=true;
456
457 if(dns) ip2name_forcedns();
458
459 if (lastlog>=0) LastLog=lastlog;
460
461 if(outdir[0] == '\0') strcpy(outdir,OutputDir);
462 if(outdir[0] != '\0') strcat(outdir,"/");
463
464 if(realt) {
465 realtime();
466 exit(EXIT_SUCCESS);
467 }
468
469 if(IndexTree == INDEX_TREE_FILE)
470 strcpy(ImageFile,"../images");
471 else
472 strcpy(ImageFile,"../../../images");
473
474 dataonly=(DataFile[0] != '\0');
475
476 if (df=='\0') df=DateFormat;
477 if (df=='\0') df='u';
478 if (df=='w')
479 IndexTree=INDEX_TREE_FILE;
480
481 if(AccessLog==NULL) {
482 AccessLog=FileList_Create();
483 if (!FileList_AddFile(AccessLog,"/var/log/squid/access.log")) {
484 debuga(_("Not enough memory to store the input log file names\n"));
485 exit(EXIT_FAILURE);
486 }
487 }
488
489 if(split) {
490 const char *file;
491
492 FIter=FileListIter_Open(AccessLog);
493 while ((file=FileListIter_Next(FIter))!=NULL)
494 splitlog(file, df, dfrom, duntil, convert, splitprefix);
495 FileListIter_Close(FIter);
496 exit(EXIT_SUCCESS);
497 }
498 if(convert) {
499 const char *file;
500
501 FIter=FileListIter_Open(AccessLog);
502 while ((file=FileListIter_Next(FIter))!=NULL)
503 convlog(file, df, dfrom, duntil);
504 FileListIter_Close(FIter);
505 exit(EXIT_SUCCESS);
506 }
507
508 load_excludecodes(ExcludeCodes);
509
510 if(access(PasswdFile, R_OK) == 0) {
511 getusers(PasswdFile,debug);
512 ReadFilter.SysUsers=true;
513 } else {
514 ReadFilter.SysUsers=false;
515 }
516
517 if(hexclude[0] == '\0')
518 strcpy(hexclude,ExcludeHosts);
519 if(hexclude[0] != '\0') {
520 gethexclude(hexclude,debug);
521 ReadFilter.HostFilter=true;
522 } else {
523 ReadFilter.HostFilter=false;
524 }
525
526 if(ReportType == 0) {
527 ReportType=REPORT_TYPE_TOPUSERS | REPORT_TYPE_TOPSITES | REPORT_TYPE_USERS_SITES |
528 REPORT_TYPE_SITES_USERS | REPORT_TYPE_DATE_TIME | REPORT_TYPE_DENIED |
529 REPORT_TYPE_AUTH_FAILURES | REPORT_TYPE_SITE_USER_TIME_DATE | REPORT_TYPE_DOWNLOADS;
530 }
531
532 if(access(ExcludeUsers, R_OK) == 0) {
533 getuexclude(ExcludeUsers,debug);
534 ReadFilter.UserFilter=true;
535 } else {
536 ReadFilter.UserFilter=false;
537 }
538 if (HostAliasFile[0] != '\0')
539 read_hostalias(HostAliasFile);
540 if (UserAliasFile[0] != '\0')
541 read_useralias(UserAliasFile);
542
543 indexonly=false;
544 if(ReadFilter.UserFilter) {
545 if(is_indexonly())
546 indexonly=true;
547 }
548 if(strcmp(ExcludeUsers,"indexonly") == 0) indexonly=true;
549 if(Index == INDEX_ONLY) indexonly=true;
550
551 if(MaxElapsed[0] != '\0')
552 ReadFilter.max_elapsed=atol(MaxElapsed);
553 else
554 ReadFilter.max_elapsed=0;
555
556 if(uagent[0] == '\0') strcpy(uagent,UserAgentLog);
557
558 if(tmp[0] == '\0') strcpy(tmp,TempDir);
559 else strcpy(TempDir,tmp);
560 /*
561 For historical reasons, the temporary directory is the subdirectory "sarg" of the path
562 provided by the user.
563 */
564 strcat(tmp,"/sarg");
565
566 if (tmp[0]!='\0' && strncmp(outdir,tmp,strlen(tmp))==0) {
567 debuga(_("The output directory \"%s\" must be outside of the temporary directory \"%s\"\n"),outdir,tmp);
568 exit(EXIT_FAILURE);
569 }
570
571 if(email[0] == '\0' && OutputEmail[0] != '\0') strcpy(email,OutputEmail);
572
573 if(email[0] != '\0') {
574 my_mkdir(tmp);
575 strcpy(outdir,tmp);
576 strcat(outdir,"/");
577 }
578
579 if(access(tmp, R_OK) == 0) {
580 if (debug) debuga(_("Deleting temporary directory \"%s\"\n"),tmp);
581 emptytmpdir(tmp);
582 }
583 my_mkdir(tmp);
584
585 if(debug) {
586 const char *file;
587
588 debuga(_("Parameters:\n"));
589 debuga(_(" Hostname or IP address (-a) = %s\n"),addr);
590 debuga(_(" Useragent log (-b) = %s\n"),uagent);
591 debuga(_(" Exclude file (-c) = %s\n"),hexclude);
592 debuga(_(" Date from-until (-d) = %s\n"),ReadFilter.DateRange);
593 debuga(_(" Email address to send reports (-e) = %s\n"),email);
594 debuga(_(" Config file (-f) = %s\n"),ConfigFile);
595 if (df=='e')
596 debuga(_(" Date format (-g) = Europe (dd/mm/yyyy)\n"));
597 else if (df=='u')
598 debuga(_(" Date format (-g) = USA (mm/dd/yyyy)\n"));
599 else if (df=='w')
600 debuga(_(" Date format (-g) = Sites & Users (yyyy/ww)\n"));
601 debuga(_(" IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
602 debuga(_(" Keep temporary files (-k) = %s\n"),(KeepTempLog) ? _("Yes") : _("No"));
603 FIter=FileListIter_Open(AccessLog);
604 while ((file=FileListIter_NextWithMask(FIter))!=NULL)
605 debuga(_(" Input log (-l) = %s\n"),file);
606 FileListIter_Close(FIter);
607 for (iarq=0 ; iarq<NRedirectorLogs ; iarq++)
608 debuga(_(" Redirector log (-L) = %s\n"),RedirectorLogs[iarq]);
609 debuga(_(" Resolve IP Address (-n) = %s\n"),(Ip2Name) ? _("Yes") : _("No"));
610 debuga(_(" Output dir (-o) = %s\n"),outdir);
611 debuga(_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp) ? _("Yes") : _("No"));
612 debuga(_(" Accessed site (-s) = %s\n"),site);
613 debuga(_(" Time (-t) = %s\n"),hm_str);
614 debuga(_(" User (-u) = %s\n"),us);
615 debuga(_(" Temporary dir (-w) = %s\n"),tmp);
616 debuga(_(" Debug messages (-x) = %s\n"),(debug) ? _("Yes") : _("No"));
617 debuga(_(" Process messages (-z) = %s\n"),(debugz) ? _("Yes") : _("No"));
618 debuga(_(" Previous reports to keep (--lastlog) = %d\n"),LastLog);
619 debuga("\n");
620 }
621
622 if(debugm) {
623 const char *file;
624 printf(_("Parameters:\n"));
625 printf(_(" Hostname or IP address (-a) = %s\n"),addr);
626 printf(_(" Useragent log (-b) = %s\n"),uagent);
627 printf(_(" Exclude file (-c) = %s\n"),hexclude);
628 printf(_(" Date from-until (-d) = %s\n"),ReadFilter.DateRange);
629 printf(_(" Email address to send reports (-e) = %s\n"),email);
630 printf(_(" Config file (-f) = %s\n"),ConfigFile);
631 if (df=='e')
632 printf(_(" Date format (-g) = Europe (dd/mm/yyyy)\n"));
633 else if (df=='u')
634 printf(_(" Date format (-g) = USA (mm/dd/yyyy)\n"));
635 else if (df=='w')
636 printf(_(" Date format (-g) = Sites & Users (yyyy/ww)\n"));
637 printf(_(" IP report (-i) = %s\n"),(iprel) ? _("Yes") : _("No"));
638 printf(_(" Keep temporary files (-k) = %s\n"),(KeepTempLog) ? _("Yes") : _("No"));
639 FIter=FileListIter_Open(AccessLog);
640 while ((file=FileListIter_NextWithMask(FIter))!=NULL)
641 printf(_(" Input log (-l) = %s\n"),file);
642 FileListIter_Close(FIter);
643 for (iarq=0 ; iarq<NRedirectorLogs ; iarq++)
644 printf(_(" Redirector log (-L) = %s\n"),RedirectorLogs[iarq]);
645 printf(_(" Resolve IP Address (-n) = %s\n"),(Ip2Name) ? _("Yes") : _("No"));
646 printf(_(" Output dir (-o) = %s\n"),outdir);
647 printf(_("Use Ip Address instead of userid (-p) = %s\n"),(UserIp) ? _("Yes") : _("No"));
648 printf(_(" Accessed site (-s) = %s\n"),site);
649 printf(_(" Time (-t) = %s\n"),hm_str);
650 printf(_(" User (-u) = %s\n"),us);
651 printf(_(" Temporary dir (-w) = %s\n"),tmp);
652 printf(_(" Debug messages (-x) = %s\n"),(debug) ? _("Yes") : _("No"));
653 printf(_(" Process messages (-z) = %s\n"),(debugz) ? _("Yes") : _("No"));
654 printf(_(" Previous reports to keep (--lastlog) = %d\n"),LastLog);
655 printf(_("sarg version: %s\n"),VERSION);
656 }
657
658 if(debug)
659 debuga(_("sarg version: %s\n"),VERSION);
660
661 #ifdef ENABLE_DOUBLE_CHECK_DATA
662 debuga(_("Sarg compiled to report warnings if the output is inconsistent\n"));
663 #endif
664
665 #ifdef HAVE_RLIM_T
666 if (Ulimit[0] != '\0') {
667 struct rlimit rl;
668 long l1, l2;
669 int rc=0;
670
671 #if defined(RLIMIT_NOFILE)
672 getrlimit (RLIMIT_NOFILE, &rl);
673 #elif defined(RLIMIT_OFILE)
674 getrlimit (RLIMIT_OFILE, &rl);
675 #else
676 #warning "No rlimit resource for the number of open files"
677 #endif
678 l1 = rl.rlim_cur;
679 l2 = rl.rlim_max;
680
681 rl.rlim_cur = atol(Ulimit);
682 rl.rlim_max = atol(Ulimit);
683 #if defined(RLIMIT_NOFILE)
684 rc=setrlimit (RLIMIT_NOFILE, &rl);
685 #elif defined(RLIMIT_OFILE)
686 rc=setrlimit (RLIMIT_OFILE, &rl);
687 #else
688 #warning "No rlimit resource for the number of open files"
689 #endif
690 if(rc == -1) {
691 debuga(_("setrlimit error - %s\n"),strerror(errno));
692 }
693
694 if(debug)
695 debuga("Maximum file descriptor: cur=%ld max=%ld, changed to cur="RLIM_STRING" max="RLIM_STRING"\n",l1,l2,rl.rlim_cur,rl.rlim_max);
696 }
697 #endif
698
699 init_usertab(UserTabFile);
700
701 read_start_time=time(NULL);
702 LogStatus=ReadLogFile(&ReadFilter);
703 read_end_time=time(NULL);
704 read_elapsed=(double)read_end_time-(double)read_start_time;
705
706 FileList_Destroy(&AccessLog);
707 free_download();
708 free_excludecodes();
709 free_exclude();
710
711 if (debug) {
712 char date0[30], date1[30];
713 struct tm Start,End;
714
715 GetLogPeriod(&Start,&End);
716 strftime(date0,sizeof(date0),"%x",&Start);
717 strftime(date1,sizeof(date1),"%x",&End);
718 // TRANSLATORS: The %s are the start and end dates in locale format.
719 debuga(_("Period covered by log files: %s-%s\n"),date0,date1);
720 }
721
722 if (!LogStatus){
723 debuga(_("No records found\n"));
724 debuga(_("End\n"));
725 userinfo_free();
726 if(userfile) free(userfile);
727 close_usertab();
728 exit(EXIT_SUCCESS);
729 }
730
731 if (debug) {
732 char date0[30], date1[30];
733
734 strftime(date0,sizeof(date0),"%x",&period.start);
735 strftime(date1,sizeof(date1),"%x",&period.end);
736 // TRANSLATORS: The %s are the start and end dates in locale format.
737 debuga(_("Period extracted from log files: %s-%s\n"),date0,date1);
738 getperiod_fromrange(&period,dfrom,duntil);
739 }
740 if (getperiod_buildtext(&period)<0) {
741 debuga(_("Failed to build the string representation of the date range\n"));
742 exit(EXIT_FAILURE);
743 }
744
745 process_start_time=time(NULL);
746 if(DataFile[0] != '\0')
747 data_file(tmp);
748 else
749 gerarel();
750 process_end_time=time(NULL);
751 process_elapsed=(double)process_end_time-(double)process_start_time;
752
753 denied_cleanup();
754 authfail_cleanup();
755 download_cleanup();
756
757 if(!KeepTempLog && strcmp(tmp,"/tmp") != 0) {
758 unlinkdir(tmp,0);
759 }
760
761 ip2name_cleanup();
762 free_hostalias();
763 free_useralias();
764 userinfo_free();
765 if(userfile)
766 free(userfile);
767 close_usertab();
768
769 end_time=time(NULL);
770
771 if (show_statis) {
772 double elapsed=(double)end_time-(double)start_time;
773 debuga(_("Total execution time: %.0lf seconds\n"),elapsed);
774 if (read_elapsed>0.) {
775 debuga(_("Lines read: %lu lines in %.0lf seconds (%.0lf lines/s)\n"),lines_read,read_elapsed,(double)lines_read/read_elapsed);
776 }
777 if (process_elapsed>0.) {
778 debuga(_("Processed records: %lu records in %.0lf seconds (%.0lf records/s)\n"),records_kept,process_elapsed,(double)records_kept/process_elapsed);
779 debuga(_("Users: %lu users in %.0lf seconds (%.0lf users/s)\n"),nusers,process_elapsed,(double)nusers/process_elapsed);
780 }
781 }
782
783 if(debug)
784 debuga(_("End\n"));
785
786 exit(EXIT_SUCCESS);
787 }
788
789
790 static void getusers(const char *pwdfile, int debug)
791 {
792 FILE *fp_usr;
793 char buf[255];
794 char *str;
795 long int nreg=0;
796
797 if(debug)
798 debuga(_("Loading password file from %s\n"),pwdfile);
799
800 if ((fp_usr = fopen(pwdfile, "r")) == NULL) {
801 debuga(_("(getusers) Cannot open file %s - %s\n"),pwdfile,strerror(errno));
802 exit(EXIT_FAILURE);
803 }
804
805 if (fseek(fp_usr, 0, SEEK_END)==-1) {
806 debuga(_("Failed to move till the end of the users file %s: %s\n"),pwdfile,strerror(errno));
807 exit(EXIT_FAILURE);
808 }
809 nreg = ftell(fp_usr);
810 if (nreg<0) {
811 debuga(_("Cannot get the size of file %s\n"),pwdfile);
812 exit(EXIT_FAILURE);
813 }
814 nreg = nreg+5000;
815 if (fseek(fp_usr, 0, SEEK_SET)==-1) {
816 debuga(_("Failed to rewind the users file %s: %s\n"),pwdfile,strerror(errno));
817 exit(EXIT_FAILURE);
818 }
819
820 if((userfile=(char *) malloc(nreg))==NULL){
821 debuga(_("malloc error (%ld)\n"),nreg);
822 exit(EXIT_FAILURE);
823 }
824
825 bzero(userfile,nreg);
826 strcpy(userfile,":");
827
828 while(fgets(buf,sizeof(buf),fp_usr)!=NULL) {
829 str=strchr(buf,':');
830 if (!str) {
831 debuga(_("You have an invalid user in your %s file\n"),pwdfile);
832 exit(EXIT_FAILURE);
833 }
834 str[1]='\0';
835 strcat(userfile,buf);
836 }
837
838 fclose(fp_usr);
839
840 return;
841 }