-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathCollect.pm
More file actions
275 lines (229 loc) · 8.36 KB
/
Copy pathCollect.pm
File metadata and controls
275 lines (229 loc) · 8.36 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package GLPI::Agent::Task::Collect;
use strict;
use warnings;
use parent 'GLPI::Agent::Task';
use English qw(-no_match_vars);
use File::Glob;
use UNIVERSAL::require;
use GLPI::Agent;
use GLPI::Agent::Logger;
use GLPI::Agent::Tools;
use GLPI::Agent::HTTP::Client::Fusion;
use GLPI::Agent::Task::Collect::Version;
use GLPI::Agent::Task::Collect::Common;
use constant MIN_TIMEOUT => 10;
use constant MAX_TIMEOUT_FACTOR => 20;
use constant MAX_DEFAULT_TIMEOUT => 3600;
our $VERSION = GLPI::Agent::Task::Collect::Version::VERSION;
my %modules;
my %json_validation;
unless (keys(%modules)) {
my ($_classpath) = $INC{module2file(__PACKAGE__)} =~ /^(.*)\.pm$/;
$_classpath =~ s{\\}{/}g if $OSNAME eq 'MSWin32';
my ($_modulepath) = module2file(__PACKAGE__) =~ /^(.*)\.pm$/;
$_modulepath =~ s{\\}{/}g if $OSNAME eq 'MSWin32';
my $subclass_path_re = qr/$_modulepath\/(\S+)\.pm$/;
foreach my $file (File::Glob::bsd_glob("$_classpath/*.pm")) {
$file =~ s{\\}{/}g if $OSNAME eq 'MSWin32';
my ($class) = $file =~ $subclass_path_re
or next;
next if $class eq "Version" or $class eq "Common";
my $module = __PACKAGE__ . "::" . $class;
$module->require()
or next;
next if $module->disabled;
next unless $module->function;
$modules{$module->function} = $module;
$json_validation{$module->function} = $module->json_validation;
}
}
sub isEnabled {
my ($self) = @_;
unless ($self->{target}->isType('server')) {
$self->{logger}->debug("Collect task only compatible with server target");
return;
}
return 1;
}
sub run {
my ($self) = @_;
# Just reset event if run as an event to not trigger another one
$self->resetEvent();
$self->{client} = GLPI::Agent::HTTP::Client::Fusion->new(
logger => $self->{logger},
config => $self->{config},
);
my $globalRemoteConfig = $self->{client}->send(
url => $self->{target}->getUrl(),
args => {
action => "getConfig",
machineid => $self->{deviceid},
task => { Collect => $VERSION },
}
);
my $id = $self->{target}->id();
if (!$globalRemoteConfig) {
$self->{logger}->info("Collect task not supported by $id");
return;
}
if (!$globalRemoteConfig->{schedule}) {
$self->{logger}->info("No job schedule returned by $id");
return;
}
if (ref( $globalRemoteConfig->{schedule} ) ne 'ARRAY') {
$self->{logger}->info("Malformed schedule from by $id");
return;
}
if ( !@{$globalRemoteConfig->{schedule}} ) {
$self->{logger}->info("No Collect job enabled or Collect support disabled server side.");
return;
}
my $run_jobs = 0;
foreach my $job ( @{ $globalRemoteConfig->{schedule} } ) {
next unless (ref($job) eq 'HASH' && exists($job->{task})
&& $job->{task} eq "Collect");
$self->_processRemote($job->{remote});
$run_jobs ++;
}
if ( !$run_jobs ) {
$self->{logger}->info("No Collect job found in server jobs list.");
return;
}
return 1;
}
sub _processRemote {
my ($self, $remoteUrl) = @_;
if ( !$remoteUrl ) {
return;
}
my $answer = $self->{client}->send(
url => $remoteUrl,
args => {
action => "getJobs",
machineid => $self->{deviceid},
}
);
if (ref($answer) eq 'HASH' && !keys %$answer) {
$self->{logger}->debug("Nothing to do");
return;
}
my $check = GLPI::Agent::Task::Collect::Common->new(logger => $self->{logger});
return unless $check->validateAnswer(
answer => $answer,
modules => \%modules,
json_validation => \%json_validation,
);
my @jobs = @{$answer->{jobs}}
or die "no jobs provided, aborting";
my $method = exists($answer->{postmethod}) && $answer->{postmethod} eq 'POST' ? 'POST' : 'GET' ;
my $token = exists($answer->{token}) ? $answer->{token} : '';
my $has_csrf_token = empty($token) ? 0 : 1;
my %jobsdone = ();
JOB:
foreach my $job (@jobs) {
$self->{logger}->debug2("Starting a collect job...");
if ( !$job->{uuid} ) {
$self->{logger}->error("UUID key missing");
next;
}
$self->{logger}->debug2("Collect job has uuid: ".$job->{uuid});
my $function = $job->{function};
unless ($function) {
$self->{logger}->error("function key missing");
next;
}
unless (defined($modules{$function})) {
$self->{logger}->error("Bad function '$function'");
next;
}
my $module = $modules{$function};
my $collect = $module->new(
logger => $self->{logger},
plugin => $self->{target}->getTaskVersion("collect") // "1.0.0",
job => $job
);
my $maxtimeout = MAX_TIMEOUT_FACTOR * int($self->{config}->{'backend-collect-timeout'})
|| MAX_DEFAULT_TIMEOUT;
my $timeout = int( !$self->{timeout} || $self->{timeout} !~ /^\d+$/ ?
$self->{config}->{'backend-collect-timeout'} : $self->{timeout}
);
# Keep a minimum timeout or a maximun one
$timeout = $timeout < MIN_TIMEOUT ? MIN_TIMEOUT : $timeout > $maxtimeout ? $maxtimeout : $timeout;
my $results = runFunction(
module => $module,
function => "results",
logger => $self->{logger},
timeout => $timeout,
params => $collect,
);
$results = [] unless ref($results) eq 'ARRAY';
my $count = int(@{$results});
my $cpt = $count;
# Glpi-Inventory plugin >= v1.6.9 supports multiple values submission one by one
# and if we can add the _count value at least for registry collect
my $updated_plugin = $collect->pluginSupport("1.6.9");
# Add an empty hash ref so send an answer with _cpt=0
push @{$results}, {} unless $count ;
foreach my $result (@{$results}) {
next unless ref($result) eq 'HASH';
next unless !$cpt || keys %$result;
$result->{uuid} = $job->{uuid};
$result->{action} = "setAnswer";
$result->{_count} = $count
if $updated_plugin && $count == $cpt;
$result->{_cpt} = $cpt;
$result->{_glpi_csrf_token} = $token
if $token ;
$result->{_sid} = $job->{_sid}
if (exists($job->{_sid}));
$answer = $self->{client}->send(
url => $remoteUrl,
method => $method,
filename => sprintf('collect_%s_%s.js', $job->{uuid}, $cpt),
args => $result
);
$token = $answer && exists($answer->{token}) ? $answer->{token} : '';
$cpt--;
# Handle CSRF access denied
if ($has_csrf_token && empty($token)) {
$self->{logger}->error("Bad answer: CSRF checking is failing");
# Send an empty answer to force an error on server job
$self->{client}->send(
url => $remoteUrl,
args => {
uuid => $job->{uuid},
action => "setAnswer",
}
);
# Send a last message for server job log
$self->{client}->send(
url => $remoteUrl,
args => {
uuid => $job->{uuid},
action => "setAnswer",
csrf_failure => 1,
}
);
# No need to send job done message
delete $jobsdone{$job->{uuid}};
last JOB;
}
}
# Set this job is done by uuid
$jobsdone{$job->{uuid}} = 1;
}
# Finally send jobsDone for each seen jobs uuid
foreach my $uuid (keys(%jobsdone)) {
my $answer = $self->{client}->send(
url => $remoteUrl,
args => {
action => "jobsDone",
uuid => $uuid
}
);
$self->{logger}->debug2("Got no response on $uuid jobsDone action")
unless $answer;
}
return $self;
}
1;