📄 generate.c
字号:
/*** Modular Logfile Analyzer** Copyright 2000 Jan Kneschke <jan@kneschke.de>**** Homepage: http://www.kneschke.de/projekte/modlogan** 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, and provided that the above copyright and permission notice is included with all distributed copies of this or derived software. 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-1307, USA**** $Id: generate.c,v 1.22 2000/10/15 19:20:20 jk Exp $*/#include <libintl.h>#include <locale.h>#include <stdlib.h>#include <stdio.h>#include <time.h>#include <math.h>#include <sys/stat.h>#include <sys/types.h>#include <fcntl.h>#include <unistd.h>#include "mconfig.h"#include "mstate.h"#include "mlocale.h"#include "mhash.h"#include "mlist.h"#include "mdatatypes.h"#include "mplugins.h"#include "pictures.h"#include "plugin_config.h"#define HIGHLIGHT 1#define GROUPING 2#define VISITS 4#define INDEX 8/* tla -> Three Letter Abbreviation */char *get_month_string(int m, int tla) { static char monthname[255]; struct tm *tm; time_t t = time(NULL); tm = localtime(&t); tm->tm_mon = m > 0 ? m - 1 : 11; strftime(monthname, sizeof(monthname)-1, tla ? "%b" : "%B", tm); return monthname;}char *htmlencode(const char *s) { char *p; char *q = NULL; int q_len = 0; if (!s) return NULL; q_len = strlen(s) * 2 + 1; q = malloc(q_len); p = q; *p = '\0'; while (*s) { switch(*s) { case '<': *p = '&'; *(++p) = 'l'; *(++p) = 't'; *(++p) = ';'; break; case '>': *p = '&'; *(++p) = 'g'; *(++p) = 't'; *(++p) = ';'; break; default: *p = *s; break; } *(++p) = '\0'; s++; if (strlen(q) > (q_len - 4)) { q_len += 128; q = realloc(q, q_len); p = q + strlen(q); } } return q;}int mhash_get_value(mhash *h[], const char *key) { int c = 0, i; if (!h) return 0; for ( i = 0; i < HASH_SIZE; i++) { mlist *l = h[i]->list; while (l) { if (l->data) { data_StrInt *data = (data_StrInt *)l->data; if (!strcmp(key, data->string)) { c = data->count; break; } } l = l->next; } if (c != 0) break; } return c;}int show_mhash (mconfig *ext_conf, FILE *f, mhash *h[], int count, int opt) { mlist *l, *first; int i = 0; config_output *conf = ext_conf->output; if (!h) return 0; first = l = mlist_init(); mhash_unfold_sorted_limited(h, l, count); while (l && (i++ < count)) { data_StrInt *data = (data_StrInt *)l->data; if (data) { char *enc_url = htmlencode(data->string); int cut_url = strlen(enc_url) > 40; if (cut_url) { enc_url[40] = '\0'; } fprintf(f,"<TR>"); if (opt & INDEX) { fprintf(f,"<TD width=\"5%%\" align=right>%i</TD>", i); } fprintf(f,"<TD width=\"15%%\" align=right>%i</TD>", data->count); if (opt & VISITS) { data_Str3Int *_data = (data_Str3Int *)l->data; fprintf(f,"<TD width=\"15%%\" align=right>%i</TD>", _data->vcount); } if ((opt & GROUPING) && data->type == M_GROUP) { fprintf(f,"<TD bgcolor=\"%s\">%s%s</TD>", conf->col_grouping, enc_url, cut_url ? "..." : ""); } else { if (opt & HIGHLIGHT) { fprintf(f,"<TD><a href=\"%s\">%s</a>%s</TD>", data->string, enc_url, cut_url ? "..." : ""); } else { fprintf(f,"<TD>%s%s</TD>", enc_url, cut_url ? "..." : ""); } } fprintf(f,"</TR>\n"); free(enc_url); } l = l->next; } mlist_free(first); return 0;}int mhash_status_unfold_sorted_limited(mhash *h[], mlist *l, int count ) { int i, j; data_StrInt *data, *ins_data; char __dummy__[] = "999"; char __dummy_2_[] = ""; char *max, *last = __dummy_2_; for ( j = 0; j < count; j++) { data = NULL; max = __dummy__; for ( i = 0; i < HASH_SIZE; i++) { if (h[i]->list) { mlist *hl; hl = h[i]->list; while (hl) { if (hl->data) { if ( strcmp(((data_StrInt *)hl->data)->string, max) < 0 && strcmp(((data_StrInt *)hl->data)->string, last) > 0) { max = ((data_StrInt *)hl->data)->string; data = (data_StrInt *)hl->data; } } hl = hl->next; } } } if (data) { ins_data = createStrInt(data->string, data->count); mlist_insert(l, ins_data); last = data->string; } } return 0;}int show_status_mhash (FILE *f, mhash *h[], int count) { mlist *l, *first; int i = 0; if (!h) return 0; first = l = mlist_init(); mhash_status_unfold_sorted_limited(h, l, count); while (l && (i++ < count)) { data_StrInt *data = (data_StrInt *)l->data; if (data) fprintf(f,"<TR><TD width=\"15%%\" align=right>%i</TD><TD>%s - %s</TD></TR>\n", data->count, data->string, mhttpcodes(strtol(data->string, NULL, 10))); l = l->next; } mlist_free(first); return 0;}void table_start(FILE *f, char *str, int colspan) { fprintf(f,"<P><CENTER><TABLE BORDER=1 WIDTH=400 BGCOLOR=\"#eeeeee\"><TR><TH colspan=%i>%s</TH></TR>\n", colspan, str);}void table_end(FILE *f) { fprintf(f,"</TABLE></CENTER><P>");}void file_start(FILE *f, mconfig *ext_conf) { config_output *conf = ext_conf->output; fprintf(f, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n\n"); fprintf(f, "<HTML><HEAD><TITLE>Statistics</TITLE></HEAD><BODY bgcolor=\"%s\">\n", conf->col_body); fprintf(f, "%s %s<HR><br>\n", _("Statistics for"), conf->hostname);}void file_end(FILE *f) {/* Tell the users that this is valid HTML 4.0 :) */ fprintf(f, "<HR><a href=\"http://validator.w3.org/check/referer\"><img border=0 "\ "src=\"http://validator.w3.org/images/vh40\" "\ "alt=\"Valid HTML 4.0!\" height=31 width=88 align=\"right\"></a>"); fprintf(f, "Output generated by <a href=\"%s\">%s %s</a>\n", "http://www.kneschke.de/projekte/modlogan/", PACKAGE, VERSION); fprintf(f, "</BODY></HTML>\n");}char *table_header(int maxcount, int count, char *str) { static char trans_buf[255]; sprintf(trans_buf, "%i %s %i %s", (maxcount > count) || (maxcount < 0) ? count : maxcount, _("of"), count, str); return trans_buf; }int mplugins_output_generate_monthly_output(mconfig *ext_conf, mstate *state, char *subpath) { unsigned int i, s_200, s_304; unsigned int min, sec; double d = 0; FILE *f; char filename[255]; char *ref; data_History sumdat, maxdat; int last_day = 1; config_output *conf = ext_conf->output; mstate_web *staweb = NULL; if (!state->ext) return -1; if (state->ext_type != M_STATE_TYPE_WEB) return -1; staweb = state->ext; if (subpath) { sprintf(filename, "%s/%s/", ext_conf->outputdir ? ext_conf->outputdir : ".", subpath); mkdir(filename, 0755); sprintf(filename, "%s/%s/m_usage_%04i%02i.html", ext_conf->outputdir ? ext_conf->outputdir : ".", subpath, state->year, state->month); } else { sprintf(filename, "%s/m_usage_%04i%02i.html", ext_conf->outputdir ? ext_conf->outputdir : ".", state->year, state->month); } if (!(f = fopen(filename, "w"))) { return -1; } file_start(f,ext_conf); sumdat.files = maxdat.files = 0; sumdat.xfersize = maxdat.xfersize = 0; sumdat.hits = maxdat.hits = 0; sumdat.hosts = maxdat.hosts = 0; sumdat.pages = maxdat.pages = 0; sumdat.visits = maxdat.visits = 0; /* count the values */ for ( i = 0; i < 31; i++) { if (staweb->days[i].hits) last_day = i+1; sumdat.files += staweb->days[i].files; sumdat.xfersize += staweb->days[i].xfersize; sumdat.hits += staweb->days[i].hits; sumdat.hosts += staweb->days[i].hosts; sumdat.pages += staweb->days[i].pages; sumdat.visits += staweb->days[i].visits; if (maxdat.files < staweb->days[i].files) maxdat.files = staweb->days[i].files; if (maxdat.hits < staweb->days[i].hits) maxdat.hits = staweb->days[i].hits; if (maxdat.hosts < staweb->days[i].hosts) maxdat.hosts = staweb->days[i].hosts; if (maxdat.pages < staweb->days[i].pages) maxdat.pages = staweb->days[i].pages; if (maxdat.visits < staweb->days[i].visits) maxdat.visits = staweb->days[i].visits; if (maxdat.xfersize < staweb->days[i].xfersize) maxdat.xfersize = staweb->days[i].xfersize; } table_start(f, _("Summary"), 3); /* Totals */ fprintf(f,"<TR><TD>%s</TD><TD colspan=2 align=right>%li</TD></TR>\n", _("Total Hits"), sumdat.hits); fprintf(f,"<TR><TD>%s</TD><TD colspan=2 align=right>%li</TD></TR>\n", _("Total Files"), sumdat.files); fprintf(f,"<TR><TD>%s</TD><TD colspan=2 align=right>%li</TD></TR>\n", _("Total Pages"), sumdat.pages); fprintf(f,"<TR><TD>%s</TD><TD colspan=2 align=right>%li</TD></TR>\n", _("Total Hosts"), sumdat.hosts); fprintf(f,"<TR><TD>%s</TD><TD colspan=2 align=right>%li</TD></TR>\n", _("Total Visits"), sumdat.visits); fprintf(f,"<TR><TD>%s</TD><TD colspan=2 align=right>%.0f</TD></TR>\n", _("Transfered Bytes"), sumdat.xfersize / 1024); fprintf(f,"<TR><TH> </TH><TH>%s</TH><TH>%s</TH></TR>\n", _("avg"), _("max")); fprintf(f,"<TR><TD>%s</TD><TD align=right>%li</TD><TD align=right>%li</TD></TR>\n", _("Hits per Day"), sumdat.hits / last_day, maxdat.hits); fprintf(f,"<TR><TD>%s</TD><TD align=right>%li</TD><TD align=right>%li</TD></TR>\n", _("Files per Day"), sumdat.files / last_day, maxdat.files); fprintf(f,"<TR><TD>%s</TD><TD align=right>%li</TD><TD align=right>%li</TD></TR>\n", _("Pages per Day"), sumdat.pages / last_day, maxdat.pages); fprintf(f,"<TR><TD>%s</TD><TD align=right>%li</TD><TD align=right>%li</TD></TR>\n", _("Hosts per Day"), sumdat.hosts / last_day, maxdat.hosts); fprintf(f,"<TR><TD>%s</TD><TD align=right>%li</TD><TD align=right>%li</TD></TR>\n", _("Visits per Day"), sumdat.visits / last_day, maxdat.visits); fprintf(f,"<TR><TD>%s</TD><TD align=right>%.0f</TD><TD align=right>%.0f</TD></TR>\n", _("Tranfered Kbytes per Day"), (sumdat.xfersize / 1024) / last_day, maxdat.xfersize / 1024); if (sumdat.visits) { d = (staweb->allvisittime / sumdat.visits); min = d / 60; sec = (int)floor(d) % 60; } else { min = 0; sec = 0; } fprintf(f,"<TR><TD>%s</TD><TD align=right>%i:%02i %s</TD><TD align=right>%s</TD></TR>\n", _("Time per visit"), min, sec, _("min"), "---"); if (sumdat.visits) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -