]> git.ipfire.org Git - thirdparty/sarg.git/blob - realtime.c
Moved all the functions declarations from conf.h to defs.h for consistency.
[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, *fp_usr;
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,nreg;
55 int cstatus;
56
57 if(UserTabFile[0] != '\0') {
58 if(debug) {
59 sprintf(msg,"%s: %s",text[86],UserTabFile);
60 debuga(msg);
61 }
62 if((fp_usr=fopen(UserTabFile,"r"))==NULL) {
63 fprintf(stderr, "SARG: (realtime) %s: %s - %s\n",text[45],UserTabFile,strerror(errno));
64 exit(1);
65 }
66 nreg = lseek(fileno(fp_usr), 0, SEEK_END);
67 lseek(fileno(fp_usr), 0, 0);
68 if((userfile=(char *) malloc(nreg+100))==NULL){
69 fprintf(stderr, "SARG ERROR: %s",text[87]);
70 exit(1);
71 }
72 bzero(userfile,nreg+100);
73 strncat(userfile,":",1);
74 z1=0;
75 z2=1;
76 while(fgets(buf,sizeof(buf),fp_usr)!=NULL){
77 buf[strlen(buf)-1]='\0';
78 if(strstr(buf,"\r") != 0) buf[strlen(buf)-1]='\0';
79 if (getword(bufy,sizeof(bufy),buf,' ')<0) {
80 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserTabFile);
81 exit(1);
82 }
83 for(z1=0; z1<=strlen(bufy); z1++) {
84 userfile[z2]=bufy[z1];
85 z2++;
86 }
87 strncat(userfile,":",1);
88 for(z1=0; z1<=strlen(buf); z1++) {
89 userfile[z2]=buf[z1];
90 z2++;
91 }
92 strncat(userfile,":",1);
93 }
94 fclose(fp_usr);
95 }
96
97 fd1 = mkstemp(template1);
98 fd2 = mkstemp(template2);
99
100 if((fd1 == -1 ) || ((tmp = fdopen (fd1, "w+" )) == NULL) ) { /* failure, bail out */
101 fprintf(stderr, "SARG: (realtime) mkstemp error - %s\n",strerror(errno));
102 exit(1);
103 }
104
105 sprintf(cmd,"tail -%d %s",realtime_access_log_lines,AccessLog);
106 fp = popen(cmd, "r");
107 while(fgets(buf,sizeof(buf),fp) != NULL )
108 if (getdata(buf,tmp)<0) {
109 fprintf(stderr,"SARG: Maybe a broken record or garbage was returned by %s.\n",cmd);
110 exit(1);
111 }
112 pclose(fp);
113 fclose(tmp);
114
115 sprintf(cmd,"sort -r -k 1,1 -k 2,2 -o %s %s",template2,template1);
116 cstatus=system(cmd);
117 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
118 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
119 fprintf(stderr, "SARG: sort command: %s\n",cmd);
120 exit(1);
121 }
122 unlink(template1);
123 datashow(template2);
124 }
125
126 int getdata(char *rec, FILE *ftmp)
127 {
128 time_t tt;
129 struct tm *t;
130
131 if (getword3(dat,sizeof(dat),rec,' ')<0) {
132 fprintf(stderr,"SARG: The time stamp at column 1 is too long.\n");
133 return(-1);
134 }
135 if (getword3(warea,sizeof(warea),rec,' ')<0) {
136 fprintf(stderr,"SARG: The connection duration at column 2 is too long.\n");
137 return(-1);
138 }
139 while(strcmp(warea,"") == 0 && strlen(rec) > 0)
140 if (getword3(warea,sizeof(warea),rec,' ')<0) {
141 return(-1);
142 }
143 if (getword3(ip,sizeof(ip),rec,' ')<0) {
144 fprintf(stderr,"SARG: The IP address at column 3 is too long.\n");
145 return(-1);
146 }
147 if (getword3(warea,sizeof(warea),rec,' ')<0) {
148 fprintf(stderr,"SARG: The status at column 4 is too long.\n");
149 return(-1);
150 }
151 if (getword3(warea,sizeof(warea),rec,' ')<0) {
152 fprintf(stderr,"SARG: The size at column 5 is too long.\n");
153 return(-1);
154 }
155 if (getword3(typ,sizeof(typ),rec,' ')<0) {
156 fprintf(stderr,"SARG: The action at column 6 is too long.\n");
157 return(-1);
158 }
159 if(strncmp(typ,"CONNECT",7) == 0) {
160 if (getword3(url,sizeof(url),rec,' ')<0) {
161 return(-1);
162 }
163 if (getword3(user,sizeof(user),rec,' ')<0) {
164 return(-1);
165 }
166 }else {
167 if (getword3(url,sizeof(url),rec,'/')<0) {
168 fprintf(stderr,"SARG: The URL at column 7 is too long.\n");
169 return(-1);
170 }
171 if (getword3(url,sizeof(url),rec,'/')<0) {
172 fprintf(stderr,"SARG: The URL at column 7 is too long.\n");
173 return(-1);
174 }
175 if (getword3(url,sizeof(url),rec,'/')<0) {
176 fprintf(stderr,"SARG: The URL at column 7 is too long.\n");
177 return(-1);
178 }
179 if (getword3(user,sizeof(user),rec,' ')<0) {
180 fprintf(stderr,"SARG: The data at column 8 is too long.\n");
181 return(-1);
182 }
183 if (getword3(user,sizeof(user),rec,' ')<0) {
184 fprintf(stderr,"SARG: The user at column 9 is too long.\n");
185 return(-1);
186 }
187 }
188
189 if(strncmp(user,"-",1) == 0 && strcmp(RealtimeUnauthRec,"ignore") == 0)
190 return(0);
191
192 tt=atoi(dat);
193 t=localtime(&tt);
194 if(strncmp(DateFormat,"u",1) == 0)
195 strftime(tbuf, 127, "%Y-%m-%d %H:%M", t);
196 else if(strncmp(DateFormat,"e",1) == 0)
197 strftime(tbuf, 127, "%d-%m-%Y %H:%M", t);
198
199 fprintf(ftmp,"%s %s %s %s %s\n",tbuf,ip,user,url,typ);
200 return(0);
201 }
202
203 void datashow(char *tmp)
204 {
205 FILE *fin;
206 char buf[MAXLEN];
207
208 if((fin=fopen(tmp,"r"))==NULL) {
209 fprintf(stderr, "SARG: (realtime) open error %s - %s\n",tmp,strerror(errno));
210 exit(1);
211 }
212
213 header();
214
215 while(fgets(buf, MAXLEN, fin)) {
216 buf[strlen(buf)-1]='\0';
217 if (getword3(dat,sizeof(dat),buf,' ')<0) {
218 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
219 exit(1);
220 }
221 if (getword3(tim,sizeof(tim),buf,' ')<0) {
222 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
223 exit(1);
224 }
225 if (getword3(ip,sizeof(ip),buf,' ')<0) {
226 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
227 exit(1);
228 }
229 if (getword3(user,sizeof(user),buf,' ')<0) {
230 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
231 exit(1);
232 }
233 if(strlen(dat) < 3 || strlen(user) < 1) continue;
234 if (getword3(url,sizeof(url),buf,' ')<0) {
235 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
236 exit(1);
237 }
238 if (getword3(typ,sizeof(typ),buf,' ')<0) {
239 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
240 exit(1);
241 }
242 if(strstr(RealtimeTypes,typ) == 0)
243 continue;
244
245 if(strcmp(ouser,user) == 0 && strcmp(ourl,url) == 0)
246 continue;
247
248 strcpy(u2,user);
249 if(strcmp(Ip2Name,"yes") == 0)
250 ip2name(u2,sizeof(u2));
251 if(UserTabFile[0] != '\0') {
252 sprintf(warea,":%s:",u2);
253 if((str=(char *) strstr(userfile,warea)) != (char *) NULL ) {
254 z1=0;
255 str2=(char *) strstr(str+1,":");
256 str2++;
257 bzero(name, MAXLEN);
258 while(str2[z1] != ':') {
259 name[z1]=str2[z1];
260 z1++;
261 }
262 } else strcpy(name,u2);
263 } else strcpy(name,u2);
264
265 if(dotinuser && strstr(name,"_")) {
266 str2=(char *)subs(name,"_",".");
267 strcpy(name,str2);
268 }
269
270 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);
271 strcpy(ouser,user);
272 strcpy(ourl,url);
273 }
274
275 puts("</table>\n</html>\n");
276 fclose(fin);
277 unlink(tmp);
278 fflush(NULL);
279
280 }
281
282 static void header(void)
283 {
284 puts("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"");
285 puts(" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
286 puts("<html>\n");
287 puts("<head>\n");
288 if(realtime_refresh)
289 printf(" <meta http-equiv=refresh content=\"%d\" url=\"sarg-php/sarg-realtime.php\"; charset=\"%s\">\n",realtime_refresh,CharSet);
290 else
291 printf(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
292 css(stdout);
293 puts("</head>\n");
294 printf(buf,"<body style=\"font-family:%s;font-size:%s;background-color:%s;background-image:url(%s)\">\n",FontFace,TitleFontSize,BgColor,BgImage);
295 puts("<center><table cellpadding=\"1\" cellspacing=\"1\">\n");
296 printf("<tr><th class=\"title2\" colspan=\"10\">SARG %s</th></tr>\n",text[134]);
297 printf("<tr><th class=\"text\" colspan=\"10\">%s: %d s</th></tr>\n",text[136],realtime_refresh);
298 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]);
299 }