cpu: Code cleanup.

pull/88/head
Siavash Safi 10 years ago
parent f93125ba10
commit 462b708742

@ -1,4 +1,4 @@
// +build !nostat // +build !cpu
package collector package collector
@ -21,8 +21,6 @@ import (
*/ */
import "C" import "C"
const ()
type statCollector struct { type statCollector struct {
cpu *prometheus.CounterVec cpu *prometheus.CounterVec
} }
@ -31,30 +29,31 @@ func init() {
Factories["cpu"] = NewStatCollector Factories["cpu"] = NewStatCollector
} }
// cpu stats. // Takes a prometheus registry and returns a new Collector exposing
// CPU stats.
func NewStatCollector() (Collector, error) { func NewStatCollector() (Collector, error) {
return &statCollector{ return &statCollector{
cpu: prometheus.NewCounterVec( cpu: prometheus.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{
Namespace: Namespace, Namespace: Namespace,
Name: "cpu", Name: "cpu_seconds_total",
Help: "Seconds the cpus spent in each mode.", Help: "Seconds the CPU spent in each mode.",
}, },
[]string{"cpu", "mode"}, []string{"cpu", "mode"},
), ),
}, nil }, nil
} }
// Expose cpu stats using kvm // Expose CPU stats using KVM.
func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) { func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) {
if os.Geteuid() != 0 && os.Getegid() != 2 { if os.Geteuid() != 0 && os.Getegid() != 2 {
return errors.New("Caller should be either root user or kmem group to access /dev/mem") return errors.New("caller should be either root user or kmem group to access /dev/mem")
} }
var errbuf *C.char var errbuf *C.char
kd := C.kvm_open(nil, nil, nil, C.O_RDONLY, errbuf) kd := C.kvm_open(nil, nil, nil, C.O_RDONLY, errbuf)
if errbuf != nil { if errbuf != nil {
return errors.New("Failed to call kvm_open().") return errors.New("failed to call kvm_open().")
} }
defer C.kvm_close(kd) defer C.kvm_close(kd)

Loading…
Cancel
Save