line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Armadito::Agent::Task::Runjobs; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
3793421
|
use strict; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
64
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
61
|
|
5
|
1
|
|
|
1
|
|
4
|
use base 'Armadito::Agent::Task'; |
|
1
|
|
|
|
|
29
|
|
|
1
|
|
|
|
|
484
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
7
|
use Armadito::Agent::Storage; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
8
|
1
|
|
|
1
|
|
21
|
use Data::Dumper; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
40
|
|
9
|
1
|
|
|
1
|
|
629
|
use MIME::Base64; |
|
1
|
|
|
|
|
558
|
|
|
1
|
|
|
|
|
47
|
|
10
|
1
|
|
|
1
|
|
447
|
use Try::Tiny; |
|
1
|
|
|
|
|
1032
|
|
|
1
|
|
|
|
|
62
|
|
11
|
1
|
|
|
1
|
|
6
|
use JSON; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
17
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
0
|
|
|
0
|
1
|
|
my ( $class, %params ) = @_; |
15
|
|
|
|
|
|
|
|
16
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new(%params); |
17
|
|
|
|
|
|
|
|
18
|
0
|
0
|
|
|
|
|
if ( $params{debug} ) { |
19
|
0
|
|
|
|
|
|
$self->{debug} = 1; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $task = { |
23
|
|
|
|
|
|
|
name => "Runjobs", |
24
|
|
|
|
|
|
|
antivirus => $self->{agent}->{antivirus}->getJobj() |
25
|
0
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
$self->{jobj}->{task} = $task; |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
return $self; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _getStoredJobs { |
33
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
my $data = $self->{agent}->{armadito_storage}->restore( name => 'Armadito-Agent-Jobs' ); |
36
|
0
|
0
|
|
|
|
|
if ( defined( $data->{jobs} ) ) { |
37
|
0
|
|
|
|
|
|
foreach my $job ( @{ $data->{jobs} } ) { |
|
0
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
$self->{logger}->info( "Job " . $job->{job_id} . " - " . $job->{job_priority} ); |
39
|
|
|
|
|
|
|
} |
40
|
0
|
|
|
|
|
|
$self->{jobs} = $data->{jobs}; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
return $self; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub _rmJobFromStorage { |
47
|
0
|
|
|
0
|
|
|
my ( $self, $job_id ) = @_; |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
my $jobs = (); |
50
|
0
|
|
|
|
|
|
my $data = $self->{agent}->{armadito_storage}->restore( name => 'Armadito-Agent-Jobs' ); |
51
|
0
|
0
|
|
|
|
|
if ( defined( $data->{jobs} ) ) { |
52
|
0
|
|
|
|
|
|
foreach ( @{ $data->{jobs} } ) { |
|
0
|
|
|
|
|
|
|
53
|
0
|
0
|
|
|
|
|
push( @$jobs, $_ ) if $_->{job_id} ne $job_id; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
$self->{agent}->{armadito_storage}->save( |
58
|
0
|
|
|
|
|
|
name => 'Armadito-Agent-Jobs', |
59
|
|
|
|
|
|
|
data => { |
60
|
|
|
|
|
|
|
jobs => $jobs |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub _sortJobsByPriority { |
66
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
@{ $self->{jobs} } = reverse sort { $a->{job_priority} <=> $b->{job_priority} } @{ $self->{jobs} }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
return $self; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub _handleError { |
74
|
0
|
|
|
0
|
|
|
my ( $self, $response ) = @_; |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
$self->{logger}->info( "Error Response : " . $response->content() ); |
77
|
0
|
|
|
|
|
|
my $obj = from_json( $response->content(), { utf8 => 1 } ); |
78
|
0
|
|
|
|
|
|
$self->{logger}->error( Dumper($obj) ); |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
return $self; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub run { |
84
|
0
|
|
|
0
|
1
|
|
my ( $self, %params ) = @_; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
$self = $self->SUPER::run(%params); |
87
|
0
|
|
|
|
|
|
$self = $self->_getStoredJobs(); |
88
|
0
|
|
|
|
|
|
$self = $self->_sortJobsByPriority(); |
89
|
0
|
|
|
|
|
|
$self = $self->_runJobs(); |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
return $self; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub _runJob { |
95
|
0
|
|
|
0
|
|
|
my ( $self, $job ) = @_; |
96
|
0
|
|
|
|
|
|
my $config = (); |
97
|
0
|
|
|
|
|
|
my $start_time = time; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
try { |
100
|
0
|
|
|
0
|
|
|
my $antivirus = $self->{jobj}->{task}->{antivirus}->{name}; |
101
|
0
|
|
|
|
|
|
my $task = $job->{job_type}; |
102
|
0
|
|
|
|
|
|
my $class = "Armadito::Agent::Task::$task"; |
103
|
|
|
|
|
|
|
|
104
|
0
|
0
|
|
|
|
|
if ( $self->{agent}->isTaskSpecificToAV($task) ) { |
105
|
0
|
|
|
|
|
|
$class = "Armadito::Agent::Antivirus::" . $antivirus . "::Task::" . $task; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
$class->require(); |
109
|
0
|
|
|
|
|
|
my $taskclass = $class->new( agent => $self->{agent}, job => $job ); |
110
|
0
|
|
|
|
|
|
$taskclass->run(); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
catch { |
113
|
0
|
|
|
0
|
|
|
$self->{logger}->error($_); |
114
|
|
|
|
|
|
|
$self->{jobj}->{task}->{obj} = { |
115
|
|
|
|
|
|
|
code => 1, |
116
|
|
|
|
|
|
|
message => encode_base64($_), |
117
|
|
|
|
|
|
|
job_id => $job->{job_id}, |
118
|
0
|
|
|
|
|
|
start_time => $start_time, |
119
|
|
|
|
|
|
|
end_time => time |
120
|
|
|
|
|
|
|
}; |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
return $self; |
123
|
0
|
|
|
|
|
|
}; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
$self->{jobj}->{task}->{obj} = { |
126
|
|
|
|
|
|
|
code => 0, |
127
|
|
|
|
|
|
|
message => "runJob successful", |
128
|
|
|
|
|
|
|
job_id => $job->{job_id}, |
129
|
0
|
|
|
|
|
|
start_time => $start_time, |
130
|
|
|
|
|
|
|
end_time => time |
131
|
|
|
|
|
|
|
}; |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
return $self; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub _runJobs { |
137
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
foreach my $job ( @{ $self->{jobs} } ) { |
|
0
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
$self->_runJob($job); |
141
|
0
|
|
|
|
|
|
$self->_sendStatus(); |
142
|
0
|
|
|
|
|
|
$self->_rmJobFromStorage( $job->{job_id} ); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
return $self; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub _sendStatus { |
149
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
150
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
my $json_text = to_json( $self->{jobj} ); |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
my $response = $self->{glpi_client}->sendRequest( |
154
|
0
|
|
|
|
|
|
"url" => $self->{agent}->{config}->{server}[0] . "/api/jobs", |
155
|
|
|
|
|
|
|
message => $json_text, |
156
|
|
|
|
|
|
|
method => "POST" |
157
|
|
|
|
|
|
|
); |
158
|
|
|
|
|
|
|
|
159
|
0
|
0
|
|
|
|
|
if ( $response->is_success() ) { |
160
|
0
|
|
|
|
|
|
$self->{logger}->info("Runjobs sendStatus successful..."); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
else { |
163
|
0
|
|
|
|
|
|
$self->_handleError($response); |
164
|
0
|
|
|
|
|
|
$self->{logger}->info("Runjobs sendStatus failed..."); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
1; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
__END__ |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 NAME |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Armadito::Agent::Task::Runjobs - Runjobs Task base class. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 DESCRIPTION |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
This task inherits from L<Armadito::Agent::Task>. Run jobs and send results to /jobs REST API of Armadito Plugin for GLPI. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 FUNCTIONS |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 run ( $self, %params ) |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Run the task. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head2 new ( $self, %params ) |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Instanciate Task. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
|