"Fossies" - the Fresh Open Source Software Archive

Member "srg-1.3.6/src/LocationReport.cc" (5 Aug 2009, 6959 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 Location
    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 "LocationReport.h"
   26 #include "srg.h"
   27 #include "prototypes.h"
   28 
   29 extern int h_errno;
   30 
   31 void LocationReport::process_line(const log_line *line) {
   32 
   33     if (srg.debug)
   34         fprintf(stderr, "In LocationReport::process_line for "
   35                 "location '%s'\n", mName);
   36 
   37     stats.connects++;
   38     stats.bytesTransferred += line->size;
   39     stats.timeSpent += line->elapsedTime;
   40     if (is_cache_hit(line->resultCode)) {
   41         stats.hits++;
   42         stats.bytesHit += line->size;
   43     } else {
   44         stats.misses++;
   45         stats.bytesMissed += line->size;
   46     }
   47    
   48     time_t tLine = line->timestamp;
   49     times.push_back(tLine);
   50 
   51     if (is_cache_denied(line->resultCode)) {
   52         stats.deniedHits++;
   53         denied.push_back(true);
   54     } else {
   55         denied.push_back(false);
   56     }
   57 
   58     if (srg.lookupHosts) {
   59         char *tclient=dnscache->get_name(line->clientAddress);
   60         if (tclient) {
   61             host.push_back(tclient);
   62         } else {
   63             host.push_back(strdup(inet_ntoa(
   64                             line->clientAddress)));
   65         }       
   66     } else {
   67         host.push_back(strdup(inet_ntoa(line->clientAddress)));
   68     }
   69 
   70 }
   71 
   72 void LocationReport::generate_report(const char * basename, const char *user,
   73                     const char *site) {
   74     
   75     char *t = NULL;
   76 
   77     /* Generate the HTML file */
   78     char *defaultname = "^index^";
   79     char *filename;
   80     FILE *outfile = NULL;
   81 
   82     if (strcmp(mName, "") == 0) 
   83         mName = strdup(defaultname);
   84     t = md5this(mName);
   85     
   86     if (srg.outputMode == OUTPUT_PHP) {
   87         asprintf(&filename, "%s/%s/%s.php", srg.outputDir, basename, t);
   88     } else {
   89         asprintf(&filename, "%s/%s/%s.html", srg.outputDir, 
   90                 basename, t);
   91     }
   92     free(t);
   93     
   94     outfile = fopen(filename, "w");
   95     if(outfile==NULL) {
   96         fprintf(stderr,"LocationReport: Cannot open output file: %s\n",
   97                 filename);
   98         exit(1);
   99     }
  100     free(filename);
  101     
  102     /* Header & Title */
  103     if (srg.groupBy > 0) {
  104         html_header(outfile, "../../../");
  105     } else {
  106         html_header(outfile, "../../");
  107     }
  108     
  109     fprintf(outfile, "<!-- SRG %s (%s) Generated LocationReport -->\n", 
  110             version, HOME_URL);
  111     
  112     /* Misc Stats */
  113     fprintf(outfile, "<center><table cellpadding=2 cellspacing=2>");
  114     fprintf(outfile, "<tr><td class=\"bodyText\">Period:</td><td class=\""
  115             "bodyText\">%d %s %d",
  116             localtime(&srg.startTime)->tm_mday, 
  117             month_names[localtime(&srg.startTime)->tm_mon], 
  118             localtime(&srg.startTime)->tm_year+1900);
  119     fprintf(outfile, " - %d %s %d</td></tr>", 
  120             localtime(&srg.endTime)->tm_mday, 
  121             month_names[localtime(&srg.endTime)->tm_mon], 
  122             localtime(&srg.endTime)->tm_year+1900);
  123     if (srg.groupBy > 0) {
  124         fprintf(outfile, "<tr><td class=\"bodyText\">Group:</td><td "
  125                 "class=\"bodyText\">%s</td></tr>", user);
  126     }
  127     fprintf(outfile, "<tr><td align=\"right\" class=\"bodyText\">"
  128             "Site:\n</td><td class=\"bodyText\"><a href=\"http://%s/\">%s"
  129             "</a></td></tr>", site, site);
  130     fprintf(outfile, "</table></center>");
  131     
  132     /* Notices Row */
  133     fprintf(outfile, "<div align=\"center\" id=\"srg-message\">"
  134             "&nbsp;</div>\n");
  135     
  136     fprintf(outfile, "<center><table cellpadding=4 cellspacing=0><tr>"
  137             "<th>LOCATION</th><th>REQUESTS</th><th>BYTES</th>"
  138             "<th>HIT</th><th>MISS</th>");
  139     if (srg.showtimes)
  140         fprintf(outfile, "<th>TIME(ms)</th>");  
  141     if (srg.showrates)
  142         fprintf(outfile, "<th>RATE (kb/s)</th>");
  143     fprintf(outfile, "</tr><tr><td class=\"cellText\"><a href=\""
  144             "http://%s%s\">", site, 
  145             strcasecmp(mName, defaultname)==0 ? "/" : mName);
  146     if (strcasecmp(mName, defaultname)==0) {
  147         fprintf(outfile, "/");
  148     } else {
  149         t = mName;
  150         unsigned int tlen = strlen(t);
  151         for (unsigned int i=0;i<tlen;i+=30) {
  152             if (i>0)
  153                 fprintf(outfile, "<br>");
  154             char tmp[31];
  155             strncpy(tmp, t, 30);
  156             tmp[30] = '\0';
  157             fprintf(outfile, "%s", tmp);
  158             t+=30;
  159         }
  160     }
  161     fprintf(outfile, "</a></td>");
  162     t = FormatOutput(stats.connects);
  163     fprintf(outfile, "<td class=\"cellNum\">%s</td>", t);
  164     free(t);
  165     t = FormatOutput(stats.bytesTransferred);
  166     fprintf(outfile, "<td class=\"cellNum\">%s</td>", t);
  167     free(t);
  168     float percentin=0, percentout=0;
  169     if (stats.bytesTransferred>0 && stats.connects>0) {
  170         percentin = ((float)stats.bytesHit/
  171                 (float)stats.bytesTransferred)*100.0;
  172         percentout = ((float)stats.bytesMissed/
  173                 (float)stats.bytesTransferred)*100.0;
  174     }
  175     fprintf(outfile, "<td class=\"cellNum\">%2.2f%%</td>", percentin);
  176     fprintf(outfile, "<td class=\"cellNum\">%2.2f%%</td>", percentout);
  177     if (srg.showtimes) {
  178         t = FormatOutput(stats.timeSpent);
  179         fprintf(outfile, "<td class=\"cellNum\">%s</td>", t);
  180         free(t);
  181     }
  182     if (srg.showrates) {
  183         fprintf(outfile, "<td class=\"cellNum\">%.2f</td>", 
  184             (float) stats.bytesTransferred/stats.timeSpent);
  185     }
  186     fprintf(outfile, "</tr></table></center><br>");
  187 
  188     /* Times Accessed Table */
  189     fprintf(outfile, "\t<center><table border=0 cellspacing=2 "
  190             "cellpadding=2 id=\"srgtable\">\n");
  191     fprintf(outfile, "\t\t<thead><tr>\n");
  192     fprintf(outfile, "\t\t<th>Time(s) Accessed</th>\n");
  193     fprintf(outfile, "\n\t\t<th>Requested by</th>\n");
  194     fprintf(outfile, "\t\t<th>Error</th>\n");
  195     fprintf(outfile, "\t\t</tr></thead>\n");
  196 
  197     list<time_t>::const_iterator iter;
  198     list<bool>::const_iterator deniediter;
  199     list<char*>::const_iterator hostiter;
  200     hostiter=host.begin();
  201     
  202     deniediter=denied.begin();
  203     int rows = 0;
  204 
  205     times.sort();
  206     for (iter=times.begin(); iter != times.end(); iter++) {
  207         time_t thistime = (*iter);
  208         fprintf(outfile, "\t<tr%s><td class=\"bodyText\">%s</td>",
  209                 ((rows%2)==0) ? " class=\"highlightRow\"" : "",
  210                 ctime(&thistime));
  211         
  212         fprintf(outfile, "<td class=\"bodyText\">%s</td>", 
  213                 (*hostiter));
  214         
  215         fprintf(outfile, "<td class=\"bodyText\">%s</td></tr>\n", 
  216                 (*deniediter) ? "DENIED" : "");
  217         deniediter++;
  218     
  219         hostiter++;
  220         rows++;
  221     }
  222     fprintf(outfile, "\n\t</table><br>");
  223 
  224     if (srg.authenticate) {
  225         fprintf(outfile, "\n\t<?php } else {\n report_error(\""
  226                 "Could not authenticate user\");\n}?>");
  227     }
  228 
  229     fprintf(outfile, "\n\t<br><a href=\"%s\">Back</a> to locations "
  230             "page</center>", srg.indexfname);
  231 
  232     /* Finish off the HTML */
  233     if (srg.groupBy > 0) {
  234         html_footer(outfile, "../../../");
  235     } else {
  236         html_footer(outfile, "../../");
  237     }
  238     fclose(outfile);
  239 
  240 }
  241 
  242 // Get statistics about this report
  243 summary_info LocationReport::getStats() {
  244 
  245     summary_info returnInfo;
  246 
  247     returnInfo = stats;
  248 
  249     return returnInfo;
  250 
  251 }