Expose CPU stats using sysctl.
(ch chan<- prometheus.Metric)
| 101 | |
| 102 | // Expose CPU stats using sysctl. |
| 103 | func (c *statCollector) Update(ch chan<- prometheus.Metric) error { |
| 104 | // We want time spent per-cpu per CPUSTATE. |
| 105 | // CPUSTATES (number of CPUSTATES) is defined as 5U. |
| 106 | // Order: CP_USER | CP_NICE | CP_SYS | CP_IDLE | CP_INTR |
| 107 | // sysctl kern.cp_times provides hw.ncpu * CPUSTATES long integers: |
| 108 | // hw.ncpu * (space-separated list of the above variables) |
| 109 | // |
| 110 | // Each value is a counter incremented at frequency |
| 111 | // kern.clockrate.(stathz | hz) |
| 112 | // |
| 113 | // Look into sys/kern/kern_clock.c for details. |
| 114 | |
| 115 | cpuTimes, err := getCPUTimes() |
| 116 | if err != nil { |
| 117 | return err |
| 118 | } |
| 119 | for cpu, t := range cpuTimes { |
| 120 | lcpu := strconv.Itoa(cpu) |
| 121 | ch <- c.cpu.mustNewConstMetric(float64(t.user), lcpu, "user") |
| 122 | ch <- c.cpu.mustNewConstMetric(float64(t.nice), lcpu, "nice") |
| 123 | ch <- c.cpu.mustNewConstMetric(float64(t.sys), lcpu, "system") |
| 124 | ch <- c.cpu.mustNewConstMetric(float64(t.intr), lcpu, "interrupt") |
| 125 | ch <- c.cpu.mustNewConstMetric(float64(t.idle), lcpu, "idle") |
| 126 | |
| 127 | temp, err := unix.SysctlUint32(fmt.Sprintf("dev.cpu.%d.temperature", cpu)) |
| 128 | if err != nil { |
| 129 | if err == unix.ENOENT { |
| 130 | // No temperature information for this CPU |
| 131 | log.Debugf("no temperature information for CPU %d", cpu) |
| 132 | } else { |
| 133 | // Unexpected error |
| 134 | ch <- c.temp.mustNewConstMetric(math.NaN(), lcpu) |
| 135 | log.Errorf("failed to query CPU temperature for CPU %d: %s", cpu, err) |
| 136 | } |
| 137 | continue |
| 138 | } |
| 139 | |
| 140 | // Temp is a signed integer in deci-degrees Kelvin. |
| 141 | // Cast uint32 to int32 and convert to float64 degrees Celsius. |
| 142 | // |
| 143 | // 2732 is used as the conversion constant for deci-degrees |
| 144 | // Kelvin, in multiple places in the kernel that feed into this |
| 145 | // sysctl, so we want to maintain consistency: |
| 146 | // |
| 147 | // sys/dev/amdtemp/amdtemp.c |
| 148 | // #define AMDTEMP_ZERO_C_TO_K 2732 |
| 149 | // |
| 150 | // sys/dev/acpica/acpi_thermal.c |
| 151 | // #define TZ_ZEROC 2732 |
| 152 | // |
| 153 | // sys/dev/coretemp/coretemp.c |
| 154 | // #define TZ_ZEROC 2732 |
| 155 | ch <- c.temp.mustNewConstMetric(float64(int32(temp)-2732)/10, lcpu) |
| 156 | } |
| 157 | ch <- prometheus.MustNewConstMetric(nodeCPUCountDesc, prometheus.GaugeValue, float64(runtime.NumCPU())) |
| 158 | |
| 159 | return err |
| 160 | } |
nothing calls this directly
no test coverage detected