]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
1164c474 | 3 | * 1998, 2010 |
25697a35 GS |
4 | * |
5 | * SARG donations: | |
6 | * please look at http://sarg.sourceforge.net/donations.php | |
1164c474 FM |
7 | * Support: |
8 | * http://sourceforge.net/projects/sarg/forums/forum/363374 | |
25697a35 GS |
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" | |
5f3cfd1d | 28 | #include "include/defs.h" |
25697a35 | 29 | |
32e71fa4 | 30 | int geramail(const char *dirname, int debug, const char *outdir, int userip, const char *email, const char *TempDir) |
25697a35 GS |
31 | { |
32 | ||
33 | FILE *fp_in, *fp_top1, *fp_top2, *fp_top3; | |
34 | long long int ttnbytes=0, ttnacc=0, tnacc=0; | |
35 | long long int tnbytes=0, ttnelap=0, tnelap=0; | |
2240dcea FM |
36 | long long int nacc, nbytes, elap; |
37 | double perc=0.00; | |
38 | double perc2=0.00; | |
25697a35 | 39 | int posicao=0; |
2240dcea FM |
40 | char olduser[MAX_USER_LEN], csort[MAXLEN], period[MAXLEN], arqper[MAXLEN]; |
41 | char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAX_USER_LEN], tusr[MAXLEN]; | |
25697a35 | 42 | char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN]; |
06b39c87 | 43 | char buf[MAXLEN]; |
25697a35 GS |
44 | int totuser=0; |
45 | time_t t; | |
46 | struct tm *local; | |
456d78a5 | 47 | int cstatus; |
fb7c5f27 | 48 | char warea[MAXLEN]; |
9c7c6346 | 49 | struct getwordstruct gwarea; |
2240dcea | 50 | struct generalitemstruct item; |
25697a35 GS |
51 | |
52 | strcpy(wger,dirname); | |
53 | strcpy(top1,dirname); | |
54 | strcpy(top2,dirname); | |
55 | strcpy(top3,dirname); | |
56 | strcpy(tusr,dirname); | |
d6e703cc | 57 | strcat(wger,"/sarg-general"); |
25697a35 GS |
58 | strcat(top1,"/top"); |
59 | strcat(top2,"/top.tmp"); | |
60 | strcat(top3,"/report"); | |
d6e703cc | 61 | strcat(tusr,"/sarg-users"); |
25697a35 GS |
62 | |
63 | if((fp_in=fopen(wger,"r"))==NULL) { | |
9f70c14e | 64 | debuga(_("(email) Cannot open file %s\n"),wger); |
06b39c87 | 65 | exit(EXIT_FAILURE); |
25697a35 GS |
66 | } |
67 | ||
68 | if((fp_top1=fopen(top1,"w"))==NULL) { | |
9f70c14e | 69 | debuga(_("(email) Cannot open file %s\n"),top1); |
06b39c87 | 70 | exit(EXIT_FAILURE); |
25697a35 GS |
71 | } |
72 | ||
73 | if((fp_top2=fopen(top2,"w"))==NULL) { | |
9f70c14e | 74 | debuga(_("(email) Cannot open file %s\n"),top2); |
06b39c87 | 75 | exit(EXIT_FAILURE); |
25697a35 GS |
76 | } |
77 | ||
fb7c5f27 FM |
78 | olduser[0]='\0'; |
79 | totuser=0; | |
80 | ||
9c7c6346 | 81 | while(fgets(warea,sizeof(warea),fp_in)) { |
2240dcea FM |
82 | ger_read(warea,&item,wger); |
83 | if(item.total) continue; | |
84 | if(strcmp(olduser,item.user) != 0) | |
25697a35 | 85 | { |
fb7c5f27 | 86 | totuser++; |
25697a35 | 87 | |
fb7c5f27 | 88 | if (olduser[0] != '\0') { |
25697a35 | 89 | #if defined(__FreeBSD__) |
120d768c | 90 | fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap); |
25697a35 | 91 | #elif defined(__alpha) || __ALPHA |
120d768c | 92 | fprintf(fp_top2,"%s\t%ld\t%ld\t%ld\n",olduser,tnbytes,tnacc,tnelap); |
25697a35 | 93 | #else |
120d768c | 94 | fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap); |
25697a35 | 95 | #endif |
2240dcea FM |
96 | ttnbytes+=tnbytes; |
97 | ttnacc+=tnacc; | |
98 | ttnelap+=tnelap; | |
fb7c5f27 | 99 | } |
2240dcea | 100 | strcpy(olduser,item.user); |
25697a35 GS |
101 | tnbytes=0; |
102 | tnacc=0; | |
103 | tnelap=0; | |
104 | } | |
105 | ||
2240dcea FM |
106 | tnbytes+=item.nbytes; |
107 | tnacc+=item.nacc; | |
108 | tnelap+=item.nelap; | |
25697a35 | 109 | } |
fb7c5f27 FM |
110 | |
111 | if (olduser[0] != '\0') { | |
25697a35 | 112 | #if defined(__FreeBSD__) |
120d768c | 113 | fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap); |
25697a35 | 114 | #elif defined(__alpha) || __ALPHA |
120d768c | 115 | fprintf(fp_top2,"%s\t%ld\t%ld\t%ld\n",olduser,tnbytes,tnacc,tnelap); |
25697a35 | 116 | #else |
120d768c | 117 | fprintf(fp_top2,"%s\t%lld\t%lld\t%lld\n",olduser,tnbytes,tnacc,tnelap); |
25697a35 | 118 | #endif |
2240dcea FM |
119 | ttnbytes+=tnbytes; |
120 | ttnacc+=tnacc; | |
121 | ttnelap+=tnelap; | |
fb7c5f27 | 122 | } |
25697a35 | 123 | |
25697a35 GS |
124 | fclose(fp_in); |
125 | fclose(fp_top2); | |
126 | ||
9a2efbd0 | 127 | sprintf(csort,"sort -n -T \"%s\" -r -k 2,2 -o \"%s\" \"%s\"", TempDir, top1, top2); |
456d78a5 FM |
128 | cstatus=system(csort); |
129 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
10210234 FM |
130 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); |
131 | debuga(_("sort command: %s\n"),csort); | |
06b39c87 | 132 | exit(EXIT_FAILURE); |
456d78a5 | 133 | } |
25697a35 GS |
134 | |
135 | unlink(top2); | |
136 | ||
137 | if((fp_top1=fopen(top1,"a"))==NULL) { | |
9f70c14e | 138 | debuga(_("(email) Cannot open file %s\n"),top1); |
06b39c87 | 139 | exit(EXIT_FAILURE); |
25697a35 | 140 | } |
354c1a68 | 141 | #if defined(__FreeBSD__) |
120d768c | 142 | fprintf(fp_top1,"TOTAL\t%qu\t%qu\t%qu\n",ttnbytes,ttnacc,ttnelap); |
354c1a68 | 143 | #elif defined(__alpha) || __ALPHA |
120d768c | 144 | fprintf(fp_top1,"TOTAL\t%ld\t%ld\t%ld\n",ttnbytes,ttnacc,ttnelap); |
354c1a68 | 145 | #else |
120d768c | 146 | fprintf(fp_top1,"TOTAL\t%lld\t%lld\t%lld\n",ttnbytes,ttnacc,ttnelap); |
354c1a68 | 147 | #endif |
25697a35 GS |
148 | fclose(fp_top1); |
149 | ||
fb7c5f27 FM |
150 | /* |
151 | * Obtem o period | |
152 | */ | |
25697a35 GS |
153 | |
154 | strcpy(arqper,dirname); | |
d6e703cc | 155 | strcat(arqper,"/sarg-period"); |
25697a35 GS |
156 | |
157 | if ((fp_in = fopen(arqper, "r")) == 0){ | |
9f70c14e | 158 | debuga(_("(email) Cannot open file %s\n"),arqper); |
06b39c87 | 159 | exit(EXIT_FAILURE); |
25697a35 GS |
160 | } |
161 | ||
05b90947 | 162 | if (!fgets(period,sizeof(period),fp_in)) { |
9f70c14e | 163 | debuga(_("(email) read error in %s\n"),arqper); |
06b39c87 | 164 | exit(EXIT_FAILURE); |
05b90947 | 165 | } |
25697a35 GS |
166 | fclose(fp_in); |
167 | ||
fb7c5f27 | 168 | if((fp_top1=fopen(top1,"r"))==NULL) { |
9f70c14e | 169 | debuga(_("(email) Cannot open file %s\n"),top1); |
06b39c87 | 170 | exit(EXIT_FAILURE); |
fb7c5f27 FM |
171 | } |
172 | ||
173 | if((fp_top3=fopen(top3,"w"))==NULL) { | |
9f70c14e | 174 | debuga(_("(email) Cannot open file %s\n"),top3); |
06b39c87 | 175 | exit(EXIT_FAILURE); |
fb7c5f27 FM |
176 | } |
177 | ||
c36c7384 | 178 | strcpy(strip1,_("Squid User Access Report")); |
25697a35 | 179 | strip_latin(strip1); |
354c1a68 | 180 | fprintf(fp_top3,"%s\n",strip1); |
25697a35 | 181 | |
c36c7384 | 182 | strcpy(strip1,_("Decreasing Access (bytes)")); |
25697a35 | 183 | strip_latin(strip1); |
354c1a68 | 184 | fprintf(fp_top3,"%s\n",strip1); |
25697a35 | 185 | |
c36c7384 | 186 | strcpy(strip1,_("Period")); |
25697a35 | 187 | strip_latin(strip1); |
354c1a68 | 188 | fprintf(fp_top3,"%s %s\n\n",strip1,period); |
25697a35 | 189 | |
c36c7384 | 190 | strcpy(strip1,_("NUM")); |
25697a35 | 191 | strip_latin(strip1); |
c36c7384 | 192 | strcpy(strip2,_("USERID")); |
25697a35 | 193 | strip_latin(strip2); |
c36c7384 | 194 | strcpy(strip3,_("CONNECT")); |
25697a35 | 195 | strip_latin(strip3); |
c36c7384 | 196 | strcpy(strip4,_("BYTES")); |
25697a35 | 197 | strip_latin(strip4); |
c36c7384 | 198 | strcpy(strip5,_("ELAPSED TIME")); |
25697a35 | 199 | strip_latin(strip5); |
c36c7384 | 200 | strcpy(strip6,_("MILISEC")); |
25697a35 | 201 | strip_latin(strip6); |
c36c7384 | 202 | strcpy(strip7,_("TIME")); |
25697a35 GS |
203 | strip_latin(strip7); |
204 | ||
354c1a68 | 205 | fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7); |
25697a35 | 206 | |
fb7c5f27 | 207 | while(fgets(warea,sizeof(warea),fp_top1)) |
25697a35 | 208 | { |
9c7c6346 FM |
209 | getword_start(&gwarea,warea); |
210 | if (getword(user,sizeof(user),&gwarea,'\t')<0) { | |
10210234 | 211 | debuga(_("There is an invalid user ID in file %s\n"),top1); |
06b39c87 | 212 | exit(EXIT_FAILURE); |
fb7c5f27 | 213 | } |
2240dcea | 214 | if (getword_atoll(&nbytes,&gwarea,'\t')<0) { |
10210234 | 215 | debuga(_("There is an invalid number of bytes in file %s\n"),top1); |
06b39c87 | 216 | exit(EXIT_FAILURE); |
fb7c5f27 | 217 | } |
2240dcea | 218 | if (getword_atoll(&nacc,&gwarea,'\t')<0) { |
10210234 | 219 | debuga(_("There is an invalid number of access in file %s\n"),top1); |
06b39c87 | 220 | exit(EXIT_FAILURE); |
fb7c5f27 | 221 | } |
2240dcea | 222 | if (getword_atoll(&elap,&gwarea,'\t')<0) { |
10210234 | 223 | debuga(_("There is an invalid elapsed time in file %s\n"),top1); |
06b39c87 | 224 | exit(EXIT_FAILURE); |
fb7c5f27 | 225 | } |
25697a35 | 226 | |
2240dcea FM |
227 | perc=(ttnbytes) ? nbytes * 100. / ttnbytes : 0; |
228 | perc2=(ttnelap) ? elap * 100. / ttnelap : 0; | |
25697a35 GS |
229 | |
230 | posicao++; | |
25697a35 | 231 | |
fb7c5f27 | 232 | if(strcmp(user,"TOTAL") == 0){ |
354c1a68 | 233 | fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3); |
25697a35 | 234 | #if defined(__FreeBSD__) |
c36c7384 | 235 | fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",_("TOTAL")," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap); |
25697a35 | 236 | #elif defined(__alpha) || __ALPHA |
c36c7384 | 237 | fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",_("TOTAL")," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap); |
25697a35 | 238 | #else |
c36c7384 | 239 | fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",_("TOTAL")," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap); |
25697a35 | 240 | #endif |
354c1a68 | 241 | } else { |
25697a35 | 242 | #if defined(__FreeBSD__) |
f2ec8c75 | 243 | fprintf(fp_top3,"%7d %20s %8lld %15s %3.2lf%% %10s %10qu %3.2lf%%\n",posicao,user,nacc,fixnum(nbytes,1),perc,buildtime(elap),elap,perc2); |
25697a35 | 244 | #elif defined(__alpha) || __ALPHA |
f2ec8c75 | 245 | fprintf(fp_top3,"%7d %20s %8lld %15s %3.2lf%% %10s %10ld %3.2lf%%\n",posicao,user,nacc,fixnum(nbytes,1),perc,buildtime(elap),elap,perc2); |
25697a35 | 246 | #else |
f2ec8c75 | 247 | fprintf(fp_top3,"%7d %20s %8lld %15s %3.2lf%% %10s %10lld %3.2lf%%\n",posicao,user,nacc,fixnum(nbytes,1),perc,buildtime(elap),elap,perc2); |
25697a35 GS |
248 | #endif |
249 | } | |
25697a35 GS |
250 | } |
251 | ||
2240dcea | 252 | tnbytes=(totuser) ? ttnbytes / totuser : 0; |
25697a35 | 253 | |
c36c7384 | 254 | strcpy(strip1,_("AVERAGE")); |
25697a35 GS |
255 | strip_latin(strip1); |
256 | #if defined(__FreeBSD__) | |
354c1a68 | 257 | fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser); |
25697a35 | 258 | #elif defined(__alpha) || __ALPHA |
354c1a68 | 259 | fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser); |
25697a35 | 260 | #else |
354c1a68 | 261 | fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser); |
25697a35 | 262 | #endif |
25697a35 GS |
263 | |
264 | fclose(fp_top1); | |
265 | unlink(top1); | |
266 | ||
267 | t = time(NULL); | |
268 | local = localtime(&t); | |
354c1a68 | 269 | fprintf(fp_top3, "\n%s\n", asctime(local)); |
25697a35 GS |
270 | |
271 | fclose(fp_top3); | |
272 | ||
273 | if(strcmp(email,"stdout") == 0) { | |
274 | if((fp_top3=fopen(top3,"r"))==NULL) { | |
9f70c14e | 275 | debuga(_("(email) Cannot open file %s\n"),top3); |
06b39c87 | 276 | exit(EXIT_FAILURE); |
25697a35 GS |
277 | } |
278 | ||
279 | while(fgets(buf,sizeof(buf),fp_top3)!=NULL) | |
f2ec8c75 | 280 | fputs(buf,stdout); |
25697a35 | 281 | } else { |
06b39c87 | 282 | snprintf(buf,sizeof(buf),"\"%s\" -s \"SARG %s, %s\" \"%s\" <\"%s\"",MailUtility,_("Report"),asctime(local),email,top3); |
456d78a5 FM |
283 | cstatus=system(buf); |
284 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
10210234 FM |
285 | debuga(_("command return status %d\n"),WEXITSTATUS(cstatus)); |
286 | debuga(_("command: %s\n"),buf); | |
06b39c87 | 287 | exit(EXIT_FAILURE); |
456d78a5 FM |
288 | } |
289 | } | |
fb7c5f27 | 290 | |
51465d08 | 291 | if (snprintf(warea,sizeof(warea),"%s/sarg",TempDir)>=sizeof(warea)) { |
10210234 | 292 | debuga(_("Temporary directory name too long: %s\n"),warea); |
06b39c87 | 293 | exit(EXIT_FAILURE); |
05b90947 | 294 | } |
51465d08 | 295 | unlinkdir(warea,0); |
25697a35 GS |
296 | |
297 | return (0); | |
298 | } |