Move stat_linux to cpu_linux and add cpufreq stats (#548)
This commit is contained in:
parent
798950d25b
commit
2e9f1913b8
15 changed files with 210 additions and 81 deletions
|
|
@ -16,18 +16,13 @@
|
|||
package collector
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"fmt"
|
||||
|
||||
"github.com/prometheus/procfs"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
const (
|
||||
userHz = 100
|
||||
)
|
||||
|
||||
type statCollector struct {
|
||||
cpu *prometheus.Desc
|
||||
intr *prometheus.Desc
|
||||
|
|
@ -83,80 +78,25 @@ func NewStatCollector() (Collector, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
// Expose kernel and system statistics.
|
||||
// Update implements Collector and exposes kernel and system statistics.
|
||||
func (c *statCollector) Update(ch chan<- prometheus.Metric) error {
|
||||
file, err := os.Open(procFilePath("stat"))
|
||||
fs, err := procfs.NewFS(*procPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open procfs: %v", err)
|
||||
}
|
||||
stats, err := fs.NewStat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
parts := strings.Fields(scanner.Text())
|
||||
if len(parts) == 0 {
|
||||
continue
|
||||
}
|
||||
switch {
|
||||
case strings.HasPrefix(parts[0], "cpu"):
|
||||
// Export only per-cpu stats, it can be aggregated up in prometheus.
|
||||
if parts[0] == "cpu" {
|
||||
break
|
||||
}
|
||||
// Only some of these may be present, depending on kernel version.
|
||||
cpuFields := []string{"user", "nice", "system", "idle", "iowait", "irq", "softirq", "steal", "guest", "guest_nice"}
|
||||
// OpenVZ guests lack the "guest" CPU field, which needs to be ignored.
|
||||
expectedFieldNum := len(cpuFields) + 1
|
||||
if expectedFieldNum > len(parts) {
|
||||
expectedFieldNum = len(parts)
|
||||
}
|
||||
for i, v := range parts[1:expectedFieldNum] {
|
||||
value, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Convert from ticks to seconds
|
||||
value /= userHz
|
||||
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, value, parts[0], cpuFields[i])
|
||||
}
|
||||
case parts[0] == "intr":
|
||||
// Only expose the overall number, use the 'interrupts' collector for more detail.
|
||||
value, err := strconv.ParseFloat(parts[1], 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(c.intr, prometheus.CounterValue, value)
|
||||
case parts[0] == "ctxt":
|
||||
value, err := strconv.ParseFloat(parts[1], 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(c.ctxt, prometheus.CounterValue, value)
|
||||
case parts[0] == "processes":
|
||||
value, err := strconv.ParseFloat(parts[1], 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(c.forks, prometheus.CounterValue, value)
|
||||
case parts[0] == "btime":
|
||||
value, err := strconv.ParseFloat(parts[1], 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(c.btime, prometheus.GaugeValue, value)
|
||||
case parts[0] == "procs_running":
|
||||
value, err := strconv.ParseFloat(parts[1], 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(c.procsRunning, prometheus.GaugeValue, value)
|
||||
case parts[0] == "procs_blocked":
|
||||
value, err := strconv.ParseFloat(parts[1], 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(c.procsBlocked, prometheus.GaugeValue, value)
|
||||
}
|
||||
}
|
||||
return scanner.Err()
|
||||
ch <- prometheus.MustNewConstMetric(c.intr, prometheus.CounterValue, float64(stats.IRQTotal))
|
||||
ch <- prometheus.MustNewConstMetric(c.ctxt, prometheus.CounterValue, float64(stats.ContextSwitches))
|
||||
ch <- prometheus.MustNewConstMetric(c.forks, prometheus.CounterValue, float64(stats.ProcessCreated))
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(c.btime, prometheus.GaugeValue, float64(stats.BootTime))
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(c.procsRunning, prometheus.GaugeValue, float64(stats.ProcessesRunning))
|
||||
ch <- prometheus.MustNewConstMetric(c.procsBlocked, prometheus.GaugeValue, float64(stats.ProcessesBlocked))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue