-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
collector: add NetBSD support for filesystem #3082
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2024 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. | ||
|
||
//go:build !nofilesystem | ||
// +build !nofilesystem | ||
|
||
package collector | ||
|
||
import ( | ||
"fmt" | ||
"github.com/go-kit/log/level" | ||
"golang.org/x/sys/unix" | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
const ( | ||
defMountPointsExcluded = "^/(dev)($|/)" | ||
defFSTypesExcluded = "^(kernfs|procfs|ptyfs|fdesc)$" | ||
_VFS_NAMELEN = 32 | ||
_VFS_MNAMELEN = 1024 | ||
) | ||
|
||
/* | ||
* Go uses the NetBSD 9 ABI and thus syscall.SYS_GETVFSSTAT is compat_90_getvfsstat. | ||
* We have to declare struct statvfs90 because it is not included in the unix package. | ||
* See NetBSD/src/sys/compat/sys/statvfs.h. | ||
*/ | ||
type statvfs90 struct { | ||
F_flag uint | ||
F_bsize uint | ||
F_frsize uint | ||
F_iosize uint | ||
|
||
F_blocks uint64 | ||
F_bfree uint64 | ||
F_bavail uint64 | ||
F_bresvd uint64 | ||
|
||
F_files uint64 | ||
F_ffree uint64 | ||
F_favail uint64 | ||
F_fresvd uint64 | ||
|
||
F_syncreads uint64 | ||
F_syncwrites uint64 | ||
|
||
F_asyncreads uint64 | ||
F_asyncwrites uint64 | ||
|
||
F_fsidx [2]uint32 | ||
F_fsid uint32 | ||
F_namemax uint | ||
F_owner uint32 | ||
F_spare [4]uint32 | ||
|
||
F_fstypename [_VFS_NAMELEN]byte | ||
F_mntonname [_VFS_MNAMELEN]byte | ||
F_mntfromname [_VFS_MNAMELEN]byte | ||
|
||
cgo_pad [4]byte | ||
} | ||
|
||
func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) { | ||
var mnt []statvfs90 | ||
if syscall.SYS_GETVFSSTAT != 356 /* compat_90_getvfsstat */ { | ||
/* | ||
* Catch if golang ever updates to newer ABI and bail. | ||
*/ | ||
return nil, fmt.Errorf("getvfsstat: ABI mismatch") | ||
} | ||
for { | ||
r1, _, errno := syscall.Syscall(syscall.SYS_GETVFSSTAT, uintptr(0), 0, unix.ST_NOWAIT) | ||
if errno != 0 { | ||
return nil, fmt.Errorf("getvfsstat: %s", string(errno)) | ||
} | ||
mnt = make([]statvfs90, r1, r1) | ||
r2, _, errno := syscall.Syscall(syscall.SYS_GETVFSSTAT, uintptr(unsafe.Pointer(&mnt[0])), unsafe.Sizeof(mnt[0])*r1, unix.ST_NOWAIT /* ST_NOWAIT */) | ||
if errno != 0 { | ||
return nil, fmt.Errorf("getvfsstat: %s", string(errno)) | ||
} | ||
if r1 == r2 { | ||
break | ||
} | ||
} | ||
|
||
stats = []filesystemStats{} | ||
for _, v := range mnt { | ||
mountpoint := unix.ByteSliceToString(v.F_mntonname[:]) | ||
if c.excludedMountPointsPattern.MatchString(mountpoint) { | ||
level.Debug(c.logger).Log("msg", "Ignoring mount point", "mountpoint", mountpoint) | ||
continue | ||
} | ||
|
||
device := unix.ByteSliceToString(v.F_mntfromname[:]) | ||
fstype := unix.ByteSliceToString(v.F_fstypename[:]) | ||
if c.excludedFSTypesPattern.MatchString(fstype) { | ||
level.Debug(c.logger).Log("msg", "Ignoring fs type", "type", fstype) | ||
continue | ||
} | ||
|
||
var ro float64 | ||
if (v.F_flag & 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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was dropped in 0909576 and re-introduced here without syncing dependencies, hence the regression on
master
. Re-dropped in #3131.