Skip to content

Commit

Permalink
Merge pull request #1774 from ston1th/openbsd_amd64
Browse files Browse the repository at this point in the history
remove openbsd amd64 cgo dependecies
  • Loading branch information
SuperQ authored Dec 14, 2020
2 parents 1889202 + 91bec7c commit 3b8a7f6
Show file tree
Hide file tree
Showing 19 changed files with 659 additions and 5 deletions.
1 change: 1 addition & 0 deletions .promu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ crossbuild:
- linux/ppc64
- linux/ppc64le
- linux/s390x
- openbsd/amd64
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [FEATURE]
* [ENHANCEMENT] Include TCP OutRsts in netstat metrics
* [ENHANCEMENT] Added XFS inode operations to XFS metrics
* [ENHANCEMENT] Remove CGO dependencies for OpenBSD amd64
* [BUGFIX]

## 1.0.1 / 2020-06-15
Expand Down
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@ else
PROMU_CONF ?= .promu-cgo.yml
endif
else
PROMU_CONF ?= .promu-cgo.yml
# Do not use CGO for openbsd/amd64 builds
ifeq ($(GOOS), openbsd)
ifeq ($(GOARCH), amd64)
PROMU_CONF ?= .promu.yml
else
PROMU_CONF ?= .promu-cgo.yml
endif
else
PROMU_CONF ?= .promu-cgo.yml
endif
endif
endif

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ bonding | Exposes the number of configured and active slaves of Linux bonding in
btrfs | Exposes btrfs statistics | Linux
boottime | Exposes system boot time derived from the `kern.boottime` sysctl. | Darwin, Dragonfly, FreeBSD, NetBSD, OpenBSD, Solaris
conntrack | Shows conntrack statistics (does nothing if no `/proc/sys/net/netfilter/` present). | Linux
cpu | Exposes CPU statistics | Darwin, Dragonfly, FreeBSD, Linux, Solaris
cpu | Exposes CPU statistics | Darwin, Dragonfly, FreeBSD, Linux, Solaris, OpenBSD
cpufreq | Exposes CPU frequency statistics | Linux, Solaris
diskstats | Exposes disk I/O statistics. | Darwin, Linux, OpenBSD
edac | Exposes error detection and correction statistics. | Linux
Expand Down
2 changes: 1 addition & 1 deletion collector/boot_time_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// +build freebsd dragonfly openbsd netbsd darwin
// +build freebsd dragonfly openbsd,!amd64 netbsd darwin
// +build !noboottime

package collector
Expand Down
61 changes: 61 additions & 0 deletions collector/boot_time_openbsd_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2020 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build !noboottime

package collector

import (
"github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sys/unix"
"unsafe"
)

type bootTimeCollector struct {
name, description string
logger log.Logger
}

func init() {
registerCollector("boottime", defaultEnabled, newBootTimeCollector)
}

// newBootTimeCollector returns a new Collector exposing system boot time on BSD systems.
func newBootTimeCollector(logger log.Logger) (Collector, error) {
return &bootTimeCollector{
name: "boot_time_seconds",
description: "Unix time of last boot, including microseconds.",
logger: logger,
}, nil
}

// Update pushes boot time onto ch
func (c *bootTimeCollector) Update(ch chan<- prometheus.Metric) error {
raw, err := unix.SysctlRaw("kern.boottime")
if err != nil {
return err
}

tv := *(*unix.Timeval)(unsafe.Pointer(&raw[0]))
v := (float64(tv.Sec) + (float64(tv.Usec) / float64(1000*1000)))

ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", c.name),
c.description,
nil, nil,
), prometheus.GaugeValue, v)

return nil
}
1 change: 1 addition & 0 deletions collector/cpu_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// +build openbsd,!amd64
// +build !nocpu

package collector
Expand Down
95 changes: 95 additions & 0 deletions collector/cpu_openbsd_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2020 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build !nocpu

package collector

import (
"strconv"
"unsafe"

"github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sys/unix"
)

type clockinfo struct {
hz int32
tick int32
tickadj int32
stathz int32
profhz int32
}

const (
CP_USER = iota
CP_NICE
CP_SYS
CP_SPIN
CP_INTR
CP_IDLE
CPUSTATES
)

type cpuCollector struct {
cpu typedDesc
logger log.Logger
}

func init() {
registerCollector("cpu", defaultEnabled, NewCPUCollector)
}

func NewCPUCollector(logger log.Logger) (Collector, error) {
return &cpuCollector{
cpu: typedDesc{nodeCPUSecondsDesc, prometheus.CounterValue},
logger: logger,
}, nil
}

func (c *cpuCollector) Update(ch chan<- prometheus.Metric) (err error) {
clockb, err := unix.SysctlRaw("kern.clockrate")
if err != nil {
return err
}
clock := *(*clockinfo)(unsafe.Pointer(&clockb[0]))
hz := float64(clock.stathz)

ncpus, err := unix.SysctlUint32("hw.ncpu")
if err != nil {
return err
}

var cpTime [][CPUSTATES]int64
for i := 0; i < int(ncpus); i++ {
cpb, err := unix.SysctlRaw("kern.cp_time2", i)
if err != nil && err != unix.ENODEV {
return err
}
if err != unix.ENODEV {
cpTime = append(cpTime, *(*[CPUSTATES]int64)(unsafe.Pointer(&cpb[0])))
}
}

for cpu, time := range cpTime {
lcpu := strconv.Itoa(cpu)
ch <- c.cpu.mustNewConstMetric(float64(time[CP_USER])/hz, lcpu, "user")
ch <- c.cpu.mustNewConstMetric(float64(time[CP_NICE])/hz, lcpu, "nice")
ch <- c.cpu.mustNewConstMetric(float64(time[CP_SYS])/hz, lcpu, "system")
ch <- c.cpu.mustNewConstMetric(float64(time[CP_SPIN])/hz, lcpu, "spin")
ch <- c.cpu.mustNewConstMetric(float64(time[CP_INTR])/hz, lcpu, "interrupt")
ch <- c.cpu.mustNewConstMetric(float64(time[CP_IDLE])/hz, lcpu, "idle")
}
return err
}
1 change: 1 addition & 0 deletions collector/diskstats_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// +build openbsd,!amd64
// +build !nodiskstats

package collector
Expand Down
89 changes: 89 additions & 0 deletions collector/diskstats_openbsd_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2020 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build !nodiskstats

package collector

import (
"unsafe"

"github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sys/unix"
)

const (
DS_DISKNAMELEN = 16
)

type DiskStats struct {
Name [DS_DISKNAMELEN]int8
Busy int32
Rxfer uint64
Wxfer uint64
Seek uint64
Rbytes uint64
Wbytes uint64
Attachtime unix.Timeval
Timestamp unix.Timeval
Time unix.Timeval
}

type diskstatsCollector struct {
rxfer typedDesc
rbytes typedDesc
wxfer typedDesc
wbytes typedDesc
time typedDesc
logger log.Logger
}

func init() {
registerCollector("diskstats", defaultEnabled, NewDiskstatsCollector)
}

// NewDiskstatsCollector returns a new Collector exposing disk device stats.
func NewDiskstatsCollector(logger log.Logger) (Collector, error) {
return &diskstatsCollector{
rxfer: typedDesc{readsCompletedDesc, prometheus.CounterValue},
rbytes: typedDesc{readBytesDesc, prometheus.CounterValue},
wxfer: typedDesc{writesCompletedDesc, prometheus.CounterValue},
wbytes: typedDesc{writtenBytesDesc, prometheus.CounterValue},
time: typedDesc{ioTimeSecondsDesc, prometheus.CounterValue},
logger: logger,
}, nil
}

func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) (err error) {
diskstatsb, err := unix.SysctlRaw("hw.diskstats")
if err != nil {
return err
}

ndisks := len(diskstatsb) / int(unsafe.Sizeof(DiskStats{}))
diskstats := *(*[]DiskStats)(unsafe.Pointer(&diskstatsb))

for i := 0; i < ndisks; i++ {
dn := *(*[DS_DISKNAMELEN]int8)(unsafe.Pointer(&diskstats[i].Name[0]))
diskname := int8ToString(dn[:])

ch <- c.rxfer.mustNewConstMetric(float64(diskstats[i].Rxfer), diskname)
ch <- c.rbytes.mustNewConstMetric(float64(diskstats[i].Rbytes), diskname)
ch <- c.wxfer.mustNewConstMetric(float64(diskstats[i].Wxfer), diskname)
ch <- c.wbytes.mustNewConstMetric(float64(diskstats[i].Wbytes), diskname)
time := float64(diskstats[i].Time.Sec) + float64(diskstats[i].Time.Usec)/1000000
ch <- c.time.mustNewConstMetric(time, diskname)
}
return nil
}
2 changes: 1 addition & 1 deletion collector/filesystem_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// +build openbsd darwin,amd64 dragonfly
// +build openbsd,!amd64 darwin,amd64 dragonfly
// +build !nofilesystem

package collector
Expand Down
77 changes: 77 additions & 0 deletions collector/filesystem_openbsd_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2020 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build openbsd darwin,amd64 dragonfly
// +build !nofilesystem

package collector

import (
"github.com/go-kit/kit/log/level"
"golang.org/x/sys/unix"
)

const (
defIgnoredMountPoints = "^/(dev)($|/)"
defIgnoredFSTypes = "^devfs$"
)

// Expose filesystem fullness.
func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) {
var mnt []unix.Statfs_t
size, err := unix.Getfsstat(mnt, unix.MNT_NOWAIT)
if err != nil {
return nil, err
}
mnt = make([]unix.Statfs_t, size)
_, err = unix.Getfsstat(mnt, unix.MNT_NOWAIT)
if err != nil {
return nil, err
}

stats = []filesystemStats{}
for _, v := range mnt {
mountpoint := int8ToString(v.F_mntonname[:])
if c.ignoredMountPointsPattern.MatchString(mountpoint) {
level.Debug(c.logger).Log("msg", "Ignoring mount point", "mountpoint", mountpoint)
continue
}

device := int8ToString(v.F_mntfromname[:])
fstype := int8ToString(v.F_fstypename[:])
if c.ignoredFSTypesPattern.MatchString(fstype) {
level.Debug(c.logger).Log("msg", "Ignoring fs type", "type", fstype)
continue
}

var ro float64
if (v.F_flags & unix.MNT_RDONLY) != 0 {
ro = 1
}

stats = append(stats, filesystemStats{
labels: filesystemLabels{
device: device,
mountPoint: mountpoint,
fsType: fstype,
},
size: float64(v.F_blocks) * float64(v.F_bsize),
free: float64(v.F_bfree) * float64(v.F_bsize),
avail: float64(v.F_bavail) * float64(v.F_bsize),
files: float64(v.F_files),
filesFree: float64(v.F_ffree),
ro: ro,
})
}
return stats, nil
}
Loading

0 comments on commit 3b8a7f6

Please sign in to comment.