1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
|
/*
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net
* 1998, 2015
*
* SARG donations:
* please look at http://sarg.sourceforge.net/donations.php
* Support:
* http://sourceforge.net/projects/sarg/forums/forum/363374
* ---------------------------------------------------------------------
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
*/
#include "include/conf.h"
#include "include/defs.h"
#include "include/filelist.h"
FileListObject UserAgentLog=NULL;
//! Log file where the user agent data are written.
static char UserAgentTempLog[MAXLEN]="";
static struct tm UserAgentStartDate;
static struct tm UserAgentEndDate;
/*!
* Open the temporary file to store the useragent entries to be
* reported.
*
* \return The file handle. It must be closed when the data have
* been written.
*/
FILE *UserAgent_Open(void)
{
FILE *fp_ou=NULL;
if (UserAgentTempLog[0]) {
debuga(__FILE__,__LINE__,_("Useragent log already opened\n"));
exit(EXIT_FAILURE);
}
if ((ReportType & REPORT_TYPE_USERAGENT)!=0) {
format_path(__FILE__, __LINE__, UserAgentTempLog, sizeof(UserAgentTempLog), "%s/squagent.int_unsort", tmp);
if ((fp_ou=fopen(UserAgentTempLog,"w"))==NULL) {
debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),UserAgentTempLog,strerror(errno));
exit(EXIT_FAILURE);
}
memset(&UserAgentStartDate,0,sizeof(UserAgentStartDate));
memset(&UserAgentEndDate,0,sizeof(UserAgentEndDate));
}
return(fp_ou);
}
/*!
* Write a user agent entry into the temporary log.
*
* \param fp The file opened by UserAgent_Open().
* \param Ip The IP address using this agent.
* \param User The user name.
* \param Agent The user agent string.
*/
void UserAgent_Write(FILE *fp,const struct tm *Time,const char *Ip,const char *User,const char *Agent)
{
if (fp) {
if (useragent_count==0 || compare_date(&UserAgentStartDate,Time)>0)
memcpy(&UserAgentStartDate,Time,sizeof(UserAgentStartDate));
if (useragent_count==0 || compare_date(&UserAgentEndDate,Time)<0)
memcpy(&UserAgentEndDate,Time,sizeof(UserAgentEndDate));
fprintf(fp,"%s\t%s\t%s\n",Ip,Agent,User);
useragent_count++;
}
}
/*!
* Read the user provided useragent file and create
* a temporary file with the data to report.
*/
void UserAgent_Readlog(const struct ReadLogDataStruct *ReadFilter)
{
FileObject *fp_log;
FILE *fp_ou = NULL;
char *ptr;
char ip[80], data[50], agent[MAXLEN], user[MAXLEN];
int day,month,year;
char monthname[5];
int hour,min;
const char *FileName;
unsigned long totregsl=0;
int ndate;
struct getwordstruct gwarea, gwarea1;
longline line;
FileListIterator FIter;
struct tm logtime;
int dfrom;
int duntil;
fp_ou=UserAgent_Open();
if ((line=longline_create())==NULL) {
debuga(__FILE__,__LINE__,_("Not enough memory to read useragent log\n"));
exit(EXIT_FAILURE);
}
memset(&logtime,0,sizeof(logtime));
getperiod_torange(&period,&dfrom,&duntil);
FIter=FileListIter_Open(UserAgentLog);
while ((FileName=FileListIter_Next(FIter))!=NULL)
{
longline_reset(line);
if ((fp_log=decomp(FileName))==NULL) {
debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),FileName,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
if (debug) {
debuga(__FILE__,__LINE__,_("Reading useragent log \"%s\"\n"),FileName);
}
while ((ptr=longline_read(fp_log,line))!=NULL) {
totregsl++;
getword_start(&gwarea,ptr);
if (getword(ip,sizeof(ip),&gwarea,' ')<0 || getword_skip(10,&gwarea,'[')<0 ||
getword(data,sizeof(data),&gwarea,' ')<0) {
debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),FileName);
exit(EXIT_FAILURE);
}
getword_start(&gwarea1,data);
if (getword_atoi(&day,&gwarea1,'/')<0 || getword(monthname,sizeof(monthname),&gwarea1,'/')<0 ||
getword_atoi(&year,&gwarea1,':')<0) {
debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n"),FileName);
exit(EXIT_FAILURE);
}
month=month2num(monthname)+1;
if (month>12) {
debuga(__FILE__,__LINE__,_("Invalid month name \"%s\" found in user agent file \"%s\""),monthname,FileName);
exit(EXIT_FAILURE);
}
if (dfrom!=0 || duntil!=0){
ndate=year*10000+month*100+day;
if (ndate<dfrom) continue;
if (ndate>duntil) break;
}
if (getword_atoi(&hour,&gwarea1,':')<0 || getword_atoi(&min,&gwarea1,':')<0) {
debuga(__FILE__,__LINE__,_("Invalid time in file \"%s\"\n"),FileName);
exit(EXIT_FAILURE);
}
if (ReadFilter->StartTime>=0 || ReadFilter->EndTime>=0)
{
int hmr=hour*100+min;
if (hmr<ReadFilter->StartTime || hmr>=ReadFilter->EndTime)
continue;
}
logtime.tm_year=year-1900;
logtime.tm_mon=month-1;
logtime.tm_mday=day;
if (getword_skip(MAXLEN,&gwarea,'"')<0 || getword(agent,sizeof(agent),&gwarea,'"')<0) {
debuga(__FILE__,__LINE__,_("Invalid useragent in file \"%s\"\n"),FileName);
exit(EXIT_FAILURE);
}
if (gwarea.current[0]!='\0') {
if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,'\n')<0) {
debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),FileName);
exit(EXIT_FAILURE);
}
if (user[0] == '-')
strcpy(user,ip);
if (user[0] == '\0')
strcpy(user,ip);
} else {
strcpy(user,ip);
}
UserAgent_Write(fp_ou,&logtime,ip,user,agent);
}
if (FileObject_Close(fp_log)==EOF) {
debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),FileName,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
}
FileListIter_Close(FIter);
longline_destroy(&line);
if (debug) {
debuga(__FILE__,__LINE__,_(" Records read: %ld\n"),totregsl);
}
if (fclose(fp_ou)==EOF) {
debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),UserAgentTempLog,strerror(errno));
exit(EXIT_FAILURE);
}
}
void UserAgent(void)
{
FILE *fp_in = NULL, *fp_ou = NULL, *fp_ht = NULL;
char buf[MAXLEN];
char ip[80], agent[MAXLEN], user[MAXLEN];
char ipbefore[MAXLEN]="";
char namebefore[MAXLEN]="";
char tagent[MAXLEN];
char user_old[MAXLEN]="";
char agent_old[MAXLEN]="";
char hfile[MAXLEN];
char idate[100], fdate[100];
char tmp2[MAXLEN];
char tmp3[MAXLEN];
char csort[MAXLEN];
int agentot=0, agentot2=0, agentdif=0, cont=0, nagent;
int cstatus;
double perc;
struct getwordstruct gwarea;
if (!UserAgentTempLog[0] || useragent_count==0) return;
format_path(__FILE__, __LINE__, tmp2, sizeof(tmp2), "%s/squagent.int_log", tmp);
if (debug) {
debuga(__FILE__,__LINE__,_("Sorting file \"%s\"\n"),tmp2);
}
if (snprintf(csort,sizeof(csort),"sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2,UserAgentTempLog)>=sizeof(csort)) {
debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2,UserAgentTempLog);
exit(EXIT_FAILURE);
}
cstatus=system(csort);
if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
if ((fp_in=fopen(tmp2,"r"))==NULL) {
debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp2,strerror(errno));
debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
if (!KeepTempLog && unlink(UserAgentTempLog)) {
debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),UserAgentTempLog,strerror(errno));
exit(EXIT_FAILURE);
}
format_path(__FILE__, __LINE__, hfile, sizeof(hfile), "%s/useragent.html", outdirname);
if ((fp_ht=fopen(hfile,"w"))==NULL) {
debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),hfile,strerror(errno));
exit(EXIT_FAILURE);
}
if (debug)
debuga(__FILE__,__LINE__,_("Making Useragent report\n"));
write_html_header(fp_ht,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Squid Useragent's Report"),HTML_JS_NONE);
fprintf(fp_ht,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Squid Useragent's Report"));
strftime(idate,sizeof(idate),"%x",&UserAgentStartDate);
strftime(fdate,sizeof(fdate),"%x",&UserAgentEndDate);
fprintf(fp_ht,"<tr><td class=\"header_c\">%s: %s - %s</td></tr>\n",_("Period"),idate,fdate);
close_html_header(fp_ht);
fputs("<br><br>\n",fp_ht);
fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
fputs("<tr><td> </td><td> </td></tr>",fp_ht);
fprintf(fp_ht,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("USERID"),_("AGENT"));
while (fgets(buf,sizeof(buf),fp_in)!=NULL) {
getword_start(&gwarea,buf);
if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
debuga(__FILE__,__LINE__,_("Invalid IP address in file \"%s\"\n"),tmp2);
exit(EXIT_FAILURE);
}
if (Ip2Name) {
if (strcmp(ip,ipbefore) != 0) {
strcpy(ipbefore,ip);
ip2name(ip,sizeof(ip));
strcpy(namebefore,ip);
} else strcpy(ip,namebefore);
}
if (getword(agent,sizeof(agent),&gwarea,'\t')<0) {
debuga(__FILE__,__LINE__,_("Invalid useragent in file \"%s\"\n"),tmp2);
exit(EXIT_FAILURE);
}
if (getword(user,sizeof(user),&gwarea,'\t')<0) {
debuga(__FILE__,__LINE__,_("Invalid user ID in file \"%s\"\n"),tmp2);
exit(EXIT_FAILURE);
}
if (strcmp(user,user_old) != 0) {
fprintf(fp_ht,"<tr><td class=\"data2\">%s</td><td class=\"data2\">",user);
output_html_string(fp_ht,agent,250);
fputs("</td></tr>\n",fp_ht);
strcpy(user_old,user);
strcpy(agent_old,agent);
} else if (strcmp(agent,agent_old) != 0) {
fputs("<tr><td></td><td class=\"data2\">",fp_ht);
output_html_string(fp_ht,agent,250);
fputs("</td></tr>\n",fp_ht);
strcpy(agent_old,agent);
}
}
fputs("</table>\n",fp_ht);
if (fclose(fp_in)==EOF) {
debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp2,strerror(errno));
exit(EXIT_FAILURE);
}
format_path(__FILE__, __LINE__, tmp3, sizeof(tmp3), "%s/squagent2.int_log", tmp);
if (snprintf(csort,sizeof(csort),"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2)>=sizeof(csort)) {
debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2,tmp3);
exit(EXIT_FAILURE);
}
cstatus=system(csort);
if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
if ((fp_in=fopen(tmp3,"r"))==NULL) {
debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno));
debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
if (!KeepTempLog && unlink(tmp2)) {
debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp2,strerror(errno));
exit(EXIT_FAILURE);
}
if ((fp_ou=fopen(tmp2,"w"))==NULL) {
debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp2,strerror(errno));
exit(EXIT_FAILURE);
}
agent_old[0]='\0';
cont=0;
while (fgets(buf,sizeof(buf),fp_in)!=NULL) {
getword_start(&gwarea,buf);
if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
debuga(__FILE__,__LINE__,_("Invalid IP address in file \"%s\"\n"),tmp3);
exit(EXIT_FAILURE);
}
if (getword(agent,sizeof(agent),&gwarea,'\t')<0) {
debuga(__FILE__,__LINE__,_("Invalid useragent in file \"%s\"\n"),tmp3);
exit(EXIT_FAILURE);
}
if (!cont) {
cont++;
strcpy(agent_old,agent);
}
if (strcmp(agent,agent_old) != 0) {
agentdif++;
fprintf(fp_ou,"%06d %s\n",agentot,agent_old);
strcpy(agent_old,agent);
agentot2+=agentot;
agentot=0;
}
agentot++;
}
agentdif++;
fprintf(fp_ou,"%06d %s\n",agentot,agent);
agentot2+=agentot;
if (fclose(fp_ou)==EOF) {
debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),tmp2,strerror(errno));
exit(EXIT_FAILURE);
}
if (fclose(fp_in)==EOF) {
debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,strerror(errno));
exit(EXIT_FAILURE);
}
if (!KeepTempLog && unlink(tmp3)) {
debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno));
exit(EXIT_FAILURE);
}
if (snprintf(csort,sizeof(csort),"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2)>=sizeof(csort)) {
debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2,tmp3);
exit(EXIT_FAILURE);
}
cstatus=system(csort);
if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
if ((fp_in=fopen(tmp3,"r"))==NULL) {
debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno));
debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
if (!KeepTempLog && unlink(tmp2)) {
debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp2,strerror(errno));
exit(EXIT_FAILURE);
}
fputs("<br><br>\n",fp_ht);
fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ht);
fprintf(fp_ht,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_c\">%%</th></tr>\n",_("AGENT"),_("TOTAL"));
perc=0.;
while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
fixendofline(buf);
getword_start(&gwarea,buf);
if (getword(tagent,sizeof(tagent),&gwarea,' ')<0) {
debuga(__FILE__,__LINE__,_("Invalid useragent in file \"%s\"\n"),tmp3);
exit(EXIT_FAILURE);
}
nagent=atoi(tagent);
perc=(agentot2>0) ? nagent * 100. / agentot2 : 0.;
fputs("<tr><td class=\"data2\">",fp_ht);
output_html_string(fp_ht,gwarea.current,250);
fprintf(fp_ht,"</td><td class=\"data\">%d</td><td class=\"data\">%3.2lf</td></tr>\n",nagent,perc);
}
if (fclose(fp_in)==EOF) {
debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,strerror(errno));
exit(EXIT_FAILURE);
}
fputs("</table></div>\n",fp_ht);
write_html_trailer(fp_ht);
if (fclose(fp_ht)==EOF) {
debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),hfile,strerror(errno));
exit(EXIT_FAILURE);
}
if (!KeepTempLog && unlink(tmp3)) {
debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno));
exit(EXIT_FAILURE);
}
return;
}
|