Enhance PID collection by adding namespace service support and extending timeout for systemd queries

This commit is contained in:
anonpenguin23 2026-02-19 10:24:49 +02:00
parent aa2da83969
commit 40600c3557

View File

@ -2,6 +2,9 @@ package report
import ( import (
"context" "context"
"fmt"
"os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -93,13 +96,49 @@ var managedServiceUnits = []string{
// collectManagedPIDs queries systemd for the MainPID of each known service. // collectManagedPIDs queries systemd for the MainPID of each known service.
// Returns a set of PIDs that are legitimately managed by systemd (not orphans). // Returns a set of PIDs that are legitimately managed by systemd (not orphans).
func collectManagedPIDs() map[int]bool { func collectManagedPIDs() map[int]bool {
// Hard deadline: stop querying if this takes too long (e.g., node with many namespaces).
deadline := time.Now().Add(10 * time.Second)
pids := make(map[int]bool) pids := make(map[int]bool)
// Collect PIDs from global services.
for _, unit := range managedServiceUnits { for _, unit := range managedServiceUnits {
addMainPID(pids, unit)
}
// Collect PIDs from namespace service instances.
// Scan the namespaces data directory (same pattern as GetProductionServices).
namespacesDir := "/opt/orama/.orama/data/namespaces"
nsEntries, err := os.ReadDir(namespacesDir)
if err == nil {
nsServiceTypes := []string{"rqlite", "olric", "gateway"}
for _, nsEntry := range nsEntries {
if !nsEntry.IsDir() {
continue
}
if time.Now().After(deadline) {
break
}
ns := nsEntry.Name()
for _, svcType := range nsServiceTypes {
envFile := filepath.Join(namespacesDir, ns, svcType+".env")
if _, err := os.Stat(envFile); err == nil {
unit := fmt.Sprintf("orama-namespace-%s@%s", svcType, ns)
addMainPID(pids, unit)
}
}
}
}
return pids
}
// addMainPID queries systemd for a unit's MainPID and adds it to the set.
func addMainPID(pids map[int]bool, unit string) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
out, err := runCmd(ctx, "systemctl", "show", unit, "--property=MainPID") out, err := runCmd(ctx, "systemctl", "show", unit, "--property=MainPID")
cancel() cancel()
if err != nil { if err != nil {
continue return
} }
props := parseProperties(out) props := parseProperties(out)
if pidStr, ok := props["MainPID"]; ok { if pidStr, ok := props["MainPID"]; ok {
@ -108,8 +147,6 @@ func collectManagedPIDs() map[int]bool {
} }
} }
} }
return pids
}
// isOramaProcess checks if a command string contains any orama-related process name. // isOramaProcess checks if a command string contains any orama-related process name.
func isOramaProcess(command string) bool { func isOramaProcess(command string) bool {