"Fossies" - the Fresh Open Source Software Archive 
Member "srg-1.3.6/src/SiteReport.cc" (5 Aug 2009, 10571 Bytes) of package /linux/privat/old/srg-1.3.6.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*
2 SRG - Squid Report Generator
3 Report by site
4 Copyright 2005 University of Waikato
5
6 This file is part of SRG.
7
8 SRG is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 SRG is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with SRG; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 */
23
24 #include "UserReport.h"
25 #include "SiteReport.h"
26 #include "LocationReport.h"
27 #include "srg.h"
28 #include "prototypes.h"
29
30 SiteReport::~SiteReport() {
31
32 list<LocationReport*>::const_iterator iter;
33 for (iter=locations.begin(); iter != locations.end(); iter++) {
34 delete (*iter);
35 }
36
37 }
38
39 void SiteReport::process_line(const log_line *line) {
40
41 if (srg.debug)
42 fprintf(stderr, "In SiteReport::process_line for site '%s'\n",
43 mName);
44
45 /* Work out the location of this line */
46 char *location = strdup(line->request->location);
47
48 LocationReport *theLocation = (LocationReport *)findLocation(location);
49 if (theLocation==NULL) {
50 if (srg.debug)
51 fprintf(stderr, "Site Location not found in %s - "
52 "creating %s\n", mName, location);
53 /* The location does not exist. Create it. */
54 theLocation = new LocationReport(location);
55 locations.push_back(theLocation);
56 }
57
58 /* Process the line */
59 theLocation->process_line(line);
60
61 free(location);
62
63 }
64
65 void SiteReport::generate_report(const char *basename, const char *user) {
66
67 char *t = NULL;
68 char *basepath = NULL;
69 char *filename = NULL;
70 FILE *outfile = NULL;
71 float percentin = 0;
72 float percentout = 0;
73
74 /* Create the output filename */
75 t = md5this(mName);
76 asprintf(&basepath, "%s/%s", basename, t);
77 free(t);
78 asprintf(&t, "%s/%s", srg.outputDir, basepath);
79 if (mkdir(t, 0755) == -1 && errno != EEXIST) {
80 fprintf(stderr, "Error (%s) creating directory in "
81 "site report for %s: %s\n", strerror(errno),
82 mName, t);
83 exit(1);
84 }
85 if (srg.debug)
86 fprintf(stderr, "Creating site report in %s\n", t);
87 free(t);
88
89 /* Create the index file */
90 asprintf(&filename, "%s/%s/%s", srg.outputDir, basepath,
91 srg.indexfname);
92 outfile = fopen(filename, "w");
93 if(outfile==NULL) {
94 fprintf(stderr,"SiteReport: Cannot open output file: %s\n",
95 filename);
96 exit(1);
97 }
98 free(filename);
99
100 /* Header & Title */
101 if (srg.groupBy > 0) {
102 html_header(outfile, "../../../");
103 } else {
104 html_header(outfile, "../../");
105 }
106
107 fprintf(outfile, "<!-- SRG %s (%s) Generated SiteReport -->\n",
108 version, HOME_URL);
109
110 /* Misc Stats */
111 fprintf(outfile, "<center><table cellpadding=2 cellspacing=2>");
112 fprintf(outfile, "<tr><td class=\"bodyText\">Period:</td><td class=\""
113 "bodyText\">%d %s %d",
114 localtime(&srg.startTime)->tm_mday,
115 month_names[localtime(&srg.startTime)->tm_mon],
116 localtime(&srg.startTime)->tm_year+1900);
117 fprintf(outfile, " - %d %s %d</td></tr>",
118 localtime(&srg.endTime)->tm_mday,
119 month_names[localtime(&srg.endTime)->tm_mon],
120 localtime(&srg.endTime)->tm_year+1900);
121 if (srg.groupBy > 0) {
122 fprintf(outfile, "<tr><td class=\"bodyText\">Group:</td><td "
123 "class=\"bodyText\">%s</td></tr>", user);
124 }
125 fprintf(outfile, "<tr><td align=right class=\"bodyText\">Site:</td><td"
126 " align=left class=\"bodyText\"> <a href=\""
127 "http://%s/\">%s</a></td></tr>", mName, mName);
128 fprintf(outfile, "</table></center>");
129
130 /* Notices Row */
131 fprintf(outfile, "<div align=\"center\" id=\"srg-message\">"
132 " </div>\n");
133
134 /* Main Table */
135 fprintf(outfile, "<center><table cellpadding=4 cellspacing=0 "
136 "id=\"srgtable\"><thead><tr>"
137 "<th>LOCATION</th><th>REQUESTS</th><th>BYTES</th><th>"
138 "BYTES %%</th><th>HIT</th><th>MISS</th>");
139 if (srg.showtimes)
140 fprintf(outfile, "<th>TIME%%</th><th>TIME(ms)</th>");
141 if (srg.showrates)
142 fprintf(outfile, "<th>RATE (kB/s)</th>");
143 fprintf(outfile, "</tr></thead>\n");
144
145 /* Totals Row */
146 summary_info thissitestats = getStats();
147 fprintf(outfile, "<tfoot><tr><th class=\"cellText\">Totals:</th>");
148 t = FormatOutput(thissitestats.connects);
149 fprintf(outfile, "<th class=\"cellNum\">%s</th>", t);
150 free(t);
151 t = FormatOutput(thissitestats.bytesTransferred);
152 fprintf(outfile, "<th class=\"cellNum\">%s</th>", t);
153 free(t);
154 fprintf(outfile, "<th class=\"cellNum\">100%%</th>");
155 if (thissitestats.bytesTransferred > 0) {
156 percentin = ((float)thissitestats.bytesHit /
157 (float)thissitestats.bytesTransferred)*100.0;
158 percentout = ((float)thissitestats.bytesMissed /
159 (float)thissitestats.bytesTransferred)*100.0;
160 fprintf(outfile, "<th align=\"center\" class=\"cellNum\">"
161 "%2.2f%%</th><th align=\"center\" class=\""
162 "cellNum\">%2.2f%%</th>", percentin,
163 percentout);
164 } else {
165 fprintf(outfile, "<th align=\"center\" class=\"cellNum\">"
166 "0.00%</th><th align=\"center\" class=\""
167 "cellNum\">0.00%</th>");
168 }
169 if (srg.showtimes) {
170 t = FormatOutput(thissitestats.timeSpent);
171 fprintf(outfile, "<th class=\"cellNum\">100%%</th><th class=\""
172 "cellNum\">%s</th>", t);
173 free(t);
174 }
175 if (srg.showrates) {
176 if (thissitestats.timeSpent == 0) {
177 fprintf(outfile, "<th class=\"cellNum\">-</th>");
178 } else {
179 fprintf(outfile, "<th class=\"cellNum\">%2.0f</th>",
180 (float)thissitestats.bytesTransferred/
181 (float)thissitestats.timeSpent);
182 }
183 }
184 fprintf(outfile, "</tr></tfoot>\n");
185
186 list<LocationReport*>::const_iterator iter;
187 summary_info locationstats;
188 float timespent = 0;
189 float bytespercent = 0;
190 int rows = 0;
191
192 /* Initialise the Summary Stats Structure */
193 locationstats.connects = 0;
194 locationstats.bytesTransferred = 0;
195 locationstats.hits = 0;
196 locationstats.misses = 0;
197 locationstats.bytesHit = 0;
198 locationstats.bytesMissed = 0;
199 locationstats.timeSpent = 0;
200 locationstats.deniedHits = 0;
201
202 locations.sort(LessByBytesTransferred<LocationReport>());
203
204 for (iter=locations.begin(); iter != locations.end(); iter++) {
205 locationstats = (*iter)->getStats();
206 if (srg.locationStats) {
207 (*iter)->generate_report(basepath, user, mName);
208 }
209 char *location = (*iter)->getName();
210
211 if (locationstats.bytesTransferred > 0 && locationstats.connects>0) {
212 percentin = ((float)locationstats.bytesHit /
213 (float)locationstats.bytesTransferred)*100.0;
214 percentout = ((float)locationstats.bytesMissed /
215 (float)locationstats.bytesTransferred)*100.0;
216 } else {
217 percentin = 0;
218 percentout = 0;
219 }
220 if (thissitestats.timeSpent == 0) {
221 timespent = 100;
222 } else {
223 timespent = (float)(100*locationstats.timeSpent) /
224 thissitestats.timeSpent;
225 }
226 if (thissitestats.bytesTransferred == 0) {
227 bytespercent = 100;
228 } else {
229 bytespercent = (float)
230 (100*locationstats.bytesTransferred) /
231 thissitestats.bytesTransferred;
232 }
233
234 fprintf(outfile, "<tr%s>",
235 ((rows%2)==0) ? " class=\"highlightRow\"" : "");
236 char *locationhref = NULL;
237 char *locationname = NULL;
238 if (srg.locationStats) {
239 /* Link to location report */
240 t = md5this(location);
241 asprintf(&locationhref, "%s.%s", t,
242 srg.outputMode == OUTPUT_PHP ? "php" : "html");
243 free(t);
244 char tmpname[41];
245 snprintf(tmpname, 40, "%s", location);
246 asprintf(&locationname, "%s%s", tmpname,
247 (strlen(location)>40) ? "…" : "");
248 } else {
249 asprintf(&locationhref, "http://%s%s", mName,
250 (strcasecmp(location, "^index^")==0) ?
251 "/" : location);
252 char tmpname[41];
253 snprintf(tmpname, 40, "%s", location);
254 asprintf(&locationname, "%s%s", tmpname,
255 (strlen(location)>40) ? "…" : "");
256 }
257 fprintf(outfile, "<td class=\"bodyText\"><a href=\"%s\">"
258 "%s</a></td>", locationhref, locationname);
259 free(locationhref);
260 free(locationname);
261 t = FormatOutput(locationstats.connects);
262 fprintf(outfile, "<td class=\"cellNum\">%s</td>", t);
263 free(t);
264 t = FormatOutput(locationstats.bytesTransferred);
265 fprintf(outfile, "<td class=\"cellNum\">%s</td>", t);
266 free(t);
267 fprintf(outfile, "<td class=\"cellNum\">%2.2f%%</td>",
268 bytespercent);
269 fprintf(outfile, "<td class=\"cellNum\">%2.2f%%</td>"
270 "<td class=\"cellNum\">%2.2f%%</td>", percentin, percentout);
271 if (srg.showtimes) {
272 t = FormatOutput(locationstats.timeSpent);
273 fprintf(outfile, "<td class=\"cellNum\">%2.2f%%</td>"
274 "<td class=\"cellNum\">%s</td>",
275 timespent, t);
276 free(t);
277 }
278 if (srg.showrates) {
279 if (locationstats.timeSpent == 0) {
280 fprintf(outfile, "<td class=\"cellNum\">"
281 "-</td>");
282 } else {
283 fprintf(outfile, "<td class=\"cellNum\">"
284 "%.2f</td>",
285 (float)locationstats.bytesTransferred/
286 (float)locationstats.timeSpent);
287 }
288 }
289 fprintf(outfile, "</tr>\n");
290 rows++;
291 }
292
293 fprintf(outfile, "\t</table><br>");
294
295 if (srg.authenticate) {
296 fprintf(outfile, "\n\t<?php } else {\n report_error(\""
297 "Could not authenticate user\");\n}?>");
298 }
299
300 fprintf(outfile, "\n\t<br><a href=\"../%s\">Back</a> to sites page"
301 "</center>", srg.indexfname);
302
303 /* Finish off the HTML */
304 if (srg.groupBy > 0) {
305 html_footer(outfile, "../../../");
306 } else {
307 html_footer(outfile, "../../");
308 }
309 fclose(outfile);
310
311 free(basepath);
312
313 }
314
315 void SiteReport::updateStats() {
316
317 zeroStats();
318
319 list<LocationReport*>::const_iterator iter;
320
321 for (iter=locations.begin(); iter != locations.end(); iter++) {
322 summary_info locationStats = (*iter)->getStats();
323 stats.connects += locationStats.connects;
324 stats.bytesTransferred += locationStats.bytesTransferred;
325 stats.timeSpent += locationStats.timeSpent;
326 stats.hits += locationStats.hits;
327 stats.bytesHit += locationStats.bytesHit;
328 stats.misses += locationStats.misses;
329 stats.bytesMissed += locationStats.bytesMissed;
330 stats.deniedHits += locationStats.deniedHits;
331 }
332
333
334 }
335
336 LocationReport *SiteReport::findLocation(char * location) {
337
338 list<LocationReport*>::const_iterator iter;
339
340 /* Iterate through list and compare each element. */
341 for (iter=locations.begin(); iter != locations.end(); iter++){
342 if(strcasecmp((*iter)->getName(),location) ==0)
343 return (LocationReport *)(*iter);
344 }
345
346 return NULL;
347 }
348
349 // Get statistics about this report
350 summary_info SiteReport::getStats() {
351
352 summary_info returnInfo;
353
354 updateStats();
355
356 returnInfo = stats;
357
358 return returnInfo;
359
360 }