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