webalizer.c

来自「统计日志的工具」· C语言 代码 · 共 1,572 行 · 第 1/5 页

C
1,572
字号
        case 'v':        case 'V': print_version();           break;  /* Version             */        case 'x': html_ext=optarg;           break;  /* HTML file extension */        case 'X': hide_sites=1;              break;  /* Hide ind. sites     */        case 'Y': ctry_graph=0;              break;  /* Supress ctry graph  */      }   }   if (argc - optind != 0) log_fname = argv[optind];   if ( log_fname && (log_fname[0]=='-')) log_fname=NULL; /* force STDIN?   */   /* check for gzipped file - .gz */   if (log_fname) if (!strcmp((log_fname+strlen(log_fname)-3),".gz")) gz_log=1;   /* setup our internal variables */   init_counters();                      /* initalize main counters         */   if (page_type==NULL)                  /* check if page types present     */   {      if ((log_type == LOG_CLF) || (log_type == LOG_SQUID))      {         add_nlist("htm*"  ,&page_type); /* if no page types specified, we  */         add_nlist("cgi"   ,&page_type); /* use the default ones here...    */         if (!isinlist(page_type,html_ext)) add_nlist(html_ext,&page_type);      }      else add_nlist("txt" ,&page_type); /* FTP logs default to .txt        */   }   for (max_ctry=0;ctry[max_ctry].desc;max_ctry++);   if (ntop_ctrys > max_ctry) ntop_ctrys = max_ctry;   /* force upper limit */   if (graph_lines> 20)       graph_lines= 20;         /* keep graphs sane! */   if (log_type == LOG_FTP)   {      /* disable stuff for ftp logs */      ntop_entry=ntop_exit=0;      ntop_search=0;   }   else   {      if (search_list==NULL)      {         /* If no search engines defined, define some :) */         add_glist("yahoo.com      p="      ,&search_list);         add_glist("altavista.com  q="      ,&search_list);         add_glist("google.com     q="      ,&search_list);         add_glist("eureka.com     q="      ,&search_list);         add_glist("lycos.com      query="  ,&search_list);         add_glist("hotbot.com     MT="     ,&search_list);         add_glist("msn.com        MT="     ,&search_list);         add_glist("infoseek.com   qt="     ,&search_list);         add_glist("webcrawler searchText=" ,&search_list);         add_glist("excite         search=" ,&search_list);         add_glist("netscape.com   search=" ,&search_list);         add_glist("mamma.com      query="  ,&search_list);         add_glist("alltheweb.com  query="  ,&search_list);         add_glist("northernlight.com qr="  ,&search_list);      }   }   /* ensure entry/exits don't exceed urls */   i=(ntop_urls>ntop_urlsK)?ntop_urls:ntop_urlsK;   if (ntop_entry>i) ntop_entry=i;   if (ntop_exit>i)  ntop_exit=i;   for (i=0;i<MAXHASH;i++)   {      sm_htab[i]=sd_htab[i]=NULL;        /* initalize hash tables           */      um_htab[i]=NULL;      rm_htab[i]=NULL;      am_htab[i]=NULL;      sr_htab[i]=NULL;   }   /* Be polite and announce yourself... */   if (verbose>1)   {      uname(&system_info);      printf("Webalizer V%s-%s (%s %s) %s\n",              version,editlvl,system_info.sysname,              system_info.release,language);   }#ifndef USE_DNS   if (strstr(argv[0],"webazolver")!=0)   {      printf("DNS support not present, aborting...\n");      exit(1);   }#endif  /* USE_DNS */   /* open log file */   if (gz_log)   {      gzlog_fp = gzopen(log_fname,"rb");      if (gzlog_fp==Z_NULL)      {         /* Error: Can't open log file ... */         fprintf(stderr, "%s %s\n",msg_log_err,log_fname);         exit(1);      }   }   else   {      if (log_fname)      {         log_fp = fopen(log_fname,"r");         if (log_fp==NULL)         {            /* Error: Can't open log file ... */            fprintf(stderr, "%s %s\n",msg_log_err,log_fname);            exit(1);         }      }   }   /* Using logfile ... */   if (verbose>1)   {      printf("%s %s (",msg_log_use,log_fname?log_fname:"STDIN");      if (gz_log) printf("gzip-");      switch (log_type)      {         /* display log file type hint */         case LOG_CLF:   printf("clf)\n");   break;         case LOG_FTP:   printf("ftp)\n");   break;         case LOG_SQUID: printf("squid)\n"); break;      }   }   /* switch directories if needed */   if (out_dir)   {      if (chdir(out_dir) != 0)      {         /* Error: Can't change directory to ... */         fprintf(stderr, "%s %s\n",msg_dir_err,out_dir);         exit(1);      }   }#ifdef USE_DNS   if (strstr(argv[0],"webazolver")!=0)   {      if (!dns_children) dns_children=5;  /* default dns children if needed */      if (!dns_cache)      {         /* No cache file specified, aborting... */         fprintf(stderr,"%s\n",msg_dns_nocf);     /* Must have a cache file */         exit(1);      }   }   if (dns_cache && dns_children)    /* run-time resolution */   {      if (dns_children > MAXCHILD) dns_children=MAXCHILD;      /* DNS Lookup (#children): */      if (verbose>1) printf("%s (%d): ",msg_dns_rslv,dns_children);      fflush(stdout);      (gz_log)?dns_resolver(gzlog_fp):dns_resolver(log_fp);      (gz_log)?gzrewind(gzlog_fp):(log_fname)?rewind(log_fp):exit(0);   }   if (strstr(argv[0],"webazolver")!=0) exit(0);   /* webazolver exits here */   if (dns_cache)   {      if (!open_cache()) { dns_cache=NULL; dns_db=NULL; }      else      {         /* Using DNS cache file <filaneme> */         if (verbose>1) printf("%s %s\n",msg_dns_usec,dns_cache);      }   }#endif  /* USE_DNS */   /* Creating output in ... */   if (verbose>1)      printf("%s %s\n",msg_dir_use,out_dir?out_dir:msg_cur_dir);   /* prep hostname */   if (!hname)   {      if (uname(&system_info)) hname="localhost";      else hname=system_info.nodename;   }   /* Hostname for reports is ... */   if (verbose>1) printf("%s '%s'\n",msg_hostname,hname);   /* get past history */   if (ignore_hist) {if (verbose>1) printf("%s\n",msg_ign_hist); }   else get_history();   if (incremental)                      /* incremental processing?         */   {      if ((i=restore_state()))           /* restore internal data structs   */      {         /* Error: Unable to restore run data (error num) */         /* if (verbose) fprintf(stderr,"%s (%d)\n",msg_bad_data,i); */         fprintf(stderr,"%s (%d)\n",msg_bad_data,i);         exit(1);      }   }   /* Allocate memory for our TOP countries array */   if (ntop_ctrys  != 0)   { if ( (top_ctrys=calloc(ntop_ctrys,sizeof(CLISTPTR))) == NULL)    /* Can't get memory, Top Countries disabled! */    {if (verbose) fprintf(stderr,"%s\n",msg_nomem_tc); ntop_ctrys=0;}}   start_time = times(&mytms);   /*********************************************/   /* MAIN PROCESS LOOP - read through log file */   /*********************************************/   while ( (gz_log)?(our_gzgets(gzlog_fp,buffer,BUFSIZE) != Z_NULL):           (fgets(buffer,BUFSIZE,log_fname?log_fp:stdin) != NULL))   {      total_rec++;      if (strlen(buffer) == (BUFSIZE-1))      {         if (verbose)         {            fprintf(stderr,"%s",msg_big_rec);            if (debug_mode) fprintf(stderr,":\n%s",buffer);            else fprintf(stderr,"\n");         }         total_bad++;                     /* bump bad record counter      */         /* get the rest of the record */         while ( (gz_log)?(our_gzgets(gzlog_fp,buffer,BUFSIZE)!=Z_NULL):                 (fgets(buffer,BUFSIZE,log_fname?log_fp:stdin)!=NULL))         {            if (strlen(buffer) < BUFSIZE-1)            {               if (debug_mode && verbose) fprintf(stderr,"%s\n",buffer);               break;            }            if (debug_mode && verbose) fprintf(stderr,"%s",buffer);         }         continue;                        /* go get next record if any    */      }      /* got a record... */      strcpy(tmp_buf, buffer);            /* save buffer in case of error */      if (parse_record(buffer))           /* parse the record             */      {         /*********************************************/         /* PASSED MINIMAL CHECKS, DO A LITTLE MORE   */         /*********************************************/         /* convert month name to lowercase */         for (i=4;i<7;i++)            log_rec.datetime[i]=tolower(log_rec.datetime[i]);         /* get year/month/day/hour/min/sec values    */         for (i=0;i<12;i++)         {            if (strncmp(log_month[i],&log_rec.datetime[4],3)==0)               { rec_month = i+1; break; }         }         rec_year=atoi(&log_rec.datetime[8]);    /* get year number (int)   */         rec_day =atoi(&log_rec.datetime[1]);    /* get day number          */         rec_hour=atoi(&log_rec.datetime[13]);   /* get hour number         */         rec_min =atoi(&log_rec.datetime[16]);   /* get minute number       */         rec_sec =atoi(&log_rec.datetime[19]);   /* get second number       */         /* Kludge for Netscape server time (0-24?) error                   */         if (rec_hour>23) rec_hour=0;         /* minimal sanity check on date */         if ((i>=12)||(rec_min>59)||(rec_sec>59)||(rec_year<1990))         {            total_bad++;                /* if a bad date, bump counter      */            if (verbose)            {               fprintf(stderr,"%s: %s [%lu]",                 msg_bad_date,log_rec.datetime,total_rec);               if (debug_mode) fprintf(stderr,":\n%s\n",tmp_buf);               else fprintf(stderr,"\n");            }            continue;                   /* and ignore this record           */         }         /*********************************************/         /* GOOD RECORD, CHECK INCREMENTAL/TIMESTAMPS */         /*********************************************/         /* Flag as a good one */         good_rec = 1;         /* get current records timestamp (seconds since epoch) */         req_tstamp=cur_tstamp;         rec_tstamp=((jdate(rec_day,rec_month,rec_year)-epoch)*86400)+                     (rec_hour*3600)+(rec_min*60)+rec_sec;         /* Do we need to check for duplicate records? (incremental mode)   */         if (check_dup)         {            /* check if less than/equal to last record processed            */            if ( rec_tstamp <= cur_tstamp )            {               /* if it is, assume we have already processed and ignore it  */               total_ignore++;               continue;            }            else            {               /* if it isn't.. disable any more checks this run            */               check_dup=0;               /* now check if it's a new month                             */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?