| Server IP : 162.144.4.212 / Your IP : 216.73.216.50 Web Server : Apache System : Linux gator2125.hostgator.com 4.19.286-203.ELK.el7.x86_64 #1 SMP Wed Jun 14 04:33:55 CDT 2023 x86_64 User : cozeellc ( 2980) PHP Version : 8.3.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /opt/PUC/lib/PUC/ |
Upload File : |
package PUC::Collector;
use strict;
use warnings;
use 5.10.1;
# Uses core modules only.
use Carp;
use Data::Dumper;
use Safe;
use Sys::Hostname;
use PUC::Collector::DataStore;
use Log::Log4perl;
Log::Log4perl->init_once('/opt/PUC/log4perl.conf');
my $log = Log::Log4perl->get_logger();
my $COLLECTOR_CONFIG_FILE = 'collector_config.conf';
my $USERDATADOMAINS = 'etc/userdatadomains';
sub run {
my %args = @_;
my $config = startup(%args);
my $domains = get_domain_list($config);
my @modules = map {
"PUC::Module::$_"->new( app_config => $config )
} @{ $config->{primary_modules} };
my @secondary_modules = map {
"PUC::Module::$_"->new( app_config => $config )
} @{ $config->{secondary_modules} };
for my $domain ( @{$domains} ) {
for my $module (@modules) {
if ( $args{single_domain} ) {
if ( $domain->{name} eq $args{single_domain} ) {
$module->process_domain($domain);
}
}
elsif ( $args{single_user} ) {
if ( $domain->{username} eq $args{single_user} ) {
$module->process_domain($domain);
}
}
else {
$module->process_domain($domain);
}
}
}
return;
}
################################################################################
sub startup {
my %args = @_;
my $config = parse_config(%args);
load_modules($config);
$config->{datastore} =
PUC::Collector::DataStore->new( %{ $config->{datastore_config} } );
return $config;
}
################################################################################
sub parse_config {
my %args = @_;
my $libdir = $args{bindir} . '/lib';
my %primary_modules;
my %secondary_modules;
my $config_data = {};
my $config_file = $args{bindir} . '/' . $COLLECTOR_CONFIG_FILE;
if ( -f $config_file ) {
open my $fh, '<', $config_file or croak "Unable to read config file: $!";
my $contents;
{
## no critic [Variables::RequireInitializationForLocalVars]
local $/;
##
$contents = <$fh>;
}
close $fh;
my $compartment = Safe->new();
$config_data = $compartment->reval($contents);
if ( ref($config_data) ne 'HASH' ) {
croak "Error in collector config file: $@";
}
for my $module_entry ( @{ $config_data->{modules} } ) {
if ( scalar( @{ $module_entry->{requires} } ) ) {
$secondary_modules{ $module_entry->{name} } = 1;
for my $secondary_name ( @{ $module_entry->{requires} } ) {
$secondary_modules{$secondary_name} = 1;
}
}
else {
$primary_modules{ $module_entry->{name} } = 1;
}
}
}
my @pri = keys %primary_modules;
my @sec = keys %secondary_modules;
my $config = {
hostname => hostname(),
debug => $args{debug},
dryrun => $args{dryrun},
dataroot => $args{dataroot},
bindir => $args{bindir},
libdir => $libdir,
primary_modules => \@pri,
secondary_modules => \@sec,
datastore_config => $config_data->{datastore_config},
};
if ( $args{datastore_base_url} ) {
$config->{datastore_config}->{base_url} = $args{datastore_base_url};
}
if ( $args{datastore_key_file} ) {
$config->{datastore_config}->{key_file} = $args{datastore_key_file};
}
if ( $config->{dryrun} ) {
$config->{datastore_config}->{base_url} = 'dryrun://localhost';
}
$log->info("config " . Dumper($config));
if ( $config->{debug} ) {
print STDERR "config " . Dumper($config);
}
return $config;
}
################################################################################
sub load_modules {
my $config = shift;
push @INC, $config->{libdir};
## no critic [Modules::RequireBarewordIncludes]
for my $primary_module ( @{ $config->{primary_modules} } ) {
$log->info("Loading Primary Module: ".$primary_module);
print STDERR "Loading Primary Module: $primary_module\n"
if $config->{debug};
my $package = "PUC::Module::$primary_module";
( my $file = $package ) =~ s|::|/|g;
require "$file.pm";
}
for my $secondary_module ( @{ $config->{secondary_modules} } ) {
$log->info("Loading Secondary Module: ".$secondary_module);
print STDERR "Loading Secondary Module: $secondary_module\n"
if $config->{debug};
my $package = "PUC::Module::$secondary_module";
( my $file = $package ) =~ s|::|/|g;
require "$file.pm";
}
##
return;
}
################################################################################
sub get_domain_list {
my $config = shift;
my @docroot_list;
my $file_name = $config->{dataroot} . $USERDATADOMAINS;
open my $fh, '<', $file_name or croak "can't open < $file_name: $!";
while (<$fh>) {
my ( $domain, $data ) = split ': ', $_, 2;
my ( $username, undef, $type, undef, $docroot ) = split '==', $data;
push @docroot_list,
{
name => $domain,
docroot => $docroot,
username => $username,
type => $type,
};
}
close $fh;
$log->info("domains " . Dumper( \@docroot_list ));
if ( $config->{debug} ) {
print STDERR "domains " . Dumper( \@docroot_list );
}
return \@docroot_list;
}
################################################################################
1;