2 * AUTHOR: Pedro Lineu Orso orso@penguintech.com.br
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
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.
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.
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.
26 #include "include/conf.h"
27 #include "include/defs.h"
29 static int getdata(char*, FILE*);
30 static void datashow(const char *);
31 static void getlog(void);
32 static void header(void);
37 char ouser
[MAXLEN
]="";
47 static void getlog(void)
50 char template1
[255]="/var/tmp/sargtpl1.XXXXXX";
51 char template2
[255]="/var/tmp/sargtpl2.XXXXXX";
57 read_usertab(UserTabFile
);
59 fd1
= mkstemp(template1
);
60 fd2
= mkstemp(template2
);
62 if((fd1
== -1 ) || ((tmp
= fdopen (fd1
, "w+" )) == NULL
) ) { /* failure, bail out */
63 fprintf(stderr
, "SARG: (realtime) mkstemp error - %s\n",strerror(errno
));
67 sprintf(cmd
,"tail -%d %s",realtime_access_log_lines
,AccessLog
[0]);
69 while(fgets(buf
,sizeof(buf
),fp
) != NULL
)
70 if (getdata(buf
,tmp
)<0) {
71 fprintf(stderr
,"SARG: Maybe a broken record or garbage was returned by %s.\n",cmd
);
77 sprintf(cmd
,"sort -r -k 1,1 -k 2,2 -o \"%s\" \"%s\"",template2
,template1
);
79 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
80 fprintf(stderr
, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus
));
81 fprintf(stderr
, "SARG: sort command: %s\n",cmd
);
88 static int getdata(char *rec
, FILE *ftmp
)
94 struct getwordstruct gwarea
;
96 getword_start(&gwarea
,rec
);
97 if (getword(dat
,sizeof(dat
),&gwarea
,' ')<0) {
98 fprintf(stderr
,"SARG: The time stamp at column 1 is too long.\n");
101 if (getword(warea
,sizeof(warea
),&gwarea
,' ')<0) {
102 fprintf(stderr
,"SARG: The connection duration at column 2 is too long.\n");
105 while(strcmp(warea
,"") == 0 && gwarea
.current
[0] != '\0')
106 if (getword(warea
,sizeof(warea
),&gwarea
,' ')<0) {
109 if (getword(ip
,sizeof(ip
),&gwarea
,' ')<0) {
110 fprintf(stderr
,"SARG: The IP address at column 3 is too long.\n");
113 if (getword_skip(MAXLEN
,&gwarea
,' ')<0) {
114 fprintf(stderr
,"SARG: The status at column 4 is too long.\n");
117 if (getword_skip(MAXLEN
,&gwarea
,' ')<0) {
118 fprintf(stderr
,"SARG: The size at column 5 is too long.\n");
121 if (getword(typ
,sizeof(typ
),&gwarea
,' ')<0) {
122 fprintf(stderr
,"SARG: The action at column 6 is too long.\n");
125 if(strncmp(typ
,"CONNECT",7) == 0) {
126 if (getword(url
,sizeof(url
),&gwarea
,' ')<0) {
129 if (getword(user
,sizeof(user
),&gwarea
,' ')<0) {
133 if (getword_skip(MAXLEN
,&gwarea
,'/')<0) {
134 fprintf(stderr
,"SARG: The URL at column 7 is too long.\n");
137 if (getword_skip(MAXLEN
,&gwarea
,'/')<0) {
138 fprintf(stderr
,"SARG: The URL at column 7 is too long.\n");
141 if (getword(url
,sizeof(url
),&gwarea
,'/')<0) {
142 fprintf(stderr
,"SARG: The URL at column 7 is too long.\n");
145 if (getword_skip(MAXLEN
,&gwarea
,' ')<0) {
146 fprintf(stderr
,"SARG: The data at column 8 is too long.\n");
149 if (getword(user
,sizeof(user
),&gwarea
,' ')<0) {
150 fprintf(stderr
,"SARG: The user at column 9 is too long.\n");
155 if(strncmp(user
,"-",1) == 0 && strcmp(RealtimeUnauthRec
,"ignore") == 0)
160 if(strncmp(DateFormat
,"u",1) == 0)
161 strftime(tbuf
, sizeof(tbuf
), "%Y-%m-%d\t%H:%M", t
);
162 else if(strncmp(DateFormat
,"e",1) == 0)
163 strftime(tbuf
, sizeof(tbuf
), "%d-%m-%Y\t%H:%M", t
);
165 fprintf(ftmp
,"%s\t%s\t%s\t%s\t%s\n",tbuf
,ip
,user
,url
,typ
);
169 static void datashow(const char *tmp
)
173 struct getwordstruct gwarea
;
175 if((fin
=fopen(tmp
,"r"))==NULL
) {
176 fprintf(stderr
, "SARG: (realtime) open error %s - %s\n",tmp
,strerror(errno
));
182 while(fgets(buf
, sizeof(buf
), fin
)) {
184 getword_start(&gwarea
,buf
);
185 if (getword(dat
,sizeof(dat
),&gwarea
,'\t')<0) {
186 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp
);
189 if (getword(tim
,sizeof(tim
),&gwarea
,'\t')<0) {
190 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp
);
193 if (getword(ip
,sizeof(ip
),&gwarea
,'\t')<0) {
194 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp
);
197 if (getword(user
,sizeof(user
),&gwarea
,'\t')<0) {
198 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp
);
201 if(strlen(dat
) < 3 || strlen(user
) < 1) continue;
202 if (getword(url
,sizeof(url
),&gwarea
,'\t')<0) {
203 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp
);
206 if (getword(typ
,sizeof(typ
),&gwarea
,'\t')<0) {
207 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp
);
210 if(strstr(RealtimeTypes
,typ
) == 0)
213 if(strcmp(ouser
,user
) == 0 && strcmp(ourl
,url
) == 0)
220 ip2name(u2
,sizeof(u2
));
221 // get_usertab_name(u2,name,sizeof(name));
224 if(dotinuser
&& strchr(name
,'_')) {
225 subs(name
,sizeof(name
),"_",".");
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
);
233 puts("</table>\n</div>\n</body>\n</html>\n");
240 static void header(void)
242 puts("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"");
243 puts(" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
247 printf(" <meta http-equiv=refresh content=\"%d\" url=\"sarg-php/sarg-realtime.php\"; charset=\"%s\">\n",realtime_refresh
,CharSet
);
249 printf(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet
);
252 printf(buf
,"<body style=\"font-family:%s;font-size:%s;background-color:%s;background-image:url(%s)\">\n",FontFace
,TitleFontSize
,BgColor
,BgImage
);
253 puts("<div align=\"center\"><table cellpadding=\"1\" cellspacing=\"1\">\n");
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
);
256 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]);