46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
// run executes a command and returns combined stdout+stderr as a string.
|
|
// Non-zero exit is NOT an error here: smartctl uses a bitmask exit code (e.g.
|
|
// bit 0 = command-line error, bits 3-7 = disk health flags) yet still prints
|
|
// valid JSON/text, and MegaCLI is similarly noisy. We want whatever it printed.
|
|
func run(name string, args ...string) string {
|
|
cmd := exec.Command(name, args...)
|
|
out, _ := cmd.CombinedOutput()
|
|
return string(out)
|
|
}
|
|
|
|
// lookPath returns the first existing executable from candidates, trying PATH
|
|
// first (via exec.LookPath) then absolute fallbacks. Returns "" if none found.
|
|
func lookPath(candidates ...string) string {
|
|
for _, c := range candidates {
|
|
if strings.ContainsRune(c, os.PathSeparator) {
|
|
if fi, err := os.Stat(c); err == nil && !fi.IsDir() {
|
|
return c
|
|
}
|
|
continue
|
|
}
|
|
if p, err := exec.LookPath(c); err == nil {
|
|
return p
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// hostname returns the host identity, preferring the FQDN from "hostname -f",
|
|
// then os.Hostname, then "unknown" so records always carry a host tag.
|
|
func hostname() string {
|
|
if h := strings.TrimSpace(run("hostname", "-f")); h != "" {
|
|
return h
|
|
}
|
|
if h, err := os.Hostname(); err == nil {
|
|
return h
|
|
}
|
|
return "unknown"
|
|
}
|