/
close.go
72 lines (61 loc) · 1.7 KB
/
close.go
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
// Copyright 2023 Adam Chalkley
//
// https://github.com/atc0005/check-ssh
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.
package main
import (
"errors"
"fmt"
"io"
"github.com/atc0005/check-ssh/internal/config"
"github.com/atc0005/go-nagios"
"golang.org/x/crypto/ssh"
)
// closeClient is responsible for closing the given client connection or
// logging the error and adding it to the accumulated error collection for
// later processing.
func closeClient(client *ssh.Client, cfg *config.Config, plugin *nagios.Plugin) {
logger := cfg.Log.With().Logger()
if closeErr := client.Close(); closeErr != nil {
if !errors.Is(closeErr, io.EOF) {
logger.Error().Err(closeErr).Msgf(
"failed to close client connection to %s",
cfg.Server,
)
plugin.AddError(fmt.Errorf(
"failed to close client connection to %s: %w",
cfg.Server,
closeErr,
))
}
}
logger.Debug().Msgf(
"Successfully closed client connection to %s",
cfg.Server,
)
}
// closeSession is responsible for closing the given session or logging the
// error and adding it to the accumulated error collection for later
// processing.
func closeSession(session *ssh.Session, cfg *config.Config, plugin *nagios.Plugin) {
logger := cfg.Log.With().Logger()
if closeErr := session.Close(); closeErr != nil {
if !errors.Is(closeErr, io.EOF) {
logger.Error().Err(closeErr).Msgf(
"failed to close connection to remote shell on %s",
cfg.Server,
)
plugin.AddError(fmt.Errorf(
"failed to close connection to remote shell on %s: %w",
cfg.Server,
closeErr,
))
}
}
logger.Debug().Msgf(
"Successfully closed remote shell on %s",
cfg.Server,
)
}