Skip to content

Commit 29f0cf3

Browse files
committed
TUN-8783: fix log collectors for the diagnostic procedure
## Summary * The host log collector now verifies if the OS is linux and has systemd if so it will use journalctl to get the logs * In linux systems docker will write the output of the command logs to the stderr therefore the function that handles the execution of the process will copy both the contents of stdout and stderr; this also affect the k8s collector Closes TUN-8783
1 parent e7dcb6e commit 29f0cf3

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

diagnostic/log_collector_host.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"os/exec"
78
"path/filepath"
89
"runtime"
910
)
1011

1112
const (
12-
linuxManagedLogsPath = "/var/log/cloudflared.err"
13-
darwinManagedLogsPath = "/Library/Logs/com.cloudflare.cloudflared.err.log"
13+
linuxManagedLogsPath = "/var/log/cloudflared.err"
14+
darwinManagedLogsPath = "/Library/Logs/com.cloudflare.cloudflared.err.log"
15+
linuxServiceConfigurationPath = "/etc/systemd/system/cloudflared.service"
16+
linuxSystemdPath = "/run/systemd/system"
1417
)
1518

1619
type HostLogCollector struct {
@@ -23,6 +26,28 @@ func NewHostLogCollector(client HTTPClient) *HostLogCollector {
2326
}
2427
}
2528

29+
func extractLogsFromJournalCtl(ctx context.Context) (*LogInformation, error) {
30+
tmp := os.TempDir()
31+
32+
outputHandle, err := os.Create(filepath.Join(tmp, logFilename))
33+
if err != nil {
34+
return nil, fmt.Errorf("error opening output file: %w", err)
35+
}
36+
37+
defer outputHandle.Close()
38+
39+
command := exec.CommandContext(
40+
ctx,
41+
"journalctl",
42+
"--since",
43+
"2 weeks ago",
44+
"-u",
45+
"cloudflared.service",
46+
)
47+
48+
return PipeCommandOutputToFile(command, outputHandle)
49+
}
50+
2651
func getServiceLogPath() (string, error) {
2752
switch runtime.GOOS {
2853
case "darwin":
@@ -55,6 +80,13 @@ func (collector *HostLogCollector) Collect(ctx context.Context) (*LogInformation
5580
}
5681

5782
if logConfiguration.uid == 0 {
83+
_, statSystemdErr := os.Stat(linuxServiceConfigurationPath)
84+
85+
_, statServiceConfigurationErr := os.Stat(linuxServiceConfigurationPath)
86+
if statSystemdErr == nil && statServiceConfigurationErr == nil && runtime.GOOS == "linux" {
87+
return extractLogsFromJournalCtl(ctx)
88+
}
89+
5890
path, err := getServiceLogPath()
5991
if err != nil {
6092
return nil, err

diagnostic/log_collector_utils.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ func PipeCommandOutputToFile(command *exec.Cmd, outputHandle *os.File) (*LogInfo
1212
stdoutReader, err := command.StdoutPipe()
1313
if err != nil {
1414
return nil, fmt.Errorf(
15-
"error retrieving output from command '%s': %w",
15+
"error retrieving stdout from command '%s': %w",
16+
command.String(),
17+
err,
18+
)
19+
}
20+
21+
stderrReader, err := command.StderrPipe()
22+
if err != nil {
23+
return nil, fmt.Errorf(
24+
"error retrieving stderr from command '%s': %w",
1625
command.String(),
1726
err,
1827
)
@@ -29,7 +38,17 @@ func PipeCommandOutputToFile(command *exec.Cmd, outputHandle *os.File) (*LogInfo
2938
_, err = io.Copy(outputHandle, stdoutReader)
3039
if err != nil {
3140
return nil, fmt.Errorf(
32-
"error copying output from %s to file %s: %w",
41+
"error copying stdout from %s to file %s: %w",
42+
command.String(),
43+
outputHandle.Name(),
44+
err,
45+
)
46+
}
47+
48+
_, err = io.Copy(outputHandle, stderrReader)
49+
if err != nil {
50+
return nil, fmt.Errorf(
51+
"error copying stderr from %s to file %s: %w",
3352
command.String(),
3453
outputHandle.Name(),
3554
err,

0 commit comments

Comments
 (0)