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