Skip to content
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

Add drive model to partitions in health report #229

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion health.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strconv"
Expand Down Expand Up @@ -69,6 +71,12 @@ const (
SrvNotInstalled = "not-installed"
)

const (
sysClassBlock = "/sys/class/block"
devDir = "/dev/"
devLoopDir = "/dev/loop"
)

// NodeInfo - Interface to abstract any struct that contains address/endpoint and error fields
type NodeInfo interface {
GetAddr() string
Expand Down Expand Up @@ -222,6 +230,7 @@ type Partition struct {
Error string `json:"error,omitempty"`

Device string `json:"device,omitempty"`
Model string `json:"model,omitempty"`
Mountpoint string `json:"mountpoint,omitempty"`
FSType string `json:"fs_type,omitempty"`
MountOptions string `json:"mount_options,omitempty"`
Expand All @@ -239,6 +248,40 @@ type Partitions struct {
Partitions []Partition `json:"partitions,omitempty"`
}

func getDeviceModel(partDevice string) (string, error) {
var model string

partDevName := strings.ReplaceAll(partDevice, devDir, "")
partDevPath := path.Join(sysClassBlock, partDevName)
devPath, err := os.Readlink(partDevPath)
if err != nil {
return model, err
}

if !path.IsAbs(devPath) {
devPath = path.Join(sysClassBlock, devPath)
}

devModelPath := path.Join(devPath, "device", "model")

_, err = os.Stat(devModelPath)
if err != nil {
// check parent dir
devModelPath = path.Join(devPath, "..", "device", "model")
_, err = os.Stat(devModelPath)
if err != nil {
return model, err
}
}

data, err := ioutil.ReadFile(devModelPath)
if err == nil {
model = strings.TrimSpace(string(data))
}

return model, err
}

// GetPartitions returns all disk partitions information of a node running linux only operating system.
func GetPartitions(ctx context.Context, addr string) Partitions {
if runtime.GOOS != "linux" {
Expand Down Expand Up @@ -270,8 +313,15 @@ func GetPartitions(ctx context.Context, addr string) Partitions {
Error: err.Error(),
})
} else {
var model string
device := parts[i].Device
if strings.HasPrefix(device, devDir) && !strings.HasPrefix(device, devLoopDir) {
// ignore any error in finding device model
model, _ = getDeviceModel(device)
}

partitions = append(partitions, Partition{
Device: parts[i].Device,
Device: device,
Mountpoint: parts[i].Mountpoint,
FSType: parts[i].Fstype,
MountOptions: strings.Join(parts[i].Opts, ","),
Expand All @@ -280,6 +330,7 @@ func GetPartitions(ctx context.Context, addr string) Partitions {
SpaceFree: usage.Free,
InodeTotal: usage.InodesTotal,
InodeFree: usage.InodesFree,
Model: model,
})
}
}
Expand Down
Loading