diff --git a/.gitignore b/.gitignore index 40a80e5..a71cb91 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ tmp/ tmp/* -newsstats.conf +etc/newsstats.conf diff --git a/feedlog.pl b/bin/feedlog.pl similarity index 93% rename from feedlog.pl rename to bin/feedlog.pl index a5ecfb0..bbfd814 100755 --- a/feedlog.pl +++ b/bin/feedlog.pl @@ -4,18 +4,19 @@ # # This script will log headers and other data to a database # for further analysis by parsing a feed from INN. -# +# # It is part of the NewsStats package. # # Copyright (c) 2010-2013 Thomas Hochstein # -# It can be redistributed and/or modified under the same terms under +# It can be redistributed and/or modified under the same terms under # which Perl itself is published. BEGIN { our $VERSION = "0.01"; use File::Basename; - push(@INC, dirname($0)); + # we're in .../bin, so our module is in ../lib + push(@INC, dirname($0).'/../lib'); } use strict; use warnings; @@ -68,14 +69,15 @@ sub PrepareDB { ################################# Main program ################################# ### read commandline options -my ($OptDebug,$OptQuiet); +my ($OptDebug,$OptQuiet,$OptConfFile); GetOptions ('d|debug!' => \$OptDebug, 'q|test!' => \$OptQuiet, + 'conffile=s' => \$OptConfFile, 'h|help' => \&ShowPOD, 'V|version' => \&ShowVersion) or exit 1; ### read configuration -my %Conf = %{ReadConfig($HomePath.'/newsstats.conf')}; +my %Conf = %{ReadConfig($OptConfFile)}; ### init syslog openlog($0, 'nofatal,pid', LOG_NEWS); @@ -129,7 +131,7 @@ while (<>) { }; }; $DBQuery->finish; - + warn sprintf("-----\nDay: %s\nDate: %s\nMID: %s\nTS: %s\nToken: %s\n". "Size: %s\nPeer: %s\nPath: %s\nNewsgroups: %s\nHeaders: %s\n", $Day, $Date, $Mid, $Timestamp, $Token, $Size, $Peer, $Path, @@ -151,7 +153,7 @@ feedlog - log data from an INN feed to a database =head1 SYNOPSIS -B [B<-Vhdq>] +B [B<-Vhdq>] [--conffile I] =head1 REQUIREMENTS @@ -197,6 +199,10 @@ find that information most probably in your B F file. Suppress logging to syslog. +=item B<--conffile> I + +Load configuration from I instead of F. + =back =head1 INSTALLATION @@ -218,15 +224,15 @@ See L for further information. =over 4 -=item F +=item F The script itself. -=item F +=item F Library functions for the NewsStats package. -=item F +=item F Runtime configuration file. diff --git a/gatherstats.pl b/bin/gatherstats.pl similarity index 96% rename from gatherstats.pl rename to bin/gatherstats.pl index f1fe7de..6096417 100755 --- a/gatherstats.pl +++ b/bin/gatherstats.pl @@ -4,18 +4,19 @@ # # This script will gather statistical information from a database # containing headers and other information from a INN feed. -# +# # It is part of the NewsStats package. # # Copyright (c) 2010-2013 Thomas Hochstein # -# It can be redistributed and/or modified under the same terms under +# It can be redistributed and/or modified under the same terms under # which Perl itself is published. BEGIN { our $VERSION = "0.01"; use File::Basename; - push(@INC, dirname($0)); + # we're in .../bin, so our module is in ../lib + push(@INC, dirname($0).'/../lib'); } use strict; use warnings; @@ -37,7 +38,7 @@ my %LegalStats; ### read commandline options my ($OptCheckgroupsFile,$OptClientsDB,$OptDebug,$OptGroupsDB,$OptTLH, - $OptHostsDB,$OptMonth,$OptRawDB,$OptStatsType,$OptTest); + $OptHostsDB,$OptMonth,$OptRawDB,$OptStatsType,$OptTest,$OptConfFile); GetOptions ('c|checkgroups=s' => \$OptCheckgroupsFile, 'clientsdb=s' => \$OptClientsDB, 'd|debug!' => \$OptDebug, @@ -48,11 +49,12 @@ GetOptions ('c|checkgroups=s' => \$OptCheckgroupsFile, 'rawdb=s' => \$OptRawDB, 's|stats=s' => \$OptStatsType, 't|test!' => \$OptTest, + 'conffile=s' => \$OptConfFile, 'h|help' => \&ShowPOD, 'V|version' => \&ShowVersion) or exit 1; ### read configuration -my %Conf = %{ReadConfig($HomePath.'/newsstats.conf')}; +my %Conf = %{ReadConfig($OptConfFile)}; ### override configuration via commandline options my %ConfOverride; @@ -160,7 +162,7 @@ foreach my $Month (&ListMonth($Period)) { } }; }; - + # delete old data for that month if (!$OptTest) { $DBQuery = $DBHandle->do(sprintf("DELETE FROM %s.%s WHERE month = ?", @@ -206,7 +208,7 @@ gatherstats - process statistical data from a raw source =head1 SYNOPSIS -B [B<-Vhdt>] [B<-m> I | I] [B<-s> I] [B<-c> I]] [B<--hierarchy> I] [B<--rawdb> I] [B<-groupsdb> I] [B<--clientsdb> I] [B<--hostsdb> I] +B [B<-Vhdt>] [B<-m> I | I] [B<-s> I] [B<-c> I]] [B<--hierarchy> I] [B<--rawdb> I] [B<-groupsdb> I] [B<--clientsdb> I] [B<--hostsdb> I] [--conffile I] =head1 REQUIREMENTS @@ -291,7 +293,7 @@ conjunction with B<--test> ... everything else seems a bit pointless. Set processing period to a single month in YYYY-MM format or to a time period between two month in YYYY-MM:YYYY-MM format (two month, separated -by a colon). +by a colon). =item B<-s>, B<--stats> I @@ -339,6 +341,10 @@ Override I from F. Override I from F. +=item B<--conffile> I + +Load configuration from I instead of F. + =back =head1 INSTALLATION @@ -368,15 +374,15 @@ checking against checkgroups-*: =over 4 -=item F +=item F The script itself. -=item F +=item F Library functions for the NewsStats package. -=item F +=item F Runtime configuration file. diff --git a/groupstats.pl b/bin/groupstats.pl similarity index 97% rename from groupstats.pl rename to bin/groupstats.pl index 499e333..7a3f7e4 100755 --- a/groupstats.pl +++ b/bin/groupstats.pl @@ -4,18 +4,19 @@ # # This script will get statistical data on newgroup usage # from a database. -# +# # It is part of the NewsStats package. # # Copyright (c) 2010-2013 Thomas Hochstein # -# It can be redistributed and/or modified under the same terms under +# It can be redistributed and/or modified under the same terms under # which Perl itself is published. BEGIN { our $VERSION = "0.01"; use File::Basename; - push(@INC, dirname($0)); + # we're in .../bin, so our module is in ../lib + push(@INC, dirname($0).'/../lib'); } use strict; use warnings; @@ -31,7 +32,7 @@ Getopt::Long::config ('bundling'); ### read commandline options my ($OptBoundType,$OptCaptions,$OptCheckgroupsFile,$OptComments, $OptFileTemplate,$OptFormat,$OptGroupBy,$OptGroupsDB,$LowBound,$OptMonth, - $OptNewsgroups,$OptOrderBy,$OptReportType,$OptSums,$UppBound); + $OptNewsgroups,$OptOrderBy,$OptReportType,$OptSums,$UppBound,$OptConfFile); GetOptions ('b|boundary=s' => \$OptBoundType, 'c|captions!' => \$OptCaptions, 'checkgroups=s' => \$OptCheckgroupsFile, @@ -47,6 +48,7 @@ GetOptions ('b|boundary=s' => \$OptBoundType, 'r|report=s' => \$OptReportType, 's|sums!' => \$OptSums, 'u|upper=i' => \$UppBound, + 'conffile=s' => \$OptConfFile, 'h|help' => \&ShowPOD, 'V|version' => \&ShowVersion) or exit 1; # parse parameters @@ -81,7 +83,7 @@ if ($OptReportType) { my $ValidGroups = &ReadGroupList($OptCheckgroupsFile) if $OptCheckgroupsFile; ### read configuration -my %Conf = %{ReadConfig($HomePath.'/newsstats.conf')}; +my %Conf = %{ReadConfig($OptConfFile)}; ### override configuration via commandline options my %ConfOverride; @@ -244,7 +246,7 @@ if ($OptCaptions && $OptComments) { ($OptOrderBy and $OptOrderBy =~ /posting/i) ? 'by number of postings ' : '', ($OptOrderBy and $OptOrderBy =~ /-?desc$/i) ? 'descending' : 'ascending'); } - + # output data &OutputData($OptFormat,$OptComments,$GroupBy,$Precision, $OptCheckgroupsFile ? $ValidGroups : '', @@ -263,7 +265,7 @@ groupstats - create reports on newsgroup usage =head1 SYNOPSIS -B [B<-Vhcs> B<--comments>] [B<-m> I[:I] | I] [B<-n> I] [B<--checkgroups> I] [B<-r> I] [B<-l> I] [B<-u> I] [B<-b> I] [B<-g> I] [B<-o> I] [B<-f> I] [B<--filetemplate> I] [B<--groupsdb> I] +B [B<-Vhcs> B<--comments>] [B<-m> I[:I] | I] [B<-n> I] [B<--checkgroups> I] [B<-r> I] [B<-l> I] [B<-u> I] [B<-b> I] [B<-g> I] [B<-o> I] [B<-f> I] [B<--filetemplate> I] [B<--groupsdb> I] [--conffile I] =head1 REQUIREMENTS @@ -346,7 +348,7 @@ Print out version and copyright information and exit. Print this man page and exit. -=item B<-m>, B<--month> I +=item B<-m>, B<--month> I Set processing period to a single month in YYYY-MM format or to a time period between two month in YYYY-MM:YYYY-MM format (two month, separated @@ -592,6 +594,10 @@ B<--nocomments> is enforced, see above. Override I from F. +=item B<--conffile> I + +Load configuration from I instead of F. + =back =head1 INSTALLATION @@ -635,15 +641,15 @@ machine-readable form (without formatting): =over 4 -=item F +=item F The script itself. -=item F +=item F Library functions for the NewsStats package. -=item F +=item F Runtime configuration file. diff --git a/newsstats.conf.sample b/etc/newsstats.conf.sample similarity index 100% rename from newsstats.conf.sample rename to etc/newsstats.conf.sample diff --git a/install/install.pl b/install/install.pl index ff392df..12cc8ec 100755 --- a/install/install.pl +++ b/install/install.pl @@ -3,19 +3,19 @@ # install.pl # # This script will create database tables as necessary. -# +# # It is part of the NewsStats package. # # Copyright (c) 2010-2013 Thomas Hochstein # -# It can be redistributed and/or modified under the same terms under +# It can be redistributed and/or modified under the same terms under # which Perl itself is published. BEGIN { our $VERSION = "0.01"; use File::Basename; - # we're in .../install, so our module is in .. - push(@INC, dirname($0).'/..'); + # we're in .../install, so our module is in ../lib + push(@INC, dirname($0).'/../lib'); } use strict; use warnings; @@ -31,8 +31,9 @@ Getopt::Long::config ('bundling'); ################################# Main program ################################# ### read commandline options -my ($OptUpdate); +my ($OptUpdate,$OptConfFile); GetOptions ('u|update=s' => \$OptUpdate, + 'conffile=s' => \$OptConfFile, 'h|help' => \&ShowPOD, 'V|version' => \&ShowVersion) or exit 1; @@ -42,7 +43,7 @@ my $Path = cwd(); ### read configuration print("Reading configuration.\n"); -my %Conf = %{ReadConfig($Path.'/newsstats.conf')}; +my %Conf = %{ReadConfig($OptConfFile)}; ##### -------------------------------------------------------------------------- ##### Database table definitions @@ -53,9 +54,9 @@ CREATE DATABASE IF NOT EXISTS `$Conf{'DBDatabase'}` DEFAULT CHARSET=utf8; SQLDB my %DBCreate = ('DBTableRaw' => < <prepare($DBCreate); $DBQuery->execute() or &Bleat(2, sprintf("Can't create database %s: %s%\n", $Conf{'DBDatabase'}, $DBI::errstr)); - + printf("Database table %s created succesfully.\n",$Conf{'DBDatabase'}); $DBHandle->disconnect; }; @@ -185,7 +186,7 @@ if (!$OptUpdate) { &CreateTable($Table); }; print "Database table generation done.\n"; - + # Display install instructions print $Install; } else { @@ -255,7 +256,7 @@ install - installation script =head1 SYNOPSIS -B [B<-Vh> [--update I] +B [B<-Vh> [--update I] [--conffile I] =head1 REQUIREMENTS @@ -288,21 +289,25 @@ Print this man page and exit. Don't do a fresh install, but update from I. +=item B<--conffile> I + +Load configuration from I instead of F. + =back =head1 FILES =over 4 -=item F +=item F The script itself. -=item F +=item F Library functions for the NewsStats package. -=item F +=item F Runtime configuration file. diff --git a/NewsStats.pm b/lib/NewsStats.pm similarity index 99% rename from NewsStats.pm rename to lib/NewsStats.pm index a237774..8a62179 100644 --- a/NewsStats.pm +++ b/lib/NewsStats.pm @@ -54,15 +54,19 @@ our $PackageVersion = '0.01'; use Data::Dumper; use File::Basename; +use Cwd qw(realpath); use Config::Auto; use DBI; #####-------------------------------- Vars --------------------------------##### -# trim the path +# save $0 in $FullPath our $FullPath = $0; -our $HomePath = dirname($0); +# strip filename and /bin or /install directory to create the $HomePath +our $HomePath = dirname(realpath($0)); +$HomePath =~ s/\/(bin|install)//; +# trim $0 $0 =~ s%.*/%%; # set version string our $MyVersion = "$0 $::VERSION (NewsStats.pm $VERSION)"; @@ -99,6 +103,8 @@ sub ReadConfig { ### IN : $ConfFile: config filename ### OUT: reference to a hash containing the configuration my ($ConfFile) = @_; + # set default + $ConfFile = $HomePath . '/etc/newsstats.conf' if !$ConfFile; # mandatory configuration options my @Mandatory = ('DBDriver','DBHost','DBUser','DBPw','DBDatabase', 'DBTableRaw','DBTableGrps');