2018-07-12 07:43:49 +08:00
|
|
|
package diskio
|
2016-07-05 06:48:37 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
2018-12-14 03:49:19 +08:00
|
|
|
"bytes"
|
2022-09-28 22:26:14 +08:00
|
|
|
"errors"
|
2016-07-05 06:48:37 +08:00
|
|
|
"fmt"
|
2022-09-28 22:26:14 +08:00
|
|
|
"io/fs"
|
2016-07-05 06:48:37 +08:00
|
|
|
"os"
|
2022-09-28 22:26:14 +08:00
|
|
|
"path/filepath"
|
2016-07-05 06:48:37 +08:00
|
|
|
"strings"
|
2017-10-25 07:22:31 +08:00
|
|
|
|
2023-07-14 21:58:19 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
|
"github.com/influxdata/telegraf/filter"
|
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/system"
|
2017-10-25 07:22:31 +08:00
|
|
|
"golang.org/x/sys/unix"
|
2016-07-05 06:48:37 +08:00
|
|
|
)
|
|
|
|
|
|
2023-07-14 21:58:19 +08:00
|
|
|
type DiskIO struct {
|
|
|
|
|
ps system.PS
|
|
|
|
|
|
|
|
|
|
Devices []string
|
|
|
|
|
DeviceTags []string
|
|
|
|
|
NameTemplates []string
|
|
|
|
|
SkipSerialNumber bool
|
|
|
|
|
|
|
|
|
|
Log telegraf.Logger
|
|
|
|
|
|
|
|
|
|
infoCache map[string]diskInfoCache
|
|
|
|
|
deviceFilter filter.Filter
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-05 06:48:37 +08:00
|
|
|
type diskInfoCache struct {
|
2019-12-31 05:52:03 +08:00
|
|
|
modifiedAt int64 // Unix Nano timestamp of the last modification of the device. This value is used to invalidate the cache
|
2017-10-25 07:22:31 +08:00
|
|
|
udevDataPath string
|
|
|
|
|
values map[string]string
|
2016-07-05 06:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
2021-02-17 07:19:50 +08:00
|
|
|
func (d *DiskIO) diskInfo(devName string) (map[string]string, error) {
|
2017-10-25 07:22:31 +08:00
|
|
|
var err error
|
|
|
|
|
var stat unix.Stat_t
|
|
|
|
|
|
|
|
|
|
path := "/dev/" + devName
|
|
|
|
|
err = unix.Stat(path, &stat)
|
2016-07-05 06:48:37 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 07:19:50 +08:00
|
|
|
if d.infoCache == nil {
|
|
|
|
|
d.infoCache = map[string]diskInfoCache{}
|
2016-07-05 06:48:37 +08:00
|
|
|
}
|
2021-02-17 07:19:50 +08:00
|
|
|
ic, ok := d.infoCache[devName]
|
2019-12-31 05:52:03 +08:00
|
|
|
|
|
|
|
|
if ok && stat.Mtim.Nano() == ic.modifiedAt {
|
2016-07-05 06:48:37 +08:00
|
|
|
return ic.values, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 23:42:46 +08:00
|
|
|
var udevDataPath string
|
|
|
|
|
if ok && len(ic.udevDataPath) > 0 {
|
|
|
|
|
// We can reuse the udev data path from a "previous" entry.
|
|
|
|
|
// This allows us to also "poison" it during test scenarios
|
|
|
|
|
udevDataPath = ic.udevDataPath
|
|
|
|
|
} else {
|
2021-03-24 23:27:46 +08:00
|
|
|
major := unix.Major(uint64(stat.Rdev)) //nolint:unconvert // Conversion needed for some architectures
|
|
|
|
|
minor := unix.Minor(uint64(stat.Rdev)) //nolint:unconvert // Conversion needed for some architectures
|
2021-02-26 23:42:46 +08:00
|
|
|
udevDataPath = fmt.Sprintf("/run/udev/data/b%d:%d", major, minor)
|
|
|
|
|
|
|
|
|
|
_, err := os.Stat(udevDataPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// This path failed, try the fallback .udev style (non-systemd)
|
|
|
|
|
udevDataPath = fmt.Sprintf("/dev/.udev/db/block:%s", devName)
|
|
|
|
|
_, err := os.Stat(udevDataPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// Giving up, cannot retrieve disk info
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Final open of the confirmed (or the previously detected/used) udev file
|
|
|
|
|
f, err := os.Open(udevDataPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-04-23 05:08:03 +08:00
|
|
|
defer f.Close()
|
2017-10-25 07:22:31 +08:00
|
|
|
|
|
|
|
|
di := map[string]string{}
|
2016-07-05 06:48:37 +08:00
|
|
|
|
2021-02-17 07:19:50 +08:00
|
|
|
d.infoCache[devName] = diskInfoCache{
|
2019-12-31 05:52:03 +08:00
|
|
|
modifiedAt: stat.Mtim.Nano(),
|
2017-10-25 07:22:31 +08:00
|
|
|
udevDataPath: udevDataPath,
|
|
|
|
|
values: di,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scnr := bufio.NewScanner(f)
|
2018-12-14 03:49:19 +08:00
|
|
|
var devlinks bytes.Buffer
|
2016-07-05 06:48:37 +08:00
|
|
|
for scnr.Scan() {
|
|
|
|
|
l := scnr.Text()
|
2018-12-14 03:49:19 +08:00
|
|
|
if len(l) < 4 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if l[:2] == "S:" {
|
|
|
|
|
if devlinks.Len() > 0 {
|
2023-04-03 21:19:43 +08:00
|
|
|
devlinks.WriteString(" ")
|
2018-12-14 03:49:19 +08:00
|
|
|
}
|
2023-04-03 21:19:43 +08:00
|
|
|
devlinks.WriteString("/dev/")
|
|
|
|
|
devlinks.WriteString(l[2:])
|
2018-12-14 03:49:19 +08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if l[:2] != "E:" {
|
2016-07-05 06:48:37 +08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
kv := strings.SplitN(l[2:], "=", 2)
|
|
|
|
|
if len(kv) < 2 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
di[kv[0]] = kv[1]
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-14 03:49:19 +08:00
|
|
|
if devlinks.Len() > 0 {
|
|
|
|
|
di["DEVLINKS"] = devlinks.String()
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-05 06:48:37 +08:00
|
|
|
return di, nil
|
|
|
|
|
}
|
2022-09-28 22:26:14 +08:00
|
|
|
|
|
|
|
|
func resolveName(name string) string {
|
|
|
|
|
resolved, err := filepath.EvalSymlinks(name)
|
|
|
|
|
if err == nil {
|
|
|
|
|
return resolved
|
|
|
|
|
}
|
|
|
|
|
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
|
|
|
|
return name
|
|
|
|
|
}
|
|
|
|
|
// Try to prepend "/dev"
|
2023-08-15 05:18:20 +08:00
|
|
|
resolved, err = filepath.EvalSymlinks("/dev/" + name)
|
2022-09-28 22:26:14 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resolved
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getDeviceWWID(name string) string {
|
|
|
|
|
path := fmt.Sprintf("/sys/block/%s/wwid", filepath.Base(name))
|
|
|
|
|
buf, err := os.ReadFile(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return strings.TrimSuffix(string(buf), "\n")
|
|
|
|
|
}
|