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
|
/*
SRG - Squid Report Generator
Report by user
Copyright 2005 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
$Id: UserReport.cc 246 2008-01-19 23:13:17Z matt $
*/
#include "UserReport.h"
#include "srg.h"
#include "prototypes.h"
UserReport::~UserReport() {
list<SiteReport*>::const_iterator iter;
for (iter=sites.begin(); iter != sites.end(); iter++) {
delete (*iter);
}
}
void UserReport::process_line(const log_line *line) {
if (srg.debug)
fprintf(stderr, "In UserReport::process_line for user '%s'\n",
mName);
/* Work out what site this line is for */
char *site = strdup(line->request->site);
SiteReport *theSite = (SiteReport *)findSite(site);
if (theSite==NULL) {
if (srg.debug)
fprintf(stderr, "Site not found in %s - creating %s\n",
mName, site);
/* The site does not exist. Create it. */
theSite = new SiteReport(site);
sites.push_back(theSite);
}
/* Process the line */
theSite->process_line(line);
free(site);
}
void UserReport::updateStats() {
/* Otherwise continue on and update our statistics */
zeroStats();
list<SiteReport*>::const_iterator iter;
for (iter=sites.begin(); iter != sites.end(); iter++) {
summary_info sitestats = (*iter)->getStats();
if (srg.minimumConnects>0 && sitestats.connects<srg.minimumConnects)
continue;
stats.connects += sitestats.connects;
stats.bytesTransferred += sitestats.bytesTransferred;
stats.timeSpent += sitestats.timeSpent;
stats.hits += sitestats.hits;
stats.bytesHit += sitestats.bytesHit;
stats.misses += sitestats.misses;
stats.bytesMissed += sitestats.bytesMissed;
stats.deniedHits += sitestats.deniedHits;
}
}
void UserReport::generate_report(const char *basename) {
char *t = NULL;
char *basepath=NULL;
char *filename=NULL;
char *rootpath=NULL;
FILE *outfile=NULL;
float percentin = 0;
float percentout = 0;
/* Create the output filename */
if (!singleUserMode) {
t = md5this(mName);
asprintf(&basepath, "%s/%s", basename, t);
free(t);
asprintf(&t, "%s/%s", srg.outputDir, basepath);
if (mkdir(t, 0755) == -1 && errno != EEXIST) {
fprintf(stderr, "Error (%s) creating directory in "
"user report for %s: %s\n", strerror(errno),
mName, t);
exit(1);
}
free(t);
rootpath = strdup("../../");
} else {
basepath = strdup(basename);
rootpath = strdup("../");
}
if (srg.debug)
fprintf(stderr, "Creating user report in %s\n", t);
/* Create the index file */
asprintf(&filename, "%s/%s/%s", srg.outputDir, basepath,
srg.indexfname);
outfile = fopen(filename, "w");
if(outfile==NULL) {
fprintf(stderr,"SiteReport: Cannot open output file: %s\n",
filename);
free(rootpath);
exit(1);
}
free(filename);
/* Header & Title */
html_header(outfile, rootpath);
fprintf(outfile, "<!-- SRG %s (%s) Generated UserReport -->\n",
version, HOME_URL);
/* misc stats */
fprintf(outfile, "<center><table cellpadding=2 cellspacing=2>\n");
fprintf(outfile, "<tr><td class=\"bodyText\">Period:</td><td class="
"\"bodyText\"> %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</td></tr>\n",
localtime(&srg.endTime)->tm_mday,
month_names[localtime(&srg.endTime)->tm_mon],
localtime(&srg.endTime)->tm_year+1900);
if (srg.groupBy > 0) {
fprintf(outfile, "<tr><td class=\"bodyText\">Group:</td><td "
"class=\"bodyText\"> %s</td></tr>", mName);
}
fprintf(outfile, "</table></center>");
/* Notices Row */
fprintf(outfile, "<div align=\"center\" id=\"srg-message\">"
" </div>\n");
/* main table for location list */
fprintf(outfile, "<center><table cellpadding=4 cellspacing=0 "
"id=\"srgtable\">"
"<thead><tr><th>LOCATION</th><th>REQUESTS</th><th>BYTES</th>"
"<th>BYTES%%</th><th>HIT</th><th>MISS</th>");
if (srg.showtimes) {
fprintf(outfile, "<th>TIME%%</th><th>TIME(ms)</th>");
}
if (srg.showrates) {
fprintf(outfile, "<th>RATE (kB/s)</th>");
}
fprintf(outfile, "</tr></thead>\n");
/* Totals Row */
summary_info thissitestats = getStats();
fprintf(outfile, "<tfoot><tr><th class=\"cellText\">Totals:</th>");
t = FormatOutput(thissitestats.connects);
fprintf(outfile, "<th class=\"cellNum\">%s</th>", t);
free(t);
t = FormatOutput(thissitestats.bytesTransferred);
fprintf(outfile, "<th class=\"cellNum\">%s</th>", t);
free(t);
fprintf(outfile, "<th class=\"cellNum\">100%%</th>");
if (thissitestats.bytesTransferred > 0) {
percentin = ((float)thissitestats.bytesHit/
(float)thissitestats.bytesTransferred)*100.0;
percentout = ((float)thissitestats.bytesMissed/
(float)thissitestats.bytesTransferred)*100.0;
fprintf(outfile, "<th align=\"center\" class=\"cellNum\">"
"%2.2f%%</th><th align=\"center\" class=\""
"cellNum\">%2.2f%%</th>", percentin,
percentout);
} else {
fprintf(outfile, "<th align=\"center\" class=\"cellNum\">"
"-</th><th align=\"center\" class=\""
"cellNum\">-</th>");
}
if (srg.showtimes) {
t = FormatOutput(thissitestats.timeSpent);
fprintf(outfile, "<th class=\"cellNum\">100%%</th><th class=\""
"cellNum\">%s</th>", t);
free(t);
}
if (srg.showrates) {
if (thissitestats.timeSpent == 0) {
fprintf(outfile, "<th class=\"cellNum\">-</th>");
} else {
fprintf(outfile, "<th class=\"cellNum\">%2.0f</th>",
(float)thissitestats.bytesTransferred/
(float)thissitestats.timeSpent);
}
}
fprintf(outfile, "</tr></tfoot>\n");
summary_info sitestats;
float timespent = 0;
float bytespercent = 0;
int rows = 0;
/* Initialise the Summary Stats Structure */
sitestats.connects = 0;
sitestats.bytesTransferred = 0;
sitestats.hits = 0;
sitestats.misses = 0;
sitestats.bytesHit = 0;
sitestats.bytesMissed = 0;
sitestats.timeSpent = 0;
sitestats.deniedHits = 0;
sites.sort(LessByBytesTransferred<SiteReport>());
list<SiteReport*>::const_iterator iter;
for (iter=sites.begin(); iter != sites.end(); iter++) {
sitestats = (*iter)->getStats();
if (srg.minimumConnects>0 && sitestats.connects<srg.minimumConnects)
continue;
if (srg.siteStats)
(*iter)->generate_report(basepath, mName);
if (sitestats.bytesTransferred>0 && sitestats.connects>0) {
percentin = ((float)sitestats.bytesHit/
(float)sitestats.bytesTransferred)*100.0;
percentout = ((float)sitestats.bytesMissed/
(float)sitestats.bytesTransferred)*100.0;
} else {
percentin = 0;
percentout = 0;
}
if (thissitestats.timeSpent == 0) {
timespent = 100;
} else {
timespent = (float)(100*sitestats.timeSpent) /
thissitestats.timeSpent;
}
if (thissitestats.bytesTransferred == 0) {
bytespercent = 100;
} else {
bytespercent = (float)(100*sitestats.bytesTransferred)
/ thissitestats.bytesTransferred;
}
t = md5this((*iter)->getName());
fprintf(outfile, "<tr%s><td class=\"bodyText\">",
rows%2==0 ? " class=\"highlightRow\"" : "");
if (srg.siteStats) {
fprintf(outfile, "<a href=\"%s/%s\">", t, srg.indexfname);
}
fprintf(outfile, "%s", (*iter)->getName());
if (srg.siteStats) {
fprintf(outfile, "</a>");
}
fprintf(outfile, "</td>");
free(t);
t = FormatOutput(sitestats.connects);
fprintf(outfile, "<td class=\"cellNum\">%s</td>", t);
free(t);
t = FormatOutput(sitestats.bytesTransferred);
fprintf(outfile, "<td class=\"cellNum\">%s</td>", t);
free(t);
fprintf(outfile, "<td class=\"cellNum\">%2.2f%%</td>",
bytespercent);
if (percentin != -1) {
fprintf(outfile, "<td class=\"cellNum\">%2.2f%%</td>"
"<td class=\"cellNum\">%2.2f%%</td>",
percentin, percentout);
} else {
fprintf(outfile, "<td class=\"cellNum\">-</td>"
"<td class=\"cellNum\">-</td>");
}
if (srg.showtimes) {
t = FormatOutput(sitestats.timeSpent);
fprintf(outfile, "<td class=\"cellNum\">%2.2f%%</td>"
"<td class=\"cellNum\">%s</td>",
timespent, t);
free(t);
}
if (srg.showrates) {
if (sitestats.timeSpent == 0) {
fprintf(outfile, "<td class=\"cellNum\">"
"-</td>");
} else {
fprintf(outfile, "<td class=\"cellNum\">"
"%.2f</td>",
(float)sitestats.bytesTransferred/
(float)sitestats.timeSpent);
}
}
fprintf(outfile, "</tr>\n");
rows++;
}
fprintf(outfile, "\t</table><br>");
if (srg.authenticate) {
fprintf(outfile, "\n\t<?php } else {\n report_error(\""
"Could not authenticate user\");\n}?>");
}
if (srg.groupBy > 0) {
fprintf(outfile, "\n\t<p><a href=\"../%s\">Back</a> to groups "
"page</p></center>\n", srg.indexfname);
} else {
fprintf(outfile, "\n\t<p><a href=\"../%s\">Back</a> to dates "
"page</p></center>\n", srg.indexfname);
}
/* Finish off the HTML */
html_footer(outfile, rootpath);
free(rootpath);
fclose(outfile);
/* If no rows were ouput, delete it all */
if (rows == 0) {
recurse_unlink(basepath);
}
free(basepath);
}
SiteReport *UserReport::findSite(char *site) {
list<SiteReport*>::const_iterator iter;
// Iterate through list and compare each element.
for (iter=sites.begin(); iter != sites.end(); iter++)
{
if(strcasecmp((*iter)->getName(), site)==0)
return (SiteReport *)(*iter);
}
return NULL;
}
// Get statistics about this report
summary_info UserReport::getStats() {
summary_info returnInfo;
updateStats();
returnInfo = stats;
return returnInfo;
}
|