hikan.ru/tools/logs.go

30 lines
526 B
Go

package tools
import (
"bytes"
"fmt"
"os/exec"
"runtime"
)
func GetJournalctlLogs(serviceName, grepPattern string, hoursAgo int) ([]byte, error) {
if runtime.GOOS != "linux" {
return nil, fmt.Errorf("not a linux")
}
cmd := exec.Command("sh", "-c",
fmt.Sprintf("journalctl -u %s --since '%d hours ago' | grep -c '%s'",
serviceName, hoursAgo, grepPattern))
var output bytes.Buffer
cmd.Stdout = &output
err := cmd.Run()
if err != nil {
return nil, err
}
return bytes.TrimSpace(output.Bytes()), nil
}