]> git.ipfire.org Git - thirdparty/sarg.git/blob - realtime.c
fix the following configure errors
[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 void getdata(char *, FILE *);
29 void datashow(char *);
30 void getlog();
31 void header();
32
33 char dat[128];
34 char tim[128];
35 char typ[128];
36 char ouser[MAXLEN]="";
37 char ourl[MAXLEN]="";
38
39 void
40 realtime(int argc, char *argv[])
41 {
42
43 getlog();
44
45 }
46
47 void getlog()
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[512];
54 int fd1,fd2;
55
56 fd1 = mkstemp(template1);
57 fd2 = mkstemp(template2);
58
59 if((fd1 == -1 ) || ((tmp = fdopen (fd1, "w+" )) == NULL) ) { /* failure, bail out */
60 fprintf(stderr, "SARG: (realtime) mkstemp error - %s\n",strerror(errno));
61 exit(1);
62 }
63
64 sprintf(cmd,"tail -%d %s",realtime_access_log_lines,AccessLog);
65 fp = popen(cmd, "r");
66 while(fgets(buf,sizeof(buf),fp) != NULL )
67 getdata(buf,tmp);
68 pclose(fp);
69 fclose(tmp);
70
71 sprintf(cmd,"sort -k 4,4 -k 5,5 -o %s %s",template2,template1);
72 system(cmd);
73 unlink(template1);
74 datashow(template2);
75 }
76
77 void getdata(char *rec, FILE *ftmp)
78 {
79 time_t tt;
80 struct tm *t;
81
82 getword3(dat,rec,' ');
83 getword3(warea,rec,' ');
84 while(strcmp(warea,"") == 0 && strlen(rec) > 0)
85 getword3(warea,rec,' ');
86 getword3(ip,rec,' ');
87 getword3(warea,rec,' ');
88 getword3(warea,rec,' ');
89 getword3(typ,rec,' ');
90 if(strncmp(typ,"CONNECT",7) == 0) {
91 getword3(url,rec,' ');
92 getword3(user,rec,' ');
93 }else {
94 getword3(url,rec,'/');
95 getword3(url,rec,'/');
96 getword3(url,rec,'/');
97 getword3(user,rec,' ');
98 getword3(user,rec,' ');
99 }
100
101 if(strncmp(user,"-",1) == 0 && strcmp(RealtimeUnauthRec,"ignore") == 0)
102 return;
103
104 tt=atoi(dat);
105 t=localtime(&tt);
106 if(strncmp(DateFormat,"u",1) == 0)
107 strftime(tbuf, 127, "%Y-%m-%d %H:%M", t);
108 else if(strncmp(DateFormat,"e",1) == 0)
109 strftime(tbuf, 127, "%d-%m-%Y %H:%M", t);
110
111 sprintf(warea,"%s %s %s %s %s\n",tbuf,ip,user,url,typ);
112 fputs(warea,ftmp);
113 }
114
115 void datashow(char *tmp)
116 {
117 FILE *fin;
118 char buf[MAXLEN];
119
120 if((fin=fopen(tmp,"r"))==NULL) {
121 fprintf(stderr, "SARG: (realtime) open error %s - %s\n",tmp,strerror(errno));
122 exit(1);
123 }
124
125 header();
126
127 while(fgets(buf, MAXLEN, fin)) {
128 buf[strlen(buf)-1]='\0';
129 getword3(dat,buf,' ');
130 getword3(tim,buf,' ');
131 getword3(ip,buf,' ');
132 getword3(user,buf,' ');
133 if(strlen(dat) < 3 || strlen(user) < 1) continue;
134 getword3(url,buf,' ');
135 getword3(typ,buf,' ');
136 if(strstr(RealtimeTypes,typ) == 0)
137 continue;
138
139 if(strcmp(ouser,user) == 0 && strcmp(ourl,url) == 0)
140 continue;
141
142 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,user,typ,url,url);
143 strcpy(ouser,user);
144 strcpy(ourl,url);
145 }
146
147 puts("</table>\n</html>\n");
148 fclose(fin);
149 unlink(tmp);
150 fflush(NULL);
151
152 }
153
154 void header()
155 {
156 puts("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"");
157 puts(" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
158 puts("<html>\n");
159 puts("<head>\n");
160 if(realtime_refresh)
161 printf(" <meta http-equiv=refresh content=\"%d\" url=\"sarg-php/sarg-realtime.php\"; charset=\"%s\">\n",realtime_refresh,CharSet);
162 else
163 printf(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
164 css(stdout);
165 puts("</head>\n");
166 printf(buf,"<body style=\"font-family:%s;font-size:%s;background-color:%s;background-image:url(%s)\">\n",FontFace,TitleFontSize,BgColor,BgImage);
167 puts("<center><table cellpadding=\"1\" cellspacing=\"1\">\n");
168 printf("<tr><th class=\"title2\" colspan=\"10\">SARG %s</th></tr>\n",text[134]);
169 printf("<tr><th class=\"text\" colspan=\"10\">%s: %d s</th></tr>\n",text[136],realtime_refresh);
170 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]);
171 }