]>
Commit | Line | Data |
---|---|---|
d5c1b1c1 GS |
1 | /* |
2 | * AUTHOR: Pedro Lineu Orso orso@penguintech.com.br | |
3 | * 1998, 2005 | |
4 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net | |
5 | * | |
6 | * SARG donations: | |
7 | * please look at http://sarg.sourceforge.net/donations.php | |
8 | * --------------------------------------------------------------------- | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
23 | * | |
24 | */ | |
25 | ||
26 | #include "include/conf.h" | |
27 | ||
4bcb77cf | 28 | int getdata(char *, FILE *); |
d5c1b1c1 | 29 | void datashow(char *); |
32e71fa4 FM |
30 | static void getlog(void); |
31 | static void header(void); | |
d5c1b1c1 GS |
32 | |
33 | char dat[128]; | |
34 | char tim[128]; | |
35 | char typ[128]; | |
36 | char ouser[MAXLEN]=""; | |
37 | char ourl[MAXLEN]=""; | |
38 | ||
32e71fa4 | 39 | void realtime(void) |
d5c1b1c1 GS |
40 | { |
41 | ||
42 | getlog(); | |
43 | ||
44 | } | |
45 | ||
32e71fa4 | 46 | static void getlog(void) |
d5c1b1c1 | 47 | { |
d6e703cc | 48 | FILE *tmp, *fp, *fp_usr; |
d5c1b1c1 GS |
49 | char template1[255]="/var/tmp/sargtpl1.XXXXXX"; |
50 | char template2[255]="/var/tmp/sargtpl2.XXXXXX"; | |
51 | char cmd[512]; | |
40d1a88d | 52 | char buf[MAXLEN]; |
d6e703cc | 53 | int fd1,fd2,nreg; |
456d78a5 | 54 | int cstatus; |
d6e703cc FM |
55 | |
56 | if(UserTabFile[0] != '\0') { | |
57 | if(debug) { | |
58 | sprintf(msg,"%s: %s",text[86],UserTabFile); | |
59 | debuga(msg); | |
60 | } | |
61 | if((fp_usr=fopen(UserTabFile,"r"))==NULL) { | |
62 | fprintf(stderr, "SARG: (realtime) %s: %s - %s\n",text[45],UserTabFile,strerror(errno)); | |
63 | exit(1); | |
64 | } | |
65 | nreg = lseek(fileno(fp_usr), 0, SEEK_END); | |
66 | lseek(fileno(fp_usr), 0, 0); | |
67 | if((userfile=(char *) malloc(nreg+100))==NULL){ | |
68 | fprintf(stderr, "SARG ERROR: %s",text[87]); | |
69 | exit(1); | |
70 | } | |
71 | bzero(userfile,nreg+100); | |
72 | strncat(userfile,":",1); | |
73 | z1=0; | |
74 | z2=1; | |
40d1a88d | 75 | while(fgets(buf,sizeof(buf),fp_usr)!=NULL){ |
d6e703cc FM |
76 | buf[strlen(buf)-1]='\0'; |
77 | if(strstr(buf,"\r") != 0) buf[strlen(buf)-1]='\0'; | |
4bcb77cf FM |
78 | if (getword(bufy,sizeof(bufy),buf,' ')<0) { |
79 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserTabFile); | |
80 | exit(1); | |
81 | } | |
d6e703cc FM |
82 | for(z1=0; z1<=strlen(bufy); z1++) { |
83 | userfile[z2]=bufy[z1]; | |
84 | z2++; | |
85 | } | |
86 | strncat(userfile,":",1); | |
87 | for(z1=0; z1<=strlen(buf); z1++) { | |
88 | userfile[z2]=buf[z1]; | |
89 | z2++; | |
90 | } | |
91 | strncat(userfile,":",1); | |
92 | } | |
93 | fclose(fp_usr); | |
94 | } | |
d5c1b1c1 GS |
95 | |
96 | fd1 = mkstemp(template1); | |
97 | fd2 = mkstemp(template2); | |
98 | ||
99 | if((fd1 == -1 ) || ((tmp = fdopen (fd1, "w+" )) == NULL) ) { /* failure, bail out */ | |
100 | fprintf(stderr, "SARG: (realtime) mkstemp error - %s\n",strerror(errno)); | |
101 | exit(1); | |
102 | } | |
103 | ||
104 | sprintf(cmd,"tail -%d %s",realtime_access_log_lines,AccessLog); | |
105 | fp = popen(cmd, "r"); | |
106 | while(fgets(buf,sizeof(buf),fp) != NULL ) | |
4bcb77cf | 107 | if (getdata(buf,tmp)<0) { |
40d1a88d | 108 | fprintf(stderr,"SARG: Maybe a broken record or garbage was returned by %s.\n",cmd); |
4bcb77cf FM |
109 | exit(1); |
110 | } | |
d5c1b1c1 GS |
111 | pclose(fp); |
112 | fclose(tmp); | |
113 | ||
d6e703cc | 114 | sprintf(cmd,"sort -r -k 1,1 -k 2,2 -o %s %s",template2,template1); |
456d78a5 FM |
115 | cstatus=system(cmd); |
116 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
117 | fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus)); | |
118 | fprintf(stderr, "SARG: sort command: %s\n",cmd); | |
119 | exit(1); | |
120 | } | |
d5c1b1c1 GS |
121 | unlink(template1); |
122 | datashow(template2); | |
123 | } | |
124 | ||
4bcb77cf | 125 | int getdata(char *rec, FILE *ftmp) |
d5c1b1c1 GS |
126 | { |
127 | time_t tt; | |
128 | struct tm *t; | |
129 | ||
4bcb77cf | 130 | if (getword3(dat,sizeof(dat),rec,' ')<0) { |
40d1a88d | 131 | fprintf(stderr,"SARG: The time stamp at column 1 is too long.\n"); |
4bcb77cf FM |
132 | return(-1); |
133 | } | |
134 | if (getword3(warea,sizeof(warea),rec,' ')<0) { | |
40d1a88d | 135 | fprintf(stderr,"SARG: The connection duration at column 2 is too long.\n"); |
4bcb77cf FM |
136 | return(-1); |
137 | } | |
d5c1b1c1 | 138 | while(strcmp(warea,"") == 0 && strlen(rec) > 0) |
4bcb77cf FM |
139 | if (getword3(warea,sizeof(warea),rec,' ')<0) { |
140 | return(-1); | |
141 | } | |
142 | if (getword3(ip,sizeof(ip),rec,' ')<0) { | |
40d1a88d | 143 | fprintf(stderr,"SARG: The IP address at column 3 is too long.\n"); |
4bcb77cf FM |
144 | return(-1); |
145 | } | |
146 | if (getword3(warea,sizeof(warea),rec,' ')<0) { | |
40d1a88d | 147 | fprintf(stderr,"SARG: The status at column 4 is too long.\n"); |
4bcb77cf FM |
148 | return(-1); |
149 | } | |
150 | if (getword3(warea,sizeof(warea),rec,' ')<0) { | |
40d1a88d | 151 | fprintf(stderr,"SARG: The size at column 5 is too long.\n"); |
4bcb77cf FM |
152 | return(-1); |
153 | } | |
154 | if (getword3(typ,sizeof(typ),rec,' ')<0) { | |
40d1a88d | 155 | fprintf(stderr,"SARG: The action at column 6 is too long.\n"); |
4bcb77cf FM |
156 | return(-1); |
157 | } | |
d5c1b1c1 | 158 | if(strncmp(typ,"CONNECT",7) == 0) { |
4bcb77cf FM |
159 | if (getword3(url,sizeof(url),rec,' ')<0) { |
160 | return(-1); | |
161 | } | |
162 | if (getword3(user,sizeof(user),rec,' ')<0) { | |
163 | return(-1); | |
164 | } | |
40d1a88d FM |
165 | }else { |
166 | if (getword3(url,sizeof(url),rec,'/')<0) { | |
167 | fprintf(stderr,"SARG: The URL at column 7 is too long.\n"); | |
168 | return(-1); | |
169 | } | |
170 | if (getword3(url,sizeof(url),rec,'/')<0) { | |
171 | fprintf(stderr,"SARG: The URL at column 7 is too long.\n"); | |
172 | return(-1); | |
173 | } | |
174 | if (getword3(url,sizeof(url),rec,'/')<0) { | |
175 | fprintf(stderr,"SARG: The URL at column 7 is too long.\n"); | |
176 | return(-1); | |
177 | } | |
178 | if (getword3(user,sizeof(user),rec,' ')<0) { | |
179 | fprintf(stderr,"SARG: The data at column 8 is too long.\n"); | |
180 | return(-1); | |
181 | } | |
182 | if (getword3(user,sizeof(user),rec,' ')<0) { | |
183 | fprintf(stderr,"SARG: The user at column 9 is too long.\n"); | |
184 | return(-1); | |
185 | } | |
d5c1b1c1 GS |
186 | } |
187 | ||
188 | if(strncmp(user,"-",1) == 0 && strcmp(RealtimeUnauthRec,"ignore") == 0) | |
4bcb77cf | 189 | return(0); |
d5c1b1c1 GS |
190 | |
191 | tt=atoi(dat); | |
192 | t=localtime(&tt); | |
193 | if(strncmp(DateFormat,"u",1) == 0) | |
194 | strftime(tbuf, 127, "%Y-%m-%d %H:%M", t); | |
195 | else if(strncmp(DateFormat,"e",1) == 0) | |
196 | strftime(tbuf, 127, "%d-%m-%Y %H:%M", t); | |
197 | ||
40d1a88d | 198 | fprintf(ftmp,"%s %s %s %s %s\n",tbuf,ip,user,url,typ); |
4bcb77cf | 199 | return(0); |
d5c1b1c1 GS |
200 | } |
201 | ||
202 | void datashow(char *tmp) | |
203 | { | |
204 | FILE *fin; | |
205 | char buf[MAXLEN]; | |
206 | ||
207 | if((fin=fopen(tmp,"r"))==NULL) { | |
208 | fprintf(stderr, "SARG: (realtime) open error %s - %s\n",tmp,strerror(errno)); | |
209 | exit(1); | |
210 | } | |
211 | ||
212 | header(); | |
213 | ||
214 | while(fgets(buf, MAXLEN, fin)) { | |
215 | buf[strlen(buf)-1]='\0'; | |
4bcb77cf FM |
216 | if (getword3(dat,sizeof(dat),buf,' ')<0) { |
217 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); | |
218 | exit(1); | |
219 | } | |
220 | if (getword3(tim,sizeof(tim),buf,' ')<0) { | |
221 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); | |
222 | exit(1); | |
223 | } | |
224 | if (getword3(ip,sizeof(ip),buf,' ')<0) { | |
225 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); | |
226 | exit(1); | |
227 | } | |
228 | if (getword3(user,sizeof(user),buf,' ')<0) { | |
229 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); | |
230 | exit(1); | |
231 | } | |
d5c1b1c1 | 232 | if(strlen(dat) < 3 || strlen(user) < 1) continue; |
4bcb77cf FM |
233 | if (getword3(url,sizeof(url),buf,' ')<0) { |
234 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); | |
235 | exit(1); | |
236 | } | |
237 | if (getword3(typ,sizeof(typ),buf,' ')<0) { | |
238 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); | |
239 | exit(1); | |
240 | } | |
d5c1b1c1 GS |
241 | if(strstr(RealtimeTypes,typ) == 0) |
242 | continue; | |
243 | ||
244 | if(strcmp(ouser,user) == 0 && strcmp(ourl,url) == 0) | |
245 | continue; | |
246 | ||
d6e703cc FM |
247 | strcpy(u2,user); |
248 | if(strcmp(Ip2Name,"yes") == 0) | |
a1c55d8c | 249 | ip2name(u2,sizeof(u2)); |
d6e703cc FM |
250 | if(UserTabFile[0] != '\0') { |
251 | sprintf(warea,":%s:",u2); | |
252 | if((str=(char *) strstr(userfile,warea)) != (char *) NULL ) { | |
253 | z1=0; | |
254 | str2=(char *) strstr(str+1,":"); | |
255 | str2++; | |
256 | bzero(name, MAXLEN); | |
257 | while(str2[z1] != ':') { | |
258 | name[z1]=str2[z1]; | |
259 | z1++; | |
260 | } | |
261 | } else strcpy(name,u2); | |
262 | } else strcpy(name,u2); | |
263 | ||
264 | if(dotinuser && strstr(name,"_")) { | |
265 | str2=(char *)subs(name,"_","."); | |
266 | strcpy(name,str2); | |
267 | } | |
268 | ||
269 | printf("<tr><td class=\"data\">%s %s</td><td class=\"data3\">%s</td><td class=\"data3\">%s</td><td class=\"data3\">%s</td><td class=\"data2\"><a href=\"http://%s\">%s</td></tr>\n",dat,tim,ip,name,typ,url,url); | |
d5c1b1c1 GS |
270 | strcpy(ouser,user); |
271 | strcpy(ourl,url); | |
272 | } | |
273 | ||
274 | puts("</table>\n</html>\n"); | |
275 | fclose(fin); | |
276 | unlink(tmp); | |
277 | fflush(NULL); | |
278 | ||
279 | } | |
280 | ||
32e71fa4 | 281 | static void header(void) |
d5c1b1c1 GS |
282 | { |
283 | puts("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\""); | |
284 | puts(" \"http://www.w3.org/TR/html4/loose.dtd\">\n"); | |
285 | puts("<html>\n"); | |
286 | puts("<head>\n"); | |
287 | if(realtime_refresh) | |
288 | printf(" <meta http-equiv=refresh content=\"%d\" url=\"sarg-php/sarg-realtime.php\"; charset=\"%s\">\n",realtime_refresh,CharSet); | |
289 | else | |
290 | printf(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet); | |
291 | css(stdout); | |
292 | puts("</head>\n"); | |
293 | printf(buf,"<body style=\"font-family:%s;font-size:%s;background-color:%s;background-image:url(%s)\">\n",FontFace,TitleFontSize,BgColor,BgImage); | |
294 | puts("<center><table cellpadding=\"1\" cellspacing=\"1\">\n"); | |
295 | printf("<tr><th class=\"title2\" colspan=\"10\">SARG %s</th></tr>\n",text[134]); | |
296 | printf("<tr><th class=\"text\" colspan=\"10\">%s: %d s</th></tr>\n",text[136],realtime_refresh); | |
297 | printf("<tr><th class=\"header3\">%s</th><th class=\"header3\">%s</th><th class=\"header3\">%s</th><th class=\"header3\">%s</th><th class=\"header\">%s</th></tr>\n",text[110],text[111],text[98],text[135],text[91]); | |
298 | } |