aboutsummaryrefslogtreecommitdiff
path: root/collector/cpu_freebsd.go
diff options
context:
space:
mode:
authorSiavash Safi <siavash.safi@gmail.com>2015-05-12 11:43:08 +0430
committerSiavash Safi <siavash.safi@gmail.com>2015-07-14 13:58:43 +0430
commit23bb9c44b95fbaca3fab86fa3929f03a302ce388 (patch)
tree3afd80fca8d6657a75b633bb944086321bd99e1f /collector/cpu_freebsd.go
parentf9fa6d05cfe7aad84cf2fec561dd39e1ce5807a5 (diff)
downloadprometheus_node_collector-23bb9c44b95fbaca3fab86fa3929f03a302ce388.tar.bz2
prometheus_node_collector-23bb9c44b95fbaca3fab86fa3929f03a302ce388.tar.xz
prometheus_node_collector-23bb9c44b95fbaca3fab86fa3929f03a302ce388.zip
Add cpu collector for FreeBSD.
Diffstat (limited to 'collector/cpu_freebsd.go')
-rw-r--r--collector/cpu_freebsd.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/collector/cpu_freebsd.go b/collector/cpu_freebsd.go
new file mode 100644
index 0000000..78bdb3a
--- /dev/null
+++ b/collector/cpu_freebsd.go
@@ -0,0 +1,71 @@
1// +build freebsd,!nostat
2
3package collector
4
5import (
6 "errors"
7 "strconv"
8 "unsafe"
9
10 "github.com/prometheus/client_golang/prometheus"
11)
12
13/*
14#cgo LDFLAGS: -lkvm
15#include <fcntl.h>
16#include <kvm.h>
17#include <sys/param.h>
18#include <sys/pcpu.h>
19#include <sys/resource.h>
20*/
21import "C"
22
23const ()
24
25type statCollector struct {
26 config Config
27 cpu *prometheus.CounterVec
28}
29
30func init() {
31 Factories["cpu"] = NewStatCollector
32}
33
34// Takes a config struct and prometheus registry and returns a new Collector exposing
35// network device stats.
36func NewStatCollector(config Config) (Collector, error) {
37 return &statCollector{
38 config: config,
39 cpu: prometheus.NewCounterVec(
40 prometheus.CounterOpts{
41 Namespace: Namespace,
42 Name: "cpu",
43 Help: "Seconds the cpus spent in each mode.",
44 },
45 []string{"cpu", "mode"},
46 ),
47 }, nil
48}
49
50// Expose cpu stats using kvm
51func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) {
52 var errbuf *C.char
53 kd := C.kvm_open(nil, nil, nil, C.O_RDONLY, errbuf)
54 if errbuf != nil {
55 return errors.New("Failed to call kvm_open().")
56 }
57 defer C.kvm_close(kd)
58
59 ncpus := C.kvm_getncpus(kd)
60 for i := 0; i < int(ncpus); i++ {
61 pcpu := C.kvm_getpcpu(kd, C.int(i))
62 cp_time := ((*C.struct_pcpu)(unsafe.Pointer(pcpu))).pc_cp_time
63 c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "user"}).Set(float64(cp_time[C.CP_USER]))
64 c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "nice"}).Set(float64(cp_time[C.CP_NICE]))
65 c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "system"}).Set(float64(cp_time[C.CP_SYS]))
66 c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "interrupt"}).Set(float64(cp_time[C.CP_INTR]))
67 c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "idle"}).Set(float64(cp_time[C.CP_IDLE]))
68 }
69 c.cpu.Collect(ch)
70 return err
71}