]>
Commit | Line | Data |
---|---|---|
d5c1b1c1 | 1 | /* |
d5c1b1c1 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
1164c474 | 3 | * 1998, 2010 |
d5c1b1c1 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 | |
d5c1b1c1 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" |
d5c1b1c1 | 29 | |
120d768c FM |
30 | static int getdata(char*, FILE*); |
31 | static void datashow(const char *); | |
32e71fa4 FM |
32 | static void getlog(void); |
33 | static void header(void); | |
d5c1b1c1 | 34 | |
32e71fa4 | 35 | void realtime(void) |
d5c1b1c1 GS |
36 | { |
37 | ||
38 | getlog(); | |
39 | ||
40 | } | |
41 | ||
32e71fa4 | 42 | static void getlog(void) |
d5c1b1c1 | 43 | { |
936c9905 | 44 | FILE *tmp, *fp; |
d5c1b1c1 GS |
45 | char template1[255]="/var/tmp/sargtpl1.XXXXXX"; |
46 | char template2[255]="/var/tmp/sargtpl2.XXXXXX"; | |
47 | char cmd[512]; | |
ac422f9b | 48 | char *buf; |
936c9905 | 49 | int fd1,fd2; |
456d78a5 | 50 | int cstatus; |
ac422f9b | 51 | struct longlinestruct line; |
d6e703cc | 52 | |
965c4a6f | 53 | init_usertab(UserTabFile); |
d5c1b1c1 GS |
54 | |
55 | fd1 = mkstemp(template1); | |
56 | fd2 = mkstemp(template2); | |
57 | ||
58 | if((fd1 == -1 ) || ((tmp = fdopen (fd1, "w+" )) == NULL) ) { /* failure, bail out */ | |
f2ec8c75 | 59 | debuga(_("(realtime) mkstemp error - %s"),strerror(errno)); |
d5c1b1c1 GS |
60 | exit(1); |
61 | } | |
62 | ||
ac422f9b FM |
63 | if (longline_prepare(&line)<0) { |
64 | debuga(_("Not enough memory to read the log file")); | |
65 | exit(1); | |
66 | } | |
67 | ||
0a4e18e1 | 68 | sprintf(cmd,"tail -%d %s",realtime_access_log_lines,AccessLog[0]); |
d5c1b1c1 | 69 | fp = popen(cmd, "r"); |
ac422f9b | 70 | while((buf=longline_read(fp,&line)) != NULL ) |
4bcb77cf | 71 | if (getdata(buf,tmp)<0) { |
f2ec8c75 | 72 | debuga(_("Maybe a broken record or garbage was returned by %s"),cmd); |
4bcb77cf FM |
73 | exit(1); |
74 | } | |
d5c1b1c1 GS |
75 | pclose(fp); |
76 | fclose(tmp); | |
ac422f9b | 77 | longline_free(&line); |
d5c1b1c1 | 78 | |
9a2efbd0 | 79 | sprintf(cmd,"sort -r -k 1,1 -k 2,2 -o \"%s\" \"%s\"",template2,template1); |
456d78a5 FM |
80 | cstatus=system(cmd); |
81 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
f2ec8c75 FM |
82 | debuga(_("sort command return status %d"),WEXITSTATUS(cstatus)); |
83 | debuga(_("sort command: %s"),cmd); | |
456d78a5 FM |
84 | exit(1); |
85 | } | |
d5c1b1c1 GS |
86 | unlink(template1); |
87 | datashow(template2); | |
88 | } | |
89 | ||
120d768c | 90 | static int getdata(char *rec, FILE *ftmp) |
d5c1b1c1 GS |
91 | { |
92 | time_t tt; | |
93 | struct tm *t; | |
ac422f9b | 94 | char dat[128]; |
120d768c | 95 | char tbuf[128]; |
e5b2c6f0 | 96 | char typ[128]; |
9c7c6346 | 97 | char warea[MAXLEN]; |
2240dcea | 98 | char user[MAX_USER_LEN]; |
e5b2c6f0 | 99 | char *url; |
9c7c6346 | 100 | struct getwordstruct gwarea; |
d5c1b1c1 | 101 | |
9c7c6346 FM |
102 | getword_start(&gwarea,rec); |
103 | if (getword(dat,sizeof(dat),&gwarea,' ')<0) { | |
f2ec8c75 | 104 | debuga(_("The time stamp at column 1 is too long.")); |
4bcb77cf FM |
105 | return(-1); |
106 | } | |
9c7c6346 | 107 | if (getword(warea,sizeof(warea),&gwarea,' ')<0) { |
f2ec8c75 | 108 | debuga(_("The connection duration at column 2 is too long.")); |
4bcb77cf FM |
109 | return(-1); |
110 | } | |
9c7c6346 FM |
111 | while(strcmp(warea,"") == 0 && gwarea.current[0] != '\0') |
112 | if (getword(warea,sizeof(warea),&gwarea,' ')<0) { | |
4bcb77cf FM |
113 | return(-1); |
114 | } | |
9c7c6346 | 115 | if (getword(ip,sizeof(ip),&gwarea,' ')<0) { |
f2ec8c75 | 116 | debuga(_("The IP address at column 3 is too long.")); |
4bcb77cf FM |
117 | return(-1); |
118 | } | |
9c7c6346 | 119 | if (getword_skip(MAXLEN,&gwarea,' ')<0) { |
f2ec8c75 | 120 | debuga(_("The status at column 4 is too long.")); |
4bcb77cf FM |
121 | return(-1); |
122 | } | |
9c7c6346 | 123 | if (getword_skip(MAXLEN,&gwarea,' ')<0) { |
f2ec8c75 | 124 | debuga(_("The size at column 5 is too long.")); |
4bcb77cf FM |
125 | return(-1); |
126 | } | |
9c7c6346 | 127 | if (getword(typ,sizeof(typ),&gwarea,' ')<0) { |
f2ec8c75 | 128 | debuga(_("The action at column 6 is too long.")); |
4bcb77cf FM |
129 | return(-1); |
130 | } | |
d5c1b1c1 | 131 | if(strncmp(typ,"CONNECT",7) == 0) { |
e5b2c6f0 | 132 | if (getword_ptr(rec,&url,&gwarea,' ')<0) { |
2240dcea | 133 | debuga(_("The URL at column 7 is too long.")); |
4bcb77cf FM |
134 | return(-1); |
135 | } | |
9c7c6346 | 136 | if (getword(user,sizeof(user),&gwarea,' ')<0) { |
2240dcea | 137 | debuga(_("The user ID at column 8 is too long.")); |
4bcb77cf FM |
138 | return(-1); |
139 | } | |
40d1a88d | 140 | }else { |
9c7c6346 | 141 | if (getword_skip(MAXLEN,&gwarea,'/')<0) { |
f2ec8c75 | 142 | debuga(_("The URL at column 7 is too long.")); |
40d1a88d FM |
143 | return(-1); |
144 | } | |
9c7c6346 | 145 | if (getword_skip(MAXLEN,&gwarea,'/')<0) { |
f2ec8c75 | 146 | debuga(_("The URL at column 7 is too long.")); |
40d1a88d FM |
147 | return(-1); |
148 | } | |
e5b2c6f0 FM |
149 | if (getword_ptr(rec,&url,&gwarea,'/')<0) { |
150 | debuga(_("The URL at column 7 is too long")); | |
40d1a88d FM |
151 | return(-1); |
152 | } | |
9c7c6346 | 153 | if (getword_skip(MAXLEN,&gwarea,' ')<0) { |
e5b2c6f0 | 154 | debuga(_("The data at column 8 is too long")); |
40d1a88d FM |
155 | return(-1); |
156 | } | |
9c7c6346 | 157 | if (getword(user,sizeof(user),&gwarea,' ')<0) { |
e5b2c6f0 | 158 | debuga(_("The user at column 9 is too long")); |
40d1a88d FM |
159 | return(-1); |
160 | } | |
d5c1b1c1 GS |
161 | } |
162 | ||
163 | if(strncmp(user,"-",1) == 0 && strcmp(RealtimeUnauthRec,"ignore") == 0) | |
4bcb77cf | 164 | return(0); |
d5c1b1c1 GS |
165 | |
166 | tt=atoi(dat); | |
167 | t=localtime(&tt); | |
168 | if(strncmp(DateFormat,"u",1) == 0) | |
c4dd8d9c | 169 | strftime(tbuf, sizeof(tbuf), "%Y-%m-%d\t%H:%M", t); |
d5c1b1c1 | 170 | else if(strncmp(DateFormat,"e",1) == 0) |
c4dd8d9c | 171 | strftime(tbuf, sizeof(tbuf), "%d-%m-%Y\t%H:%M", t); |
d5c1b1c1 | 172 | |
120d768c | 173 | fprintf(ftmp,"%s\t%s\t%s\t%s\t%s\n",tbuf,ip,user,url,typ); |
4bcb77cf | 174 | return(0); |
d5c1b1c1 GS |
175 | } |
176 | ||
120d768c | 177 | static void datashow(const char *tmp) |
d5c1b1c1 GS |
178 | { |
179 | FILE *fin; | |
ac422f9b FM |
180 | char dat[128]; |
181 | char tim[128]; | |
e5b2c6f0 FM |
182 | char *buf; |
183 | char *url; | |
184 | char *ourl=NULL; | |
2240dcea | 185 | char ouser[MAX_USER_LEN]=""; |
e5b2c6f0 | 186 | char typ[128]; |
2240dcea FM |
187 | char user[MAX_USER_LEN]; |
188 | char u2[MAX_USER_LEN]; | |
e5b2c6f0 FM |
189 | int url_len; |
190 | int ourl_size=0; | |
9c7c6346 | 191 | struct getwordstruct gwarea; |
e5b2c6f0 | 192 | struct longlinestruct line; |
d5c1b1c1 GS |
193 | |
194 | if((fin=fopen(tmp,"r"))==NULL) { | |
f2ec8c75 | 195 | debuga(_("(realtime) open error %s - %s"),tmp,strerror(errno)); |
d5c1b1c1 GS |
196 | exit(1); |
197 | } | |
198 | ||
199 | header(); | |
200 | ||
e5b2c6f0 FM |
201 | if (longline_prepare(&line)<0) { |
202 | debuga(_("Not enough memory to read the log file")); | |
203 | exit(1); | |
204 | } | |
205 | ||
206 | while((buf=longline_read(fin,&line))!=NULL) { | |
120d768c | 207 | fixendofline(buf); |
9c7c6346 FM |
208 | getword_start(&gwarea,buf); |
209 | if (getword(dat,sizeof(dat),&gwarea,'\t')<0) { | |
2240dcea | 210 | debuga(_("Maybe you have a broken record or garbage in your %s file"),tmp); |
4bcb77cf FM |
211 | exit(1); |
212 | } | |
9c7c6346 | 213 | if (getword(tim,sizeof(tim),&gwarea,'\t')<0) { |
2240dcea | 214 | debuga(_("Maybe you have a broken record or garbage in your %s file"),tmp); |
4bcb77cf FM |
215 | exit(1); |
216 | } | |
9c7c6346 | 217 | if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { |
2240dcea | 218 | debuga(_("Maybe you have a broken record or garbage in your %s file"),tmp); |
4bcb77cf FM |
219 | exit(1); |
220 | } | |
9c7c6346 | 221 | if (getword(user,sizeof(user),&gwarea,'\t')<0) { |
2240dcea | 222 | debuga(_("Maybe you have a broken record or garbage in your %s file"),tmp); |
4bcb77cf FM |
223 | exit(1); |
224 | } | |
d5c1b1c1 | 225 | if(strlen(dat) < 3 || strlen(user) < 1) continue; |
e5b2c6f0 FM |
226 | if (getword_ptr(buf,&url,&gwarea,'\t')<0) { |
227 | debuga(_("Maybe you have a broken url in your %s file"),tmp); | |
4bcb77cf FM |
228 | exit(1); |
229 | } | |
9c7c6346 | 230 | if (getword(typ,sizeof(typ),&gwarea,'\t')<0) { |
4bcb77cf FM |
231 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); |
232 | exit(1); | |
233 | } | |
d5c1b1c1 GS |
234 | if(strstr(RealtimeTypes,typ) == 0) |
235 | continue; | |
236 | ||
e5b2c6f0 | 237 | if(strcmp(ouser,user) == 0 && ourl && strcmp(ourl,url) == 0) |
d5c1b1c1 GS |
238 | continue; |
239 | ||
c4dd8d9c FM |
240 | if(userip) |
241 | strcpy(user,ip); | |
d6e703cc | 242 | strcpy(u2,user); |
246c8489 | 243 | if(Ip2Name) |
a1c55d8c | 244 | ip2name(u2,sizeof(u2)); |
965c4a6f | 245 | user_find(name, sizeof(name), u2); |
d6e703cc | 246 | |
d6e703cc | 247 | 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 | 248 | strcpy(ouser,user); |
e5b2c6f0 FM |
249 | |
250 | url_len=strlen(url); | |
251 | if (!ourl || url_len>=ourl_size) { | |
252 | ourl_size=url_len+1; | |
253 | ourl=realloc(ourl,ourl_size); | |
254 | if (!ourl) { | |
255 | debuga(_("Not enough memory to store the url")); | |
256 | exit(1); | |
257 | } | |
258 | } | |
d5c1b1c1 GS |
259 | strcpy(ourl,url); |
260 | } | |
e5b2c6f0 FM |
261 | longline_free(&line); |
262 | if (ourl) free(ourl); | |
d5c1b1c1 | 263 | |
dfb337be | 264 | puts("</table>\n</div>\n</body>\n</html>\n"); |
d5c1b1c1 GS |
265 | fclose(fin); |
266 | unlink(tmp); | |
267 | fflush(NULL); | |
dfb337be | 268 | |
d5c1b1c1 GS |
269 | } |
270 | ||
32e71fa4 | 271 | static void header(void) |
d5c1b1c1 GS |
272 | { |
273 | puts("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\""); | |
274 | puts(" \"http://www.w3.org/TR/html4/loose.dtd\">\n"); | |
275 | puts("<html>\n"); | |
276 | puts("<head>\n"); | |
277 | if(realtime_refresh) | |
278 | printf(" <meta http-equiv=refresh content=\"%d\" url=\"sarg-php/sarg-realtime.php\"; charset=\"%s\">\n",realtime_refresh,CharSet); | |
279 | else | |
280 | printf(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet); | |
281 | css(stdout); | |
282 | puts("</head>\n"); | |
e5b2c6f0 | 283 | printf("<body style=\"font-family:%s;font-size:%s;background-color:%s;background-image:url(%s)\">\n",FontFace,TitleFontSize,BgColor,BgImage); |
dfb337be | 284 | puts("<div align=\"center\"><table cellpadding=\"1\" cellspacing=\"1\">\n"); |
c0ec9cc7 | 285 | printf("<tr><th class=\"title_l\" colspan=\"10\">SARG %s</th></tr>\n",text[134]); |
d5c1b1c1 | 286 | printf("<tr><th class=\"text\" colspan=\"10\">%s: %d s</th></tr>\n",text[136],realtime_refresh); |
b18ce4a0 | 287 | printf("<tr><th class=\"header_c\">%s</th><th class=\"header_c\">%s</th><th class=\"header_c\">%s</th><th class=\"header_c\">%s</th><th class=\"header_l\">%s</th></tr>\n",text[110],text[111],text[98],text[135],text[91]); |
d5c1b1c1 | 288 | } |