1
0
mirror of https://github.com/lxc/incus.git synced 2026-02-05 09:46:19 +01:00

incus-agent: Reduce code duplication

Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
This commit is contained in:
Stéphane Graber
2025-04-26 16:34:50 -04:00
parent d9458b78c1
commit 2c0baecffa
2 changed files with 3 additions and 45 deletions

View File

@@ -49,10 +49,7 @@ func metricsGet(d *Daemon, r *http.Request) response.Response {
out.Network = netStats
}
out.ProcessesTotal, err = osGetTotalProcesses(d)
if err != nil {
logger.Warn("Failed to get total processes", logger.Ctx{"err": err})
}
out.ProcessesTotal = uint64(osGetProcessesState())
cpuStats, err := osGetCPUMetrics(d)
if err != nil {

View File

@@ -245,45 +245,6 @@ func osGetCPUMetrics(d *Daemon) ([]metrics.CPUMetrics, error) {
return out, nil
}
func osGetTotalProcesses(d *Daemon) (uint64, error) {
entries, err := os.ReadDir("/proc")
if err != nil {
return 0, fmt.Errorf("Failed to read dir %q: %w", "/proc", err)
}
pidCount := uint64(0)
for _, entry := range entries {
// Skip everything which isn't a directory
if !entry.IsDir() {
continue
}
name := entry.Name()
// Skip all non-PID directories
_, err := strconv.ParseUint(name, 10, 64)
if err != nil {
continue
}
cmdlinePath := filepath.Join("/proc", name, "cmdline")
cmdline, err := os.ReadFile(cmdlinePath)
if err != nil {
continue
}
if string(cmdline) == "" {
continue
}
pidCount++
}
return pidCount, nil
}
func osGetDiskMetrics(d *Daemon) ([]metrics.DiskMetrics, error) {
diskStats, err := os.ReadFile("/proc/diskstats")
if err != nil {
@@ -637,12 +598,12 @@ func osGetNetworkState() map[string]api.InstanceStateNetwork {
func osGetProcessesState() int64 {
pids := []int64{1}
// Go through the pid list, adding new pids at the end so we go through them all
// Go through the pid list, adding new pids at the end so we go through them all.
for i := 0; i < len(pids); i++ {
fname := fmt.Sprintf("/proc/%d/task/%d/children", pids[i], pids[i])
fcont, err := os.ReadFile(fname)
if err != nil {
// the process terminated during execution of this loop
// The process terminated during execution of this loop.
continue
}