Skip to content

Commit 531464b

Browse files
committed
fix: Updated RemoteryInventory SSH debug & error messages
They are now prefixed to know if we are in libssh2 or ssh context when specific
1 parent 5d2a17c commit 531464b

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

Changes

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ inventory:
3434
remoteinventory:
3535
* Limit the number of attempts and reported errors when libssh2 fails but ssh command
3636
works. Libssh2 attempts will be disabled for a minute on failed attempt.
37+
* Clarify debug, warning and error messages
3738

3839
netdiscovery/netinventory:
3940
* Keep device mac address found via snmp during netdiscovery as this is the one

lib/GLPI/Agent/Task/RemoteInventory/Remote/Ssh.pm

+31-31
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ sub disconnect {
3434
if ($self->{_ssh2}) {
3535
$self->{_ssh2}->disconnect() if $self->{_ssh2}->sock;
3636
delete $self->{_ssh2};
37-
$self->{logger}->debug2("Disconnected from '".$self->host()."' remote host...");
37+
$self->{logger}->debug2("[libssh2] Disconnected from '".$self->host()."' remote host...");
3838
}
3939

4040
# Cleanup cache
@@ -73,18 +73,18 @@ sub _connect {
7373
my $host = $self->host();
7474
my $port = $self->port();
7575
my $remote = $host . ($port && $port == 22 ? "" : ":$port");
76-
$self->{logger}->debug2("Connecting to '$remote' remote host...");
76+
$self->{logger}->debug2("[libssh2] Connecting to '$remote' remote host...");
7777
if (!$ssh2->connect($host, $port // 22)) {
7878
my @error = $ssh2->error;
79-
$self->{logger}->debug("Can't reach $remote for ssh remoteinventory via libssh2: @error");
79+
$self->{logger}->debug("[libssh2] Can't reach $remote for ssh remoteinventory: @error");
8080
undef $self->{_ssh2};
8181
# Don't retry to connect with libssh2 before a minute
8282
$self->{_ssh2_dont_retry_before} = time + 60;
8383
return 0;
8484
}
8585

8686
# Use Trust On First Use policy to verify remote host
87-
$self->{logger}->debug2("Check remote host key...");
87+
$self->{logger}->debug2("[libssh2] Check remote host key...");
8888
if ($OSNAME eq 'MSWin32') {
8989
# On windows, use vardir as HOME to store known_hosts file
9090
my $home = $self->config->{vardir};
@@ -122,23 +122,23 @@ sub _connect {
122122

123123
# Support authentication by password
124124
if ($self->pass()) {
125-
$self->{logger}->debug2("Try authentication by password...");
125+
$self->{logger}->debug2("[libssh2] Trying password authentication...");
126126
my $user = $self->user();
127127
unless ($user) {
128128
if ($ENV{USER}) {
129129
$user = $ENV{USER};
130-
$self->{logger}->debug2("Trying '$user' as login");
130+
$self->{logger}->debug2("[libssh2] Trying '$user' as login");
131131
} else {
132-
$self->{logger}->error("No user given for password authentication");
132+
$self->{logger}->error("[libssh2] No user given for password authentication");
133133
}
134134
}
135135
if ($user) {
136136
unless ($ssh2->auth_password($user, $self->pass())) {
137137
my @error = $ssh2->error;
138-
$self->{logger}->debug("Can't authenticate to $remote with given password for ssh remoteinventory: @error");
138+
$self->{logger}->debug("[libssh2] Can't authenticate to $remote with given password for ssh remoteinventory: @error");
139139
}
140140
if ($ssh2->auth_ok) {
141-
$self->{logger}->debug2("Authenticated on $remote remote with given password");
141+
$self->{logger}->debug2("[libssh2] Authenticated on $remote remote with given password");
142142
$self->user($user);
143143
return 1;
144144
}
@@ -159,21 +159,21 @@ sub _connect {
159159
$self->{_private_keys_lastscan} = time;
160160
}
161161

162-
# Support public key athentication
162+
# Support public key authentication
163163
my $user = $self->user() // $ENV{USER};
164164
foreach my $private (sort(keys(%{$self->{_private_keys}}))) {
165-
$self->{logger}->debug2("Try authentication using $private key...");
165+
$self->{logger}->debug2("[libssh2] Trying publickey authentication using $private key...");
166166
my $file = $self->{_private_keys}->{$private};
167167
my $pubkey;
168168
$pubkey = $file.".pub" if -e $file.".pub";
169169
next unless $ssh2->auth_publickey($user, $pubkey, $file, $self->pass());
170170
if ($ssh2->auth_ok) {
171-
$self->{logger}->debug2("Authenticated on $remote remote with $private key");
171+
$self->{logger}->debug2("[libssh2] Authenticated on $remote remote with $private key");
172172
return 1;
173173
}
174174
}
175175

176-
$self->{logger}->error("Can't authenticate on $remote remote host via libssh2");
176+
$self->{logger}->error("[libssh2] Can't authenticate on $remote remote host");
177177
undef $self->{_ssh2};
178178

179179
# Don't retry libssh2 before a minute
@@ -196,7 +196,7 @@ sub _ssh2_exec_status {
196196
$ret = $chan->exit_status();
197197
$chan->close;
198198
} else {
199-
$self->{logger}->debug2("Failed to start '$command' using ssh2 lib");
199+
$self->{logger}->debug2("[libssh2] Failed to start '$command'");
200200
}
201201

202202
return $ret;
@@ -223,7 +223,7 @@ sub checking_error {
223223
my ($self) = @_;
224224

225225
my $libssh2 = $self->_connect();
226-
return "Can't run simple command on remote via libssh2, check server is up and ssh access is setup"
226+
return "[libssh2] Can't run simple command on remote, check server is up and ssh access is setup"
227227
if $self->mode('libssh2') && !$self->mode('ssh') && !$libssh2;
228228

229229
my $root = $self->getRemoteFirstLine(command => "id -u");
@@ -252,18 +252,18 @@ sub checking_error {
252252
my $ret = $self->_ssh2_exec_status($command);
253253
if (defined($ret)) {
254254
if ($ret) {
255-
$self->{logger}->warning("Failed to store deviceid using ssh2");
255+
$self->{logger}->warning("[libssh2] Failed to store deviceid");
256256
} else {
257257
return '';
258258
}
259259
}
260260

261261
# Don't try ssh command if mode has been set to libssh2 only
262-
return "Failed to store deviceid on remote with libssh2"
262+
return "[libssh2] Failed to store deviceid on remote"
263263
if $self->mode('libssh2') && !$self->mode('ssh');
264264

265265
system($self->_ssh(), "sh", "-c", "'$command'") == 0
266-
or return "Can't store deviceid on remote";
266+
or return "[ssh] Can't store deviceid on remote";
267267
}
268268

269269
return '';
@@ -280,19 +280,19 @@ sub getRemoteFileHandle {
280280
$self->_connect();
281281
my $sftp = $self->{_ssh2}->sftp();
282282
if ($sftp) {
283-
$self->{logger}->debug2("Trying to read '$params{file}' via sftp subsystem");
283+
$self->{logger}->debug2("[libssh2] Trying to read '$params{file}' via sftp subsystem");
284284
my $fh = $sftp->open($params{file});
285285
return $fh if $fh;
286286
my @error = $sftp->error;
287287
if (@error && $error[0]) {
288288
if ($error[0] == 2) { # SSH_FX_NO_SUCH_FILE
289-
$self->{logger}->debug2("'$params{file}' file not found");
289+
$self->{logger}->debug2("[libssh2] '$params{file}' file not found");
290290
return;
291291
} elsif ($error[0] == 3) { # SSH_FX_PERMISSION_DENIED
292-
$self->{logger}->debug2("Not authorized to read '$params{file}'");
292+
$self->{logger}->debug2("[libssh2] Not authorized to read '$params{file}'");
293293
return;
294294
} else {
295-
$self->{logger}->debug2("Unsupported SFTP error (@error)");
295+
$self->{logger}->debug2("[libssh2] Unsupported SFTP error (@error)");
296296
}
297297
}
298298

@@ -317,7 +317,7 @@ sub getRemoteFileHandle {
317317
my $chan = $self->{_ssh2}->channel();
318318
if ($chan) {
319319
$chan->ext_data('ignore');
320-
$self->{logger}->debug2("Running \"$command\"...");
320+
$self->{logger}->debug2("[libssh2] Running \"$command\"...");
321321
if ($chan->exec("LANG=C $command")) {
322322
return $chan;
323323
}
@@ -326,7 +326,7 @@ sub getRemoteFileHandle {
326326

327327
# Don't try ssh command if mode has been set to libssh2 only
328328
if ($self->mode('libssh2') && !$self->mode('ssh')) {
329-
$self->{logger}->debug("Failed to run \"$command\" in libssh2 mode only");
329+
$self->{logger}->debug("[libssh2] Failed to run \"$command\" in libssh2 mode only");
330330
return;
331331
}
332332

@@ -447,24 +447,24 @@ sub remoteTestFile {
447447
my $sftp = $self->{_ssh2}->sftp();
448448
if ($sftp) {
449449
if ($filetest && $filetest eq "r") {
450-
$self->{logger}->debug2("Trying to stat if '$file' is readable via sftp subsystem");
450+
$self->{logger}->debug2("[libssh2] Trying to stat if '$file' is readable via sftp subsystem");
451451
my $fh = $sftp->open($file);
452452
return 0 unless $fh;
453453
close($fh);
454454
return 1;
455455
}
456-
$self->{logger}->debug2("Trying to stat '$file' via sftp subsystem");
456+
$self->{logger}->debug2("[libssh2] Trying to stat '$file' via sftp subsystem");
457457
my $stat = $sftp->stat($file);
458458
return 1 if defined($stat);
459459
my @error = $sftp->error;
460460
if (@error && $error[0]) {
461461
if ($error[0] == 2) { # SSH_FX_NO_SUCH_FILE
462462
return 0;
463463
} elsif ($error[0] == 3) { # SSH_FX_PERMISSION_DENIED
464-
$self->{logger}->debug2("Not authorized to access '$file'");
464+
$self->{logger}->debug2("[libssh2] Not authorized to access '$file'");
465465
return 0;
466466
} else {
467-
$self->{logger}->debug2("Unsupported SFTP error (@error)");
467+
$self->{logger}->debug2("[libssh2] Unsupported SFTP error (@error)");
468468
}
469469
}
470470

@@ -514,7 +514,7 @@ sub remoteFileStat {
514514
$self->_connect();
515515
my $sftp = $self->{_ssh2}->sftp();
516516
if ($sftp) {
517-
$self->{logger}->debug2("Trying to stat '$file' via sftp subsystem");
517+
$self->{logger}->debug2("[libssh2] Trying to stat '$file' via sftp subsystem");
518518
my $stat = $sftp->stat($file);
519519
if (ref($stat) eq 'HASH') {
520520
return (
@@ -537,10 +537,10 @@ sub remoteFileStat {
537537
if ($error[0] == 2) { # SSH_FX_NO_SUCH_FILE
538538
return;
539539
} elsif ($error[0] == 3) { # SSH_FX_PERMISSION_DENIED
540-
$self->{logger}->debug2("Not authorized to access '$file'");
540+
$self->{logger}->debug2("[libssh2] Not authorized to access '$file'");
541541
return;
542542
} else {
543-
$self->{logger}->debug2("Unsupported SFTP error (@error)");
543+
$self->{logger}->debug2("[libssh2] Unsupported SFTP error (@error)");
544544
}
545545
}
546546

0 commit comments

Comments
 (0)