[HELP] How do I read SMART data from disk?

1,163 views
Skip to first unread message

Hotei

unread,
Mar 22, 2012, 10:59:03 AM3/22/12
to golan...@googlegroups.com
Was wondering if anyone has done/seen Go code to read SMART data from hard drive?  I'm looking for temperature and sector-relocation count initially.  I can get it by running a command line app and parsing the result, but was hoping not to have to crank up that many processes just to get two integers. 

Any pointers would be appreciated.


minux

unread,
Mar 22, 2012, 11:59:09 AM3/22/12
to Hotei, golan...@googlegroups.com
On Thu, Mar 22, 2012 at 10:59 PM, Hotei <hote...@gmail.com> wrote:
Was wondering if anyone has done/seen Go code to read SMART data from hard drive?  I'm looking for temperature and sector-relocation count initially.  I can get it by running a command line app and parsing the result, but was hoping not to have to crank up that many processes just to get two integers. 

Any pointers would be appreciated.
you can strace(1) the command line app and find out how to read these data. 

Jeff R. Allen

unread,
Mar 23, 2012, 7:15:16 AM3/23/12
to golan...@googlegroups.com, Hotei


On Thursday, March 22, 2012 4:59:09 PM UTC+1, minux wrote:
you can strace(1) the command line app and find out how to read these data. 

Out of curiosity, I did just that, and I found that smartctl is using ioctl SG_IO to do SCSI Generic commands on the device:

4602  ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[6]=[12, 00, 00, 00, 24, 00], mx_sb_len=32, iovec_count=0, dxfer_len=36, timeout=20000, flags=0, data[36]=["\0\0\5\2[\0\0\0ATA     TOSHIBA THNSNC12"...], status=00, masked_status=00, sb[0]=[], host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0

(The request and reply are emboldened.)

You can see an example of how to access ioctl from Go in the exp/terminal package: http://code.google.com/p/go/source/browse/src/pkg/exp/terminal/util.go

If you want to make this portable, you'll need to think about making a package in Go that can do SCSI Generic commands, hiding the platform-dependent parts inside of it. Take a look at the source to the net package to see how to make one Go package that's got OS-dependent implementations of the same external API.

Then you can use your new package to talk S.M.A.R.T. with your device.

I think that project would be a very good fit for Go; at the low levels, you need control of the bits to do the ioctl, and once you get the data, you have all the niceities of Go to transform it, store it in a database, graph it, etc. Go for it!

 -jeff

daniel.s...@gmail.com

unread,
May 13, 2017, 10:01:26 PM5/13/17
to golang-nuts, hote...@gmail.com
I realise this is a very old thread, but I am in the early stages of writing a pure Go library for reading SMART attributes from HDDs / SSDs. So far it works for simple SCSI block devices (i.e., those that have a sd* device name) via SCSI pass-through, and I am in the process of adding support for devices attached to MegaRAID controllers.

You can find the code at https://github.com/dswarbrick/smart

cube...@gmail.com

unread,
May 21, 2017, 3:21:49 AM5/21/17
to golang-nuts, hote...@gmail.com
Liking it so far. I may look into assisting with getting additional controllers added to this (aacraid/adaptec, 3ware, etc), as this is something I've been needing for awhile. I just need to freshen up a bit on the direct scsi interactions and similar.
Reply all
Reply to author
Forward
0 new messages