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