This repository has been archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logcfg.go
121 lines (102 loc) · 2.98 KB
/
logcfg.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
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
package syscfg
// This file contains tweaks for logging/journald, such as max size limits.
import (
"io/fs"
"os"
"os/exec"
"regexp"
errw "github.com/pkg/errors"
sysd "github.com/sergeymakinen/go-systemdconf/v2"
"github.com/sergeymakinen/go-systemdconf/v2/conf"
"go.uber.org/zap"
)
var (
journaldConfPath = "/etc/systemd/journald.conf.d/90-viam.conf"
defaultLogLimit = "512M"
)
type LogConfig struct {
Disable bool `json:"disable"`
SystemMaxUse string `json:"system_max_use"`
RuntimeMaxUse string `json:"runtime_max_use"`
}
func EnforceLogging(cfg LogConfig, log *zap.SugaredLogger) {
if cfg.Disable {
if err := os.Remove(journaldConfPath); err != nil {
if errw.Is(err, fs.ErrNotExist) {
return
}
log.Error(errw.Wrapf(err, "deleting %s", journaldConfPath))
return
}
if !checkJournaldEnabled(log) {
return
}
if err := restartJournald(); err != nil {
log.Error(err)
return
}
log.Infof("Logging config disabled. Removing customized %s", journaldConfPath)
return
}
if !checkJournaldEnabled(log) {
return
}
persistSize := cfg.SystemMaxUse
tempSize := cfg.RuntimeMaxUse
if persistSize == "" {
persistSize = defaultLogLimit
}
if tempSize == "" {
tempSize = defaultLogLimit
}
sizeRegEx := regexp.MustCompile(`^[0-9]+[KMGTPE]$`)
if !(sizeRegEx.MatchString(persistSize) && sizeRegEx.MatchString(tempSize)) {
log.Error(errw.New("logfile size limits must be specificed in bytes, with one optional suffix character [KMGTPE]"))
return
}
journalConf := &conf.JournaldFile{
Journal: conf.JournaldJournalSection{
SystemMaxUse: sysd.Value{persistSize},
RuntimeMaxUse: sysd.Value{tempSize},
},
}
newFileBytes, err := sysd.Marshal(journalConf)
if err != nil {
log.Error(errw.Wrapf(err, "marshaling new file for %s", journaldConfPath))
return
}
isNew, err := writeFileIfNew(journaldConfPath, newFileBytes)
if err != nil {
log.Error(err)
// We may have written a corrupt file, try to remove to salvage at least default behavior.
if err := os.RemoveAll(journaldConfPath); err != nil {
log.Error(errw.Wrapf(err, "deleting %s", journaldConfPath))
}
return
}
if isNew {
if err := restartJournald(); err != nil {
log.Error(err)
return
}
log.Infof("Updated %s, setting SystemMaxUse=%s and RuntimeMaxUse=%s", journaldConfPath, persistSize, tempSize)
}
}
func restartJournald() error {
cmd := exec.Command("systemctl", "restart", "systemd-journald")
output, err := cmd.CombinedOutput()
if err != nil {
return errw.Wrapf(err, "executing 'systemctl restart systemd-journald' %s", output)
}
return nil
}
func checkJournaldEnabled(log *zap.SugaredLogger) bool {
cmd := exec.Command("systemctl", "is-enabled", "systemd-journald")
output, err := cmd.CombinedOutput()
if err != nil {
log.Error(errw.Wrapf(err, "executing 'systemctl is-enabled systemd-journald' %s", output))
log.Error("agent-syscfg can only adjust logging settings for systems using systemd with journald enabled")
return false
}
return true
}