1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
package Ocsinventory::Agent::Hooks;
# This package give possibility to use hooks in unified unix agent.
use strict;
use warnings;
#use FindBin qw($Bin);
sub new {
my (undef, $context) = @_;
my $self = {};
$self->{accountinfo} = $context->{accountinfo};
$self->{accountconfig} = $context->{accountconfig};
my $logger = $self->{logger}=$context->{logger};
$self->{config} = $context->{config};
$self->{dontuse} = 1;
my $modulefile;
foreach (@{$self->{config}->{etcdir}}) {
$modulefile = $_.'/modules.conf';
if (-f $modulefile) {
if (do $modulefile) {
$logger->debug("Turns hooks on for $modulefile");
$self->{dontuse} = 0;
last;
} else {
$logger->debug("Failed to load `$modulefile': $?");
}
}
}
if ($self->{dontuse}) {
$logger->debug("No modules will be used.");
} else {
my $ocsAgentServerUri;
# to avoid a warning if $self->{config}->{server} is not defined
if ($self->{config}->{server}) {
$ocsAgentServerUri = "http://".$self->{config}->{server}.$self->{config}->{remotedir};
}
if ($self->{config}->{debug}) {
$::debug = 2;
}
}
#Create objects for modules
foreach my $mod (keys %Ocsinventory::Agent::Modules::) {
$mod =~ s/\:\://;
my $package ="Ocsinventory::Agent::Modules::".$mod;
my $module = new $package($context) ;
my $name= $module->{structure}->{name};
#Store the reference in a key to access modules easily
$self->{modules}->{$name}=$module;
}
bless $self;
}
sub run {
my ($self, $args, $moduleparam) = @_;
return if $self->{dontuse};
my $name = $args->{name}; #type of hook asked
my $logger = $self->{logger};
$logger->debug("Calling handlers : `$name'");
#Launching hook for modules if not 'undef' and if modules are not disabled by start_handler
for (keys %{$self->{modules}}) {
my $module = $self->{modules}->{$_};
unless ($module->{disabled}) {
my $hook = $module->{structure}->{$name};
if ($hook) {
$module->$hook($moduleparam);
}
}
}
}
1;
|