Linux usage edit
sudo tclsh ssd-info.tcl /dev/sdX ?-nogui?Installation edit
Requirements
You will need Tcl 8.5 or later, Tk and smartctl installed to run ssd-info. All are likely to be available to install from your Linux distribution's package repositories.Installing with wiki-reaper
wiki-reaper -x 41244 0 | tee ssd-info.tcl && chmod +x ssd-info.tclorwiki-reaper -x 41244 0 | sudo tee /usr/local/bin/ssd-info && sudo chmod +x /usr/local/bin/ssd-infoWarning: Make sure to review the code before running it as root.Screenshot edit
Code edit
#!/usr/bin/env tclsh # ssd-info, a utility that reports the remaining write resource of an # SSD drive. # Copyright (c) 2015, 2016 dbohdan # License: MIT namespace eval ssd-info { namespace import ::tcl::mathop::* variable version 0.1.2 variable message {%1$s has %2$s%% of its write resource remaining.} } # Get the value of the SMART vendor attribute $name for $device. proc ::ssd-info::get-attribute {device name} { set lines [split [exec smartctl -A $device] \n] set line [lsearch -inline -glob $lines "*$name*"] set value [string trimleft [lindex $line 3] 0] if {![string is integer -strict $value]} { error "$device has no attribute $name" } return $value } # Get SSD wearout value using one of the two common vendor attributes. proc ::ssd-info::get-wearout {device} { set result {} set error [catch { set result [get-attribute $device Remaining_Lifetime_Perc] }] if {$error} { # Intel SSD. set result [get-attribute $device Media_Wearout_Indicator] } } # Linear interpolation. proc ::ssd-info::interpolate-color {color1 color2 {x 0.5}} { set result {} foreach v1 $color1 v2 $color2 { lappend result [expr { round($v1 * (1 - $x) + $v2 * $x) }] } return $result } # Draw a progress bar-like gradient with text ${value}% over it. proc ::ssd-info::draw-bar {canvas value color {steps 10} {font barFont}} { set width [$canvas cget -width] set height [$canvas cget -height] set barWidth [/ [* $value $width] 100.0] set stepSize [/ $barWidth $steps] set color1 {} foreach x $color { lappend color1 [/ $x 2.0] } set color2 $color # Create a gradient out of rectangles. for {set step 0} {$step < $steps} {incr step} { set color [interpolate-color \ $color1 \ $color2 \ [expr { (1.0 * $step) / ($steps - 1) }]] $canvas create rectangle \ [* $step $stepSize] 0 [* [+ 1 $step] $stepSize] $height \ -width 0 \ -fill [format "#%02x%02x%02x" {*}$color] } $canvas create text \ [/ $barWidth 2] \ [/ $height 2] \ -text "[expr {round($value)}]%" \ -font $font \ -fill white } # Display a GUI showing device wear. proc ::ssd-info::gui {device wear} { variable message package require Tk wm title . "ssd-info" canvas .canvas -width 800 -height 50 ::ttk::label .status \ -text [format $message $device $wear] set font [font actual .canvas] dict set font \ -size [* 2 [dict get $font -size]] font create barFont {*}$font ::ssd-info::draw-bar \ .canvas \ $wear \ [interpolate-color {128 0 0} {0 128 0} [/ $wear 100.0]] \ 100 pack .canvas pack .status } # Produce text output reporting device wear. proc ::ssd-info::report {device wear} { variable message puts [format $message $device $wear] } # From http://wiki.tcl.tk/40097. proc ::ssd-info::main-script? {} { global argv0 if {[info exists argv0] && [file exists [info script]] && [file exists $argv0]} { file stat $argv0 argv0Info file stat [info script] scriptInfo expr {$argv0Info(dev) == $scriptInfo(dev) && $argv0Info(ino) == $scriptInfo(ino)} } else { return 0 } } proc ::ssd-info::main {argv0 argv} { lassign $argv device if {![file exists $device]} { puts "usage: $argv0 device ?-nogui?" exit 0 } set wear [::ssd-info::get-wearout $device] if {[string trimleft [lindex $argv 1] -] ne "nogui"} { gui $device $wear } else { report $device $wear } } if {[::ssd-info::main-script?]} { ::ssd-info::main $argv0 $argv }
AMG: Rather than exec'ing awk in the pipeline, I suggest using Tcl's built-in string manipulations.
proc ::ssd-info::get-attribute {device name} { set name [regsub -all {[][*+?{}()<>|.^$\\]} $name {\\&}] if {![regsub [format {.*\n\s*\S+\s+%s\s+\S+\s+(\S+).*} $name]\ [exec smartctl -A $device] {\1} result]} { error "$device has no attribute $name" } return $result }Feel free to delete the second line (set name ...). I only threw that in there to give a more complete example of building a regular expression that (in part) matches a literal string, even if that literal string contains what would normally be interpreted as metacharacters.dbohdan: I think there is nothing wrong in principle with using Awk in Tcl software as long as it is only intended to run on POSIX operating systems. The problem is generating Awk scripts at runtime. If values of $name were not limited to the characters a-zA-Z0-9_ the pipeline would be error-prone.I have replaced the Awk pipe with an alternative that uses split and lsearch, which I consider closer in spirit to the Awk original (regsub is more like sed). One could have also used ::textutil::splitx here.The original proc is preserved below:
# Get the value of the SMART vendor attribute $name for $device. proc ::ssd-info::get-attribute {device name} { set value [string trimleft \ [exec smartctl -A $device | awk "/$name/ { print \$4 }"] 0] if {![string is integer -strict $value]} { error "$device has no attribute $name" } return $value }AMG: Ah, [lsearch]. Good choice. I tend to forget just how ridiculously many things it can do.