2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
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.
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.
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.
27 #include "include/conf.h"
28 #include "include/defs.h"
30 static int getdata(char*, FILE*);
31 static void datashow(const char *);
32 static void getlog(void);
33 static void header(void);
42 static void getlog(void)
45 char template1
[255]="/var/tmp/sargtpl1.XXXXXX";
46 char template2
[255]="/var/tmp/sargtpl2.XXXXXX";
51 struct longlinestruct line
;
53 init_usertab(UserTabFile
);
55 fd1
= mkstemp(template1
);
56 fd2
= mkstemp(template2
);
58 if((fd1
== -1 ) || ((tmp
= fdopen (fd1
, "w+" )) == NULL
) ) { /* failure, bail out */
59 debuga(_("(realtime) mkstemp error - %s\n"),strerror(errno
));
63 if (longline_prepare(&line
)<0) {
64 debuga(_("Not enough memory to read the log file\n"));
68 sprintf(cmd
,"tail -%d %s",realtime_access_log_lines
,AccessLog
[0]);
70 while((buf
=longline_read(fp
,&line
)) != NULL
)
71 if (getdata(buf
,tmp
)<0) {
72 debuga(_("Maybe a broken record or garbage was returned by %s\n"),cmd
);
79 sprintf(cmd
,"sort -r -k 1,1 -k 2,2 -o \"%s\" \"%s\"",template2
,template1
);
81 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
82 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus
));
83 debuga(_("sort command: %s\n"),cmd
);
90 static int getdata(char *rec
, FILE *ftmp
)
98 char user
[MAX_USER_LEN
];
100 struct getwordstruct gwarea
;
102 getword_start(&gwarea
,rec
);
103 if (getword(dat
,sizeof(dat
),&gwarea
,' ')<0) {
104 debuga(_("The time stamp at column 1 is too long\n"));
107 if (getword(warea
,sizeof(warea
),&gwarea
,' ')<0) {
108 debuga(_("The connection duration at column 2 is too long\n"));
111 while(strcmp(warea
,"") == 0 && gwarea
.current
[0] != '\0')
112 if (getword(warea
,sizeof(warea
),&gwarea
,' ')<0) {
115 if (getword(ip
,sizeof(ip
),&gwarea
,' ')<0) {
116 debuga(_("The IP address at column 3 is too long\n"));
119 if (getword_skip(MAXLEN
,&gwarea
,' ')<0) {
120 debuga(_("The status at column 4 is too long\n"));
123 if (getword_skip(MAXLEN
,&gwarea
,' ')<0) {
124 debuga(_("The size at column 5 is too long\n"));
127 if (getword(typ
,sizeof(typ
),&gwarea
,' ')<0) {
128 debuga(_("The action at column 6 is too long\n"));
131 if(strncmp(typ
,"CONNECT",7) == 0) {
132 if (getword_ptr(rec
,&url
,&gwarea
,' ')<0) {
133 debuga(_("The URL at column 7 is too long\n"));
136 if (getword(user
,sizeof(user
),&gwarea
,' ')<0) {
137 debuga(_("The user ID at column 8 is too long\n"));
141 if (getword_skip(MAXLEN
,&gwarea
,'/')<0) {
142 debuga(_("The URL at column 7 is too long\n"));
145 if (getword_skip(MAXLEN
,&gwarea
,'/')<0) {
146 debuga(_("The URL at column 7 is too long\n"));
149 if (getword_ptr(rec
,&url
,&gwarea
,'/')<0) {
150 debuga(_("The URL at column 7 is too long\n"));
153 if (getword_skip(MAXLEN
,&gwarea
,' ')<0) {
154 debuga(_("The data at column 8 is too long\n"));
157 if (getword(user
,sizeof(user
),&gwarea
,' ')<0) {
158 debuga(_("The user at column 9 is too long\n"));
163 if(strncmp(user
,"-",1) == 0 && strcmp(RealtimeUnauthRec
,"ignore") == 0)
168 if(strncmp(DateFormat
,"u",1) == 0)
169 strftime(tbuf
, sizeof(tbuf
), "%Y-%m-%d\t%H:%M", t
);
170 else if(strncmp(DateFormat
,"e",1) == 0)
171 strftime(tbuf
, sizeof(tbuf
), "%d-%m-%Y\t%H:%M", t
);
173 fprintf(ftmp
,"%s\t%s\t%s\t%s\t%s\n",tbuf
,ip
,user
,url
,typ
);
177 static void datashow(const char *tmp
)
185 char ouser
[MAX_USER_LEN
]="";
187 char user
[MAX_USER_LEN
];
188 char u2
[MAX_USER_LEN
];
191 struct getwordstruct gwarea
;
192 struct longlinestruct line
;
194 if((fin
=fopen(tmp
,"r"))==NULL
) {
195 debuga(_("(realtime) open error %s - %s\n"),tmp
,strerror(errno
));
201 if (longline_prepare(&line
)<0) {
202 debuga(_("Not enough memory to read the log file\n"));
206 while((buf
=longline_read(fin
,&line
))!=NULL
) {
208 getword_start(&gwarea
,buf
);
209 if (getword(dat
,sizeof(dat
),&gwarea
,'\t')<0) {
210 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp
);
213 if (getword(tim
,sizeof(tim
),&gwarea
,'\t')<0) {
214 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp
);
217 if (getword(ip
,sizeof(ip
),&gwarea
,'\t')<0) {
218 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp
);
221 if (getword(user
,sizeof(user
),&gwarea
,'\t')<0) {
222 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp
);
225 if(strlen(dat
) < 3 || strlen(user
) < 1) continue;
226 if (getword_ptr(buf
,&url
,&gwarea
,'\t')<0) {
227 debuga(_("Maybe you have a broken url in your %s file\n"),tmp
);
230 if (getword(typ
,sizeof(typ
),&gwarea
,'\t')<0) {
231 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp
);
234 if(strstr(RealtimeTypes
,typ
) == 0)
237 if(strcmp(ouser
,user
) == 0 && ourl
&& strcmp(ourl
,url
) == 0)
244 ip2name(u2
,sizeof(u2
));
245 user_find(name
, sizeof(name
), u2
);
247 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
);
251 if (!ourl
|| url_len
>=ourl_size
) {
253 ourl
=realloc(ourl
,ourl_size
);
255 debuga(_("Not enough memory to store the url\n"));
261 longline_free(&line
);
262 if (ourl
) free(ourl
);
264 puts("</table>\n</div>\n</body>\n</html>\n");
271 static void header(void)
273 puts("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"");
274 puts(" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
278 printf(" <meta http-equiv=refresh content=\"%d\" url=\"sarg-php/sarg-realtime.php\"; charset=\"%s\">\n",realtime_refresh
,CharSet
);
280 printf(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet
);
283 printf("<body style=\"font-family:%s;font-size:%s;background-color:%s;background-image:url(%s)\">\n",FontFace
,TitleFontSize
,BgColor
,BgImage
);
284 puts("<div align=\"center\"><table cellpadding=\"1\" cellspacing=\"1\">\n");
285 printf("<tr><th class=\"title_l\" colspan=\"10\">SARG %s</th></tr>\n",_("Realtime"));
286 printf("<tr><th class=\"text\" colspan=\"10\">%s: %d s</th></tr>\n",_("Auto refresh"),realtime_refresh
);
287 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",_("DATE/TIME"),_("IP/NAME"),_("USERID"),_("TYPE"),_("ACCESSED SITE"));