diff options
author | Kirk Russell <kirussel@users.noreply.github.com> | 2015-09-26 18:08:18 -0400 |
---|---|---|
committer | Kirk Russell <kirussel@users.noreply.github.com> | 2015-09-26 20:58:47 -0400 |
commit | 89875cefe8e416f71d4ec0863ca10bf8db515b22 (patch) | |
tree | 6f26b9daf3baf327ca6090ff8200b9f93ccfa71d /collector/cpu_freebsd.go | |
parent | a942e719c87464c0a4db352d0edeec2647e35749 (diff) | |
download | prometheus_node_collector-89875cefe8e416f71d4ec0863ca10bf8db515b22.tar.bz2 prometheus_node_collector-89875cefe8e416f71d4ec0863ca10bf8db515b22.tar.xz prometheus_node_collector-89875cefe8e416f71d4ec0863ca10bf8db515b22.zip |
converted freebsd cpu metrics to seconds
Diffstat (limited to 'collector/cpu_freebsd.go')
-rw-r--r-- | collector/cpu_freebsd.go | 48 |
1 files changed, 43 insertions, 5 deletions
diff --git a/collector/cpu_freebsd.go b/collector/cpu_freebsd.go index aa02c1a..a04007a 100644 --- a/collector/cpu_freebsd.go +++ b/collector/cpu_freebsd.go | |||
@@ -28,9 +28,26 @@ import ( | |||
28 | #cgo LDFLAGS: -lkvm | 28 | #cgo LDFLAGS: -lkvm |
29 | #include <fcntl.h> | 29 | #include <fcntl.h> |
30 | #include <kvm.h> | 30 | #include <kvm.h> |
31 | #include <stdlib.h> | ||
31 | #include <sys/param.h> | 32 | #include <sys/param.h> |
32 | #include <sys/pcpu.h> | 33 | #include <sys/pcpu.h> |
33 | #include <sys/resource.h> | 34 | #include <sys/resource.h> |
35 | #include <sys/sysctl.h> | ||
36 | #include <sys/time.h> | ||
37 | |||
38 | long _clockrate() { | ||
39 | struct clockinfo clockrate; | ||
40 | size_t size = sizeof(clockrate); | ||
41 | int res = sysctlbyname("kern.clockrate", &clockrate, &size, NULL, 0); | ||
42 | if (res == -1) { | ||
43 | return -1; | ||
44 | } | ||
45 | if (size != sizeof(clockrate)) { | ||
46 | return -2; | ||
47 | } | ||
48 | return clockrate.stathz > 0 ? clockrate.stathz : clockrate.hz; | ||
49 | } | ||
50 | |||
34 | */ | 51 | */ |
35 | import "C" | 52 | import "C" |
36 | 53 | ||
@@ -70,16 +87,37 @@ func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) { | |||
70 | } | 87 | } |
71 | defer C.kvm_close(kd) | 88 | defer C.kvm_close(kd) |
72 | 89 | ||
90 | // The cp_time variable is an array of CPUSTATES long integers -- in | ||
91 | // the same format as the kern.cp_time sysctl. According to the | ||
92 | // comments in sys/kern/kern_clock.c, the frequency of this timer will | ||
93 | // be stathz (or hz, if stathz is zero). | ||
94 | clockrate, err := getClockRate() | ||
95 | if err != nil { | ||
96 | return err | ||
97 | } | ||
98 | |||
73 | ncpus := C.kvm_getncpus(kd) | 99 | ncpus := C.kvm_getncpus(kd) |
74 | for i := 0; i < int(ncpus); i++ { | 100 | for i := 0; i < int(ncpus); i++ { |
75 | pcpu := C.kvm_getpcpu(kd, C.int(i)) | 101 | pcpu := C.kvm_getpcpu(kd, C.int(i)) |
76 | cp_time := ((*C.struct_pcpu)(unsafe.Pointer(pcpu))).pc_cp_time | 102 | cp_time := ((*C.struct_pcpu)(unsafe.Pointer(pcpu))).pc_cp_time |
77 | c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "user"}).Set(float64(cp_time[C.CP_USER])) | 103 | c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "user"}).Set(float64(cp_time[C.CP_USER])/clockrate) |
78 | c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "nice"}).Set(float64(cp_time[C.CP_NICE])) | 104 | c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "nice"}).Set(float64(cp_time[C.CP_NICE])/clockrate) |
79 | c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "system"}).Set(float64(cp_time[C.CP_SYS])) | 105 | c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "system"}).Set(float64(cp_time[C.CP_SYS])/clockrate) |
80 | c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "interrupt"}).Set(float64(cp_time[C.CP_INTR])) | 106 | c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "interrupt"}).Set(float64(cp_time[C.CP_INTR])/clockrate) |
81 | c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "idle"}).Set(float64(cp_time[C.CP_IDLE])) | 107 | c.cpu.With(prometheus.Labels{"cpu": strconv.Itoa(i), "mode": "idle"}).Set(float64(cp_time[C.CP_IDLE])/clockrate) |
82 | } | 108 | } |
83 | c.cpu.Collect(ch) | 109 | c.cpu.Collect(ch) |
84 | return err | 110 | return err |
85 | } | 111 | } |
112 | |||
113 | func getClockRate() (float64, error) { | ||
114 | clockrate := C._clockrate() | ||
115 | if clockrate == -1 { | ||
116 | return 0, errors.New("sysctl(kern.clockrate) failed") | ||
117 | } else if clockrate == -2 { | ||
118 | return 0, errors.New("sysctl(kern.clockrate) failed, wrong buffer size") | ||
119 | } else if clockrate <= 0 { | ||
120 | return 0, errors.New("sysctl(kern.clockrate) bad clocktime") | ||
121 | } | ||
122 | return float64(clockrate), nil | ||
123 | } | ||