/*
SRG - Squid Report Generator
Report by Location
Copyright 2003 University of Waikato
This file is part of SRG.
SRG 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.
SRG 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 SRG; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "UserReport.h"
#include "LocationReport.h"
#include "srg.h"
#include "prototypes.h"
extern int h_errno;
void LocationReport::process_line(const log_line *line) {
if (srg.debug)
fprintf(stderr, "In LocationReport::process_line for "
"location '%s'\n", mName);
stats.connects++;
stats.bytesTransferred += line->size;
stats.timeSpent += line->elapsedTime;
if (is_cache_hit(line->resultCode)) {
stats.hits++;
stats.bytesHit += line->size;
} else {
stats.misses++;
stats.bytesMissed += line->size;
}
time_t tLine = line->timestamp;
times.push_back(tLine);
if (is_cache_denied(line->resultCode)) {
stats.deniedHits++;
denied.push_back(true);
} else {
denied.push_back(false);
}
if (srg.lookupHosts) {
char *tclient=dnscache->get_name(line->clientAddress);
if (tclient) {
host.push_back(tclient);
} else {
host.push_back(strdup(inet_ntoa(
line->clientAddress)));
}
} else {
host.push_back(strdup(inet_ntoa(line->clientAddress)));
}
}
void LocationReport::generate_report(const char * basename, const char *user,
const char *site) {
char *t = NULL;
/* Generate the HTML file */
char *defaultname = "^index^";
char *filename;
FILE *outfile = NULL;
if (strcmp(mName, "") == 0)
mName = strdup(defaultname);
t = md5this(mName);
if (srg.usephp) {
asprintf(&filename, "%s/%s/%s.php", srg.outputDir, basename, t);
} else {
asprintf(&filename, "%s/%s/%s.html", srg.outputDir,
basename, t);
}
free(t);
outfile = fopen(filename, "w");
if(outfile==NULL) {
fprintf(stderr,"LocationReport: Cannot open output file: %s\n",
filename);
exit(1);
}
free(filename);
/* Header & Title */
if (srg.groupBy > 0) {
html_header(outfile, "../../../");
} else {
html_header(outfile, "../../");
}
fprintf(outfile, "\n",
version, HOME_URL);
/* Misc Stats */
fprintf(outfile, "
");
fprintf(outfile, "| Period: | %d %s %d",
localtime(&srg.startTime)->tm_mday,
month_names[localtime(&srg.startTime)->tm_mon],
localtime(&srg.startTime)->tm_year+1900);
fprintf(outfile, " - %d %s %d |
",
localtime(&srg.endTime)->tm_mday,
month_names[localtime(&srg.endTime)->tm_mon],
localtime(&srg.endTime)->tm_year+1900);
if (srg.groupBy > 0) {
fprintf(outfile, "| Group: | %s |
", user);
}
fprintf(outfile, "| "
"Site:\n | %s"
" |
", site, site);
fprintf(outfile, "
");
/* Notices Row */
fprintf(outfile, ""
"
\n");
float percentin, percentout;
unsigned int total = stats.hits+stats.misses;
percentin = (float)(stats.hits/total)*100;
percentout = (float)(stats.misses/total)*100;
fprintf(outfile, ""
"| LOCATION | HITS | BYTES | "
"HIT | MISS | ");
if (srg.showtimes)
fprintf(outfile, "TIME(ms) | ");
if (srg.showrates)
fprintf(outfile, "RATE (kb/s) | ");
fprintf(outfile, "
", site,
strcasecmp(mName, defaultname)==0 ? "/" : mName);
if (strcasecmp(mName, defaultname)==0) {
fprintf(outfile, "/");
} else {
t = mName;
unsigned int tlen = strlen(t);
for (unsigned int i=0;i0)
fprintf(outfile, " ");
char tmp[31];
strncpy(tmp, t, 30);
tmp[30] = '\0';
fprintf(outfile, "%s", tmp);
t+=30;
}
}
fprintf(outfile, " | ");
t = FormatOutput(stats.connects);
fprintf(outfile, "%s | ", t);
free(t);
t = FormatOutput(stats.bytesTransferred);
fprintf(outfile, "%s | ", t);
free(t);
fprintf(outfile, "%2.2f%% | ", percentin);
fprintf(outfile, "%2.2f%% | ", percentout);
if (srg.showtimes) {
t = FormatOutput(stats.timeSpent);
fprintf(outfile, "%s | ", t);
free(t);
}
if (srg.showrates) {
fprintf(outfile, "%2.2f | ",
(float) stats.bytesTransferred/stats.timeSpent);
}
fprintf(outfile, "
");
/* Times Accessed Table */
fprintf(outfile, "\t\n");
fprintf(outfile, "\t\t\n");
fprintf(outfile, "\t\t| Time(s) Accessed | \n");
fprintf(outfile, "\n\t\tRequested by | \n");
fprintf(outfile, "\t\tError | \n");
fprintf(outfile, "\t\t
\n");
list::const_iterator iter;
list::const_iterator deniediter;
list::const_iterator hostiter;
hostiter=host.begin();
deniediter=denied.begin();
int rows = 0;
for (iter=times.begin(); iter != times.end(); iter++) {
time_t thistime = (*iter);
fprintf(outfile, "\t| %s | ",
((rows%2)==0) ? " class=\"highlightRow\"" : "",
ctime(&thistime));
fprintf(outfile, "%s | ",
(*hostiter));
fprintf(outfile, "%s |
\n",
(*deniediter) ? "DENIED" : "");
deniediter++;
hostiter++;
rows++;
}
fprintf(outfile, "\n\t
");
if (srg.usephp && srg.authenticate) {
fprintf(outfile, "\n\t");
}
fprintf(outfile, "\n\t
Back to locations page",
srg.indexfname);
/* Finish off the HTML */
if (srg.groupBy > 0) {
html_footer(outfile, "../../../");
} else {
html_footer(outfile, "../../");
}
fclose(outfile);
}
// Get statistics about this report
summary_info LocationReport::getStats() {
summary_info returnInfo;
returnInfo = stats;
return returnInfo;
}