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

Unable to get disks' serial number with IOCounters on linux #1139

Closed
jbliao opened this issue Sep 30, 2021 · 2 comments
Closed

Unable to get disks' serial number with IOCounters on linux #1139

jbliao opened this issue Sep 30, 2021 · 2 comments

Comments

@jbliao
Copy link

jbliao commented Sep 30, 2021

Describe the bug
IOCountersStat.SerialNumber always have no value on linux

I use telegraf to monitor disk which use this go module to get disk info, and the serial of disk always be null.

To Reproduce

package main

import (
    "github.com/shirou/gopsutil/v3/disk"
    "fmt"
)

func main() {
    info, err := disk.IOCounters("/dev/sda")
    if (err != nil) {
        fmt.Print(err)
    }
    fmt.Println(info)

    serialNumber, err := disk.SerialNumber("/dev/sda")
    if (err != nil) {
        fmt.Print(err)
    }
    fmt.Println(serialNumber)

    serialNumber, err = disk.SerialNumber("sda")
    if (err != nil) {
        fmt.Print(err)
    }
    fmt.Println(serialNumber)

}                                 

result:

map[sda:{"readCount":398790,"mergedReadCount":1,"writeCount":613726,"mergedWriteCount":360,"readBytes":20676746240,"writeBytes":55764192768,"readTime":3302034,"writeTime":2927377,"iopsInProgress":0,"ioTime":6360176,"weightedIO":8757288,"name":"sda","serialNumber":"","label":""}]
INTEL_SSDSC2BW240A4_CVDA4152****<CENSORED>***
no such file or directory

Expected behavior
disk.IOCounters should contain serial number of disks.

Environment (please complete the following information):
Linux: [paste contents of /etc/os-release and the result of uname -a]

  • cat /etc/os_release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
  • uname -a
Linux truenas.local 5.10.42+truenas #1 SMP Mon Aug 30 21:54:59 UTC 2021 x86_64 GNU/Linux

Additional context
Actually this line will fail every time when it was called from IOCounters

err := unix.Stat(name, &stat)

The name variable should prepend /dev/

@shirou
Copy link
Owner

shirou commented Oct 21, 2021

According to your report, disk.SerialNumber("/dev/sda") seems succeeded (and I confirmed it works on my local). Why do you want to specify "sda"?

@jbliao
Copy link
Author

jbliao commented Oct 23, 2021

Let me clarify the problem:
I want to use disk.IOCounters() to get disk metrics and disks' serial number. So I called the disk.IOCounters() function with parameter names be something like ["/dev/sda", "/dev/sdb"...].
And in the disk.IOCountersWithContext(), the d.SerialNumber is assigned by disk.SerialNumberWithContext() with name as parameter and ignoring errors.
But the name variable gets from /proc/diskstats in sd* form. So it always call disk.SerialNumberWithContext(ctx, "sd*") and return nil. Resulting the d.SerialNumber always be nil.

My /proc/diskstats content:

   8      32 sdc 6508176 2140 343843184 128561381 47193964 128738 10152207944 88815557 0 129623272 259779266 0 0 0 0 617986 42402327
   8      33 sdc1 4141977 1731 198099728 59194364 34480392 56622 1384411560 16132962 0 75348828 75327327 0 0 0 0 0 0
   8      34 sdc2 803820 409 145191421 25014458 12713572 72116 8767796384 72682595 0 36338908 97697053 0 0 0 0 0 0
   8      16 sdb 9370854 6991 875144226 10833192 26557788 865337 2008727912 14142041 0 47574900 27519676 0 0 0 0 1268299 2544441

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants