]>
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; |
afaa3b67 | 51 | longline 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 */ | |
10210234 | 59 | debuga(_("(realtime) mkstemp error - %s\n"),strerror(errno)); |
06b39c87 | 60 | exit(EXIT_FAILURE); |
d5c1b1c1 GS |
61 | } |
62 | ||
afaa3b67 | 63 | if ((line=longline_create())==NULL) { |
10210234 | 64 | debuga(_("Not enough memory to read the log file\n")); |
06b39c87 | 65 | exit(EXIT_FAILURE); |
ac422f9b FM |
66 | } |
67 | ||
0a4e18e1 | 68 | sprintf(cmd,"tail -%d %s",realtime_access_log_lines,AccessLog[0]); |
d5c1b1c1 | 69 | fp = popen(cmd, "r"); |
afaa3b67 | 70 | while((buf=longline_read(fp,line)) != NULL ) |
4bcb77cf | 71 | if (getdata(buf,tmp)<0) { |
10210234 | 72 | debuga(_("Maybe a broken record or garbage was returned by %s\n"),cmd); |
06b39c87 | 73 | exit(EXIT_FAILURE); |
4bcb77cf | 74 | } |
d5c1b1c1 GS |
75 | pclose(fp); |
76 | fclose(tmp); | |
afaa3b67 | 77 | longline_destroy(&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)) { | |
10210234 FM |
82 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); |
83 | debuga(_("sort command: %s\n"),cmd); | |
06b39c87 | 84 | exit(EXIT_FAILURE); |
456d78a5 | 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]; |
06b39c87 | 99 | char ip[25]; |
e5b2c6f0 | 100 | char *url; |
9c7c6346 | 101 | struct getwordstruct gwarea; |
d5c1b1c1 | 102 | |
9c7c6346 FM |
103 | getword_start(&gwarea,rec); |
104 | if (getword(dat,sizeof(dat),&gwarea,' ')<0) { | |
10210234 | 105 | debuga(_("The time stamp at column 1 is too long\n")); |
4bcb77cf FM |
106 | return(-1); |
107 | } | |
9c7c6346 | 108 | if (getword(warea,sizeof(warea),&gwarea,' ')<0) { |
10210234 | 109 | debuga(_("The connection duration at column 2 is too long\n")); |
4bcb77cf FM |
110 | return(-1); |
111 | } | |
9c7c6346 FM |
112 | while(strcmp(warea,"") == 0 && gwarea.current[0] != '\0') |
113 | if (getword(warea,sizeof(warea),&gwarea,' ')<0) { | |
4bcb77cf FM |
114 | return(-1); |
115 | } | |
9c7c6346 | 116 | if (getword(ip,sizeof(ip),&gwarea,' ')<0) { |
10210234 | 117 | debuga(_("The IP address at column 3 is too long\n")); |
4bcb77cf FM |
118 | return(-1); |
119 | } | |
9c7c6346 | 120 | if (getword_skip(MAXLEN,&gwarea,' ')<0) { |
10210234 | 121 | debuga(_("The status at column 4 is too long\n")); |
4bcb77cf FM |
122 | return(-1); |
123 | } | |
9c7c6346 | 124 | if (getword_skip(MAXLEN,&gwarea,' ')<0) { |
10210234 | 125 | debuga(_("The size at column 5 is too long\n")); |
4bcb77cf FM |
126 | return(-1); |
127 | } | |
9c7c6346 | 128 | if (getword(typ,sizeof(typ),&gwarea,' ')<0) { |
10210234 | 129 | debuga(_("The action at column 6 is too long\n")); |
4bcb77cf FM |
130 | return(-1); |
131 | } | |
d5c1b1c1 | 132 | if(strncmp(typ,"CONNECT",7) == 0) { |
e5b2c6f0 | 133 | if (getword_ptr(rec,&url,&gwarea,' ')<0) { |
10210234 | 134 | debuga(_("The URL at column 7 is too long\n")); |
4bcb77cf FM |
135 | return(-1); |
136 | } | |
9c7c6346 | 137 | if (getword(user,sizeof(user),&gwarea,' ')<0) { |
10210234 | 138 | debuga(_("The user ID at column 8 is too long\n")); |
4bcb77cf FM |
139 | return(-1); |
140 | } | |
40d1a88d | 141 | }else { |
9c7c6346 | 142 | if (getword_skip(MAXLEN,&gwarea,'/')<0) { |
10210234 | 143 | debuga(_("The URL at column 7 is too long\n")); |
40d1a88d FM |
144 | return(-1); |
145 | } | |
9c7c6346 | 146 | if (getword_skip(MAXLEN,&gwarea,'/')<0) { |
10210234 | 147 | debuga(_("The URL at column 7 is too long\n")); |
40d1a88d FM |
148 | return(-1); |
149 | } | |
e5b2c6f0 | 150 | if (getword_ptr(rec,&url,&gwarea,'/')<0) { |
10210234 | 151 | debuga(_("The URL at column 7 is too long\n")); |
40d1a88d FM |
152 | return(-1); |
153 | } | |
9c7c6346 | 154 | if (getword_skip(MAXLEN,&gwarea,' ')<0) { |
10210234 | 155 | debuga(_("The data at column 8 is too long\n")); |
40d1a88d FM |
156 | return(-1); |
157 | } | |
9c7c6346 | 158 | if (getword(user,sizeof(user),&gwarea,' ')<0) { |
10210234 | 159 | debuga(_("The user at column 9 is too long\n")); |
40d1a88d FM |
160 | return(-1); |
161 | } | |
d5c1b1c1 GS |
162 | } |
163 | ||
71d78774 | 164 | if(strncmp(user,"-",1) == 0 && RealtimeUnauthRec==REALTIME_UNAUTH_REC_IGNORE) |
4bcb77cf | 165 | return(0); |
d5c1b1c1 GS |
166 | |
167 | tt=atoi(dat); | |
168 | t=localtime(&tt); | |
169 | if(strncmp(DateFormat,"u",1) == 0) | |
c4dd8d9c | 170 | strftime(tbuf, sizeof(tbuf), "%Y-%m-%d\t%H:%M", t); |
d5c1b1c1 | 171 | else if(strncmp(DateFormat,"e",1) == 0) |
c4dd8d9c | 172 | strftime(tbuf, sizeof(tbuf), "%d-%m-%Y\t%H:%M", t); |
d5c1b1c1 | 173 | |
120d768c | 174 | fprintf(ftmp,"%s\t%s\t%s\t%s\t%s\n",tbuf,ip,user,url,typ); |
4bcb77cf | 175 | return(0); |
d5c1b1c1 GS |
176 | } |
177 | ||
120d768c | 178 | static void datashow(const char *tmp) |
d5c1b1c1 GS |
179 | { |
180 | FILE *fin; | |
ac422f9b FM |
181 | char dat[128]; |
182 | char tim[128]; | |
e5b2c6f0 FM |
183 | char *buf; |
184 | char *url; | |
185 | char *ourl=NULL; | |
2240dcea | 186 | char ouser[MAX_USER_LEN]=""; |
e5b2c6f0 | 187 | char typ[128]; |
2240dcea FM |
188 | char user[MAX_USER_LEN]; |
189 | char u2[MAX_USER_LEN]; | |
06b39c87 | 190 | char ip[25]; |
e5b2c6f0 FM |
191 | int url_len; |
192 | int ourl_size=0; | |
9c7c6346 | 193 | struct getwordstruct gwarea; |
afaa3b67 | 194 | longline line; |
d5c1b1c1 GS |
195 | |
196 | if((fin=fopen(tmp,"r"))==NULL) { | |
10210234 | 197 | debuga(_("(realtime) open error %s - %s\n"),tmp,strerror(errno)); |
06b39c87 | 198 | exit(EXIT_FAILURE); |
d5c1b1c1 GS |
199 | } |
200 | ||
201 | header(); | |
202 | ||
afaa3b67 | 203 | if ((line=longline_create())==NULL) { |
10210234 | 204 | debuga(_("Not enough memory to read the log file\n")); |
06b39c87 | 205 | exit(EXIT_FAILURE); |
e5b2c6f0 FM |
206 | } |
207 | ||
afaa3b67 | 208 | while((buf=longline_read(fin,line))!=NULL) { |
120d768c | 209 | fixendofline(buf); |
9c7c6346 FM |
210 | getword_start(&gwarea,buf); |
211 | if (getword(dat,sizeof(dat),&gwarea,'\t')<0) { | |
10210234 | 212 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); |
06b39c87 | 213 | exit(EXIT_FAILURE); |
4bcb77cf | 214 | } |
9c7c6346 | 215 | if (getword(tim,sizeof(tim),&gwarea,'\t')<0) { |
10210234 | 216 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); |
06b39c87 | 217 | exit(EXIT_FAILURE); |
4bcb77cf | 218 | } |
9c7c6346 | 219 | if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { |
10210234 | 220 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); |
06b39c87 | 221 | exit(EXIT_FAILURE); |
4bcb77cf | 222 | } |
9c7c6346 | 223 | if (getword(user,sizeof(user),&gwarea,'\t')<0) { |
10210234 | 224 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); |
06b39c87 | 225 | exit(EXIT_FAILURE); |
4bcb77cf | 226 | } |
d5c1b1c1 | 227 | if(strlen(dat) < 3 || strlen(user) < 1) continue; |
e5b2c6f0 | 228 | if (getword_ptr(buf,&url,&gwarea,'\t')<0) { |
10210234 | 229 | debuga(_("Maybe you have a broken url in your %s file\n"),tmp); |
06b39c87 | 230 | exit(EXIT_FAILURE); |
4bcb77cf | 231 | } |
9c7c6346 | 232 | if (getword(typ,sizeof(typ),&gwarea,'\t')<0) { |
10210234 | 233 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); |
06b39c87 | 234 | exit(EXIT_FAILURE); |
4bcb77cf | 235 | } |
d5c1b1c1 GS |
236 | if(strstr(RealtimeTypes,typ) == 0) |
237 | continue; | |
238 | ||
e5b2c6f0 | 239 | if(strcmp(ouser,user) == 0 && ourl && strcmp(ourl,url) == 0) |
d5c1b1c1 GS |
240 | continue; |
241 | ||
8bb4048f | 242 | if(UserIp) |
c4dd8d9c | 243 | strcpy(user,ip); |
d6e703cc | 244 | strcpy(u2,user); |
246c8489 | 245 | if(Ip2Name) |
a1c55d8c | 246 | ip2name(u2,sizeof(u2)); |
965c4a6f | 247 | user_find(name, sizeof(name), u2); |
d6e703cc | 248 | |
d6e703cc | 249 | 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 | 250 | strcpy(ouser,user); |
e5b2c6f0 FM |
251 | |
252 | url_len=strlen(url); | |
253 | if (!ourl || url_len>=ourl_size) { | |
254 | ourl_size=url_len+1; | |
255 | ourl=realloc(ourl,ourl_size); | |
256 | if (!ourl) { | |
10210234 | 257 | debuga(_("Not enough memory to store the url\n")); |
06b39c87 | 258 | exit(EXIT_FAILURE); |
e5b2c6f0 FM |
259 | } |
260 | } | |
d5c1b1c1 GS |
261 | strcpy(ourl,url); |
262 | } | |
afaa3b67 | 263 | longline_destroy(&line); |
e5b2c6f0 | 264 | if (ourl) free(ourl); |
d5c1b1c1 | 265 | |
dfb337be | 266 | puts("</table>\n</div>\n</body>\n</html>\n"); |
d5c1b1c1 GS |
267 | fclose(fin); |
268 | unlink(tmp); | |
269 | fflush(NULL); | |
dfb337be | 270 | |
d5c1b1c1 GS |
271 | } |
272 | ||
32e71fa4 | 273 | static void header(void) |
d5c1b1c1 GS |
274 | { |
275 | puts("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\""); | |
276 | puts(" \"http://www.w3.org/TR/html4/loose.dtd\">\n"); | |
277 | puts("<html>\n"); | |
278 | puts("<head>\n"); | |
279 | if(realtime_refresh) | |
280 | printf(" <meta http-equiv=refresh content=\"%d\" url=\"sarg-php/sarg-realtime.php\"; charset=\"%s\">\n",realtime_refresh,CharSet); | |
281 | else | |
282 | printf(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet); | |
283 | css(stdout); | |
284 | puts("</head>\n"); | |
e5b2c6f0 | 285 | printf("<body style=\"font-family:%s;font-size:%s;background-color:%s;background-image:url(%s)\">\n",FontFace,TitleFontSize,BgColor,BgImage); |
dfb337be | 286 | puts("<div align=\"center\"><table cellpadding=\"1\" cellspacing=\"1\">\n"); |
c36c7384 FM |
287 | printf("<tr><th class=\"title_l\" colspan=\"10\">SARG %s</th></tr>\n",_("Realtime")); |
288 | printf("<tr><th class=\"text\" colspan=\"10\">%s: %d s</th></tr>\n",_("Auto refresh"),realtime_refresh); | |
289 | 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",_("DATE/TIME"),_("IP/NAME"),_("USERID"),_("TYPE"),_("ACCESSED SITE")); | |
d5c1b1c1 | 290 | } |