I have the following code that reads information from a MySQL database, determines log file format, then calls other programs as necessary to convert from IIS to NCSA format, and finally generate pretty pictures for customers. I am a big fan of efficiency, so is there a better way to do what I have here? Bear in mind that there are other programs that, for space reasons, I have not posted.
#!/usr/bin/perl # -*- perl -*- @(#) $Id: webalize,v 1.3 2000/06/26 20:04:11 root Exp $ # # $Revision: 1.3 $ # $Date: 2000/06/26 20:04:11 $ # $Author: root $ # $Locker: $ # $RCSfile: webalize,v $ # $State: Exp $ # $Source: /opt/webalizer/bin/RCS/webalize,v $ require 5.005; use strict; use DBI; umask 002; my @dom_list = ''; my $dom_restrict_query = ''; if ( scalar(@ARGV) > 0 ) { @dom_list = @ARGV; $dom_restrict_query = " and domain in ('" . join("','", @dom_list) . "') "; } my $prog_name = $0; # my $log_basedir = '/webstat-filer/NT/'; my $log_basedir = '/webstat/NT/'; my $log_dir = ''; my $syscmd_webalizer_wrapper = '/opt/webalizer/bin/webalizer-wrapper'; # The parse2ncsa command takes a directory name as a parameter, # and parses all of the logs in that directory in chronological # order, converting to NCSA format wherever necessary. It sends # the NCSA common/combined log format to STDOUT. my $syscmd_parse2ncsa = '/opt/webalizer/bin/parse2ncsa'; chdir $log_dir or die "Could not change to $log_dir"; my $domain; my $webserver; my $w3svc; my $ip_addr; my $line; my $driver = 'mysql'; my $database = 'webstat'; my $table = 'sites'; my $user = 'statadmin'; my $password = 'statsrus'; my $dbh; my $dsn = "DBI:$driver:$database"; my $dbh = DBI->connect($dsn, $user, $password, {RaiseError => 1, AutoC +ommit => 1}); my $query = "select domain, server_type, server_name, log_code, log_lo +cation from sites " . " where active = 1 " . $dom_restrict_query # gets domains from command line . " order by priority "; my $sth = $dbh->prepare($query); $sth->execute(); my $res_hashref; while (defined ( $res_hashref = $sth->fetchrow_hashref )) { # variables are # $$res_hashref{'domain'} # domain name for stats # $$res_hashref{'server_name'} # server such as "ntpub1" # $$res_hashref{'log_code'} # W3SVC number # change ARGV[0] so that current info will appear in a "ps" listin +g $0 = sprintf ("%s - processing %s from %s/%s", $prog_name, $$res_hashref{'domain'}, $$res_hashref{'server_name'}, $$res_hashref{'log_code'}); if ( $$res_hashref{'server_type'} eq 'iis4.0' or $$res_hashref{'se +rver_type'} eq 'iis5.0' ) { #next unless $$res_hashref{'log_code'}; my $log_dir = join('/', $log_basedir, $$res_hashref{'server_name'}, $$res_hashref{'log_code'}); open(NCSA_STREAM,"$syscmd_parse2ncsa $log_dir $$res_hashref{'domai +n'} |") or next; } elsif ( $$res_hashref{'server_type'} eq 'apache' ) { my $log_file = $$res_hashref{'log_location'}; open(NCSA_STREAM,"<$log_file"); } else # not iis4.0/iis5.0 nor apache { next; } open(WEBALIZER, "|$syscmd_webalizer_wrapper $$res_hashref{'domain' +}") or next; while (defined ($line = <NCSA_STREAM>)) { $line =~ tr/\000//d; # remove ASCII nulls that NT IIS puts in log + files print WEBALIZER $line while defined ($line = <NCSA_STREAM>); } close(NCSA_STREAM); close(WEBALIZER); } $dbh->disconnect;

In reply to Efficiency by tekniko

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.