gopsutil icon indicating copy to clipboard operation
gopsutil copied to clipboard

Unable to get disks' serial number with IOCounters on linux

Open jbliao opened this issue 4 years ago • 2 comments

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 https://github.com/shirou/gopsutil/blob/8d7a3abddb09aa082668d23e1e50cf719bbf875d/v3/disk/disk_linux.go#L444

The name variable should prepend /dev/

jbliao avatar Sep 30 '21 06:09 jbliao

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"?

shirou avatar Oct 21 '21 12:10 shirou

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

jbliao avatar Oct 23 '21 09:10 jbliao