updateStat reads /proc/stat through procfs and exports cpu related metrics.
(ch chan<- prometheus.Metric)
| 189 | |
| 190 | // updateStat reads /proc/stat through procfs and exports cpu related metrics. |
| 191 | func (c *cpuCollector) updateStat(ch chan<- prometheus.Metric) error { |
| 192 | fs, err := procfs.NewFS("/proc") |
| 193 | if err != nil { |
| 194 | return fmt.Errorf("failed to open procfs: %v", err) |
| 195 | } |
| 196 | stats, err := fs.NewStat() |
| 197 | if err != nil { |
| 198 | return err |
| 199 | } |
| 200 | |
| 201 | for cpuID, cpuStat := range stats.CPU { |
| 202 | cpuNum := fmt.Sprintf("%d", cpuID) |
| 203 | ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.User, cpuNum, "user") |
| 204 | ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.Nice, cpuNum, "nice") |
| 205 | ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.System, cpuNum, "system") |
| 206 | ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.Idle, cpuNum, "idle") |
| 207 | ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.Iowait, cpuNum, "iowait") |
| 208 | ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.IRQ, cpuNum, "irq") |
| 209 | ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.SoftIRQ, cpuNum, "softirq") |
| 210 | ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.Steal, cpuNum, "steal") |
| 211 | |
| 212 | // Guest CPU is also accounted for in cpuStat.User and cpuStat.Nice, expose these as separate metrics. |
| 213 | ch <- prometheus.MustNewConstMetric(c.cpuGuest, prometheus.CounterValue, cpuStat.Guest, cpuNum, "user") |
| 214 | ch <- prometheus.MustNewConstMetric(c.cpuGuest, prometheus.CounterValue, cpuStat.GuestNice, cpuNum, "nice") |
| 215 | } |
| 216 | |
| 217 | ch <- prometheus.MustNewConstMetric(nodeCPUCountDesc, prometheus.GaugeValue, float64(runtime.NumCPU())) |
| 218 | |
| 219 | return nil |
| 220 | } |