"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "topuser.c" between
sarg-2.3.11.tar.gz and sarg-2.4.0.tar.gz

About: SARG ia a Squid Analysis Report Generator.

topuser.c  (sarg-2.3.11):topuser.c  (sarg-2.4.0)
/* /*
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
* 1998, 2013 * 1998, 2015
* *
* SARG donations: * SARG donations:
* please look at http://sarg.sourceforge.net/donations.php * please look at http://sarg.sourceforge.net/donations.php
* Support: * Support:
* http://sourceforge.net/projects/sarg/forums/forum/363374 * http://sourceforge.net/projects/sarg/forums/forum/363374
* --------------------------------------------------------------------- * ---------------------------------------------------------------------
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
skipping to change at line 29 skipping to change at line 29
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
* *
*/ */
#include "include/conf.h" #include "include/conf.h"
#include "include/defs.h" #include "include/defs.h"
#include "include/filelist.h"
struct TopUserStatistics
{
long long int ttnbytes;
long long int ttnacc;
long long int ttnelap;
long long int ttnincache;
long long int ttnoucache;
int totuser;
};
struct SortInfoStruct
{
const char *sort_field;
const char *sort_order;
};
extern struct globalstatstruct globstat; extern struct globalstatstruct globstat;
extern bool smartfilter;
/*! /*!
Save the total number of users. The number is written in sarg-users and set Save the total number of users. The number is written in sarg-users and set
in a global variable for further reference. in a global variable for further reference.
\param totuser The total number of users. \param totuser The total number of users.
*/ */
static void set_total_users(int totuser) static void set_total_users(int totuser)
{ {
char tusr[1024]; char tusr[1024];
FILE *fp_ou; FILE *fp_ou;
snprintf(tusr,sizeof(tusr),"%s/sarg-users",outdirname); format_path(__FILE__, __LINE__, tusr, sizeof(tusr), "%s/sarg-users", outd
if((fp_ou=fopen(tusr,"w"))==NULL) { irname);
debugapos("topuser",_("Cannot open file \"%s\": %s\n"),tusr,strer if ((fp_ou=fopen(tusr,"w"))==NULL) {
ror(errno)); debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tusr,
strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
fprintf(fp_ou,"%d\n",totuser); fprintf(fp_ou,"%d\n",totuser);
if (fclose(fp_ou)==EOF) if (fclose(fp_ou)==EOF) {
debuga(_("Failed to close file \"%s\": %s\n"),tusr,strerror(errno debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),tusr,st
)); rerror(errno));
exit(EXIT_FAILURE);
}
globstat.totuser=totuser; globstat.totuser=totuser;
} }
void topuser(void) /*!
* Generate a HTML report with the users downloading the most.
*
* \param ListFile Name of the file with the sorted list of users.
* \param Statis Statistics about the data collected from the log file.
* \param SortInfo Strings explaining how the list was sorted.
*/
static void TopUser_HtmlReport(const char *ListFile,struct TopUserStatistics *St
atis,struct SortInfoStruct *SortInfo)
{ {
FILE *fp_in = NULL, *fp_top1 = NULL, *fp_top2 = NULL, *fp_top3 = NULL; FileObject *fp_top1 = NULL;
long long int ttnbytes=0, ttnacc=0, tnacc=0; FILE *fp_top3 = NULL;
long long int tnbytes=0, ttnelap=0, tnelap=0;
long long int tnincache=0, tnoucache=0, ttnincache=0, ttnoucache=0;
long long int nbytes; long long int nbytes;
long long int nacc; long long int nacc;
long long int elap, incac, oucac; long long int elap, incac, oucac;
double perc=0.00; double perc=0.00;
double perc2=0.00; double perc2=0.00;
double inperc=0.00, ouperc=0.00; double inperc=0.00, ouperc=0.00;
int posicao=0; int posicao=0;
char olduser[MAX_USER_LEN], csort[MAXLEN]; char top3[MAXLEN];
char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN];
char user[MAX_USER_LEN]; char user[MAX_USER_LEN];
const char *sfield="-n -k 2,2";
const char *order;
const char *sort_field;
const char *sort_order;
char title[80]; char title[80];
char *warea; char *warea;
int totuser=0; bool ntopuser=false;
int topcount=0; int topcount=0;
int cstatus;
struct getwordstruct gwarea; struct getwordstruct gwarea;
longline line; longline line;
struct generalitemstruct item;
struct userinfostruct *uinfo; struct userinfostruct *uinfo;
ntopuser = 0; if ((fp_top1=FileObject_Open(ListFile))==NULL) {
snprintf(wger,sizeof(wger),"%s/sarg-general",outdirname); debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),ListF
if((fp_in=fopen(wger,"r"))==NULL) { ile,FileObject_GetLastOpenError());
debugapos("topuser",_("Cannot open file \"%s\": %s\n"),wger,strer
ror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
snprintf(top2,sizeof(top2),"%s/top.tmp",outdirname); format_path(__FILE__, __LINE__, top3, sizeof(top3), "%s/"INDEX_HTML_FILE,
if((fp_top2=fopen(top2,"w"))==NULL) { outdirname);
debugapos("topuser",_("Cannot open file \"%s\": %s\n"),top2,strer if ((fp_top3=fopen(top3,"w"))==NULL) {
ror(errno)); debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top3,
exit(EXIT_FAILURE); strerror(errno));
}
olduser[0]='\0';
totuser=0;
if ((line=longline_create())==NULL) {
debuga(_("Not enough memory to read file \"%s\"\n"),wger);
exit(EXIT_FAILURE);
}
while((warea=longline_read(fp_in,line))!=NULL) {
ger_read(warea,&item,wger);
if(item.total) continue;
if(strcmp(olduser,item.user) != 0) {
totuser++;
if (olduser[0] != '\0') {
/*
This complicated printf is due to Microsoft's ina
bility to comply with any standard. Msvcrt is unable
to print a long long int unless it is exactly 64-
bits long.
*/
fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu
64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t
)tnelap,(uint64_t)tnincache,(uint64_t)tnoucache);
ttnbytes+=tnbytes;
ttnacc+=tnacc;
ttnelap+=tnelap;
ttnincache+=tnincache;
ttnoucache+=tnoucache;
}
strcpy(olduser,item.user);
tnbytes=0;
tnacc=0;
tnelap=0;
tnincache=0;
tnoucache=0;
}
tnbytes+=item.nbytes;
tnacc+=item.nacc;
tnelap+=item.nelap;
tnincache+=item.incache;
tnoucache+=item.oucache;
}
fclose(fp_in);
longline_destroy(&line);
if (olduser[0] != '\0') {
/*
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_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t
%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap,(uint64_
t)tnincache,(uint64_t)tnoucache);
ttnbytes+=tnbytes;
ttnacc+=tnacc;
ttnelap+=tnelap;
ttnincache+=tnincache;
ttnoucache+=tnoucache;
}
fclose(fp_top2);
#ifdef ENABLE_DOUBLE_CHECK_DATA
if (ttnacc!=globstat.nacc || ttnbytes!=globstat.nbytes || ttnelap!=globst
at.elap ||
ttnincache!=globstat.incache || ttnoucache!=globstat.oucache) {
debuga(_("Total statistics mismatch when reading %s to produce th
e top users\n"),wger);
exit(EXIT_FAILURE);
}
#endif
set_total_users(totuser);
if((TopuserSort & TOPUSER_SORT_USER) != 0) {
sfield="-k 1,1";
sort_field=_("user");
} else if((TopuserSort & TOPUSER_SORT_CONNECT) != 0) {
sfield="-n -k 3,3";
sort_field=_("connect");
} else if((TopuserSort & TOPUSER_SORT_TIME) != 0) {
sfield="-n -k 4,4";
sort_field=_("time");
} else {
sort_field=_("bytes");
}
if((TopuserSort & TOPUSER_SORT_REVERSE) == 0) {
order="";
sort_order=_("normal");
} else {
order="-r";
sort_order=_("reverse");
}
snprintf(top1,sizeof(top1),"%s/top",outdirname);
if (snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" %s %s -o \"%s\
" \"%s\"", tmp, order, sfield, top1, top2)>=sizeof(csort)) {
debuga(_("Command too long: "));
debuga_more("sort -T \"%s\" -t \"\t\" %s %s -o \"%s\" \"%s\"", tm
p, order, sfield, top1, top2);
exit(EXIT_FAILURE);
}
cstatus=system(csort);
if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus))
;
debuga(_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
if((fp_top1=fopen(top1,"r"))==NULL) {
debugapos("topuser",_("Cannot open file \"%s\": %s\n"),top1,strer
ror(errno));
exit(EXIT_FAILURE);
}
if (!KeepTempLog && unlink(top2)) {
debuga(_("Cannot delete \"%s\": %s\n"),top2,strerror(errno));
exit(EXIT_FAILURE);
}
snprintf(top3,sizeof(top3),"%s/index.html",outdirname);
if((fp_top3=fopen(top3,"w"))==NULL) {
debugapos("topuser",_("Cannot open file \"%s\": %s\n"),top3,strer
ror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
snprintf(title,sizeof(title),_("SARG report for %s"),period.text); snprintf(title,sizeof(title),_("SARG report for %s"),period.text);
write_html_header(fp_top3,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,title,HT ML_JS_SORTTABLE); write_html_header(fp_top3,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,title,HT ML_JS_SORTTABLE);
fputs("<tr><td class=\"header_c\">",fp_top3); fputs("<tr><td class=\"header_c\">",fp_top3);
fprintf(fp_top3,_("Period: %s"),period.html); fprintf(fp_top3,_("Period: %s"),period.html);
fputs("</td></tr>\n",fp_top3); fputs("</td></tr>\n",fp_top3);
if ((ReportType & REPORT_TYPE_TOPUSERS) != 0) { if ((ReportType & REPORT_TYPE_TOPUSERS) != 0) {
fputs("<tr><td class=\"header_c\">",fp_top3); fputs("<tr><td class=\"header_c\">",fp_top3);
fprintf(fp_top3,_("Sort: %s, %s"),sort_field,sort_order); fprintf(fp_top3,_("Sort: %s, %s"),SortInfo->sort_field,SortInfo-> sort_order);
fputs("</td></tr>\n",fp_top3); fputs("</td></tr>\n",fp_top3);
fprintf(fp_top3,"<tr><th class=\"header_c\">%s</th></tr>\n",_("To p users")); fprintf(fp_top3,"<tr><th class=\"header_c\">%s</th></tr>\n",_("To p users"));
} else { } else {
/* TRANSLATORS: This is the title of the main report page when no /* TRANSLATORS: This is the title of the main report page when no
* top users list are requested. * top users list are requested.
*/ */
fprintf(fp_top3,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Ta ble of content")); fprintf(fp_top3,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Ta ble of content"));
} }
close_html_header(fp_top3); close_html_header(fp_top3);
if (!indexonly) { if (!indexonly) {
fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing =\"2\">\n",fp_top3); fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing =\"2\">\n",fp_top3);
if((ReportType & REPORT_TYPE_TOPSITES) != 0 && !Privacy) fprintf( if ((ReportType & REPORT_TYPE_TOPSITES) != 0 && !Privacy) fprintf
fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"topsites.html\">%s</a></ (fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"topsites.html\">%s</a><
td></tr>\n",_("Top sites")); /td></tr>\n",_("Top sites"));
if((ReportType & REPORT_TYPE_SITES_USERS) != 0 && !Privacy) fprin if ((ReportType & REPORT_TYPE_SITES_USERS) != 0 && !Privacy) fpri
tf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"siteuser.html\">%s</a ntf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"siteuser.html\">%s</
></td></tr>\n",_("Sites & Users")); a></td></tr>\n",_("Sites & Users"));
if(dansguardian_count) fprintf(fp_top3,"<tr><td class=\"link\" co if (dansguardian_count) fprintf(fp_top3,"<tr><td class=\"link\" c
lspan=\"0\"><a href=\"dansguardian.html\">%s</a></td></tr>\n",_("DansGuardian")) olspan=\"0\"><a href=\"dansguardian.html\">%s</a></td></tr>\n",_("DansGuardian")
; );
if(redirector_count) fprintf(fp_top3,"<tr><td class=\"link\" cols if (redirector_count) fprintf(fp_top3,"<tr><td class=\"link\" col
pan=\"0\"><a href=\"redirector.html\">%s</a></td></tr>\n",_("Redirector")); span=\"0\"><a href=\"redirector.html\">%s</a></td></tr>\n",_("Redirector"));
if ((ReportType & REPORT_TYPE_DOWNLOADS) != 0 && download_count & if (is_download()) fprintf(fp_top3,"<tr><td class=\"link\" colspa
& !Privacy && ndownload) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\">< n=\"0\"><a href=\"download.html\">%s</a></td></tr>\n",_("Downloads"));
a href=\"download.html\">%s</a></td></tr>\n",_("Downloads")); if (is_denied()) fprintf(fp_top3,"<tr><td class=\"link\" colspan=
if ((ReportType & REPORT_TYPE_DENIED) != 0 && denied_count && !Pr \"0\"><a href=\"denied.html\">%s</a></td></tr>\n",_("Denied accesses"));
ivacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"denied.ht if (is_authfail()) fprintf(fp_top3,"<tr><td class=\"link\" colspa
ml\">%s</a></td></tr>\n",_("Denied accesses")); n=\"0\"><a href=\"authfail.html\">%s</a></td></tr>\n",_("Authentication Failures
if ((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0 && authfail_cou "));
nt && !Privacy) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\" if (smartfilter) fprintf(fp_top3,"<tr><td class=\"link\" colspan=
authfail.html\">%s</a></td></tr>\n",_("Authentication Failures")); \"0\"><a href=\"smartfilter.html\">%s</a></td></tr>\n",_("SmartFilter"));
if(smartfilter) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\ if (useragent_count) fprintf(fp_top3,"<tr><td class=\"link\" cols
"0\"><a href=\"smartfilter.html\">%s</a></td></tr>\n",_("SmartFilter")); pan=\"0\"><a href=\"useragent.html\">%s</a></td></tr>\n",_("Useragent"));
if(UserAgentLog[0] != '\0' && useragent_count) fprintf(fp_top3,"<
tr><td class=\"link\" colspan=\"0\"><a href=\"useragent.html\">%s</a></td></tr>\
n",_("Useragent"));
fputs("<tr><td></td></tr>\n</table></div>\n",fp_top3); fputs("<tr><td></td></tr>\n</table></div>\n",fp_top3);
} }
if ((ReportType & REPORT_TYPE_TOPUSERS) == 0) { if ((ReportType & REPORT_TYPE_TOPUSERS) == 0) {
fputs("</body>\n</html>\n",fp_top3); fputs("</body>\n</html>\n",fp_top3);
fclose (fp_top3); if (fclose (fp_top3)==EOF) {
if (debugz) debugaz(_("No top users report because it is not conf debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n")
igured in report_type\n")); ,top3,strerror(errno));
exit(EXIT_FAILURE);
}
if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("No top
users report because it is not configured in report_type\n"));
return; return;
} }
fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\"", fp_top3); fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\"", fp_top3);
if (SortTableJs[0]) if (SortTableJs[0])
fputs(" class=\"sortable\"",fp_top3); fputs(" class=\"sortable\"",fp_top3);
fputs(">\n<thead><tr>",fp_top3); fputs(">\n<thead><tr>",fp_top3);
if((TopUserFields & TOPUSERFIELDS_NUM) != 0) if ((TopUserFields & TOPUSERFIELDS_NUM) != 0)
fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("NUM")); fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("NUM"));
if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_ TYPE_DATE_TIME) != 0 && !indexonly) { if ((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT _TYPE_DATE_TIME) != 0 && !indexonly) {
fputs("<th class=\"header_l",fp_top3); fputs("<th class=\"header_l",fp_top3);
if (SortTableJs[0]) fputs(" sorttable_nosort",fp_top3); if (SortTableJs[0]) fputs(" sorttable_nosort",fp_top3);
fputs("\"></th>",fp_top3); fputs("\"></th>",fp_top3);
} }
if((TopUserFields & TOPUSERFIELDS_USERID) != 0) { if ((TopUserFields & TOPUSERFIELDS_USERID) != 0) {
fputs("<th class=\"header_l",fp_top3); fputs("<th class=\"header_l",fp_top3);
if (SortTableJs[0]) fputs(" sorttable_alpha",fp_top3); if (SortTableJs[0]) fputs(" sorttable_alpha",fp_top3);
fprintf(fp_top3,"\">%s</th>",_("USERID")); fprintf(fp_top3,"\">%s</th>",_("USERID"));
} }
if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) if ((TopUserFields & TOPUSERFIELDS_USERIP) != 0) {
fputs("<th class=\"header_l",fp_top3);
if (SortTableJs[0]) fputs(" sorttable_alpha",fp_top3);
fprintf(fp_top3,"\">%s</th>",_("USERIP"));
}
if ((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("CONNECT")); fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("CONNECT"));
if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) if ((TopUserFields & TOPUSERFIELDS_BYTES) != 0)
fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("BYTES")); fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("BYTES"));
if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) if ((TopUserFields & TOPUSERFIELDS_SETYB) != 0)
fprintf(fp_top3,"<th class=\"header_l\">%%%s</th>",_("BYTES")); fprintf(fp_top3,"<th class=\"header_l\">%%%s</th>",_("BYTES"));
if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) if ((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0)
fprintf(fp_top3,"<th class=\"header_c\" colspan=\"2\">%s</th><th style=\"display:none;\"></th>",_("IN-CACHE-OUT")); fprintf(fp_top3,"<th class=\"header_c\" colspan=\"2\">%s</th><th style=\"display:none;\"></th>",_("IN-CACHE-OUT"));
if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) if ((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0)
fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("ELAPSED TIME" )); fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("ELAPSED TIME" ));
if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) if ((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("MILLISEC")); fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("MILLISEC"));
if((TopUserFields & TOPUSERFIELDS_PTIME) != 0) if ((TopUserFields & TOPUSERFIELDS_PTIME) != 0)
fprintf(fp_top3,"<th class=\"header_l\">%%%s</th>",_("TIME")); fprintf(fp_top3,"<th class=\"header_l\">%%%s</th>",pgettext("dura
tion","TIME"));
fputs("</tr></thead>\n",fp_top3); fputs("</tr></thead>\n",fp_top3);
greport_prepare(); greport_prepare();
ntopuser = 0;
if ((line=longline_create())==NULL) { if ((line=longline_create())==NULL) {
debuga(_("Not enough memory to read file \"%s\"\n"),top1); debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\" \n"),ListFile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
while((warea=longline_read(fp_top1,line))!=NULL) { while ((warea=longline_read(fp_top1,line))!=NULL) {
getword_start(&gwarea,warea); getword_start(&gwarea,warea);
if (getword(user,sizeof(user),&gwarea,'\t')<0) { if (getword(user,sizeof(user),&gwarea,'\t')<0) {
debuga(_("Invalid user in file \"%s\"\n"),top1); debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n "),ListFile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (getword_atoll(&nbytes,&gwarea,'\t')<0) { if (getword_atoll(&nbytes,&gwarea,'\t')<0) {
debuga(_("Invalid number of bytes in file \"%s\"\n"),top1 ); debuga(__FILE__,__LINE__,_("Invalid number of bytes in fi le \"%s\"\n"),ListFile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (getword_atoll(&nacc,&gwarea,'\t')<0) { if (getword_atoll(&nacc,&gwarea,'\t')<0) {
debuga(_("Invalid number of accesses in file \"%s\"\n"),t op1); debuga(__FILE__,__LINE__,_("Invalid number of accesses in file \"%s\"\n"),ListFile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (getword_atoll(&elap,&gwarea,'\t')<0) { if (getword_atoll(&elap,&gwarea,'\t')<0) {
debuga(_("Invalid elapsed time in file \"%s\"\n"),top1); debuga(__FILE__,__LINE__,_("Invalid elapsed time in file \"%s\"\n"),ListFile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (getword_atoll(&incac,&gwarea,'\t')<0) { if (getword_atoll(&incac,&gwarea,'\t')<0) {
debuga(_("Invalid in-cache size in file \"%s\"\n"),top1); debuga(__FILE__,__LINE__,_("Invalid in-cache size in file \"%s\"\n"),ListFile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (getword_atoll(&oucac,&gwarea,'\n')<0) { if (getword_atoll(&oucac,&gwarea,'\n')<0) {
debuga(_("Invalid out-of-cache size in file \"%s\"\n"),to p1); debuga(__FILE__,__LINE__,_("Invalid out-of-cache size in file \"%s\"\n"),ListFile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if(nacc < 1) if (nacc < 1)
continue; continue;
ntopuser = 1; ntopuser=true;
if(TopUsersNum > 0 && topcount >= TopUsersNum) break; if (TopUsersNum>0 && topcount>=TopUsersNum) break;
tnbytes=nbytes;
tnelap=elap;
uinfo=userinfo_find_from_id(user); uinfo=userinfo_find_from_id(user);
if (!uinfo) { if (!uinfo) {
debuga(_("Unknown user ID %s in file \"%s\"\n"),user,top1 ); debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \" %s\"\n"),user,ListFile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
uinfo->topuser=1; uinfo->topuser=1;
report_day(uinfo);
greport_day(uinfo);
fputs("<tr>",fp_top3); fputs("<tr>",fp_top3);
posicao++; posicao++;
if((TopUserFields & TOPUSERFIELDS_NUM) != 0) if ((TopUserFields & TOPUSERFIELDS_NUM) != 0)
fprintf(fp_top3,"<td class=\"data\">%d</td>",posicao); fprintf(fp_top3,"<td class=\"data\">%d</td>",posicao);
if (!indexonly) { if (!indexonly) {
if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (Repo rtType & REPORT_TYPE_DATE_TIME) != 0) { if ((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (Rep ortType & REPORT_TYPE_DATE_TIME) != 0) {
fputs("<td class=\"data2\">",fp_top3); fputs("<td class=\"data2\">",fp_top3);
#ifdef HAVE_GD #ifdef HAVE_GD
if(Graphs && GraphFont[0]!='\0') { if (Graphs && GraphFont[0]!='\0') {
greport_day(uinfo);
//fprintf(fp_top3,"<a href=\"%s/graph_day .png\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a>&nbsp;",uinfo->filen ame,ImageFile,_("Graphic")); //fprintf(fp_top3,"<a href=\"%s/graph_day .png\"><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a>&nbsp;",uinfo->filen ame,ImageFile,_("Graphic"));
fprintf(fp_top3,"<a href=\"%s/graph.html\ "><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a>&nbsp;",uinfo->filename,I mageFile,_("Graphic")); fprintf(fp_top3,"<a href=\"%s/graph.html\ "><img src=\"%s/graph.png\" title=\"%s\" alt=\"G\"></a>&nbsp;",uinfo->filename,I mageFile,_("Graphic"));
} }
#endif #endif
report_day(uinfo);
fprintf(fp_top3,"<a href=\"%s/d%s.html\"><img src =\"%s/datetime.png\" title=\"%s\" alt=\"T\"></a></td>",uinfo->filename,uinfo->fi lename,ImageFile,_("date/time report")); fprintf(fp_top3,"<a href=\"%s/d%s.html\"><img src =\"%s/datetime.png\" title=\"%s\" alt=\"T\"></a></td>",uinfo->filename,uinfo->fi lename,ImageFile,_("date/time report"));
} else { day_deletefile(uinfo);
sprintf(val1,"%s/d%s.html",outdirname,uinfo->file
name);
if (unlink(val1)) {
debuga(_("Cannot delete \"%s\": %s\n"),va
l1,strerror(errno));
exit(EXIT_FAILURE);
}
} }
} }
if((TopUserFields & TOPUSERFIELDS_USERID) != 0) { if ((TopUserFields & TOPUSERFIELDS_USERID) != 0) {
if((ReportType & REPORT_TYPE_USERS_SITES) == 0 || indexon if ((ReportType & REPORT_TYPE_USERS_SITES) == 0 || indexo
ly) nly)
fprintf(fp_top3,"<td class=\"data2\">%s</td>",uin fo->label); fprintf(fp_top3,"<td class=\"data2\">%s</td>",uin fo->label);
else else
fprintf(fp_top3,"<td class=\"data2\"><a href=\"%s /%s.html\">%s</a></td>",uinfo->filename,uinfo->filename,uinfo->label); fprintf(fp_top3,"<td class=\"data2\"><a href=\"%s /%s.html\">%s</a></td>",uinfo->filename,uinfo->filename,uinfo->label);
} }
if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) { if ((TopUserFields & TOPUSERFIELDS_USERIP) != 0) {
fprintf(fp_top3,"<td class=\"data2\">%s</td>",uinfo->ip);
}
if ((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) {
fputs("<td class=\"data\"",fp_top3); fputs("<td class=\"data\"",fp_top3);
if (SortTableJs[0]) fprintf(fp_top3," sorttable_customkey =\"%"PRId64"\"",(int64_t)nacc); if (SortTableJs[0]) fprintf(fp_top3," sorttable_customkey =\"%"PRId64"\"",(int64_t)nacc);
fprintf(fp_top3,">%s</td>",fixnum(nacc,1)); fprintf(fp_top3,">%s</td>",fixnum(nacc,1));
} }
if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) { if ((TopUserFields & TOPUSERFIELDS_BYTES) != 0) {
fputs("<td class=\"data\"",fp_top3); fputs("<td class=\"data\"",fp_top3);
if (SortTableJs[0]) fprintf(fp_top3," sorttable_customkey if (SortTableJs[0]) fprintf(fp_top3," sorttable_customkey
=\"%"PRId64"\"",(int64_t)tnbytes); =\"%"PRId64"\"",(int64_t)nbytes);
fprintf(fp_top3,">%s</td>",fixnum(tnbytes,1)); fprintf(fp_top3,">%s</td>",fixnum(nbytes,1));
} }
if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) { if ((TopUserFields & TOPUSERFIELDS_SETYB) != 0) {
perc=(ttnbytes) ? tnbytes * 100. / ttnbytes : 0.; perc=(Statis->ttnbytes) ? nbytes * 100. / Statis->ttnbyte
s : 0.;
fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td>",perc); fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td>",perc);
} }
if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) { if ((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) {
inperc=(tnbytes) ? incac * 100. / tnbytes : 0.; inperc=(nbytes) ? incac * 100. / nbytes : 0.;
ouperc=(tnbytes) ? oucac * 100. / tnbytes : 0.; ouperc=(nbytes) ? oucac * 100. / nbytes : 0.;
fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td><td clas s=\"data\">%3.2lf%%</td>",inperc,ouperc); fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td><td clas s=\"data\">%3.2lf%%</td>",inperc,ouperc);
#ifdef ENABLE_DOUBLE_CHECK_DATA #ifdef ENABLE_DOUBLE_CHECK_DATA
if ((inperc!=0. || ouperc!=0.) && fabs(inperc+ouperc-100. )>=0.01) { if ((inperc!=0. || ouperc!=0.) && fabs(inperc+ouperc-100. )>=0.01) {
debuga(_("The total of the in-cache and cache-mis s is not 100%% at position %d (user %s)\n"),posicao,uinfo->label); debuga(__FILE__,__LINE__,_("The total of the in-c ache and cache-miss is not 100%% at position %d (user %s)\n"),posicao,uinfo->lab el);
} }
#endif #endif
} }
if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) { if ((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) {
fputs("<td class=\"data\"",fp_top3); fputs("<td class=\"data\"",fp_top3);
if (SortTableJs[0]) fprintf(fp_top3," sorttable_customkey if (SortTableJs[0]) fprintf(fp_top3," sorttable_customkey
=\"%"PRId64"\"",(int64_t)tnelap); =\"%"PRId64"\"",(int64_t)elap);
fprintf(fp_top3,">%s</td>",buildtime(tnelap)); fprintf(fp_top3,">%s</td>",buildtime(elap));
} }
if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) { if ((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) {
fputs("<td class=\"data\"",fp_top3); fputs("<td class=\"data\"",fp_top3);
if (SortTableJs[0]) fprintf(fp_top3," sorttable_customkey if (SortTableJs[0]) fprintf(fp_top3," sorttable_customkey
=\"%"PRId64"\"",(int64_t)tnelap); =\"%"PRId64"\"",(int64_t)elap);
fprintf(fp_top3,">%s</td>",fixnum2(tnelap,1)); fprintf(fp_top3,">%s</td>",fixnum2(elap,1));
} }
if((TopUserFields & TOPUSERFIELDS_PTIME) != 0) { if ((TopUserFields & TOPUSERFIELDS_PTIME) != 0) {
perc2=(ttnelap) ? elap * 100. / ttnelap : 0.; perc2=(Statis->ttnelap) ? elap * 100. / Statis->ttnelap :
0.;
fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td>",perc2) ; fprintf(fp_top3,"<td class=\"data\">%3.2lf%%</td>",perc2) ;
} }
fputs("</tr>\n",fp_top3); fputs("</tr>\n",fp_top3);
topcount++; topcount++;
} }
fclose(fp_top1); if (FileObject_Close(fp_top1)) {
if (!KeepTempLog && unlink(top1)) { debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),ListFile
debuga(_("Cannot delete \"%s\": %s\n"),top1,strerror(errno)); ,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
if (!KeepTempLog && unlink(ListFile)) {
debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),ListFile
,strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
longline_destroy(&line); longline_destroy(&line);
if((TopUserFields & TOPUSERFIELDS_TOTAL) != 0) { if ((TopUserFields & TOPUSERFIELDS_TOTAL) != 0) {
fputs("<tfoot><tr>",fp_top3); fputs("<tfoot><tr>",fp_top3);
if((TopUserFields & TOPUSERFIELDS_NUM) != 0) if ((TopUserFields & TOPUSERFIELDS_NUM) != 0)
fputs("<td></td>",fp_top3); fputs("<td></td>",fp_top3);
if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0 && !indexonly) if ((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0 && !indexonly)
fputs("<td></td>",fp_top3); fputs("<td></td>",fp_top3);
fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("TOTAL")); if ((TopUserFields & TOPUSERFIELDS_USERIP) != 0)
fprintf(fp_top3,"<th class=\"header_l\" colspan=\"2\">%s<
if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) /th>",_("TOTAL"));
fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum(t else
tnacc,1)); fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("TOTAL
if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) "));
fprintf(fp_top3,"<th class=\"header_r\">%15s</th>",fixnum
(ttnbytes,1)); if ((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum(S
tatis->ttnacc,1));
if ((TopUserFields & TOPUSERFIELDS_BYTES) != 0)
fprintf(fp_top3,"<th class=\"header_r\">%15s</th>",fixnum
(Statis->ttnbytes,1));
if ((TopUserFields & TOPUSERFIELDS_SETYB) != 0)
fputs("<td></td>",fp_top3); fputs("<td></td>",fp_top3);
if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) if ((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0)
{ {
inperc=(ttnbytes) ? ttnincache * 100. / ttnbytes : 0.; inperc=(Statis->ttnbytes) ? Statis->ttnincache * 100. / S
ouperc=(ttnbytes) ? ttnoucache *100. / ttnbytes : 0.; tatis->ttnbytes : 0.;
ouperc=(Statis->ttnbytes) ? Statis->ttnoucache *100. / St
atis->ttnbytes : 0.;
fprintf(fp_top3,"<th class=\"header_r\">%3.2lf%%</th><th class=\"header_r\">%3.2lf%%</th>",inperc,ouperc); fprintf(fp_top3,"<th class=\"header_r\">%3.2lf%%</th><th class=\"header_r\">%3.2lf%%</th>",inperc,ouperc);
#ifdef ENABLE_DOUBLE_CHECK_DATA #ifdef ENABLE_DOUBLE_CHECK_DATA
if (fabs(inperc+ouperc-100.)>=0.01) { if (fabs(inperc+ouperc-100.)>=0.01) {
debuga(_("The total of the in-cache and cache-mis s is not 100%%\n")); debuga(__FILE__,__LINE__,_("The total of the in-c ache and cache-miss is not 100%%\n"));
} }
#endif #endif
} }
if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) if ((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0)
fprintf(fp_top3,"<th class=\"header_r\">%s</th>",buildtim fprintf(fp_top3,"<th class=\"header_r\">%s</th>",buildtim
e(ttnelap)); e(Statis->ttnelap));
if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) if ((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum2( fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum2(
ttnelap,1)); Statis->ttnelap,1));
fputs("</tr>\n",fp_top3); fputs("</tr>\n",fp_top3);
} }
greport_cleanup(); greport_cleanup();
if(ntopuser && (TopUserFields & TOPUSERFIELDS_AVERAGE) != 0) { if (ntopuser && (TopUserFields & TOPUSERFIELDS_AVERAGE) != 0) {
fputs("<tr>",fp_top3); fputs("<tr>",fp_top3);
if((TopUserFields & TOPUSERFIELDS_NUM) != 0) if ((TopUserFields & TOPUSERFIELDS_NUM) != 0)
fputs("<td></td>",fp_top3); fputs("<td></td>",fp_top3);
if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0 && !indexonly) if ((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0 && !indexonly)
fputs("<td></td>",fp_top3); fputs("<td></td>",fp_top3);
fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("AVERAGE")); if ((TopUserFields & TOPUSERFIELDS_USERIP) != 0)
fprintf(fp_top3,"<th class=\"header_l\" colspan=\"2\">%s<
if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) /th>",_("AVERAGE"));
fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum(t else
tnacc/totuser,1)); fprintf(fp_top3,"<th class=\"header_l\">%s</th>",_("AVERA
if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) { GE"));
tnbytes=(totuser) ? ttnbytes / totuser : 0;
fprintf(fp_top3,"<th class=\"header_r\">%15s</th>",fixnum if ((TopUserFields & TOPUSERFIELDS_CONNECT) != 0)
(tnbytes,1)); fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum(S
tatis->ttnacc/Statis->totuser,1));
if ((TopUserFields & TOPUSERFIELDS_BYTES) != 0) {
nbytes=(Statis->totuser) ? Statis->ttnbytes / Statis->tot
user : 0;
fprintf(fp_top3,"<th class=\"header_r\">%15s</th>",fixnum
(nbytes,1));
} }
if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) if ((TopUserFields & TOPUSERFIELDS_SETYB) != 0)
fputs("<td></td>",fp_top3); fputs("<td></td>",fp_top3);
if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) if ((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0)
fputs("<td></td><td></td>",fp_top3); fputs("<td></td><td></td>",fp_top3);
if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) if ((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0)
fprintf(fp_top3,"<th class=\"header_r\">%s</th>",buildtim fprintf(fp_top3,"<th class=\"header_r\">%s</th>",buildtim
e(ttnelap/totuser)); e(Statis->ttnelap/Statis->totuser));
if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) if ((TopUserFields & TOPUSERFIELDS_MILISEC) != 0)
fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum2( fprintf(fp_top3,"<th class=\"header_r\">%s</th>",fixnum2(
ttnelap/totuser,1)); Statis->ttnelap/Statis->totuser,1));
fputs("</tr></tfoot>\n",fp_top3); fputs("</tr></tfoot>\n",fp_top3);
} }
fputs("</table></div>\n",fp_top3); fputs("</table></div>\n",fp_top3);
if (write_html_trailer(fp_top3)<0) write_html_trailer(fp_top3);
debuga(_("Write error in top user list %s\n"),top3); if (fclose(fp_top3)==EOF) {
if (fclose(fp_top3)==EOF) debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),top3,st
debuga(_("Failed to close file \"%s\": %s\n"),top3,strerror(errno rerror(errno));
)); exit(EXIT_FAILURE);
}
}
/*!
Generate the top user email report.
*/
static void TopUser_TextEmail(const char *ListFile,struct TopUserStatistics *Sta
tis,struct SortInfoStruct *SortInfo)
{
FileObject *fp_top1;
FILE *fp_mail;
longline line;
struct getwordstruct gwarea;
char *warea;
char user[MAX_USER_LEN];
char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], stri
p5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN];
long long int nbytes;
long long int nacc;
long long int elap, incac, oucac;
double perc=0.00;
double perc2=0.00;
long long int tnbytes=0;
long long int avgacc, avgelap;
int topcount=0;
struct userinfostruct *uinfo;
time_t t;
struct tm *local;
const char *Subject;
if ((fp_top1=FileObject_Open(ListFile))==NULL) {
debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),ListF
ile,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
fp_mail=Email_OutputFile("topuser");
if ((line=longline_create())==NULL) {
debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"
\n"),ListFile);
exit(EXIT_FAILURE);
}
safe_strcpy(strip1,_("Squid User Access Report"),sizeof(strip1));
strip_latin(strip1);
fprintf(fp_mail,"%s\n",strip1);
snprintf(strip1,sizeof(strip1),_("Sort: %s, %s"),SortInfo->sort_field,Sor
tInfo->sort_order);
strip_latin(strip1);
fprintf(fp_mail,"%s\n",strip1);
snprintf(strip1,sizeof(strip1),_("Period: %s"),period.text);
strip_latin(strip1);
fprintf(fp_mail,"%s\n\n",strip1);
safe_strcpy(strip1,_("NUM"),sizeof(strip1));
strip_latin(strip1);
safe_strcpy(strip2,_("USERID"),sizeof(strip2));
strip_latin(strip2);
safe_strcpy(strip3,_("CONNECT"),sizeof(strip3));
strip_latin(strip3);
safe_strcpy(strip4,_("BYTES"),sizeof(strip4));
strip_latin(strip4);
safe_strcpy(strip5,_("ELAPSED TIME"),sizeof(strip5));
strip_latin(strip5);
safe_strcpy(strip6,_("MILLISEC"),sizeof(strip6));
strip_latin(strip6);
safe_strcpy(strip7,pgettext("duration","TIME"),sizeof(strip7));
strip_latin(strip7);
fprintf(fp_mail,"%-7s %-20s %-9s %-15s %%%-6s %-11s %-10s %%%-7s\n-------
-------------------- -------- --------------- ------- ---------- ---------- ---
----\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7);
while ((warea=longline_read(fp_top1,line))!=NULL) {
getword_start(&gwarea,warea);
if (getword(user,sizeof(user),&gwarea,'\t')<0) {
debuga(__FILE__,__LINE__,_("Invalid user in file \"%s\"\n
"),ListFile);
exit(EXIT_FAILURE);
}
if (getword_atoll(&nbytes,&gwarea,'\t')<0) {
debuga(__FILE__,__LINE__,_("Invalid number of bytes in fi
le \"%s\"\n"),ListFile);
exit(EXIT_FAILURE);
}
if (getword_atoll(&nacc,&gwarea,'\t')<0) {
debuga(__FILE__,__LINE__,_("Invalid number of accesses in
file \"%s\"\n"),ListFile);
exit(EXIT_FAILURE);
}
if (getword_atoll(&elap,&gwarea,'\t')<0) {
debuga(__FILE__,__LINE__,_("Invalid elapsed time in file
\"%s\"\n"),ListFile);
exit(EXIT_FAILURE);
}
if (getword_atoll(&incac,&gwarea,'\t')<0) {
debuga(__FILE__,__LINE__,_("Invalid in-cache size in file
\"%s\"\n"),ListFile);
exit(EXIT_FAILURE);
}
if (getword_atoll(&oucac,&gwarea,'\n')<0) {
debuga(__FILE__,__LINE__,_("Invalid out-of-cache size in
file \"%s\"\n"),ListFile);
exit(EXIT_FAILURE);
}
if (nacc < 1)
continue;
if (TopUsersNum>0 && topcount>=TopUsersNum) break;
uinfo=userinfo_find_from_id(user);
if (!uinfo) {
debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \"
%s\"\n"),user,ListFile);
exit(EXIT_FAILURE);
}
uinfo->topuser=1;
perc=(Statis->ttnbytes) ? nbytes * 100. / Statis->ttnbytes : 0;
perc2=(Statis->ttnelap) ? elap * 100. / Statis->ttnelap : 0;
topcount++;
#if defined(__FreeBSD__)
fprintf(fp_mail,"%7d %20s %8lld %15s %5.2lf%% %10s %10qu %3.2lf%%
\n",topcount,uinfo->label,nacc,fixnum(nbytes,1),perc,buildtime(elap),elap,perc2)
;
#else
fprintf(fp_mail,"%7d %20s %8"PRIu64" %15s %6.2lf%% %10s %10"PRIu6
4" %3.2lf%%\n",topcount,uinfo->label,(uint64_t)nacc,fixnum(nbytes,1),perc,buildt
ime(elap),(uint64_t)elap,perc2);
#endif
}
if (FileObject_Close(fp_top1)) {
debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),ListFile
,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
if (!KeepTempLog && unlink(ListFile)) {
debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),ListFile
,strerror(errno));
exit(EXIT_FAILURE);
}
longline_destroy(&line);
// output total
fputs("------- -------------------- -------- --------------- ------- ----
------ ---------- -------\n",fp_mail);
#if defined(__FreeBSD__)
fprintf(fp_mail,"%-7s %20s %8qu %15s %8s %9s %10qu\n",_("TOTAL")," ",Stat
is->ttnacc,fixnum(Statis->ttnbytes,1)," ",buildtime(Statis->ttnelap),Statis->ttn
elap);
#else
fprintf(fp_mail,"%-7s %20s %8"PRIu64" %15s %8s %9s %10"PRIu64"\n",_("TOTA
L")," ",(uint64_t)Statis->ttnacc,fixnum(Statis->ttnbytes,1)," ",buildtime(Statis
->ttnelap),(uint64_t)Statis->ttnelap);
#endif
// compute and write average
if (Statis->totuser>0) {
tnbytes=Statis->ttnbytes / Statis->totuser;
avgacc=Statis->ttnacc/Statis->totuser;
avgelap=Statis->ttnelap/Statis->totuser;
} else {
tnbytes=0;
avgacc=0;
avgelap=0;
}
safe_strcpy(strip1,_("AVERAGE"),sizeof(strip1));
strip_latin(strip1);
#if defined(__FreeBSD__)
fprintf(fp_mail,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",avgacc,f
ixnum(tnbytes,1)," ",buildtime(avgelap),avgelap);
#else
fprintf(fp_mail,"%-7s %20s %8"PRIu64" %15s %8s %9s %10"PRIu64"\n",strip1,
" ",(uint64_t)avgacc,fixnum(tnbytes,1)," ",buildtime(avgelap),(uint64_t)avgelap)
;
#endif
t = time(NULL);
local = localtime(&t);
fprintf(fp_mail, "\n%s\n", asctime(local));
/* TRANSLATORS: This is the e-mail subject. */
Subject=_("Sarg: top user report");
Email_Send(fp_mail,Subject);
}
/*!
* Produce a report with the user downloading the most data.
*/
void topuser(void)
{
FileObject *fp_in = NULL;
FILE *fp_top2;
char wger[MAXLEN];
char top1[MAXLEN];
char top2[MAXLEN];
longline line;
long long int tnacc=0;
long long int tnbytes=0, tnelap=0;
long long int tnincache=0, tnoucache=0;
char *warea;
struct generalitemstruct item;
char olduser[MAX_USER_LEN], csort[MAXLEN];
const char *sfield="-n -k 2,2";
const char *order;
int cstatus;
struct TopUserStatistics Statis;
struct SortInfoStruct SortInfo;
if (debugz>=LogLevel_Process)
debuga(__FILE__,__LINE__,_("Creating top users report...\n"));
memset(&Statis,0,sizeof(Statis));
format_path(__FILE__, __LINE__, wger, sizeof(wger), "%s/sarg-general", ou
tdirname);
if ((fp_in=FileObject_Open(wger))==NULL) {
debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wger,
FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
format_path(__FILE__, __LINE__, top2, sizeof(top2), "%s/top.tmp", outdirn
ame);
if ((fp_top2=fopen(top2,"w"))==NULL) {
debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top2,
strerror(errno));
exit(EXIT_FAILURE);
}
olduser[0]='\0';
if ((line=longline_create())==NULL) {
debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"
\n"),wger);
exit(EXIT_FAILURE);
}
while ((warea=longline_read(fp_in,line))!=NULL) {
ger_read(warea,&item,wger);
if (item.total) continue;
if (strcmp(olduser,item.user) != 0) {
Statis.totuser++;
if (olduser[0] != '\0') {
/*
This complicated printf is due to Microsoft's ina
bility to comply with any standard. Msvcrt is unable
to print a long long int unless it is exactly 64-
bits long.
*/
fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu
64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t
)tnelap,(uint64_t)tnincache,(uint64_t)tnoucache);
Statis.ttnbytes+=tnbytes;
Statis.ttnacc+=tnacc;
Statis.ttnelap+=tnelap;
Statis.ttnincache+=tnincache;
Statis.ttnoucache+=tnoucache;
}
safe_strcpy(olduser,item.user,sizeof(olduser));
tnbytes=0;
tnacc=0;
tnelap=0;
tnincache=0;
tnoucache=0;
}
tnbytes+=item.nbytes;
tnacc+=item.nacc;
tnelap+=item.nelap;
tnincache+=item.incache;
tnoucache+=item.oucache;
}
if (FileObject_Close(fp_in)) {
debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wger,Fil
eObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line);
if (olduser[0] != '\0') {
/*
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_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t
%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap,(uint64_
t)tnincache,(uint64_t)tnoucache);
Statis.ttnbytes+=tnbytes;
Statis.ttnacc+=tnacc;
Statis.ttnelap+=tnelap;
Statis.ttnincache+=tnincache;
Statis.ttnoucache+=tnoucache;
}
if (fclose(fp_top2)==EOF) {
debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),top2,st
rerror(errno));
exit(EXIT_FAILURE);
}
#ifdef ENABLE_DOUBLE_CHECK_DATA
if (Statis.ttnacc!=globstat.nacc || Statis.ttnbytes!=globstat.nbytes || S
tatis.ttnelap!=globstat.elap ||
Statis.ttnincache!=globstat.incache || Statis.ttnoucache!=globsta
t.oucache) {
debuga(__FILE__,__LINE__,_("Total statistics mismatch when readin
g \"%s\" to produce the top users\n"),wger);
exit(EXIT_FAILURE);
}
#endif
set_total_users(Statis.totuser);
if ((TopuserSort & TOPUSER_SORT_USER) != 0) {
sfield="-k 1,1";
SortInfo.sort_field=_("user");
} else if ((TopuserSort & TOPUSER_SORT_CONNECT) != 0) {
sfield="-n -k 3,3";
SortInfo.sort_field=_("connect");
} else if ((TopuserSort & TOPUSER_SORT_TIME) != 0) {
sfield="-n -k 4,4";
SortInfo.sort_field=pgettext("duration","time");
} else {
SortInfo.sort_field=_("bytes");
}
if ((TopuserSort & TOPUSER_SORT_REVERSE) == 0) {
order="";
SortInfo.sort_order=_("normal");
} else {
order="-r";
SortInfo.sort_order=_("reverse");
}
format_path(__FILE__, __LINE__, top1, sizeof(top1), "%s/top", outdirname)
;
if (snprintf(csort,sizeof(csort),"sort -T \"%s\" -t \"\t\" %s %s -o \"%s\
" \"%s\"", tmp, order, sfield, top1, top2)>=sizeof(csort)) {
debuga(__FILE__,__LINE__,_("Sort command too long when sorting fi
le \"%s\" to \"%s\"\n"),top2,top1);
exit(EXIT_FAILURE);
}
cstatus=system(csort);
if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEX
ITSTATUS(cstatus));
debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
if (!KeepTempLog && unlink(top2)) {
debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),top2,str
error(errno));
exit(EXIT_FAILURE);
}
return; if (email[0])
TopUser_TextEmail(top1,&Statis,&SortInfo);
else
TopUser_HtmlReport(top1,&Statis,&SortInfo);
} }
 End of changes. 74 change blocks. 
310 lines changed or deleted 581 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)