-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NPM-3666] Add dmesg to agent flare #32559
base: main
Are you sure you want to change the base?
Changes from 1 commit
ac2a0b2
4f8bd8f
8ca4c85
2359779
0da00ab
f63a52e
a9daf3f
f1ca4a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,14 +9,80 @@ | |||||
package debug | ||||||
|
||||||
import ( | ||||||
"bytes" | ||||||
"context" | ||||||
"errors" | ||||||
"fmt" | ||||||
"io" | ||||||
"net/http" | ||||||
"os/exec" | ||||||
"regexp" | ||||||
"syscall" | ||||||
"time" | ||||||
) | ||||||
|
||||||
var klogRegexp = regexp.MustCompile(`<(\d+)>(.*)`) | ||||||
|
||||||
func readAllDmesg() ([]byte, error) { | ||||||
const syslogActionSizeBuffer = 10 | ||||||
const syslogActionReadAll = 3 | ||||||
|
||||||
n, err := syscall.Klogctl(syslogActionSizeBuffer, nil) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if err != nil { | ||||||
return nil, fmt.Errorf("failed to query size of log buffer [%w]", err) | ||||||
} | ||||||
|
||||||
b := make([]byte, n) | ||||||
|
||||||
m, err := syscall.Klogctl(syslogActionReadAll, b) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if err != nil { | ||||||
return nil, fmt.Errorf("failed to read messages from log buffer [%w]", err) | ||||||
} | ||||||
|
||||||
return b[:m], nil | ||||||
} | ||||||
|
||||||
func parseDmesg(buffer []byte) (string, error) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
buf := bytes.NewBuffer(buffer) | ||||||
var result string | ||||||
|
||||||
for { | ||||||
line, err := buf.ReadString('\n') | ||||||
if err == io.EOF { | ||||||
break | ||||||
} else if err != nil { | ||||||
return result, err | ||||||
} | ||||||
|
||||||
parts := klogRegexp.FindStringSubmatch(line) | ||||||
if parts != nil { | ||||||
result += parts[2] + "\n" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason we are discarding the log level? It seems like it could be helpful in finding important messages. Perhaps we should translate from the number to the text version of the level and prefix the message? |
||||||
} else { | ||||||
result += line | ||||||
} | ||||||
} | ||||||
|
||||||
return result, nil | ||||||
} | ||||||
|
||||||
// HandleDmesg writes linux dmesg into the HTTP response. | ||||||
func HandleDmesg(w http.ResponseWriter, _ *http.Request) { | ||||||
dmesg, err := readAllDmesg() | ||||||
if err != nil { | ||||||
w.WriteHeader(500) | ||||||
fmt.Fprintf(w, "failed to read dmesg: %s", err) | ||||||
return | ||||||
} | ||||||
dmesgStr, err := parseDmesg(dmesg) | ||||||
if err != nil { | ||||||
w.WriteHeader(500) | ||||||
fmt.Fprintf(w, "failed to parse dmesg: %s", err) | ||||||
return | ||||||
} | ||||||
|
||||||
io.WriteString(w, dmesgStr) | ||||||
} | ||||||
|
||||||
// handleCommand runs commandName with the provided arguments and writes it to the HTTP response. | ||||||
// If the command exits with a failure or doesn't exist in the PATH, it will still 200 but report the failure. | ||||||
// Any other kind of error will 500. | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,7 +54,7 @@ func createSecurityAgentArchive(fb flaretypes.FlareBuilder, logFilePath string, | |
fb.AddFileFromFunc("envvars.log", getEnvVars) //nolint:errcheck | ||
linuxKernelSymbols(fb) //nolint:errcheck | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did we need to refactor this function? |
||
getLinuxPid1MountInfo(fb) //nolint:errcheck | ||
getLinuxDmesg(fb) //nolint:errcheck | ||
fb.AddFileFromFunc("dmesg", getLinuxDmesg) //nolint:errcheck | ||
getLinuxKprobeEvents(fb) //nolint:errcheck | ||
getLinuxTracingAvailableEvents(fb) //nolint:errcheck | ||
getLinuxTracingAvailableFilterFunctions(fb) //nolint:errcheck | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
readAllDmesg()
is relocated fromflare/archive_linux.go