]>
Commit | Line | Data |
---|---|---|
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" | |
27 | ||
28 | void getdata(char *, FILE *); | |
29 | void datashow(char *); | |
14c2eac6 GS |
30 | void getlog(); |
31 | void header(); | |
d5c1b1c1 GS |
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 | { | |
d6e703cc | 49 | FILE *tmp, *fp, *fp_usr; |
d5c1b1c1 GS |
50 | char template1[255]="/var/tmp/sargtpl1.XXXXXX"; |
51 | char template2[255]="/var/tmp/sargtpl2.XXXXXX"; | |
52 | char cmd[512]; | |
53 | char buf[512]; | |
d6e703cc FM |
54 | int fd1,fd2,nreg; |
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 | getword(bufy,buf,' '); | |
79 | for(z1=0; z1<=strlen(bufy); z1++) { | |
80 | userfile[z2]=bufy[z1]; | |
81 | z2++; | |
82 | } | |
83 | strncat(userfile,":",1); | |
84 | for(z1=0; z1<=strlen(buf); z1++) { | |
85 | userfile[z2]=buf[z1]; | |
86 | z2++; | |
87 | } | |
88 | strncat(userfile,":",1); | |
89 | } | |
90 | fclose(fp_usr); | |
91 | } | |
d5c1b1c1 GS |
92 | |
93 | fd1 = mkstemp(template1); | |
94 | fd2 = mkstemp(template2); | |
95 | ||
96 | if((fd1 == -1 ) || ((tmp = fdopen (fd1, "w+" )) == NULL) ) { /* failure, bail out */ | |
97 | fprintf(stderr, "SARG: (realtime) mkstemp error - %s\n",strerror(errno)); | |
98 | exit(1); | |
99 | } | |
100 | ||
101 | sprintf(cmd,"tail -%d %s",realtime_access_log_lines,AccessLog); | |
102 | fp = popen(cmd, "r"); | |
103 | while(fgets(buf,sizeof(buf),fp) != NULL ) | |
104 | getdata(buf,tmp); | |
105 | pclose(fp); | |
106 | fclose(tmp); | |
107 | ||
d6e703cc | 108 | sprintf(cmd,"sort -r -k 1,1 -k 2,2 -o %s %s",template2,template1); |
d5c1b1c1 GS |
109 | system(cmd); |
110 | unlink(template1); | |
111 | datashow(template2); | |
112 | } | |
113 | ||
114 | void getdata(char *rec, FILE *ftmp) | |
115 | { | |
116 | time_t tt; | |
117 | struct tm *t; | |
118 | ||
119 | getword3(dat,rec,' '); | |
120 | getword3(warea,rec,' '); | |
121 | while(strcmp(warea,"") == 0 && strlen(rec) > 0) | |
122 | getword3(warea,rec,' '); | |
123 | getword3(ip,rec,' '); | |
124 | getword3(warea,rec,' '); | |
125 | getword3(warea,rec,' '); | |
126 | getword3(typ,rec,' '); | |
127 | if(strncmp(typ,"CONNECT",7) == 0) { | |
128 | getword3(url,rec,' '); | |
129 | getword3(user,rec,' '); | |
130 | }else { | |
131 | getword3(url,rec,'/'); | |
132 | getword3(url,rec,'/'); | |
133 | getword3(url,rec,'/'); | |
134 | getword3(user,rec,' '); | |
135 | getword3(user,rec,' '); | |
136 | } | |
137 | ||
138 | if(strncmp(user,"-",1) == 0 && strcmp(RealtimeUnauthRec,"ignore") == 0) | |
139 | return; | |
140 | ||
141 | tt=atoi(dat); | |
142 | t=localtime(&tt); | |
143 | if(strncmp(DateFormat,"u",1) == 0) | |
144 | strftime(tbuf, 127, "%Y-%m-%d %H:%M", t); | |
145 | else if(strncmp(DateFormat,"e",1) == 0) | |
146 | strftime(tbuf, 127, "%d-%m-%Y %H:%M", t); | |
147 | ||
148 | sprintf(warea,"%s %s %s %s %s\n",tbuf,ip,user,url,typ); | |
149 | fputs(warea,ftmp); | |
150 | } | |
151 | ||
152 | void datashow(char *tmp) | |
153 | { | |
154 | FILE *fin; | |
155 | char buf[MAXLEN]; | |
156 | ||
157 | if((fin=fopen(tmp,"r"))==NULL) { | |
158 | fprintf(stderr, "SARG: (realtime) open error %s - %s\n",tmp,strerror(errno)); | |
159 | exit(1); | |
160 | } | |
161 | ||
162 | header(); | |
163 | ||
164 | while(fgets(buf, MAXLEN, fin)) { | |
165 | buf[strlen(buf)-1]='\0'; | |
166 | getword3(dat,buf,' '); | |
167 | getword3(tim,buf,' '); | |
168 | getword3(ip,buf,' '); | |
169 | getword3(user,buf,' '); | |
170 | if(strlen(dat) < 3 || strlen(user) < 1) continue; | |
171 | getword3(url,buf,' '); | |
172 | getword3(typ,buf,' '); | |
173 | if(strstr(RealtimeTypes,typ) == 0) | |
174 | continue; | |
175 | ||
176 | if(strcmp(ouser,user) == 0 && strcmp(ourl,url) == 0) | |
177 | continue; | |
178 | ||
d6e703cc FM |
179 | strcpy(u2,user); |
180 | if(strcmp(Ip2Name,"yes") == 0) | |
181 | ip2name(u2); | |
182 | if(UserTabFile[0] != '\0') { | |
183 | sprintf(warea,":%s:",u2); | |
184 | if((str=(char *) strstr(userfile,warea)) != (char *) NULL ) { | |
185 | z1=0; | |
186 | str2=(char *) strstr(str+1,":"); | |
187 | str2++; | |
188 | bzero(name, MAXLEN); | |
189 | while(str2[z1] != ':') { | |
190 | name[z1]=str2[z1]; | |
191 | z1++; | |
192 | } | |
193 | } else strcpy(name,u2); | |
194 | } else strcpy(name,u2); | |
195 | ||
196 | if(dotinuser && strstr(name,"_")) { | |
197 | str2=(char *)subs(name,"_","."); | |
198 | strcpy(name,str2); | |
199 | } | |
200 | ||
201 | 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 |
202 | strcpy(ouser,user); |
203 | strcpy(ourl,url); | |
204 | } | |
205 | ||
206 | puts("</table>\n</html>\n"); | |
207 | fclose(fin); | |
208 | unlink(tmp); | |
209 | fflush(NULL); | |
210 | ||
211 | } | |
212 | ||
213 | void header() | |
214 | { | |
215 | puts("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\""); | |
216 | puts(" \"http://www.w3.org/TR/html4/loose.dtd\">\n"); | |
217 | puts("<html>\n"); | |
218 | puts("<head>\n"); | |
219 | if(realtime_refresh) | |
220 | printf(" <meta http-equiv=refresh content=\"%d\" url=\"sarg-php/sarg-realtime.php\"; charset=\"%s\">\n",realtime_refresh,CharSet); | |
221 | else | |
222 | printf(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet); | |
223 | css(stdout); | |
224 | puts("</head>\n"); | |
225 | printf(buf,"<body style=\"font-family:%s;font-size:%s;background-color:%s;background-image:url(%s)\">\n",FontFace,TitleFontSize,BgColor,BgImage); | |
226 | puts("<center><table cellpadding=\"1\" cellspacing=\"1\">\n"); | |
227 | printf("<tr><th class=\"title2\" colspan=\"10\">SARG %s</th></tr>\n",text[134]); | |
228 | printf("<tr><th class=\"text\" colspan=\"10\">%s: %d s</th></tr>\n",text[136],realtime_refresh); | |
229 | 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]); | |
230 | } |