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"
28 int getdata(char *, FILE *);
29 void datashow(char *);
30 static void getlog(void);
31 static void header(void);
36 char ouser
[MAXLEN
]="";
46 static void getlog(void)
48 FILE *tmp
, *fp
, *fp_usr
;
49 char template1
[255]="/var/tmp/sargtpl1.XXXXXX";
50 char template2
[255]="/var/tmp/sargtpl2.XXXXXX";
56 if(UserTabFile
[0] != '\0') {
58 sprintf(msg
,"%s: %s",text
[86],UserTabFile
);
61 if((fp_usr
=fopen(UserTabFile
,"r"))==NULL
) {
62 fprintf(stderr
, "SARG: (realtime) %s: %s - %s\n",text
[45],UserTabFile
,strerror(errno
));
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]);
71 bzero(userfile
,nreg
+100);
72 strncat(userfile
,":",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 if (getword(bufy
,sizeof(bufy
),buf
,' ')<0) {
79 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserTabFile
);
82 for(z1
=0; z1
<=strlen(bufy
); z1
++) {
83 userfile
[z2
]=bufy
[z1
];
86 strncat(userfile
,":",1);
87 for(z1
=0; z1
<=strlen(buf
); z1
++) {
91 strncat(userfile
,":",1);
96 fd1
= mkstemp(template1
);
97 fd2
= mkstemp(template2
);
99 if((fd1
== -1 ) || ((tmp
= fdopen (fd1
, "w+" )) == NULL
) ) { /* failure, bail out */
100 fprintf(stderr
, "SARG: (realtime) mkstemp error - %s\n",strerror(errno
));
104 sprintf(cmd
,"tail -%d %s",realtime_access_log_lines
,AccessLog
);
105 fp
= popen(cmd
, "r");
106 while(fgets(buf
,sizeof(buf
),fp
) != NULL
)
107 if (getdata(buf
,tmp
)<0) {
108 printf("SARG: Maybe a broken record or garbage was returned by %s.\n",cmd
);
114 sprintf(cmd
,"sort -r -k 1,1 -k 2,2 -o %s %s",template2
,template1
);
116 if (!WIFEXITED(cstatus
) || WEXITSTATUS(cstatus
)) {
117 fprintf(stderr
, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus
));
118 fprintf(stderr
, "SARG: sort command: %s\n",cmd
);
125 int getdata(char *rec
, FILE *ftmp
)
130 if (getword3(dat
,sizeof(dat
),rec
,' ')<0) {
133 if (getword3(warea
,sizeof(warea
),rec
,' ')<0) {
136 while(strcmp(warea
,"") == 0 && strlen(rec
) > 0)
137 if (getword3(warea
,sizeof(warea
),rec
,' ')<0) {
140 if (getword3(ip
,sizeof(ip
),rec
,' ')<0) {
143 if (getword3(warea
,sizeof(warea
),rec
,' ')<0) {
146 if (getword3(warea
,sizeof(warea
),rec
,' ')<0) {
149 if (getword3(typ
,sizeof(typ
),rec
,' ')<0) {
152 if(strncmp(typ
,"CONNECT",7) == 0) {
153 if (getword3(url
,sizeof(url
),rec
,' ')<0) {
156 if (getword3(user
,sizeof(user
),rec
,' ')<0) {
160 if (getword3(url
,sizeof(url
),rec
,'/')<0) {
163 if (getword3(url
,sizeof(url
),rec
,'/')<0) {
166 if (getword3(url
,sizeof(url
),rec
,'/')<0) {
169 if (getword3(user
,sizeof(user
),rec
,' ')<0) {
172 if (getword3(user
,sizeof(user
),rec
,' ')<0) {
177 if(strncmp(user
,"-",1) == 0 && strcmp(RealtimeUnauthRec
,"ignore") == 0)
182 if(strncmp(DateFormat
,"u",1) == 0)
183 strftime(tbuf
, 127, "%Y-%m-%d %H:%M", t
);
184 else if(strncmp(DateFormat
,"e",1) == 0)
185 strftime(tbuf
, 127, "%d-%m-%Y %H:%M", t
);
187 sprintf(warea
,"%s %s %s %s %s\n",tbuf
,ip
,user
,url
,typ
);
192 void datashow(char *tmp
)
197 if((fin
=fopen(tmp
,"r"))==NULL
) {
198 fprintf(stderr
, "SARG: (realtime) open error %s - %s\n",tmp
,strerror(errno
));
204 while(fgets(buf
, MAXLEN
, fin
)) {
205 buf
[strlen(buf
)-1]='\0';
206 if (getword3(dat
,sizeof(dat
),buf
,' ')<0) {
207 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp
);
210 if (getword3(tim
,sizeof(tim
),buf
,' ')<0) {
211 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp
);
214 if (getword3(ip
,sizeof(ip
),buf
,' ')<0) {
215 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp
);
218 if (getword3(user
,sizeof(user
),buf
,' ')<0) {
219 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp
);
222 if(strlen(dat
) < 3 || strlen(user
) < 1) continue;
223 if (getword3(url
,sizeof(url
),buf
,' ')<0) {
224 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp
);
227 if (getword3(typ
,sizeof(typ
),buf
,' ')<0) {
228 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp
);
231 if(strstr(RealtimeTypes
,typ
) == 0)
234 if(strcmp(ouser
,user
) == 0 && strcmp(ourl
,url
) == 0)
238 if(strcmp(Ip2Name
,"yes") == 0)
239 ip2name(u2
,sizeof(u2
));
240 if(UserTabFile
[0] != '\0') {
241 sprintf(warea
,":%s:",u2
);
242 if((str
=(char *) strstr(userfile
,warea
)) != (char *) NULL
) {
244 str2
=(char *) strstr(str
+1,":");
247 while(str2
[z1
] != ':') {
251 } else strcpy(name
,u2
);
252 } else strcpy(name
,u2
);
254 if(dotinuser
&& strstr(name
,"_")) {
255 str2
=(char *)subs(name
,"_",".");
259 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
);
264 puts("</table>\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(buf
,"<body style=\"font-family:%s;font-size:%s;background-color:%s;background-image:url(%s)\">\n",FontFace
,TitleFontSize
,BgColor
,BgImage
);
284 puts("<center><table cellpadding=\"1\" cellspacing=\"1\">\n");
285 printf("<tr><th class=\"title2\" colspan=\"10\">SARG %s</th></tr>\n",text
[134]);
286 printf("<tr><th class=\"text\" colspan=\"10\">%s: %d s</th></tr>\n",text
[136],realtime_refresh
);
287 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]);