]>
Commit | Line | Data |
---|---|---|
d5c1b1c1 | 1 | /* |
d5c1b1c1 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
1164c474 | 3 | * 1998, 2010 |
d5c1b1c1 GS |
4 | * |
5 | * SARG donations: | |
6 | * please look at http://sarg.sourceforge.net/donations.php | |
1164c474 FM |
7 | * Support: |
8 | * http://sourceforge.net/projects/sarg/forums/forum/363374 | |
d5c1b1c1 GS |
9 | * --------------------------------------------------------------------- |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
24 | * | |
25 | */ | |
26 | ||
27 | #include "include/conf.h" | |
5f3cfd1d | 28 | #include "include/defs.h" |
d5c1b1c1 | 29 | |
120d768c FM |
30 | static int getdata(char*, FILE*); |
31 | static void datashow(const char *); | |
32e71fa4 FM |
32 | static void getlog(void); |
33 | static void header(void); | |
d5c1b1c1 GS |
34 | |
35 | char dat[128]; | |
36 | char tim[128]; | |
37 | char typ[128]; | |
38 | char ouser[MAXLEN]=""; | |
39 | char ourl[MAXLEN]=""; | |
40 | ||
32e71fa4 | 41 | void realtime(void) |
d5c1b1c1 GS |
42 | { |
43 | ||
44 | getlog(); | |
45 | ||
46 | } | |
47 | ||
32e71fa4 | 48 | static void getlog(void) |
d5c1b1c1 | 49 | { |
936c9905 | 50 | FILE *tmp, *fp; |
d5c1b1c1 GS |
51 | char template1[255]="/var/tmp/sargtpl1.XXXXXX"; |
52 | char template2[255]="/var/tmp/sargtpl2.XXXXXX"; | |
53 | char cmd[512]; | |
40d1a88d | 54 | char buf[MAXLEN]; |
936c9905 | 55 | int fd1,fd2; |
456d78a5 | 56 | int cstatus; |
d6e703cc | 57 | |
965c4a6f | 58 | init_usertab(UserTabFile); |
d5c1b1c1 GS |
59 | |
60 | fd1 = mkstemp(template1); | |
61 | fd2 = mkstemp(template2); | |
62 | ||
63 | if((fd1 == -1 ) || ((tmp = fdopen (fd1, "w+" )) == NULL) ) { /* failure, bail out */ | |
64 | fprintf(stderr, "SARG: (realtime) mkstemp error - %s\n",strerror(errno)); | |
65 | exit(1); | |
66 | } | |
67 | ||
0a4e18e1 | 68 | sprintf(cmd,"tail -%d %s",realtime_access_log_lines,AccessLog[0]); |
d5c1b1c1 GS |
69 | fp = popen(cmd, "r"); |
70 | while(fgets(buf,sizeof(buf),fp) != NULL ) | |
4bcb77cf | 71 | if (getdata(buf,tmp)<0) { |
40d1a88d | 72 | fprintf(stderr,"SARG: Maybe a broken record or garbage was returned by %s.\n",cmd); |
4bcb77cf FM |
73 | exit(1); |
74 | } | |
d5c1b1c1 GS |
75 | pclose(fp); |
76 | fclose(tmp); | |
77 | ||
9a2efbd0 | 78 | sprintf(cmd,"sort -r -k 1,1 -k 2,2 -o \"%s\" \"%s\"",template2,template1); |
456d78a5 FM |
79 | cstatus=system(cmd); |
80 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
81 | fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus)); | |
82 | fprintf(stderr, "SARG: sort command: %s\n",cmd); | |
83 | exit(1); | |
84 | } | |
d5c1b1c1 GS |
85 | unlink(template1); |
86 | datashow(template2); | |
87 | } | |
88 | ||
120d768c | 89 | static int getdata(char *rec, FILE *ftmp) |
d5c1b1c1 GS |
90 | { |
91 | time_t tt; | |
92 | struct tm *t; | |
120d768c | 93 | char tbuf[128]; |
9c7c6346 FM |
94 | char warea[MAXLEN]; |
95 | struct getwordstruct gwarea; | |
d5c1b1c1 | 96 | |
9c7c6346 FM |
97 | getword_start(&gwarea,rec); |
98 | if (getword(dat,sizeof(dat),&gwarea,' ')<0) { | |
40d1a88d | 99 | fprintf(stderr,"SARG: The time stamp at column 1 is too long.\n"); |
4bcb77cf FM |
100 | return(-1); |
101 | } | |
9c7c6346 | 102 | if (getword(warea,sizeof(warea),&gwarea,' ')<0) { |
40d1a88d | 103 | fprintf(stderr,"SARG: The connection duration at column 2 is too long.\n"); |
4bcb77cf FM |
104 | return(-1); |
105 | } | |
9c7c6346 FM |
106 | while(strcmp(warea,"") == 0 && gwarea.current[0] != '\0') |
107 | if (getword(warea,sizeof(warea),&gwarea,' ')<0) { | |
4bcb77cf FM |
108 | return(-1); |
109 | } | |
9c7c6346 | 110 | if (getword(ip,sizeof(ip),&gwarea,' ')<0) { |
40d1a88d | 111 | fprintf(stderr,"SARG: The IP address at column 3 is too long.\n"); |
4bcb77cf FM |
112 | return(-1); |
113 | } | |
9c7c6346 | 114 | if (getword_skip(MAXLEN,&gwarea,' ')<0) { |
40d1a88d | 115 | fprintf(stderr,"SARG: The status at column 4 is too long.\n"); |
4bcb77cf FM |
116 | return(-1); |
117 | } | |
9c7c6346 | 118 | if (getword_skip(MAXLEN,&gwarea,' ')<0) { |
40d1a88d | 119 | fprintf(stderr,"SARG: The size at column 5 is too long.\n"); |
4bcb77cf FM |
120 | return(-1); |
121 | } | |
9c7c6346 | 122 | if (getword(typ,sizeof(typ),&gwarea,' ')<0) { |
40d1a88d | 123 | fprintf(stderr,"SARG: The action at column 6 is too long.\n"); |
4bcb77cf FM |
124 | return(-1); |
125 | } | |
d5c1b1c1 | 126 | if(strncmp(typ,"CONNECT",7) == 0) { |
9c7c6346 | 127 | if (getword(url,sizeof(url),&gwarea,' ')<0) { |
4bcb77cf FM |
128 | return(-1); |
129 | } | |
9c7c6346 | 130 | if (getword(user,sizeof(user),&gwarea,' ')<0) { |
4bcb77cf FM |
131 | return(-1); |
132 | } | |
40d1a88d | 133 | }else { |
9c7c6346 | 134 | if (getword_skip(MAXLEN,&gwarea,'/')<0) { |
40d1a88d FM |
135 | fprintf(stderr,"SARG: The URL at column 7 is too long.\n"); |
136 | return(-1); | |
137 | } | |
9c7c6346 | 138 | if (getword_skip(MAXLEN,&gwarea,'/')<0) { |
40d1a88d FM |
139 | fprintf(stderr,"SARG: The URL at column 7 is too long.\n"); |
140 | return(-1); | |
141 | } | |
9c7c6346 | 142 | if (getword(url,sizeof(url),&gwarea,'/')<0) { |
40d1a88d FM |
143 | fprintf(stderr,"SARG: The URL at column 7 is too long.\n"); |
144 | return(-1); | |
145 | } | |
9c7c6346 | 146 | if (getword_skip(MAXLEN,&gwarea,' ')<0) { |
40d1a88d FM |
147 | fprintf(stderr,"SARG: The data at column 8 is too long.\n"); |
148 | return(-1); | |
149 | } | |
9c7c6346 | 150 | if (getword(user,sizeof(user),&gwarea,' ')<0) { |
40d1a88d FM |
151 | fprintf(stderr,"SARG: The user at column 9 is too long.\n"); |
152 | return(-1); | |
153 | } | |
d5c1b1c1 GS |
154 | } |
155 | ||
156 | if(strncmp(user,"-",1) == 0 && strcmp(RealtimeUnauthRec,"ignore") == 0) | |
4bcb77cf | 157 | return(0); |
d5c1b1c1 GS |
158 | |
159 | tt=atoi(dat); | |
160 | t=localtime(&tt); | |
161 | if(strncmp(DateFormat,"u",1) == 0) | |
c4dd8d9c | 162 | strftime(tbuf, sizeof(tbuf), "%Y-%m-%d\t%H:%M", t); |
d5c1b1c1 | 163 | else if(strncmp(DateFormat,"e",1) == 0) |
c4dd8d9c | 164 | strftime(tbuf, sizeof(tbuf), "%d-%m-%Y\t%H:%M", t); |
d5c1b1c1 | 165 | |
120d768c | 166 | fprintf(ftmp,"%s\t%s\t%s\t%s\t%s\n",tbuf,ip,user,url,typ); |
4bcb77cf | 167 | return(0); |
d5c1b1c1 GS |
168 | } |
169 | ||
120d768c | 170 | static void datashow(const char *tmp) |
d5c1b1c1 GS |
171 | { |
172 | FILE *fin; | |
173 | char buf[MAXLEN]; | |
9c7c6346 | 174 | struct getwordstruct gwarea; |
d5c1b1c1 GS |
175 | |
176 | if((fin=fopen(tmp,"r"))==NULL) { | |
177 | fprintf(stderr, "SARG: (realtime) open error %s - %s\n",tmp,strerror(errno)); | |
178 | exit(1); | |
179 | } | |
180 | ||
181 | header(); | |
182 | ||
120d768c FM |
183 | while(fgets(buf, sizeof(buf), fin)) { |
184 | fixendofline(buf); | |
9c7c6346 FM |
185 | getword_start(&gwarea,buf); |
186 | if (getword(dat,sizeof(dat),&gwarea,'\t')<0) { | |
4bcb77cf FM |
187 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); |
188 | exit(1); | |
189 | } | |
9c7c6346 | 190 | if (getword(tim,sizeof(tim),&gwarea,'\t')<0) { |
4bcb77cf FM |
191 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); |
192 | exit(1); | |
193 | } | |
9c7c6346 | 194 | if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { |
4bcb77cf FM |
195 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); |
196 | exit(1); | |
197 | } | |
9c7c6346 | 198 | if (getword(user,sizeof(user),&gwarea,'\t')<0) { |
4bcb77cf FM |
199 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); |
200 | exit(1); | |
201 | } | |
d5c1b1c1 | 202 | if(strlen(dat) < 3 || strlen(user) < 1) continue; |
9c7c6346 | 203 | if (getword(url,sizeof(url),&gwarea,'\t')<0) { |
4bcb77cf FM |
204 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); |
205 | exit(1); | |
206 | } | |
9c7c6346 | 207 | if (getword(typ,sizeof(typ),&gwarea,'\t')<0) { |
4bcb77cf FM |
208 | printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp); |
209 | exit(1); | |
210 | } | |
d5c1b1c1 GS |
211 | if(strstr(RealtimeTypes,typ) == 0) |
212 | continue; | |
213 | ||
214 | if(strcmp(ouser,user) == 0 && strcmp(ourl,url) == 0) | |
215 | continue; | |
216 | ||
c4dd8d9c FM |
217 | if(userip) |
218 | strcpy(user,ip); | |
d6e703cc | 219 | strcpy(u2,user); |
246c8489 | 220 | if(Ip2Name) |
a1c55d8c | 221 | ip2name(u2,sizeof(u2)); |
965c4a6f | 222 | user_find(name, sizeof(name), u2); |
d6e703cc | 223 | |
48864d28 FM |
224 | if(dotinuser && strchr(name,'_')) { |
225 | subs(name,sizeof(name),"_","."); | |
d6e703cc FM |
226 | } |
227 | ||
228 | 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 |
229 | strcpy(ouser,user); |
230 | strcpy(ourl,url); | |
231 | } | |
232 | ||
dfb337be | 233 | puts("</table>\n</div>\n</body>\n</html>\n"); |
d5c1b1c1 GS |
234 | fclose(fin); |
235 | unlink(tmp); | |
236 | fflush(NULL); | |
dfb337be | 237 | |
d5c1b1c1 GS |
238 | } |
239 | ||
32e71fa4 | 240 | static void header(void) |
d5c1b1c1 GS |
241 | { |
242 | puts("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\""); | |
243 | puts(" \"http://www.w3.org/TR/html4/loose.dtd\">\n"); | |
244 | puts("<html>\n"); | |
245 | puts("<head>\n"); | |
246 | if(realtime_refresh) | |
247 | printf(" <meta http-equiv=refresh content=\"%d\" url=\"sarg-php/sarg-realtime.php\"; charset=\"%s\">\n",realtime_refresh,CharSet); | |
248 | else | |
249 | printf(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet); | |
250 | css(stdout); | |
251 | puts("</head>\n"); | |
252 | printf(buf,"<body style=\"font-family:%s;font-size:%s;background-color:%s;background-image:url(%s)\">\n",FontFace,TitleFontSize,BgColor,BgImage); | |
dfb337be | 253 | puts("<div align=\"center\"><table cellpadding=\"1\" cellspacing=\"1\">\n"); |
d5c1b1c1 GS |
254 | printf("<tr><th class=\"title2\" colspan=\"10\">SARG %s</th></tr>\n",text[134]); |
255 | printf("<tr><th class=\"text\" colspan=\"10\">%s: %d s</th></tr>\n",text[136],realtime_refresh); | |
b18ce4a0 | 256 | printf("<tr><th class=\"header_c\">%s</th><th class=\"header_c\">%s</th><th class=\"header_c\">%s</th><th class=\"header_c\">%s</th><th class=\"header_l\">%s</th></tr>\n",text[110],text[111],text[98],text[135],text[91]); |
d5c1b1c1 | 257 | } |