]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - topsites.c
Display some debug messages when -z is used to track the progress
[thirdparty/sarg.git] / topsites.c
index 59067636220fec6616c47523c6e2f16e6eb3ae24..0e4f83ab181a722340e2196ccbcdb6d88b0b55e0 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
- *                                                            1998, 2012
+ *                                                            1998, 2013
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
@@ -38,12 +38,13 @@ void topsites(void)
        char *buf;
        char *url;
        char *ourl=NULL;
-       char csort[255];
+       char csort[4096];
        char general[MAXLEN];
        char general2[MAXLEN];
        char general3[MAXLEN];
        char sites[MAXLEN];
        char report[MAXLEN];
+       char ouser[MAX_USER_LEN]="";
        const char *sortf;
        const char *sortt;
        long long int nacc;
@@ -58,6 +59,7 @@ void topsites(void)
        long long int ttnbytes=0;
        long long int ttntime=0;
 #endif
+       int nusers=0;
        int regs=0;
        int cstatus;
        int url_len;
@@ -67,9 +69,11 @@ void topsites(void)
        struct generalitemstruct item;
 
        if(Privacy) {
-               if (debugz) debugaz(_("Top sites report not produced because privacy option is on\n"));
+               if (debugz>=LogLevel_Process) debugaz(_("Top sites report not produced because privacy option is on\n"));
                return;
        }
+       if (debugz>=LogLevel_Process)
+               debuga(_("Creating top sites report...\n"));
 
        sprintf(general,"%s/sarg-general",outdirname);
        sprintf(sites,"%s/sarg-sites",outdirname);
@@ -78,7 +82,7 @@ void topsites(void)
 
        sprintf(report,"%s/topsites.html",outdirname);
 
-       if (snprintf(csort,sizeof(csort),"sort -t \"\t\" -k 4,4 -o \"%s\" \"%s\"",general2,general)>=sizeof(csort)) {
+       if (snprintf(csort,sizeof(csort),"sort -t \"\t\" -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2,general)>=sizeof(csort)) {
                debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general,general2);
                exit(EXIT_FAILURE);
        }
@@ -90,13 +94,13 @@ void topsites(void)
        }
 
        if((fp_in=fopen(general2,"r"))==NULL) {
-               debuga(_("(topsites) Cannot open log file %s\n"),general2);
+               debuga(_("(topsites) Cannot open log file %s: %s\n"),general2,strerror(errno));
                debuga(_("sort command: %s\n"),csort);
                exit(EXIT_FAILURE);
        }
 
        if((fp_ou=fopen(general3,"w"))==NULL) {
-               debuga(_("(topsites) Cannot open log file %s\n"),general3);
+               debuga(_("(topsites) Cannot open log file %s: %s\n"),general3,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -128,7 +132,7 @@ void topsites(void)
                        This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
                        to print a long long int unless it is exactly 64-bits long.
                        */
-                       fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,ourl);
+                       fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%d\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,nusers,ourl);
                        url_len=strlen(item.url);
                        if (url_len>=ourl_size) {
                                ourl_size=url_len+1;
@@ -139,9 +143,14 @@ void topsites(void)
                                }
                        }
                        strcpy(ourl,item.url);
+                       strcpy(ouser,item.user);
                        tnacc=0;
                        tnbytes=0;
                        tntime=0;
+                       nusers=1;
+               } else if (strcmp(item.user,ouser)!=0) {
+                       strcpy(ouser,item.user);
+                       nusers++;
                }
 
                tnacc+=item.nacc;
@@ -161,11 +170,14 @@ void topsites(void)
                This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable
                to print a long long int unless it is exactly 64-bits long.
                */
-               fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,ourl);
+               fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%d\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,nusers,ourl);
                free(ourl);
        }
 
-       fclose(fp_ou);
+       if (fclose(fp_ou)==EOF) {
+               debuga(_("Write error in %s: %s\n"),general3,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
 #ifdef ENABLE_DOUBLE_CHECK_DATA
        if (ttnacc!=globstat.nacc || ttnbytes!=globstat.nbytes || ttntime!=globstat.elap) {
@@ -174,8 +186,8 @@ void topsites(void)
        }
 #endif
 
-       if (unlink(general2)) {
-               debuga(_("Cannot delete %s - %s\n"),general2,strerror(errno));
+       if (!KeepTempLog && unlink(general2)) {
+               debuga(_("Cannot delete \"%s\": %s\n"),general2,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -185,6 +197,8 @@ void topsites(void)
                sortf="-k 2,2 -k 1,1";
        } else if((TopsitesSort & TOPSITE_SORT_TIME) != 0) {
                sortf="-k 3,3";
+       } else if((TopsitesSort & TOPSITE_SORT_USER) != 0) {
+               sortf="-k 4,4 -k 1,1 -k 2,2";
        } else {
                sortf="-k 2,2 -k 1,1"; //default is BYTES
        }
@@ -205,18 +219,18 @@ void topsites(void)
                exit(EXIT_FAILURE);
        }
        if((fp_in=fopen(sites,"r"))==NULL) {
-               debuga(_("(topsites) Cannot open log file %s\n"),sites);
+               debuga(_("(topsites) Cannot open log file %s: %s\n"),sites,strerror(errno));
                debuga(_("sort command: %s\n"),csort);
                exit(EXIT_FAILURE);
        }
 
-       if (unlink(general3)) {
-               debuga(_("Cannot delete %s - %s\n"),general3,strerror(errno));
+       if (!KeepTempLog && unlink(general3)) {
+               debuga(_("Cannot delete \"%s\": %s\n"),general3,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
        if((fp_ou=fopen(report,"w"))==NULL) {
-               debuga(_("(topsites) Cannot open log file %s\n"),report);
+               debuga(_("(topsites) Cannot open log file %s: %s\n"),report,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -232,9 +246,21 @@ void topsites(void)
        fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\"",fp_ou);
        if (SortTableJs[0]) fputs(" class=\"sortable\"",fp_ou);
        fputs(">\n",fp_ou);
-       fprintf(fp_ou,"<thead><tr><th class=\"header_l\">%s</th><th class=\"header_l",_("NUM"));
+       fprintf(fp_ou,"<thead><tr><th class=\"header_l\">%s</th><th class=\"header_l",
+       /* TRANSLATORS: This is a column header showing the position of the entry in the sorted list. */
+       _("NUM"));
        if (SortTableJs[0]) fputs(" sorttable_alpha",fp_ou);
-       fprintf(fp_ou,"\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr></thead>\n",_("ACCESSED SITE"),_("CONNECT"),_("BYTES"),_("TIME"));
+       fprintf(fp_ou,"\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr></thead>\n",
+       /* TRANSLATORS: This is a column header showing the URL of the visited sites. */
+       _("ACCESSED SITE"),
+       /* TRANSLATORS: This is a column header showing the number of connections to a visited site. */
+       _("CONNECT"),
+       /* TRANSLATORS: This is a column header showing the number of transfered bytes. */
+       _("BYTES"),
+       /* TRANSLATORS: This is a column header showing the time spent by the proxy processing the requests. */
+       pgettext("duration","TIME"),
+       /* TRANSLATORS: This is a column header showing the number of users who visited a sites. */
+       _("USERS"));
 
        regs=0;
        ntopsites = 0;
@@ -255,6 +281,10 @@ void topsites(void)
                        debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites);
                        exit(EXIT_FAILURE);
                }
+               if (getword_atoi(&nusers,&gwarea,'\t')<0) {
+                       debuga(_("The number of users is invalid in file %s\n"),sites);
+                       exit(EXIT_FAILURE);
+               }
                if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
                        debuga(_("The url is invalid in file %s\n"),sites);
                        exit(EXIT_FAILURE);
@@ -281,7 +311,10 @@ void topsites(void)
                fprintf(fp_ou,">%s</td>",fixnum(twork2,1));
                fputs("<td class=\"data\"",fp_ou);
                if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(uint64_t)twork3);
-               fprintf(fp_ou,">%s</td></tr>\n",fixtime(twork3));
+               fprintf(fp_ou,">%s</td>",fixtime(twork3));
+               fputs("<td class=\"data\"",fp_ou);
+               if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%d\"",nusers);
+               fprintf(fp_ou,">%s</td></tr>\n",fixnum(nusers,1));
        }
        fclose(fp_in);
        longline_destroy(&line);
@@ -289,8 +322,10 @@ void topsites(void)
        fputs("</table></div>\n",fp_ou);
        if (write_html_trailer(fp_ou)<0)
                debuga(_("Write error in file %s\n"),report);
-       if (fclose(fp_ou)==EOF)
-               debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
+       if (fclose(fp_ou)==EOF) {
+               debuga(_("Write error in %s: %s\n"),report,strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
        return;
 }