func (c *meminfoCollector) getMemInfo() (map[string]float64, error) { info := make(map[string]float64) size, err := unix.SysctlUint32("vm.stats.vm.v_page_size") if err != nil { return nil, fmt.Errorf("sysctl(vm.stats.vm.v_page_size) failed: %s", err) } for key, v := range map[string]string{ "active": "vm.stats.vm.v_active_count", "inactive": "vm.stats.vm.v_inactive_count", "wire": "vm.stats.vm.v_wire_count", "cache": "vm.stats.vm.v_cache_count", "free": "vm.stats.vm.v_free_count", "swappgsin": "vm.stats.vm.v_swappgsin", "swappgsout": "vm.stats.vm.v_swappgsout", "total": "vm.stats.vm.v_page_count", } { value, err := unix.SysctlUint32(v) if err != nil { return nil, err } // Convert metrics to kB (same as Linux meminfo). info[key] = float64(value) * float64(size) } return info, nil }
// Expose CPU stats using sysctl. func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) { // We want time spent per-cpu per CPUSTATE. // CPUSTATES (number of CPUSTATES) is defined as 5U. // Order: CP_USER | CP_NICE | CP_SYS | CP_IDLE | CP_INTR // sysctl kern.cp_times provides hw.ncpu * CPUSTATES long integers: // hw.ncpu * (space-separated list of the above variables) // // Each value is a counter incremented at frequency // kern.clockrate.(stathz | hz) // // Look into sys/kern/kern_clock.c for details. cpuTimes, err := getCPUTimes() if err != nil { return err } for cpu, t := range cpuTimes { lcpu := strconv.Itoa(cpu) ch <- c.cpu.mustNewConstMetric(float64(t.user), lcpu, "user") ch <- c.cpu.mustNewConstMetric(float64(t.nice), lcpu, "nice") ch <- c.cpu.mustNewConstMetric(float64(t.sys), lcpu, "system") ch <- c.cpu.mustNewConstMetric(float64(t.intr), lcpu, "interrupt") ch <- c.cpu.mustNewConstMetric(float64(t.idle), lcpu, "idle") temp, err := unix.SysctlUint32(fmt.Sprintf("dev.cpu.%d.temperature", cpu)) if err != nil { if err == unix.ENOENT { // No temperature information for this CPU log.Debugf("no temperature information for CPU %d", cpu) } else { // Unexpected error ch <- c.temp.mustNewConstMetric(math.NaN(), lcpu) log.Errorf("failed to query CPU temperature for CPU %d: %s", cpu, err) } continue } ch <- c.temp.mustNewConstMetric(float64(temp-2732)/10, lcpu) } return err }