logresolve.c revision 76afc31650e2621ed0882c45bbb900e550f9c96a
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder/* Licensed to the Apache Software Foundation (ASF) under one or more
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * contributor license agreements. See the NOTICE file distributed with
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * this work for additional information regarding copyright ownership.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * The ASF licenses this file to You under the Apache License, Version 2.0
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * (the "License"); you may not use this file except in compliance with
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * the License. You may obtain a copy of the License at
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * http://www.apache.org/licenses/LICENSE-2.0
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Unless required by applicable law or agreed to in writing, software
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * distributed under the License is distributed on an "AS IS" BASIS,
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * See the License for the specific language governing permissions and
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * limitations under the License.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * logresolve 2.0
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Tom Rathborne - tomr uunet.ca - http://www.uunet.ca/~tomr/
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * UUNET Canada, April 16, 1995
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Rewritten by David Robinson. (drtr ast.cam.ac.uk)
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Rewritten again, and ported to APR by Colm MacCarthaigh
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Usage: logresolve [-s filename] [-c] < access_log > new_log
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * -s filename name of a file to record statistics
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * -c check the DNS for a matching A record for the host.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Notes: (For historical interest)
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * To generate meaningful statistics from an HTTPD log file, it's good
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * to have the domain name of each machine that accessed your site, but
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * doing this on the fly can slow HTTPD down.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Compiling NCSA HTTPD with the -DMINIMAL_DNS flag turns IP#->hostname
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * resolution off. Before running your stats program, just run your log
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * file through this program (logresolve) and all of your IP numbers will
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * be resolved into hostnames (where possible).
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * logresolve takes an HTTPD access log (in the COMMON log file format,
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * or any other format that has the IP number/domain name as the first
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * field for that matter), and outputs the same file with all of the
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * domain names looked up. Where no domain name can be found, the IP
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * number is left in.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * To minimize impact on your nameserver, logresolve has its very own
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * internal hash-table cache. This means that each IP number will only
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * be looked up the first time it is found in the log file.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * The -c option causes logresolve to apply the same check as httpd
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * compiled with -DMAXIMUM_DNS; after finding the hostname from the IP
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * address, it looks up the IP addresses for the hostname and checks
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * that one of these matches the original address.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroederstatic const char *shortname = "logresolve";
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder/* Statistics */
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * prints various statistics to output
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroederstatic void print_statistics (apr_file_t *output)
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(output, "logresolve Statistics:" NL);
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(output, "Entries: %d" NL, entries);
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(output, " With name : %d" NL, withname);
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(output, " Resolves : %d" NL, resolves);
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(output, " - No reverse : %d" NL,
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(output, " - Double lookup failed : %d" NL,
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(output, "Cache hits : %d" NL, cachehits);
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder apr_file_printf(output, "Cache size : %d" NL, cachesize);
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroederstatic void usage(void)
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder "%s -- Resolve IP-addresses to hostnames in Apache log files." NL
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder " -s Record statistics to STATFILE when finished." NL
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder " -c Perform double lookups when resolving IP addresses." NL,
apr_getopt_t * o;
const char * arg;
char opt;
char * space;
char * hostname;
char * inbuffer;
char * outbuffer;
int doublelookups = 0;
if (argc) {
usage();
switch (opt) {
if (doublelookups) {
usage();
if (stats) {
usage();
entries++;
withname++;
if (hostname) {
cachehits++;
withname++;
resolves++;
cachesize++;
noreverse++;
if (doublelookups) {
0, pool);
doublefailed++;
if (stats) {