Apache-DBILogger

 view release on metacpan or  search on metacpan

bin/webstat_mail.pl  view on Meta::CPAN

	  die "Could not connect to smtp server" unless ($smtp);	  
	  my $mailfrom = $serverconfig->{mail}->{from} ? $serverconfig->{mail}->{from} : 
		$WebStat::Config::server{"default"}->{mail}->{from};
	  $smtp->mail($mailfrom);
	  my @mailto = $opts{T} ? @{$WebStat::Config::server{"default"}->{mail}->{rcpt}}
				: @{$serverconfig->{mail}->{rcpt}}; 
	  $smtp->to(@mailto); 
	  $smtp->data();
	  $smtp->datasend("From: $mailfrom\n");
	  $smtp->datasend("To: ". join (", ", @mailto)."\n" );
	  $smtp->datasend("Subject: Webstats for $server (". time2str("%Y-%m-%e", time) .")\n\n");
	  $smtp->datasend("$data\n");
	  $smtp->dataend();
	  $smtp->quit;
	}
}

#  
# Count users, hits and more for from the given period ..
#    args: $fromtime, $serverquery
#    returns: a hash with "the stats" (read the source :) )
sub CountThings {
  my ($fromtime, $serverquery) = @_;

  my ($sth, $timequery);

  my $table = $WebStat::Config::database{"table"};
  my $today = time2str("%Y-%m-%e", time);

  unless ("$fromtime" eq "$today") {
	$timequery = qq[(timeserved >= "$fromtime" and timeserved < "$today")];
  } else {
	$timequery = qq[(timeserved >= "$fromtime")];
  }

  my %stat;
  # hits
  $sth = DoSql(qq[select count(server),sum(bytes)/1024 from $table where $serverquery 
				  and $timequery]);
  ($stat{hits}, $stat{traffic}) = $sth->fetchrow;
  
 ## pageviews
 # $sth = DoSql(qq[select count(server),sum(bytes)/1024 from $table where $serverquery 
 #				  and $timequery
 #				  and (contenttype = 'text/plain' or contenttype = 'text/html')]);
 #($stat{pagehits}, $stat{pagetraffic}) = $sth->fetchrow;

  # users
  # use something like this to look at the "users" .
  # select server,urlpath,usertrack,remoteip,left(useragent,30),timeserved,
  # count(server) as c from  requests where server="www.monsted.com" and timeserved
  # >= "1998-06-20"  group by usertrack,remoteip order by remoteip;
  $sth = DoSql(qq[select count(server),remoteip from $table where $serverquery
				  and $timequery
				  group by usertrack]);

  $stat{usercount} = 0;
  my %visitors = ();
  while (my ($counts, $remoteip) = $sth->fetchrow) {
	# only count those who've looked at more than one page
	# (throw away spiders (and other who doesn't support cookies))
	if ($counts > 1) { 
	  # count it, he support cookies
	  $stat{usercount}++;
	} else { 
	  # remember the looser by hand
	  $visitors{$remoteip}++;
	}
  }
  # add the noncookies if they hit more than one page (maybe we just should add them?)
  for my $count (values %visitors) {
	$stat{usercount}++ if ($count > 1);
  }
  
  return \%stat;

}

sub DoSql {
	my $sqlcommand = shift;
	print "sqlcommand: $sqlcommand\n" if $opts{d};
	my $sth = $dbh->prepare($sqlcommand) 
	  or die "Could not prepare [$DBI::errstr] ($sqlcommand)";
	$sth->execute 
	  or die "Could not execute [$DBI::errstr] ($sqlcommand)";
	
    return $sth;
}

sub readconfigfile {
	my $conffile = $opts{c} || "./webstat.conf";	
	require $conffile;
 	if ($opts{d}) {
	 	#print Data::Dumper->Dump([\%WebStat::Config::server], [qw(server)]);
	 	#print Data::Dumper->Dump([\%WebStat::Config::database], [qw(database)]);
 	}
}


# count hits and traffic
#	 select count(bytes) as hits,sum(bytes) as trafik,server from requests group by server order by hits limit 20 ;


$dbh->disconnect;

undef %WebStat::Config::server;

exit;


sub usage {
    print STDERR <<EOT;
Usage: $0 [options]

Options:
    -d               Debug: print debug information.
	-c /config/file  Select configurationfile
	-t               Show hits for today (only)
	-M               Mail reports (default: print to stdout)
	-T               Test mode, only send mails to the default mail rcpt
    -h               This help



( run in 0.503 second using v1.01-cache-2.11-cpan-39bf76dae61 )