As it often happens, I jumped ahead to reinvent the wheel that already exists in smartmontools
package. This package provides whole lot of diagnostic tools for hard-drives and SSDs (or in more technical terms, SCSI devices). In particular, smartctl
command is what we're looking for:
$ sudo smartctl -A /dev/sda
[sudo] password for xieerqi:
smartctl 6.5 2016-01-24 r4214 [x86_64-linux-4.4.0-65-generic] (local build)
Copyright (C) 2002-16, Bruce Allen, Christian Franke, www.smartmontools.org
=== START OF READ SMART DATA SECTION ===
SMART Attributes Data Structure revision number: 18
Vendor Specific SMART Attributes with Thresholds:
ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE
5 Runtime_Bad_Block 0x0000 000 000 000 Old_age Offline - 0
9 Power_On_Hours 0x0000 100 100 000 Old_age Offline - 8080
12 Power_Cycle_Count 0x0000 100 100 000 Old_age Offline - 1419
171 Avail_OP_Block_Count 0x0000 100 100 000 Old_age Offline - 40907856
174 Pwr_Cycle_Ct_Unplanned 0x0000 100 100 000 Old_age Offline - 78
195 Total_Prog_Failures 0x0000 100 100 000 Old_age Offline - 0
196 Total_Erase_Failures 0x0000 100 100 000 Old_age Offline - 0
197 Total_Unc_Read_Failures 0x0000 100 100 000 Old_age Offline - 0
208 Average_Erase_Count 0x0000 100 100 000 Old_age Offline - 179
210 SATA_CRC_Error_Count 0x0000 100 100 000 Old_age Offline - 0
224 In_Warranty 0x0000 100 100 000 Old_age Offline - 1
233 Remaining_Lifetime_Perc 0x0000 095 095 000 Old_age Offline - 95
241 Host_Writes_GiB 0x0000 100 100 000 Old_age Offline - 3133
242 Host_Reads_GiB 0x0000 100 100 000 Old_age Offline - 2745
249 Total_NAND_Prog_Ct_GiB 0x0000 100 100 000 Old_age Offline - 301548705
What you see above is list of attributes for my SSD. Of particular interest is attribute 241 (Host_Writes_GiB) and 242 (Host_Reads_GiB). These values are in GiB (which is a classical unit of measure in computer science in powers of 1024 bytes, in this case 1024^3, instead of SI units of powers of 10).
On the low level, as far as I understand, this utility parses actual lifetime_write_kbytes
file in /sys/fs
folder. Particularly, in my example:
$ cat /sys/fs/ext4/sda1/lifetime_write_kbytes
4793857835
Of course, this is a command-line utility. If you don't have it installed (check via apt-cache policy smartmontools
) then install it via sudo apt-get install smartmontools
. There is Gnome Disks Utility as GUI alternative and it comes with Ubuntu.
There are couple caveats however:
- SMART information has to be provided by the device. If the device is fairly cheap and doesn't have such information, then it would require reinventing the wheel just like I originally wanted to do via monitoring
/proc/diskstats
file.
- The specific attributes must be provided by the device. For instance, information on tnfd22 ssd found on smartmontools website doesn't provide such information.
There's also a bit of a problem: the resulting information shown is total history of reads/writes. Thus, you cannot extract reads/writes within last x number of days. At least it's not possible with smartctl
or none of the tools I found thus far. However, what you can do, is schedule a daily cron
task that will read /sys/fs/ext4/sda1/lifetime_write_kbytes
and append the data with time stamp to a file. Later on, you can subtract differences to see how much was written in each day. For instance, contents of such command would be:
30 08 * * * bash -c '{ date; cat /sys/fs/ext4/sda1/lifetime_write_kbytes; echo ;} >> /home/user/my_disk_log.txt'