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