This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
sysctl.go
90 lines (76 loc) · 2.43 KB
/
sysctl.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
Copyright 2017 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package monitoring
import (
"context"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"github.com/gravitational/satellite/agent/health"
pb "github.com/gravitational/satellite/agent/proto/agentpb"
"github.com/gravitational/trace"
)
// SysctlCHecker verifies various /proc filesystem runtime parameters
type SysctlChecker struct {
// Name is checker name
CheckerName string
// Param is parameter name
Param string
// Expected is expected parameter value
Expected string
// OnMissing is description when parameter is missing
OnMissing string
// OnValueMismatch is description when parameter value is not equal to expected
OnValueMismatch string
}
// Name returns name of checker
func (c *SysctlChecker) Name() string {
return c.CheckerName
}
// Check will verify the parameter value is as expected or complain otherwise
func (c *SysctlChecker) Check(ctx context.Context, reporter health.Reporter) {
value, err := Sysctl(c.Param)
if err == nil && value == c.Expected {
reporter.Add(&pb.Probe{
Checker: c.CheckerName,
Status: pb.Probe_Running,
})
return
}
if err == nil && value != c.Expected {
reporter.Add(&pb.Probe{
Checker: c.CheckerName,
Detail: c.OnValueMismatch,
Status: pb.Probe_Failed})
return
}
if trace.IsNotFound(err) {
reporter.Add(NewProbeFromErr(c.CheckerName, c.OnMissing, trace.Wrap(err)))
return
}
reporter.Add(NewProbeFromErr(c.CheckerName,
fmt.Sprintf("failed to query sysctl parameter %s", c.Param), trace.Wrap(err)))
}
// Sysctl returns kernel parameter by reading proc/sys
func Sysctl(name string) (string, error) {
path := filepath.Clean(filepath.Join("/proc", "sys", strings.Replace(name, ".", "/", -1)))
data, err := ioutil.ReadFile(path)
if err != nil {
return "", trace.ConvertSystemError(err)
}
if len(data) == 0 {
return "", trace.BadParameter("empty output from sysctl")
}
return string(data[:len(data)-1]), nil
}