2015-05-12 11:06:41 +00:00
|
|
|
// +build !nostat
|
2014-06-05 15:59:51 +00:00
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
procStat = "/proc/stat"
|
2015-05-12 14:24:48 +00:00
|
|
|
userHz = 100
|
2014-06-05 15:59:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type statCollector struct {
|
2014-11-25 02:00:17 +00:00
|
|
|
cpu *prometheus.CounterVec
|
|
|
|
intr prometheus.Counter
|
|
|
|
ctxt prometheus.Counter
|
|
|
|
forks prometheus.Counter
|
|
|
|
btime prometheus.Gauge
|
|
|
|
procsRunning prometheus.Gauge
|
|
|
|
procsBlocked prometheus.Gauge
|
2014-06-05 15:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Factories["stat"] = NewStatCollector
|
|
|
|
}
|
|
|
|
|
2015-05-20 18:04:49 +00:00
|
|
|
// Takes a prometheus registry and returns a new Collector exposing
|
2014-06-05 15:59:51 +00:00
|
|
|
// network device stats.
|
2015-05-20 18:04:49 +00:00
|
|
|
func NewStatCollector() (Collector, error) {
|
2014-11-25 02:00:17 +00:00
|
|
|
return &statCollector{
|
|
|
|
cpu: prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Name: "cpu",
|
|
|
|
Help: "Seconds the cpus spent in each mode.",
|
|
|
|
},
|
|
|
|
[]string{"cpu", "mode"},
|
|
|
|
),
|
|
|
|
intr: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Name: "intr",
|
|
|
|
Help: "Total number of interrupts serviced.",
|
|
|
|
}),
|
|
|
|
ctxt: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Name: "context_switches",
|
|
|
|
Help: "Total number of context switches.",
|
|
|
|
}),
|
|
|
|
forks: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Name: "forks",
|
|
|
|
Help: "Total number of forks.",
|
|
|
|
}),
|
|
|
|
btime: prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Name: "boot_time",
|
|
|
|
Help: "Node boot time, in unixtime.",
|
|
|
|
}),
|
|
|
|
procsRunning: prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Name: "procs_running",
|
|
|
|
Help: "Number of processes in runnable state.",
|
|
|
|
}),
|
|
|
|
procsBlocked: prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Name: "procs_blocked",
|
|
|
|
Help: "Number of processes blocked waiting for I/O to complete.",
|
|
|
|
}),
|
|
|
|
}, nil
|
2014-06-05 15:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expose a variety of stats from /proc/stats.
|
2014-10-29 14:16:43 +00:00
|
|
|
func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
2014-06-05 15:59:51 +00:00
|
|
|
file, err := os.Open(procStat)
|
|
|
|
if err != nil {
|
2014-10-29 14:16:43 +00:00
|
|
|
return err
|
2014-06-05 15:59:51 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
parts := strings.Fields(scanner.Text())
|
2015-02-06 18:44:22 +00:00
|
|
|
if len(parts) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2014-06-05 15:59:51 +00:00
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(parts[0], "cpu"):
|
2014-06-06 09:53:40 +00:00
|
|
|
// Export only per-cpu stats, it can be aggregated up in prometheus.
|
2014-06-05 15:59:51 +00:00
|
|
|
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"}
|
2015-02-06 18:44:22 +00:00
|
|
|
// OpenVZ guests lack the "guest" CPU field, which needs to be ignored.
|
2015-07-14 12:57:20 +00:00
|
|
|
expectedFieldNum := len(cpuFields) + 1
|
2015-02-06 18:44:22 +00:00
|
|
|
if expectedFieldNum > len(parts) {
|
|
|
|
expectedFieldNum = len(parts)
|
|
|
|
}
|
2015-07-14 12:57:20 +00:00
|
|
|
for i, v := range parts[1:expectedFieldNum] {
|
2014-06-05 15:59:51 +00:00
|
|
|
value, err := strconv.ParseFloat(v, 64)
|
|
|
|
if err != nil {
|
2014-10-29 14:16:43 +00:00
|
|
|
return err
|
2014-06-05 15:59:51 +00:00
|
|
|
}
|
2014-06-26 17:20:36 +00:00
|
|
|
// Convert from ticks to seconds
|
2015-05-12 14:24:48 +00:00
|
|
|
value /= userHz
|
2014-11-25 02:00:17 +00:00
|
|
|
c.cpu.With(prometheus.Labels{"cpu": parts[0], "mode": cpuFields[i]}).Set(value)
|
2014-06-05 15:59:51 +00:00
|
|
|
}
|
|
|
|
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 {
|
2014-10-29 14:16:43 +00:00
|
|
|
return err
|
2014-06-05 15:59:51 +00:00
|
|
|
}
|
2014-11-25 02:00:17 +00:00
|
|
|
c.intr.Set(value)
|
2014-06-05 15:59:51 +00:00
|
|
|
case parts[0] == "ctxt":
|
|
|
|
value, err := strconv.ParseFloat(parts[1], 64)
|
|
|
|
if err != nil {
|
2014-10-29 14:16:43 +00:00
|
|
|
return err
|
2014-06-05 15:59:51 +00:00
|
|
|
}
|
2014-11-25 02:00:17 +00:00
|
|
|
c.ctxt.Set(value)
|
2014-06-05 15:59:51 +00:00
|
|
|
case parts[0] == "processes":
|
|
|
|
value, err := strconv.ParseFloat(parts[1], 64)
|
|
|
|
if err != nil {
|
2014-10-29 14:16:43 +00:00
|
|
|
return err
|
2014-06-05 15:59:51 +00:00
|
|
|
}
|
2014-11-25 02:00:17 +00:00
|
|
|
c.forks.Set(value)
|
2014-06-05 15:59:51 +00:00
|
|
|
case parts[0] == "btime":
|
|
|
|
value, err := strconv.ParseFloat(parts[1], 64)
|
|
|
|
if err != nil {
|
2014-10-29 14:16:43 +00:00
|
|
|
return err
|
2014-06-05 15:59:51 +00:00
|
|
|
}
|
2014-11-25 02:00:17 +00:00
|
|
|
c.btime.Set(value)
|
2014-06-05 15:59:51 +00:00
|
|
|
case parts[0] == "procs_running":
|
|
|
|
value, err := strconv.ParseFloat(parts[1], 64)
|
|
|
|
if err != nil {
|
2014-10-29 14:16:43 +00:00
|
|
|
return err
|
2014-06-05 15:59:51 +00:00
|
|
|
}
|
2014-11-25 02:00:17 +00:00
|
|
|
c.procsRunning.Set(value)
|
2014-06-05 15:59:51 +00:00
|
|
|
case parts[0] == "procs_blocked":
|
|
|
|
value, err := strconv.ParseFloat(parts[1], 64)
|
|
|
|
if err != nil {
|
2014-10-29 14:16:43 +00:00
|
|
|
return err
|
2014-06-05 15:59:51 +00:00
|
|
|
}
|
2014-11-25 02:00:17 +00:00
|
|
|
c.procsBlocked.Set(value)
|
2014-06-05 15:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-25 02:00:17 +00:00
|
|
|
c.cpu.Collect(ch)
|
|
|
|
c.ctxt.Collect(ch)
|
|
|
|
c.intr.Collect(ch)
|
|
|
|
c.forks.Collect(ch)
|
|
|
|
c.btime.Collect(ch)
|
|
|
|
c.procsRunning.Collect(ch)
|
|
|
|
c.procsBlocked.Collect(ch)
|
2014-10-29 14:16:43 +00:00
|
|
|
return err
|
2014-06-05 15:59:51 +00:00
|
|
|
}
|