email.c (sarg-2.3.11) | : | email.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 34 | skipping to change at line 34 | |||
* | * | |||
*/ | */ | |||
#include "include/conf.h" | #include "include/conf.h" | |||
#include "include/defs.h" | #include "include/defs.h" | |||
#ifdef ENABLE_DOUBLE_CHECK_DATA | #ifdef ENABLE_DOUBLE_CHECK_DATA | |||
extern struct globalstatstruct globstat; | extern struct globalstatstruct globstat; | |||
#endif | #endif | |||
int geramail(const char *dirname, int debug, const char *outdir, const char *ema | //! Name of the file containing the e-mail to send. | |||
il, const char *TempDir) | static char EmailFileName[MAXLEN]=""; | |||
{ | ||||
FILE *fp_in, *fp_top1, *fp_top2, *fp_top3; | ||||
long long int ttnbytes=0, ttnacc=0, tnacc=0; | ||||
long long int tnbytes=0, ttnelap=0, tnelap=0; | ||||
long long int nacc, nbytes, elap; | ||||
long long int avgacc, avgelap; | ||||
double perc=0.00; | ||||
double perc2=0.00; | ||||
int posicao=0; | ||||
char olduser[MAX_USER_LEN], csort[MAXLEN]; | ||||
char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAX_USE | ||||
R_LEN]; | ||||
char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], stri | ||||
p5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN]; | ||||
char *buf; | ||||
char warea[MAXLEN]; | ||||
char Subject[120]; | ||||
int totuser=0; | ||||
time_t t; | ||||
struct tm *local; | ||||
int cstatus; | ||||
struct getwordstruct gwarea; | ||||
struct generalitemstruct item; | ||||
longline line; | ||||
const struct userinfostruct *uinfo; | ||||
snprintf(wger,sizeof(wger),"%s/sarg-general",dirname); | /*! | |||
if((fp_in=fopen(wger,"r"))==NULL) { | * Generate a file name to write the e-mail and open the file. | |||
debugapos("email",_("Cannot open file \"%s\": %s\n"),wger,strerro | * | |||
r(errno)); | * \param Module The module for which the e-mail is generated. | |||
exit(EXIT_FAILURE); | * | |||
} | * \return The file to which the e-mail can be written. | |||
*/ | ||||
snprintf(top1,sizeof(top1),"%s/top",dirname); | FILE *Email_OutputFile(const char *Module) | |||
if((fp_top1=fopen(top1,"w"))==NULL) { | { | |||
debugapos("email",_("Cannot open file \"%s\": %s\n"),top1,strerro | FILE *fp; | |||
r(errno)); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
snprintf(top2,sizeof(top2),"%s/top.tmp",dirname); | if (strcmp(email,"stdout") == 0) { | |||
if((fp_top2=fopen(top2,"w"))==NULL) { | EmailFileName[0]='\0'; | |||
debugapos("email",_("Cannot open file \"%s\": %s\n"),top2,strerro | return(stdout); | |||
r(errno)); | ||||
exit(EXIT_FAILURE); | ||||
} | } | |||
olduser[0]='\0'; | format_path(__FILE__, __LINE__, EmailFileName, sizeof(EmailFileName), "%s | |||
totuser=0; | /%s.int_unsort", tmp, Module); | |||
if ((fp=fopen(EmailFileName,"w"))==NULL) { | ||||
if ((line=longline_create())==NULL) { | debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),Email | |||
debuga(_("Not enough memory to read file \"%s\"\n"),wger); | FileName,strerror(errno)); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
return(fp); | ||||
} | ||||
while((buf=longline_read(fp_in,line))!=NULL) { | /*! | |||
ger_read(buf,&item,wger); | * Send the e-mail. | |||
if(item.total) continue; | * | |||
if(strcmp(olduser,item.user) != 0) { | * \param fp The file opened by Email_OutputFile(). | |||
totuser++; | */ | |||
void Email_Send(FILE *fp,const char *Subject) | ||||
if (olduser[0] != '\0') { | { | |||
#if defined(__FreeBSD__) | char warea[MAXLEN]; | |||
fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnb | int cstatus; | |||
ytes,tnacc,tnelap); | ||||
#else | ||||
fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu | ||||
64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap); | ||||
#endif | ||||
ttnbytes+=tnbytes; | ||||
ttnacc+=tnacc; | ||||
ttnelap+=tnelap; | ||||
} | ||||
strcpy(olduser,item.user); | ||||
tnbytes=0; | ||||
tnacc=0; | ||||
tnelap=0; | ||||
} | ||||
tnbytes+=item.nbytes; | ||||
tnacc+=item.nacc; | ||||
tnelap+=item.nelap; | ||||
} | ||||
fclose(fp_in); | ||||
longline_destroy(&line); | ||||
if (olduser[0] != '\0') { | ||||
#if defined(__FreeBSD__) | ||||
fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnela | ||||
p); | ||||
#else | ||||
fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,( | ||||
uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap); | ||||
#endif | ||||
ttnbytes+=tnbytes; | ||||
ttnacc+=tnacc; | ||||
ttnelap+=tnelap; | ||||
} | ||||
fclose(fp_top2); | if (fp==stdout) return;//to stdout | |||
#ifdef ENABLE_DOUBLE_CHECK_DATA | if (fclose(fp)==EOF) { | |||
if (ttnacc!=globstat.nacc || ttnbytes!=globstat.nbytes || ttnelap!=globst | debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),EmailFi | |||
at.elap) { | leName,strerror(errno)); | |||
debuga(_("Total statistics mismatch when reading %s to produce th | ||||
e email report\n"),wger); | ||||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
#endif | ||||
if (snprintf(csort,sizeof(csort),"sort -n -T \"%s\" -t \"\t\" -r -k 2,2 - | format_path(__FILE__, __LINE__, warea, sizeof(warea), "%s -s \"%s\" \"%s\ | |||
o \"%s\" \"%s\"", TempDir, top1, top2)>=sizeof(csort)) { | " <\"%s\"", MailUtility, Subject, email, EmailFileName); | |||
debuga(_("Command too long: ")); | if (debug) | |||
debuga_more("sort -n -T \"%s\" -t \"\t\" -r -k 2,2 -o \"%s\" \"%s | debuga(__FILE__,__LINE__,_("Sending mail with command: %s\n"),war | |||
\"", TempDir, top1, top2); | ea); | |||
exit(EXIT_FAILURE); | cstatus=system(warea); | |||
} | ||||
cstatus=system(csort); | ||||
if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |||
debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)) | debuga(__FILE__,__LINE__,_("command return status %d\n"),WEXITSTA | |||
; | TUS(cstatus)); | |||
debuga(_("sort command: %s\n"),csort); | debuga(__FILE__,__LINE__,_("command: %s\n"),warea); | |||
exit(EXIT_FAILURE); | ||||
} | ||||
if (!KeepTempLog && unlink(top2)) { | ||||
debuga(_("Cannot delete \"%s\": %s\n"),top2,strerror(errno)); | ||||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
if (!KeepTempLog && unlink(EmailFileName)) { | ||||
if((fp_top1=fopen(top1,"r"))==NULL) { | debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),EmailFil | |||
debugapos("email",_("Cannot open file \"%s\": %s\n"),top1,strerro | eName,strerror(errno)); | |||
r(errno)); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
snprintf(top3,sizeof(top3),"%s/report",dirname); | ||||
if((fp_top3=fopen(top3,"w"))==NULL) { | ||||
debugapos("email",_("Cannot open file \"%s\": %s\n"),top3,strerro | ||||
r(errno)); | ||||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
safe_strcpy(strip1,_("Squid User Access Report"),sizeof(strip1)); | ||||
strip_latin(strip1); | ||||
fprintf(fp_top3,"%s\n",strip1); | ||||
safe_strcpy(strip1,_("Decreasing Access (bytes)"),sizeof(strip1)); | ||||
strip_latin(strip1); | ||||
fprintf(fp_top3,"%s\n",strip1); | ||||
safe_strcpy(strip1,_("Period"),sizeof(strip1)); | ||||
strip_latin(strip1); | ||||
fprintf(fp_top3,"%s %s\n\n",strip1,period.text); | ||||
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,_("TIME"),sizeof(strip7)); | ||||
strip_latin(strip7); | ||||
fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- | ||||
-------------------- -------- --------------- ------- ---------- ---------- --- | ||||
----\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7); | ||||
while(fgets(warea,sizeof(warea),fp_top1)) { | ||||
fixendofline(warea); | ||||
getword_start(&gwarea,warea); | ||||
if (getword(user,sizeof(user),&gwarea,'\t')<0) { | ||||
debuga(_("Invalid user ID in file \"%s\"\n"),top1); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
if (getword_atoll(&nbytes,&gwarea,'\t')<0) { | ||||
debuga(_("Invalid number of bytes in file \"%s\"\n"),top1 | ||||
); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
if (getword_atoll(&nacc,&gwarea,'\t')<0) { | ||||
debuga(_("Invalid number of accesses in file \"%s\"\n"),t | ||||
op1); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
if (getword_atoll(&elap,&gwarea,'\0')<0) { | ||||
debuga(_("Invalid elapsed time in file \"%s\"\n"),top1); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
uinfo=userinfo_find_from_id(user); | ||||
if (!uinfo) { | ||||
debuga(_("Unknown user ID %s in file \"%s\"\n"),user,top1 | ||||
); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
perc=(ttnbytes) ? nbytes * 100. / ttnbytes : 0; | ||||
perc2=(ttnelap) ? elap * 100. / ttnelap : 0; | ||||
posicao++; | ||||
#if defined(__FreeBSD__) | ||||
fprintf(fp_top3,"%7d %20s %8lld %15s %3.2lf%% %10s %10qu %3.2lf%% | ||||
\n",posicao,uinfo->label,nacc,fixnum(nbytes,1),perc,buildtime(elap),elap,perc2); | ||||
#else | ||||
fprintf(fp_top3,"%7d %20s %8"PRIu64" %15s %3.2lf%% %10s %10"PRIu6 | ||||
4" %3.2lf%%\n",posicao,uinfo->label,(uint64_t)nacc,fixnum(nbytes,1),perc,buildti | ||||
me(elap),(uint64_t)elap,perc2); | ||||
#endif | ||||
} | ||||
// output total | ||||
fputs("------- -------------------- -------- --------------- ------- ---- | ||||
------ ---------- -------\n",fp_top3); | ||||
#if defined(__FreeBSD__) | ||||
fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",_("TOTAL")," ",ttna | ||||
cc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap); | ||||
#else | ||||
fprintf(fp_top3,"%-7s %20s %8"PRIu64" %15s %8s %9s %10"PRIu64"\n",_("TOTA | ||||
L")," ",(uint64_t)ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),(uint64_t)ttn | ||||
elap); | ||||
#endif | ||||
// compute and write average | ||||
if (totuser>0) { | ||||
tnbytes=(totuser) ? ttnbytes / totuser : 0; | ||||
avgacc=ttnacc/totuser; | ||||
avgelap=ttnelap/totuser; | ||||
} else { | ||||
tnbytes=0; | ||||
avgacc=0; | ||||
avgelap=0; | ||||
} | ||||
safe_strcpy(strip1,_("AVERAGE"),sizeof(strip1)); | ||||
strip_latin(strip1); | ||||
#if defined(__FreeBSD__) | ||||
fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",avgacc,f | ||||
ixnum(tnbytes,1)," ",buildtime(avgelap),avgelap); | ||||
#else | ||||
fprintf(fp_top3,"%-7s %20s %8"PRIu64" %15s %8s %9s %10"PRIu64"\n",strip1, | ||||
" ",(uint64_t)avgacc,fixnum(tnbytes,1)," ",buildtime(avgelap),(uint64_t)avgelap) | ||||
; | ||||
#endif | ||||
fclose(fp_top1); | ||||
if (!KeepTempLog && unlink(top1)) { | ||||
debuga(_("Cannot delete \"%s\": %s\n"),top1,strerror(errno)); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
t = time(NULL); | ||||
local = localtime(&t); | ||||
fprintf(fp_top3, "\n%s\n", asctime(local)); | ||||
fclose(fp_top3); | ||||
if(strcmp(email,"stdout") == 0) { | ||||
if((fp_top3=fopen(top3,"r"))==NULL) { | ||||
debugapos("email",_("Cannot open file \"%s\": %s\n"),top3 | ||||
,strerror(errno)); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
while(fgets(warea,sizeof(warea),fp_top3)!=NULL) | ||||
fputs(warea,stdout); | ||||
} else { | ||||
/* TRANSLATORS: The string is formatted using strftime. You can u | ||||
se | ||||
* any string formatting marker allowed by strftime. The %c is th | ||||
e | ||||
* time formatted according to the currently selected locale. | ||||
*/ | ||||
strftime(Subject,sizeof(Subject),_("SARG report, %c"),local); | ||||
snprintf(warea,sizeof(warea),"%s -s \"%s\" \"%s\" <\"%s\"",MailUt | ||||
ility,Subject,email,top3); | ||||
if (debug) | ||||
debuga(_("Sending mail with command: %s\n"),warea); | ||||
cstatus=system(warea); | ||||
if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | ||||
debuga(_("command return status %d\n"),WEXITSTATUS(cstatu | ||||
s)); | ||||
debuga(_("command: %s\n"),warea); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
} | ||||
//unlinkdir(TempDir,0); | ||||
return (0); | ||||
} | } | |||
End of changes. 14 change blocks. | ||||
283 lines changed or deleted | 48 lines changed or added |