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