| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package FusionInventory::Agent; |
|
2
|
|
|
|
|
|
|
|
|
3
|
29
|
|
|
29
|
|
537992
|
use strict; |
|
|
29
|
|
|
|
|
68
|
|
|
|
29
|
|
|
|
|
842
|
|
|
4
|
29
|
|
|
29
|
|
158
|
use warnings; |
|
|
29
|
|
|
|
|
57
|
|
|
|
29
|
|
|
|
|
925
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
29
|
|
|
29
|
|
158
|
use Cwd; |
|
|
29
|
|
|
|
|
153
|
|
|
|
29
|
|
|
|
|
2414
|
|
|
7
|
29
|
|
|
29
|
|
1739
|
use English qw(-no_match_vars); |
|
|
29
|
|
|
|
|
6268
|
|
|
|
29
|
|
|
|
|
238
|
|
|
8
|
29
|
|
|
29
|
|
17049
|
use UNIVERSAL::require; |
|
|
29
|
|
|
|
|
5830
|
|
|
|
29
|
|
|
|
|
239
|
|
|
9
|
29
|
|
|
29
|
|
872
|
use File::Glob; |
|
|
29
|
|
|
|
|
55
|
|
|
|
29
|
|
|
|
|
1660
|
|
|
10
|
29
|
|
|
29
|
|
22761
|
use IO::Handle; |
|
|
29
|
|
|
|
|
173970
|
|
|
|
29
|
|
|
|
|
1516
|
|
|
11
|
29
|
|
|
29
|
|
24366
|
use POSIX ":sys_wait_h"; # WNOHANG |
|
|
29
|
|
|
|
|
203330
|
|
|
|
29
|
|
|
|
|
187
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
29
|
|
|
29
|
|
60787
|
use FusionInventory::Agent::Config; |
|
|
29
|
|
|
|
|
91
|
|
|
|
29
|
|
|
|
|
356
|
|
|
14
|
29
|
|
|
29
|
|
18656
|
use FusionInventory::Agent::HTTP::Client::OCS; |
|
|
29
|
|
|
|
|
98
|
|
|
|
29
|
|
|
|
|
577
|
|
|
15
|
29
|
|
|
29
|
|
3872
|
use FusionInventory::Agent::Logger; |
|
|
29
|
|
|
|
|
62
|
|
|
|
29
|
|
|
|
|
2187
|
|
|
16
|
29
|
|
|
29
|
|
17063
|
use FusionInventory::Agent::Storage; |
|
|
29
|
|
|
|
|
110
|
|
|
|
29
|
|
|
|
|
335
|
|
|
17
|
29
|
|
|
29
|
|
12872
|
use FusionInventory::Agent::Task; |
|
|
29
|
|
|
|
|
82
|
|
|
|
29
|
|
|
|
|
456
|
|
|
18
|
29
|
|
|
29
|
|
17119
|
use FusionInventory::Agent::Target::Local; |
|
|
29
|
|
|
|
|
78
|
|
|
|
29
|
|
|
|
|
402
|
|
|
19
|
29
|
|
|
29
|
|
17289
|
use FusionInventory::Agent::Target::Server; |
|
|
29
|
|
|
|
|
78
|
|
|
|
29
|
|
|
|
|
330
|
|
|
20
|
29
|
|
|
29
|
|
787
|
use FusionInventory::Agent::Tools; |
|
|
29
|
|
|
|
|
51
|
|
|
|
29
|
|
|
|
|
5095
|
|
|
21
|
29
|
|
|
29
|
|
17158
|
use FusionInventory::Agent::Tools::Hostname; |
|
|
29
|
|
|
|
|
86
|
|
|
|
29
|
|
|
|
|
1561
|
|
|
22
|
29
|
|
|
29
|
|
17494
|
use FusionInventory::Agent::XML::Query::Prolog; |
|
|
29
|
|
|
|
|
73
|
|
|
|
29
|
|
|
|
|
317
|
|
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our $VERSION = '2.3.17'; |
|
25
|
|
|
|
|
|
|
our $VERSION_STRING = _versionString($VERSION); |
|
26
|
|
|
|
|
|
|
our $AGENT_STRING = "FusionInventory-Agent_v$VERSION"; |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub _versionString { |
|
29
|
29
|
|
|
29
|
|
66
|
my ($VERSION) = @_; |
|
30
|
|
|
|
|
|
|
|
|
31
|
29
|
|
|
|
|
88
|
my $string = "FusionInventory Agent ($VERSION)"; |
|
32
|
29
|
50
|
|
|
|
170
|
if ($VERSION =~ /^\d\.\d\.99(\d\d)/) { |
|
33
|
0
|
|
|
|
|
0
|
$string .= " **THIS IS A DEVELOPMENT RELEASE **"; |
|
34
|
|
|
|
|
|
|
} |
|
35
|
|
|
|
|
|
|
|
|
36
|
29
|
|
|
|
|
84
|
return $string; |
|
37
|
|
|
|
|
|
|
} |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub new { |
|
40
|
1
|
|
|
1
|
1
|
861
|
my ($class, %params) = @_; |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $self = { |
|
43
|
|
|
|
|
|
|
status => 'unknown', |
|
44
|
|
|
|
|
|
|
confdir => $params{confdir}, |
|
45
|
|
|
|
|
|
|
datadir => $params{datadir}, |
|
46
|
|
|
|
|
|
|
libdir => $params{libdir}, |
|
47
|
|
|
|
|
|
|
vardir => $params{vardir}, |
|
48
|
1
|
|
|
|
|
8
|
}; |
|
49
|
1
|
|
|
|
|
3
|
bless $self, $class; |
|
50
|
|
|
|
|
|
|
|
|
51
|
1
|
|
|
|
|
4
|
return $self; |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub init { |
|
55
|
0
|
|
|
0
|
1
|
0
|
my ($self, %params) = @_; |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my $config = FusionInventory::Agent::Config->new( |
|
58
|
|
|
|
|
|
|
confdir => $self->{confdir}, |
|
59
|
|
|
|
|
|
|
options => $params{options}, |
|
60
|
0
|
|
|
|
|
0
|
); |
|
61
|
0
|
|
|
|
|
0
|
$self->{config} = $config; |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my $verbosity = $config->{debug} && $config->{debug} == 1 ? LOG_DEBUG : |
|
64
|
0
|
0
|
0
|
|
|
0
|
$config->{debug} && $config->{debug} == 2 ? LOG_DEBUG2 : |
|
|
|
0
|
0
|
|
|
|
|
|
65
|
|
|
|
|
|
|
LOG_INFO ; |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
my $logger = FusionInventory::Agent::Logger->new( |
|
68
|
|
|
|
|
|
|
config => $config, |
|
69
|
|
|
|
|
|
|
backends => $config->{logger}, |
|
70
|
0
|
|
|
|
|
0
|
verbosity => $verbosity |
|
71
|
|
|
|
|
|
|
); |
|
72
|
0
|
|
|
|
|
0
|
$self->{logger} = $logger; |
|
73
|
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
0
|
$logger->debug("Configuration directory: $self->{confdir}"); |
|
75
|
0
|
|
|
|
|
0
|
$logger->debug("Data directory: $self->{datadir}"); |
|
76
|
0
|
|
|
|
|
0
|
$logger->debug("Storage directory: $self->{vardir}"); |
|
77
|
0
|
|
|
|
|
0
|
$logger->debug("Lib directory: $self->{libdir}"); |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
$self->{storage} = FusionInventory::Agent::Storage->new( |
|
80
|
|
|
|
|
|
|
logger => $logger, |
|
81
|
|
|
|
|
|
|
directory => $self->{vardir} |
|
82
|
0
|
|
|
|
|
0
|
); |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# handle persistent state |
|
85
|
0
|
|
|
|
|
0
|
$self->_loadState(); |
|
86
|
|
|
|
|
|
|
|
|
87
|
0
|
0
|
|
|
|
0
|
$self->{deviceid} = _computeDeviceId() if !$self->{deviceid}; |
|
88
|
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
0
|
$self->_saveState(); |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# create target list |
|
92
|
0
|
0
|
|
|
|
0
|
if ($config->{local}) { |
|
93
|
0
|
|
|
|
|
0
|
foreach my $path (@{$config->{local}}) { |
|
|
0
|
|
|
|
|
0
|
|
|
94
|
0
|
|
|
|
|
0
|
push @{$self->{targets}}, |
|
95
|
|
|
|
|
|
|
FusionInventory::Agent::Target::Local->new( |
|
96
|
|
|
|
|
|
|
logger => $logger, |
|
97
|
|
|
|
|
|
|
deviceid => $self->{deviceid}, |
|
98
|
|
|
|
|
|
|
delaytime => $config->{delaytime}, |
|
99
|
|
|
|
|
|
|
basevardir => $self->{vardir}, |
|
100
|
|
|
|
|
|
|
path => $path, |
|
101
|
|
|
|
|
|
|
html => $config->{html}, |
|
102
|
0
|
|
|
|
|
0
|
); |
|
103
|
|
|
|
|
|
|
} |
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
|
|
106
|
0
|
0
|
|
|
|
0
|
if ($config->{server}) { |
|
107
|
0
|
|
|
|
|
0
|
foreach my $url (@{$config->{server}}) { |
|
|
0
|
|
|
|
|
0
|
|
|
108
|
0
|
|
|
|
|
0
|
push @{$self->{targets}}, |
|
109
|
|
|
|
|
|
|
FusionInventory::Agent::Target::Server->new( |
|
110
|
|
|
|
|
|
|
logger => $logger, |
|
111
|
|
|
|
|
|
|
deviceid => $self->{deviceid}, |
|
112
|
|
|
|
|
|
|
delaytime => $config->{delaytime}, |
|
113
|
|
|
|
|
|
|
basevardir => $self->{vardir}, |
|
114
|
|
|
|
|
|
|
url => $url, |
|
115
|
|
|
|
|
|
|
tag => $config->{tag}, |
|
116
|
0
|
|
|
|
|
0
|
); |
|
117
|
|
|
|
|
|
|
} |
|
118
|
|
|
|
|
|
|
} |
|
119
|
|
|
|
|
|
|
|
|
120
|
0
|
0
|
|
|
|
0
|
if (!$self->{targets}) { |
|
121
|
0
|
|
|
|
|
0
|
$logger->error("No target defined, aborting"); |
|
122
|
0
|
|
|
|
|
0
|
exit 1; |
|
123
|
|
|
|
|
|
|
} |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# compute list of allowed tasks |
|
126
|
0
|
|
|
|
|
0
|
my %available = $self->getAvailableTasks(disabledTasks => $config->{'no-task'}); |
|
127
|
0
|
|
|
|
|
0
|
my @tasks = keys %available; |
|
128
|
|
|
|
|
|
|
|
|
129
|
0
|
0
|
|
|
|
0
|
if (!@tasks) { |
|
130
|
0
|
|
|
|
|
0
|
$logger->error("No tasks available, aborting"); |
|
131
|
0
|
|
|
|
|
0
|
exit 1; |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
0
|
$logger->debug("Available tasks:"); |
|
135
|
0
|
|
|
|
|
0
|
foreach my $task (keys %available) { |
|
136
|
0
|
|
|
|
|
0
|
$logger->debug("- $task: $available{$task}"); |
|
137
|
|
|
|
|
|
|
} |
|
138
|
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
0
|
$self->{tasks} = \@tasks; |
|
140
|
|
|
|
|
|
|
|
|
141
|
0
|
0
|
|
|
|
0
|
if ($config->{daemon}) { |
|
142
|
|
|
|
|
|
|
my $pidfile = $config->{pidfile} || |
|
143
|
0
|
|
0
|
|
|
0
|
$self->{vardir} . '/fusioninventory.pid'; |
|
144
|
|
|
|
|
|
|
|
|
145
|
0
|
0
|
|
|
|
0
|
if ($self->_isAlreadyRunning($pidfile)) { |
|
146
|
0
|
|
|
|
|
0
|
$logger->error("An agent is already running, exiting..."); |
|
147
|
0
|
|
|
|
|
0
|
exit 1; |
|
148
|
|
|
|
|
|
|
} |
|
149
|
0
|
0
|
|
|
|
0
|
if (!$config->{'no-fork'}) { |
|
150
|
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
0
|
Proc::Daemon->require(); |
|
152
|
0
|
0
|
|
|
|
0
|
if ($EVAL_ERROR) { |
|
153
|
0
|
|
|
|
|
0
|
$logger->error("Failed to load Proc::Daemon: $EVAL_ERROR"); |
|
154
|
0
|
|
|
|
|
0
|
exit 1; |
|
155
|
|
|
|
|
|
|
} |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
# If we use relative path, we must stay in the current directory |
|
158
|
0
|
0
|
|
|
|
0
|
my $workdir = substr($self->{libdir}, 0, 1) eq '/' ? '/' : getcwd(); |
|
159
|
|
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
0
|
Proc::Daemon::Init({ |
|
161
|
|
|
|
|
|
|
work_dir => $workdir, |
|
162
|
|
|
|
|
|
|
pid_file => $pidfile |
|
163
|
|
|
|
|
|
|
}); |
|
164
|
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
0
|
$self->{logger}->debug("Agent daemonized"); |
|
166
|
|
|
|
|
|
|
} |
|
167
|
|
|
|
|
|
|
} |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
# create HTTP interface |
|
170
|
0
|
0
|
0
|
|
|
0
|
if (($config->{daemon} || $config->{service}) && !$config->{'no-httpd'}) { |
|
|
|
|
0
|
|
|
|
|
|
171
|
0
|
|
|
|
|
0
|
FusionInventory::Agent::HTTP::Server->require(); |
|
172
|
0
|
0
|
|
|
|
0
|
if ($EVAL_ERROR) { |
|
173
|
0
|
|
|
|
|
0
|
$logger->error("Failed to load HTTP server: $EVAL_ERROR"); |
|
174
|
|
|
|
|
|
|
} else { |
|
175
|
|
|
|
|
|
|
$self->{server} = FusionInventory::Agent::HTTP::Server->new( |
|
176
|
|
|
|
|
|
|
logger => $logger, |
|
177
|
|
|
|
|
|
|
agent => $self, |
|
178
|
|
|
|
|
|
|
htmldir => $self->{datadir} . '/html', |
|
179
|
|
|
|
|
|
|
ip => $config->{'httpd-ip'}, |
|
180
|
|
|
|
|
|
|
port => $config->{'httpd-port'}, |
|
181
|
0
|
|
|
|
|
0
|
trust => $config->{'httpd-trust'} |
|
182
|
|
|
|
|
|
|
); |
|
183
|
0
|
|
|
|
|
0
|
$self->{server}->init(); |
|
184
|
|
|
|
|
|
|
} |
|
185
|
|
|
|
|
|
|
} |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
# install signal handler to handle graceful exit |
|
188
|
0
|
|
|
0
|
|
0
|
$SIG{INT} = sub { $self->terminate(); exit 0; }; |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
189
|
0
|
|
|
0
|
|
0
|
$SIG{TERM} = sub { $self->terminate(); exit 0; }; |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
$self->{logger}->info("FusionInventory Agent starting") |
|
192
|
0
|
0
|
0
|
|
|
0
|
if $self->{config}->{daemon} || $self->{config}->{service}; |
|
193
|
|
|
|
|
|
|
} |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub run { |
|
196
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
|
197
|
|
|
|
|
|
|
|
|
198
|
0
|
|
|
|
|
0
|
$self->{status} = 'waiting'; |
|
199
|
|
|
|
|
|
|
|
|
200
|
0
|
0
|
0
|
|
|
0
|
if ($self->{config}->{daemon} || $self->{config}->{service}) { |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# background mode: |
|
203
|
0
|
|
|
|
|
0
|
while (1) { |
|
204
|
0
|
|
|
|
|
0
|
my $time = time(); |
|
205
|
0
|
|
|
|
|
0
|
foreach my $target (@{$self->{targets}}) { |
|
|
0
|
|
|
|
|
0
|
|
|
206
|
0
|
0
|
|
|
|
0
|
next if $time < $target->getNextRunDate(); |
|
207
|
|
|
|
|
|
|
|
|
208
|
0
|
|
|
|
|
0
|
eval { |
|
209
|
0
|
|
|
|
|
0
|
$self->_runTarget($target); |
|
210
|
|
|
|
|
|
|
}; |
|
211
|
0
|
0
|
|
|
|
0
|
$self->{logger}->error($EVAL_ERROR) if $EVAL_ERROR; |
|
212
|
0
|
|
|
|
|
0
|
$target->resetNextRunDate(); |
|
213
|
|
|
|
|
|
|
} |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
# check for http interface messages |
|
216
|
0
|
0
|
|
|
|
0
|
$self->{server}->handleRequests() if $self->{server}; |
|
217
|
0
|
|
|
|
|
0
|
delay(1); |
|
218
|
|
|
|
|
|
|
} |
|
219
|
|
|
|
|
|
|
} else { |
|
220
|
|
|
|
|
|
|
# foreground mode: check each targets once |
|
221
|
0
|
|
|
|
|
0
|
my $time = time(); |
|
222
|
0
|
|
|
|
|
0
|
foreach my $target (@{$self->{targets}}) { |
|
|
0
|
|
|
|
|
0
|
|
|
223
|
0
|
0
|
0
|
|
|
0
|
if ($self->{config}->{lazy} && $time < $target->getNextRunDate()) { |
|
224
|
|
|
|
|
|
|
$self->{logger}->info( |
|
225
|
0
|
|
|
|
|
0
|
"$target->{id} is not ready yet, next server contact " . |
|
226
|
|
|
|
|
|
|
"planned for " . localtime($target->getNextRunDate()) |
|
227
|
|
|
|
|
|
|
); |
|
228
|
0
|
|
|
|
|
0
|
next; |
|
229
|
|
|
|
|
|
|
} |
|
230
|
|
|
|
|
|
|
|
|
231
|
0
|
|
|
|
|
0
|
eval { |
|
232
|
0
|
|
|
|
|
0
|
$self->_runTarget($target); |
|
233
|
|
|
|
|
|
|
}; |
|
234
|
0
|
0
|
|
|
|
0
|
$self->{logger}->error($EVAL_ERROR) if $EVAL_ERROR; |
|
235
|
|
|
|
|
|
|
} |
|
236
|
|
|
|
|
|
|
} |
|
237
|
|
|
|
|
|
|
} |
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
sub terminate { |
|
240
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
$self->{logger}->info("FusionInventory Agent exiting") |
|
243
|
0
|
0
|
0
|
|
|
0
|
if $self->{config}->{daemon} || $self->{config}->{service}; |
|
244
|
0
|
0
|
|
|
|
0
|
$self->{current_task}->abort() if $self->{current_task}; |
|
245
|
|
|
|
|
|
|
} |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
sub _runTarget { |
|
248
|
0
|
|
|
0
|
|
0
|
my ($self, $target) = @_; |
|
249
|
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
# the prolog dialog must be done once for all tasks, |
|
251
|
|
|
|
|
|
|
# but only for server targets |
|
252
|
0
|
|
|
|
|
0
|
my $response; |
|
253
|
0
|
0
|
|
|
|
0
|
if ($target->isa('FusionInventory::Agent::Target::Server')) { |
|
254
|
|
|
|
|
|
|
my $client = FusionInventory::Agent::HTTP::Client::OCS->new( |
|
255
|
|
|
|
|
|
|
logger => $self->{logger}, |
|
256
|
|
|
|
|
|
|
timeout => $self->{timeout}, |
|
257
|
|
|
|
|
|
|
user => $self->{config}->{user}, |
|
258
|
|
|
|
|
|
|
password => $self->{config}->{password}, |
|
259
|
|
|
|
|
|
|
proxy => $self->{config}->{proxy}, |
|
260
|
|
|
|
|
|
|
ca_cert_file => $self->{config}->{'ca-cert-file'}, |
|
261
|
|
|
|
|
|
|
ca_cert_dir => $self->{config}->{'ca-cert-dir'}, |
|
262
|
0
|
|
|
|
|
0
|
no_ssl_check => $self->{config}->{'no-ssl-check'}, |
|
263
|
|
|
|
|
|
|
); |
|
264
|
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
my $prolog = FusionInventory::Agent::XML::Query::Prolog->new( |
|
266
|
|
|
|
|
|
|
deviceid => $self->{deviceid}, |
|
267
|
0
|
|
|
|
|
0
|
); |
|
268
|
|
|
|
|
|
|
|
|
269
|
0
|
|
|
|
|
0
|
$self->{logger}->info("sending prolog request to server $target->{id}"); |
|
270
|
0
|
|
|
|
|
0
|
$response = $client->send( |
|
271
|
|
|
|
|
|
|
url => $target->getUrl(), |
|
272
|
|
|
|
|
|
|
message => $prolog |
|
273
|
|
|
|
|
|
|
); |
|
274
|
0
|
0
|
|
|
|
0
|
die "No answer from the server" unless $response; |
|
275
|
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
# update target |
|
277
|
0
|
|
|
|
|
0
|
my $content = $response->getContent(); |
|
278
|
0
|
0
|
|
|
|
0
|
if (defined($content->{PROLOG_FREQ})) { |
|
279
|
0
|
|
|
|
|
0
|
$target->setMaxDelay($content->{PROLOG_FREQ} * 3600); |
|
280
|
|
|
|
|
|
|
} |
|
281
|
|
|
|
|
|
|
} |
|
282
|
|
|
|
|
|
|
|
|
283
|
0
|
|
|
|
|
0
|
foreach my $name (@{$self->{tasks}}) { |
|
|
0
|
|
|
|
|
0
|
|
|
284
|
0
|
|
|
|
|
0
|
eval { |
|
285
|
0
|
|
|
|
|
0
|
$self->_runTask($target, $name, $response); |
|
286
|
|
|
|
|
|
|
}; |
|
287
|
0
|
0
|
|
|
|
0
|
$self->{logger}->error($EVAL_ERROR) if $EVAL_ERROR; |
|
288
|
0
|
|
|
|
|
0
|
$self->{status} = 'waiting'; |
|
289
|
|
|
|
|
|
|
} |
|
290
|
|
|
|
|
|
|
} |
|
291
|
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
sub _runTask { |
|
293
|
0
|
|
|
0
|
|
0
|
my ($self, $target, $name, $response) = @_; |
|
294
|
|
|
|
|
|
|
|
|
295
|
0
|
|
|
|
|
0
|
$self->{status} = "running task $name"; |
|
296
|
|
|
|
|
|
|
|
|
297
|
0
|
0
|
0
|
|
|
0
|
if ($self->{config}->{daemon} || $self->{config}->{service}) { |
|
298
|
|
|
|
|
|
|
# server mode: run each task in a child process |
|
299
|
0
|
0
|
|
|
|
0
|
if (my $pid = fork()) { |
|
300
|
|
|
|
|
|
|
# parent |
|
301
|
0
|
|
|
|
|
0
|
while (waitpid($pid, WNOHANG) == 0) { |
|
302
|
0
|
0
|
|
|
|
0
|
$self->{server}->handleRequests() if $self->{server}; |
|
303
|
0
|
|
|
|
|
0
|
delay(1); |
|
304
|
|
|
|
|
|
|
} |
|
305
|
|
|
|
|
|
|
} else { |
|
306
|
|
|
|
|
|
|
# child |
|
307
|
0
|
0
|
|
|
|
0
|
die "fork failed: $ERRNO" unless defined $pid; |
|
308
|
|
|
|
|
|
|
|
|
309
|
0
|
|
|
|
|
0
|
$self->{logger}->debug("forking process $PID to handle task $name"); |
|
310
|
0
|
|
|
|
|
0
|
$self->_runTaskReal($target, $name, $response); |
|
311
|
0
|
|
|
|
|
0
|
exit(0); |
|
312
|
|
|
|
|
|
|
} |
|
313
|
|
|
|
|
|
|
} else { |
|
314
|
|
|
|
|
|
|
# standalone mode: run each task directly |
|
315
|
0
|
|
|
|
|
0
|
$self->_runTaskReal($target, $name, $response); |
|
316
|
|
|
|
|
|
|
} |
|
317
|
|
|
|
|
|
|
} |
|
318
|
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
sub _runTaskReal { |
|
320
|
0
|
|
|
0
|
|
0
|
my ($self, $target, $name, $response) = @_; |
|
321
|
|
|
|
|
|
|
|
|
322
|
0
|
|
|
|
|
0
|
my $class = "FusionInventory::Agent::Task::$name"; |
|
323
|
|
|
|
|
|
|
|
|
324
|
0
|
|
|
|
|
0
|
$class->require(); |
|
325
|
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
my $task = $class->new( |
|
327
|
|
|
|
|
|
|
config => $self->{config}, |
|
328
|
|
|
|
|
|
|
confdir => $self->{confdir}, |
|
329
|
|
|
|
|
|
|
datadir => $self->{datadir}, |
|
330
|
|
|
|
|
|
|
logger => $self->{logger}, |
|
331
|
|
|
|
|
|
|
target => $target, |
|
332
|
|
|
|
|
|
|
deviceid => $self->{deviceid}, |
|
333
|
0
|
|
|
|
|
0
|
); |
|
334
|
|
|
|
|
|
|
|
|
335
|
0
|
0
|
|
|
|
0
|
return if !$task->isEnabled($response); |
|
336
|
|
|
|
|
|
|
|
|
337
|
0
|
|
|
|
|
0
|
$self->{logger}->info("running task $name"); |
|
338
|
0
|
|
|
|
|
0
|
$self->{current_task} = $task; |
|
339
|
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
$task->run( |
|
341
|
|
|
|
|
|
|
user => $self->{config}->{user}, |
|
342
|
|
|
|
|
|
|
password => $self->{config}->{password}, |
|
343
|
|
|
|
|
|
|
proxy => $self->{config}->{proxy}, |
|
344
|
|
|
|
|
|
|
ca_cert_file => $self->{config}->{'ca-cert-file'}, |
|
345
|
|
|
|
|
|
|
ca_cert_dir => $self->{config}->{'ca-cert-dir'}, |
|
346
|
0
|
|
|
|
|
0
|
no_ssl_check => $self->{config}->{'no-ssl-check'}, |
|
347
|
|
|
|
|
|
|
); |
|
348
|
0
|
|
|
|
|
0
|
delete $self->{current_task}; |
|
349
|
|
|
|
|
|
|
} |
|
350
|
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
sub getStatus { |
|
352
|
6
|
|
|
6
|
1
|
15
|
my ($self) = @_; |
|
353
|
6
|
|
|
|
|
191
|
return $self->{status}; |
|
354
|
|
|
|
|
|
|
} |
|
355
|
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
sub getTargets { |
|
357
|
25
|
|
|
25
|
1
|
114
|
my ($self) = @_; |
|
358
|
|
|
|
|
|
|
|
|
359
|
25
|
|
|
|
|
46
|
return @{$self->{targets}}; |
|
|
25
|
|
|
|
|
160
|
|
|
360
|
|
|
|
|
|
|
} |
|
361
|
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
sub getAvailableTasks { |
|
363
|
4
|
|
|
4
|
1
|
16736
|
my ($self, %params) = @_; |
|
364
|
|
|
|
|
|
|
|
|
365
|
4
|
|
|
|
|
9
|
my %tasks; |
|
366
|
4
|
|
|
|
|
6
|
my %disabled = map { lc($_) => 1 } @{$params{disabledTasks}}; |
|
|
0
|
|
|
|
|
0
|
|
|
|
4
|
|
|
|
|
16
|
|
|
367
|
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
# tasks may be located only in agent libdir |
|
369
|
4
|
|
|
|
|
15
|
my $directory = $self->{libdir}; |
|
370
|
4
|
|
|
|
|
10
|
$directory =~ s,\\,/,g; |
|
371
|
4
|
|
|
|
|
6
|
my $subdirectory = "FusionInventory/Agent/Task"; |
|
372
|
|
|
|
|
|
|
# look for all perl modules here |
|
373
|
4
|
|
|
|
|
456
|
foreach my $file (File::Glob::glob("$directory/$subdirectory/*.pm")) { |
|
374
|
10
|
50
|
|
|
|
92
|
next unless $file =~ m{($subdirectory/(\S+)\.pm)$}; |
|
375
|
10
|
|
|
|
|
35
|
my $module = file2module($1); |
|
376
|
10
|
|
|
|
|
30
|
my $name = file2module($2); |
|
377
|
|
|
|
|
|
|
|
|
378
|
10
|
50
|
|
|
|
28
|
next if $disabled{lc($name)}; |
|
379
|
|
|
|
|
|
|
|
|
380
|
10
|
|
|
|
|
12
|
my $version; |
|
381
|
10
|
50
|
33
|
|
|
45
|
if ($self->{config}->{daemon} || $self->{config}->{service}) { |
|
382
|
|
|
|
|
|
|
# server mode: check each task version in a child process |
|
383
|
0
|
|
|
|
|
0
|
my ($reader, $writer); |
|
384
|
0
|
|
|
|
|
0
|
pipe($reader, $writer); |
|
385
|
0
|
|
|
|
|
0
|
$writer->autoflush(1); |
|
386
|
|
|
|
|
|
|
|
|
387
|
0
|
0
|
|
|
|
0
|
if (my $pid = fork()) { |
|
388
|
|
|
|
|
|
|
# parent |
|
389
|
0
|
|
|
|
|
0
|
close $writer; |
|
390
|
0
|
|
|
|
|
0
|
$version = <$reader>; |
|
391
|
0
|
|
|
|
|
0
|
close $reader; |
|
392
|
0
|
|
|
|
|
0
|
waitpid($pid, 0); |
|
393
|
|
|
|
|
|
|
} else { |
|
394
|
|
|
|
|
|
|
# child |
|
395
|
0
|
0
|
|
|
|
0
|
die "fork failed: $ERRNO" unless defined $pid; |
|
396
|
|
|
|
|
|
|
|
|
397
|
0
|
|
|
|
|
0
|
close $reader; |
|
398
|
0
|
|
|
|
|
0
|
$version = $self->_getTaskVersion($module); |
|
399
|
0
|
0
|
|
|
|
0
|
print $writer $version if $version; |
|
400
|
0
|
|
|
|
|
0
|
close $writer; |
|
401
|
0
|
|
|
|
|
0
|
exit(0); |
|
402
|
|
|
|
|
|
|
} |
|
403
|
|
|
|
|
|
|
} else { |
|
404
|
|
|
|
|
|
|
# standalone mode: check each task version directly |
|
405
|
10
|
|
|
|
|
24
|
$version = $self->_getTaskVersion($module); |
|
406
|
|
|
|
|
|
|
} |
|
407
|
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
# no version means non-functionning task |
|
409
|
10
|
100
|
|
|
|
26
|
next unless $version; |
|
410
|
|
|
|
|
|
|
|
|
411
|
7
|
|
|
|
|
20
|
$tasks{$name} = $version; |
|
412
|
|
|
|
|
|
|
} |
|
413
|
|
|
|
|
|
|
|
|
414
|
4
|
|
|
|
|
30
|
return %tasks; |
|
415
|
|
|
|
|
|
|
} |
|
416
|
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
sub _getTaskVersion { |
|
418
|
10
|
|
|
10
|
|
18
|
my ($self, $module) = @_; |
|
419
|
|
|
|
|
|
|
|
|
420
|
10
|
|
|
|
|
14
|
my $logger = $self->{logger}; |
|
421
|
|
|
|
|
|
|
|
|
422
|
10
|
100
|
|
|
|
78
|
if (!$module->require()) { |
|
423
|
2
|
50
|
|
|
|
24
|
$logger->debug2("module $module does not compile: $@") if $logger; |
|
424
|
2
|
|
|
|
|
5
|
return; |
|
425
|
|
|
|
|
|
|
} |
|
426
|
|
|
|
|
|
|
|
|
427
|
8
|
100
|
|
|
|
204
|
if (!$module->isa('FusionInventory::Agent::Task')) { |
|
428
|
1
|
50
|
|
|
|
4
|
$logger->debug2("module $module is not a task") if $logger; |
|
429
|
1
|
|
|
|
|
4
|
return; |
|
430
|
|
|
|
|
|
|
} |
|
431
|
|
|
|
|
|
|
|
|
432
|
7
|
|
|
|
|
10
|
my $version; |
|
433
|
|
|
|
|
|
|
{ |
|
434
|
29
|
|
|
29
|
|
74651
|
no strict 'refs'; ## no critic |
|
|
29
|
|
|
|
|
59
|
|
|
|
29
|
|
|
|
|
9638
|
|
|
|
7
|
|
|
|
|
10
|
|
|
435
|
7
|
|
|
|
|
8
|
$version = ${$module . '::VERSION'}; |
|
|
7
|
|
|
|
|
24
|
|
|
436
|
|
|
|
|
|
|
} |
|
437
|
|
|
|
|
|
|
|
|
438
|
7
|
|
|
|
|
15
|
return $version; |
|
439
|
|
|
|
|
|
|
} |
|
440
|
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
sub _isAlreadyRunning { |
|
442
|
0
|
|
|
0
|
|
|
my ($self, $pidfile) = @_; |
|
443
|
|
|
|
|
|
|
|
|
444
|
0
|
|
|
|
|
|
Proc::PID::File->require(); |
|
445
|
0
|
0
|
|
|
|
|
if ($EVAL_ERROR) { |
|
446
|
|
|
|
|
|
|
$self->{logger}->debug( |
|
447
|
0
|
|
|
|
|
|
'Proc::PID::File unavailable, unable to check for running agent' |
|
448
|
|
|
|
|
|
|
); |
|
449
|
0
|
|
|
|
|
|
return 0; |
|
450
|
|
|
|
|
|
|
} |
|
451
|
|
|
|
|
|
|
|
|
452
|
0
|
|
|
|
|
|
my $pid = Proc::PID::File->new(); |
|
453
|
0
|
|
|
|
|
|
$pid->{path} = $pidfile; |
|
454
|
0
|
|
|
|
|
|
return $pid->alive(); |
|
455
|
|
|
|
|
|
|
} |
|
456
|
|
|
|
|
|
|
|
|
457
|
|
|
|
|
|
|
sub _loadState { |
|
458
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
|
459
|
|
|
|
|
|
|
|
|
460
|
0
|
|
|
|
|
|
my $data = $self->{storage}->restore(name => 'FusionInventory-Agent'); |
|
461
|
|
|
|
|
|
|
|
|
462
|
0
|
0
|
|
|
|
|
$self->{deviceid} = $data->{deviceid} if $data->{deviceid}; |
|
463
|
|
|
|
|
|
|
} |
|
464
|
|
|
|
|
|
|
|
|
465
|
|
|
|
|
|
|
sub _saveState { |
|
466
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
|
467
|
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
$self->{storage}->save( |
|
469
|
|
|
|
|
|
|
name => 'FusionInventory-Agent', |
|
470
|
|
|
|
|
|
|
data => { |
|
471
|
|
|
|
|
|
|
deviceid => $self->{deviceid}, |
|
472
|
|
|
|
|
|
|
} |
|
473
|
0
|
|
|
|
|
|
); |
|
474
|
|
|
|
|
|
|
} |
|
475
|
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
# compute an unique agent identifier, based on host name and current time |
|
477
|
|
|
|
|
|
|
sub _computeDeviceId { |
|
478
|
0
|
|
|
0
|
|
|
my $hostname = getHostname(); |
|
479
|
|
|
|
|
|
|
|
|
480
|
0
|
|
|
|
|
|
my ($year, $month , $day, $hour, $min, $sec) = |
|
481
|
|
|
|
|
|
|
(localtime (time))[5, 4, 3, 2, 1, 0]; |
|
482
|
|
|
|
|
|
|
|
|
483
|
0
|
|
|
|
|
|
return sprintf "%s-%02d-%02d-%02d-%02d-%02d-%02d", |
|
484
|
|
|
|
|
|
|
$hostname, $year + 1900, $month + 1, $day, $hour, $min, $sec; |
|
485
|
|
|
|
|
|
|
} |
|
486
|
|
|
|
|
|
|
|
|
487
|
|
|
|
|
|
|
1; |
|
488
|
|
|
|
|
|
|
__END__ |