prometheus/node_exporter · error

Invalid cpu number

Error message

Invalid cpu number

What it means

The NetBSD CPU collector (cpu_netbsd.go) reads the number of CPUs via sysctl kern.ncpu; if ncpus is less than 1 it cannot iterate per-CPU kern.cp_time counters, so getCPUTimes returns errors.New("Invalid cpu number") and CPU metrics are skipped.

Solutions

  1. Check 'sysctl -n kern.ncpu' on the host returns >= 1; fix the guest/kernel config if it reports 0
  2. Confirm node_exporter was built for your NetBSD release; older/newer kernels may change sysctl binary layout
  3. Upgrade node_exporter to a version matching your NetBSD version's golang.org/x/sys unix wrappers
  4. Disable the cpu collector on affected hosts as a workaround
Defensive patterns

Strategy: validation

Validate before calling

// shell pre-flight on the host: kern.ncpu must be >= 1
[ "$(sysctl -n kern.ncpu 2>/dev/null || echo 0)" -ge 1 ] || echo "kern.ncpu invalid; cpu collector will fail"

Prevention

When it happens

Trigger: getCPUTimes on NetBSD reads kern.ncpu via unix.SysctlRaw and gets a value < 1 (0 or a garbage/byte-order-garbled value), before looping 'kern.cp_time' for each cpu index.

Common situations: Virtualized/containerized NetBSD guests reporting ncpu=0; kernel versions where the sysctl layout differs so the raw uint32 read is misinterpreted; unusual jail/chroot sysctl restrictions.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at collector/cpu_netbsd.go:187

		return nil, err
	}
	clock := *(*clockinfo)(unsafe.Pointer(&clockb[0]))

	var cpufreq float64
	if clock.stathz > 0 {
		cpufreq = float64(clock.stathz)
	} else {
		cpufreq = float64(clock.hz)
	}

	ncpusb, err := unix.SysctlRaw("hw.ncpu")
	if err != nil {
		return nil, err
	}
	ncpus := int(*(*uint32)(unsafe.Pointer(&ncpusb[0])))

	if ncpus < 1 {
		return nil, errors.New("Invalid cpu number")
	}

	var times []float64
	for ncpu := 0; ncpu < ncpus; ncpu++ {
		cpb, err := unix.SysctlRaw("kern.cp_time", ncpu)
		if err != nil {
			return nil, err
		}
		for len(cpb) >= int(unsafe.Sizeof(uint64(0))) {
			t := *(*uint64)(unsafe.Pointer(&cpb[0]))
			times = append(times, float64(t)/cpufreq)
			cpb = cpb[unsafe.Sizeof(uint64(0)):]
		}
	}

	cpus := make([]cputime, len(times)/states)
	for i := 0; i < len(times); i += states {
		cpu := &cpus[i/states]

View on GitHub (pinned to 17ddd77c59)