2016-12-26 13:50:52 -08:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
2021-10-03 04:35:24 -07:00
|
|
|
//go:build !nofilesystem
|
2016-12-26 13:50:52 -08:00
|
|
|
// +build !nofilesystem
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
2020-11-14 02:53:51 -08:00
|
|
|
"github.com/go-kit/log/level"
|
2016-12-26 13:50:52 -08:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-03-23 04:00:06 -07:00
|
|
|
defMountPointsExcluded = "^/(dev)($|/)"
|
|
|
|
defFSTypesExcluded = "^devfs$"
|
2016-12-26 13:50:52 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Expose filesystem fullness.
|
2017-02-28 10:47:20 -08:00
|
|
|
func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
|
2021-12-01 02:21:31 -08:00
|
|
|
n, err := unix.Getfsstat(nil, unix.MNT_NOWAIT)
|
2018-04-16 03:39:15 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
buf := make([]unix.Statfs_t, n)
|
2021-12-01 02:21:31 -08:00
|
|
|
_, err = unix.Getfsstat(buf, unix.MNT_NOWAIT)
|
2018-04-16 03:39:15 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-12-26 13:50:52 -08:00
|
|
|
}
|
2017-02-28 10:47:20 -08:00
|
|
|
stats := []filesystemStats{}
|
2016-12-26 13:50:52 -08:00
|
|
|
for _, fs := range buf {
|
2020-06-02 04:07:09 -07:00
|
|
|
mountpoint := bytesToString(fs.Mntonname[:])
|
2021-03-23 04:00:06 -07:00
|
|
|
if c.excludedMountPointsPattern.MatchString(mountpoint) {
|
2019-12-31 08:19:37 -08:00
|
|
|
level.Debug(c.logger).Log("msg", "Ignoring mount point", "mountpoint", mountpoint)
|
2016-12-26 13:50:52 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-06-02 04:07:09 -07:00
|
|
|
device := bytesToString(fs.Mntfromname[:])
|
|
|
|
fstype := bytesToString(fs.Fstypename[:])
|
2021-03-23 04:00:06 -07:00
|
|
|
if c.excludedFSTypesPattern.MatchString(fstype) {
|
2019-12-31 08:19:37 -08:00
|
|
|
level.Debug(c.logger).Log("msg", "Ignoring fs type", "type", fstype)
|
2016-12-26 13:50:52 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-12-01 02:21:31 -08:00
|
|
|
if (fs.Flags & unix.MNT_IGNORE) != 0 {
|
|
|
|
level.Debug(c.logger).Log("msg", "Ignoring mount flagged as ignore", "mountpoint", mountpoint)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-12-26 13:50:52 -08:00
|
|
|
var ro float64
|
2021-12-01 02:21:31 -08:00
|
|
|
if (fs.Flags & unix.MNT_RDONLY) != 0 {
|
2016-12-26 13:50:52 -08:00
|
|
|
ro = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
stats = append(stats, filesystemStats{
|
2017-01-03 06:55:40 -08:00
|
|
|
labels: filesystemLabels{
|
|
|
|
device: device,
|
2019-07-19 07:51:17 -07:00
|
|
|
mountPoint: rootfsStripPrefix(mountpoint),
|
2017-01-03 06:55:40 -08:00
|
|
|
fsType: fstype,
|
|
|
|
},
|
|
|
|
size: float64(fs.Blocks) * float64(fs.Bsize),
|
|
|
|
free: float64(fs.Bfree) * float64(fs.Bsize),
|
|
|
|
avail: float64(fs.Bavail) * float64(fs.Bsize),
|
|
|
|
files: float64(fs.Files),
|
|
|
|
filesFree: float64(fs.Ffree),
|
|
|
|
ro: ro,
|
2016-12-26 13:50:52 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return stats, nil
|
|
|
|
}
|