prometheus/node_exporter · error

could not retrieve CPU times

Error message

could not retrieve CPU times

What it means

In the DragonFly BSD CPU collector (cpu_dragonfly.go), getDragonFlyCPUTimes calls the cgo helper C.getCPUTimes; when that helper returns -1 the collector has no way to read per-CPU time counters from the kernel, so it returns errors.New("could not retrieve CPU times") and CPU metrics are skipped for that scrape.

Solutions

  1. Verify 'sysctl kern.cp_time' works on the host; if it fails, fix kernel/sysctl permissions there first
  2. Rebuild node_exporter for your DragonFly version — the cgo helper may target an older ABI
  3. Check host memory pressure; the helper's internal allocation can fail under memory exhaustion
  4. Disable the cpu collector if the platform cannot provide the data
Defensive patterns

Strategy: try-catch

Try / catch

if err := c.Update(ch); err != nil {
    if strings.Contains(err.Error(), "could not retrieve CPU times") {
        c.logger.Warn("CPU times unavailable on this host", "err", err)
        return nil // degrade gracefully
    }
    return err
}

Prevention

When it happens

Trigger: C.getCPUTimes(&cpuTimesC, &cpuTimesLength) == -1 — the underlying sysctl (kern.cp_time via cgo) failed inside the helper, e.g. sysctl unavailable, permission issue, or memory allocation failure in the C helper on DragonFly BSD.

Common situations: Running node_exporter on DragonFly BSD where the kern.cp_time sysctl is restricted or the kernel lacks expected CPU statistics; resource exhaustion preventing the C helper from allocating its output buffer.

Related errors


AI-assisted analysis of prometheus/node_exporter@17ddd77c59 (2026-09-07). Data as JSON: /api/errors/66aeb237a7e9f0e7. Report an issue: GitHub.

Appendix: source

Thrown at collector/cpu_dragonfly.go:110

	}, nil
}

func getDragonFlyCPUTimes() ([]float64, error) {
	// We want time spent per-CPU per CPUSTATE.
	// CPUSTATES (number of CPUSTATES) is defined as 5U.
	// States: CP_USER | CP_NICE | CP_SYS | CP_IDLE | CP_INTR
	//
	// Each value is in microseconds
	//
	// Look into sys/kern/kern_clock.c for details.

	var (
		cpuTimesC      *C.uint64_t
		cpuTimesLength C.size_t
	)

	if C.getCPUTimes(&cpuTimesC, &cpuTimesLength) == -1 {
		return nil, errors.New("could not retrieve CPU times")
	}
	defer C.free(unsafe.Pointer(cpuTimesC))

	cput := (*[maxCPUTimesLen]C.uint64_t)(unsafe.Pointer(cpuTimesC))[:cpuTimesLength:cpuTimesLength]

	cpuTimes := make([]float64, cpuTimesLength)
	for i, value := range cput {
		cpuTimes[i] = float64(value) / float64(1000000)
	}
	return cpuTimes, nil
}

// Expose CPU stats using sysctl.
func (c *statCollector) Update(ch chan<- prometheus.Metric) error {
	var fieldsCount = 5
	cpuTimes, err := getDragonFlyCPUTimes()
	if err != nil {
		return err

View on GitHub (pinned to 17ddd77c59)