mirror of
https://github.com/jetkvm/kvm.git
synced 2026-05-21 05:20:35 +00:00
d84ff1b12a
* Enhance diagnostics logging with context-aware timeouts * Add full system diagnostics logging on supervisor crash * Clear diagnostics context after logging to prevent memory leaks * remove pr.txt
82 lines
2.3 KiB
Go
82 lines
2.3 KiB
Go
package diagnostics
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const defaultCmdTimeout = 2 * time.Second
|
|
|
|
// runCmdLog runs a command and logs its output.
|
|
func (d *Diagnostics) runCmdLog(label string, cmd string, args ...string) {
|
|
parentCtx := d.ctx
|
|
if parentCtx == nil {
|
|
parentCtx = context.Background()
|
|
}
|
|
ctx, cancel := context.WithTimeout(parentCtx, defaultCmdTimeout)
|
|
defer cancel()
|
|
|
|
output, err := exec.CommandContext(ctx, cmd, args...).CombinedOutput()
|
|
if err != nil {
|
|
d.logger.Warn().Str("cmd", label).Err(err).
|
|
Str("output", strings.TrimSpace(string(output))).Msg("command failed")
|
|
return
|
|
}
|
|
d.logger.Info().Str("output", strings.TrimSpace(string(output))).Msg(label)
|
|
}
|
|
|
|
// runShellLog runs a shell command (for pipelines) and logs its output.
|
|
func (d *Diagnostics) runShellLog(label, script string) {
|
|
parentCtx := d.ctx
|
|
if parentCtx == nil {
|
|
parentCtx = context.Background()
|
|
}
|
|
ctx, cancel := context.WithTimeout(parentCtx, defaultCmdTimeout)
|
|
defer cancel()
|
|
|
|
output, err := exec.CommandContext(ctx, "sh", "-c", script).CombinedOutput()
|
|
if err != nil {
|
|
d.logger.Warn().Str("cmd", label).Err(err).
|
|
Str("output", strings.TrimSpace(string(output))).Msg("shell command failed")
|
|
return
|
|
}
|
|
d.logger.Info().Str("output", strings.TrimSpace(string(output))).Msg(label)
|
|
}
|
|
|
|
// readFileLog reads a file and logs its content.
|
|
func (d *Diagnostics) readFileLog(label, path string) {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
d.logger.Warn().Str("path", path).Err(err).Msg(label + " NOT found")
|
|
return
|
|
}
|
|
d.logger.Info().Str("content", strings.TrimSpace(string(data))).Msg(label)
|
|
}
|
|
|
|
// checkFileLog checks if a file/device exists and logs its status.
|
|
func (d *Diagnostics) checkFileLog(label, path string) {
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
d.logger.Warn().Str("path", path).Err(err).Msg(label + " NOT found")
|
|
return
|
|
}
|
|
d.logger.Info().Str("path", path).Str("mode", info.Mode().String()).Msg(label + " exists")
|
|
}
|
|
|
|
// listDirLog lists a directory and logs its contents.
|
|
func (d *Diagnostics) listDirLog(label, path string) {
|
|
entries, err := os.ReadDir(path)
|
|
if err != nil {
|
|
d.logger.Warn().Str("path", path).Err(err).Msg(label + " NOT found")
|
|
return
|
|
}
|
|
var names []string
|
|
for _, entry := range entries {
|
|
names = append(names, entry.Name())
|
|
}
|
|
d.logger.Info().Strs("entries", names).Msg(label)
|
|
}
|