Skip to content

Commit 3b8a7f6

Browse files
authored
Merge pull request #1774 from ston1th/openbsd_amd64
remove openbsd amd64 cgo dependecies
2 parents 1889202 + 91bec7c commit 3b8a7f6

19 files changed

+659
-5
lines changed

.promu.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ crossbuild:
3131
- linux/ppc64
3232
- linux/ppc64le
3333
- linux/s390x
34+
- openbsd/amd64

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [FEATURE]
66
* [ENHANCEMENT] Include TCP OutRsts in netstat metrics
77
* [ENHANCEMENT] Added XFS inode operations to XFS metrics
8+
* [ENHANCEMENT] Remove CGO dependencies for OpenBSD amd64
89
* [BUGFIX]
910

1011
## 1.0.1 / 2020-06-15

Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,16 @@ else
4545
PROMU_CONF ?= .promu-cgo.yml
4646
endif
4747
else
48-
PROMU_CONF ?= .promu-cgo.yml
48+
# Do not use CGO for openbsd/amd64 builds
49+
ifeq ($(GOOS), openbsd)
50+
ifeq ($(GOARCH), amd64)
51+
PROMU_CONF ?= .promu.yml
52+
else
53+
PROMU_CONF ?= .promu-cgo.yml
54+
endif
55+
else
56+
PROMU_CONF ?= .promu-cgo.yml
57+
endif
4958
endif
5059
endif
5160

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ bonding | Exposes the number of configured and active slaves of Linux bonding in
9090
btrfs | Exposes btrfs statistics | Linux
9191
boottime | Exposes system boot time derived from the `kern.boottime` sysctl. | Darwin, Dragonfly, FreeBSD, NetBSD, OpenBSD, Solaris
9292
conntrack | Shows conntrack statistics (does nothing if no `/proc/sys/net/netfilter/` present). | Linux
93-
cpu | Exposes CPU statistics | Darwin, Dragonfly, FreeBSD, Linux, Solaris
93+
cpu | Exposes CPU statistics | Darwin, Dragonfly, FreeBSD, Linux, Solaris, OpenBSD
9494
cpufreq | Exposes CPU frequency statistics | Linux, Solaris
9595
diskstats | Exposes disk I/O statistics. | Darwin, Linux, OpenBSD
9696
edac | Exposes error detection and correction statistics. | Linux

collector/boot_time_bsd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
// +build freebsd dragonfly openbsd netbsd darwin
14+
// +build freebsd dragonfly openbsd,!amd64 netbsd darwin
1515
// +build !noboottime
1616

1717
package collector
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2020 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
// +build !noboottime
15+
16+
package collector
17+
18+
import (
19+
"github.com/go-kit/kit/log"
20+
"github.com/prometheus/client_golang/prometheus"
21+
"golang.org/x/sys/unix"
22+
"unsafe"
23+
)
24+
25+
type bootTimeCollector struct {
26+
name, description string
27+
logger log.Logger
28+
}
29+
30+
func init() {
31+
registerCollector("boottime", defaultEnabled, newBootTimeCollector)
32+
}
33+
34+
// newBootTimeCollector returns a new Collector exposing system boot time on BSD systems.
35+
func newBootTimeCollector(logger log.Logger) (Collector, error) {
36+
return &bootTimeCollector{
37+
name: "boot_time_seconds",
38+
description: "Unix time of last boot, including microseconds.",
39+
logger: logger,
40+
}, nil
41+
}
42+
43+
// Update pushes boot time onto ch
44+
func (c *bootTimeCollector) Update(ch chan<- prometheus.Metric) error {
45+
raw, err := unix.SysctlRaw("kern.boottime")
46+
if err != nil {
47+
return err
48+
}
49+
50+
tv := *(*unix.Timeval)(unsafe.Pointer(&raw[0]))
51+
v := (float64(tv.Sec) + (float64(tv.Usec) / float64(1000*1000)))
52+
53+
ch <- prometheus.MustNewConstMetric(
54+
prometheus.NewDesc(
55+
prometheus.BuildFQName(namespace, "", c.name),
56+
c.description,
57+
nil, nil,
58+
), prometheus.GaugeValue, v)
59+
60+
return nil
61+
}

collector/cpu_openbsd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14+
// +build openbsd,!amd64
1415
// +build !nocpu
1516

1617
package collector

collector/cpu_openbsd_amd64.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2020 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
// +build !nocpu
15+
16+
package collector
17+
18+
import (
19+
"strconv"
20+
"unsafe"
21+
22+
"github.com/go-kit/kit/log"
23+
"github.com/prometheus/client_golang/prometheus"
24+
"golang.org/x/sys/unix"
25+
)
26+
27+
type clockinfo struct {
28+
hz int32
29+
tick int32
30+
tickadj int32
31+
stathz int32
32+
profhz int32
33+
}
34+
35+
const (
36+
CP_USER = iota
37+
CP_NICE
38+
CP_SYS
39+
CP_SPIN
40+
CP_INTR
41+
CP_IDLE
42+
CPUSTATES
43+
)
44+
45+
type cpuCollector struct {
46+
cpu typedDesc
47+
logger log.Logger
48+
}
49+
50+
func init() {
51+
registerCollector("cpu", defaultEnabled, NewCPUCollector)
52+
}
53+
54+
func NewCPUCollector(logger log.Logger) (Collector, error) {
55+
return &cpuCollector{
56+
cpu: typedDesc{nodeCPUSecondsDesc, prometheus.CounterValue},
57+
logger: logger,
58+
}, nil
59+
}
60+
61+
func (c *cpuCollector) Update(ch chan<- prometheus.Metric) (err error) {
62+
clockb, err := unix.SysctlRaw("kern.clockrate")
63+
if err != nil {
64+
return err
65+
}
66+
clock := *(*clockinfo)(unsafe.Pointer(&clockb[0]))
67+
hz := float64(clock.stathz)
68+
69+
ncpus, err := unix.SysctlUint32("hw.ncpu")
70+
if err != nil {
71+
return err
72+
}
73+
74+
var cpTime [][CPUSTATES]int64
75+
for i := 0; i < int(ncpus); i++ {
76+
cpb, err := unix.SysctlRaw("kern.cp_time2", i)
77+
if err != nil && err != unix.ENODEV {
78+
return err
79+
}
80+
if err != unix.ENODEV {
81+
cpTime = append(cpTime, *(*[CPUSTATES]int64)(unsafe.Pointer(&cpb[0])))
82+
}
83+
}
84+
85+
for cpu, time := range cpTime {
86+
lcpu := strconv.Itoa(cpu)
87+
ch <- c.cpu.mustNewConstMetric(float64(time[CP_USER])/hz, lcpu, "user")
88+
ch <- c.cpu.mustNewConstMetric(float64(time[CP_NICE])/hz, lcpu, "nice")
89+
ch <- c.cpu.mustNewConstMetric(float64(time[CP_SYS])/hz, lcpu, "system")
90+
ch <- c.cpu.mustNewConstMetric(float64(time[CP_SPIN])/hz, lcpu, "spin")
91+
ch <- c.cpu.mustNewConstMetric(float64(time[CP_INTR])/hz, lcpu, "interrupt")
92+
ch <- c.cpu.mustNewConstMetric(float64(time[CP_IDLE])/hz, lcpu, "idle")
93+
}
94+
return err
95+
}

collector/diskstats_openbsd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14+
// +build openbsd,!amd64
1415
// +build !nodiskstats
1516

1617
package collector
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2020 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
// +build !nodiskstats
15+
16+
package collector
17+
18+
import (
19+
"unsafe"
20+
21+
"github.com/go-kit/kit/log"
22+
"github.com/prometheus/client_golang/prometheus"
23+
"golang.org/x/sys/unix"
24+
)
25+
26+
const (
27+
DS_DISKNAMELEN = 16
28+
)
29+
30+
type DiskStats struct {
31+
Name [DS_DISKNAMELEN]int8
32+
Busy int32
33+
Rxfer uint64
34+
Wxfer uint64
35+
Seek uint64
36+
Rbytes uint64
37+
Wbytes uint64
38+
Attachtime unix.Timeval
39+
Timestamp unix.Timeval
40+
Time unix.Timeval
41+
}
42+
43+
type diskstatsCollector struct {
44+
rxfer typedDesc
45+
rbytes typedDesc
46+
wxfer typedDesc
47+
wbytes typedDesc
48+
time typedDesc
49+
logger log.Logger
50+
}
51+
52+
func init() {
53+
registerCollector("diskstats", defaultEnabled, NewDiskstatsCollector)
54+
}
55+
56+
// NewDiskstatsCollector returns a new Collector exposing disk device stats.
57+
func NewDiskstatsCollector(logger log.Logger) (Collector, error) {
58+
return &diskstatsCollector{
59+
rxfer: typedDesc{readsCompletedDesc, prometheus.CounterValue},
60+
rbytes: typedDesc{readBytesDesc, prometheus.CounterValue},
61+
wxfer: typedDesc{writesCompletedDesc, prometheus.CounterValue},
62+
wbytes: typedDesc{writtenBytesDesc, prometheus.CounterValue},
63+
time: typedDesc{ioTimeSecondsDesc, prometheus.CounterValue},
64+
logger: logger,
65+
}, nil
66+
}
67+
68+
func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) (err error) {
69+
diskstatsb, err := unix.SysctlRaw("hw.diskstats")
70+
if err != nil {
71+
return err
72+
}
73+
74+
ndisks := len(diskstatsb) / int(unsafe.Sizeof(DiskStats{}))
75+
diskstats := *(*[]DiskStats)(unsafe.Pointer(&diskstatsb))
76+
77+
for i := 0; i < ndisks; i++ {
78+
dn := *(*[DS_DISKNAMELEN]int8)(unsafe.Pointer(&diskstats[i].Name[0]))
79+
diskname := int8ToString(dn[:])
80+
81+
ch <- c.rxfer.mustNewConstMetric(float64(diskstats[i].Rxfer), diskname)
82+
ch <- c.rbytes.mustNewConstMetric(float64(diskstats[i].Rbytes), diskname)
83+
ch <- c.wxfer.mustNewConstMetric(float64(diskstats[i].Wxfer), diskname)
84+
ch <- c.wbytes.mustNewConstMetric(float64(diskstats[i].Wbytes), diskname)
85+
time := float64(diskstats[i].Time.Sec) + float64(diskstats[i].Time.Usec)/1000000
86+
ch <- c.time.mustNewConstMetric(time, diskname)
87+
}
88+
return nil
89+
}

0 commit comments

Comments
 (0)