File: Hooks.pm

package info (click to toggle)
ocsinventory-agent 2%3A2.0.5-1.2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 4,120 kB
  • ctags: 899
  • sloc: perl: 20,687; sh: 576; objc: 468; ansic: 333; makefile: 55
file content (95 lines) | stat: -rw-r--r-- 2,129 bytes parent folder | download | duplicates (2)
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;