Monitor hard disk usage with Go

Bright Paint

The Update() function, which Listing 2 defines starting in line 59, writes new values to the display. It expects two values: the fill level of the hard disk as an integer and the measured fill speed as a float64 value. It passes the fill level to the Gauge widget of the termui library in line 62 and the fill speed to the pie chart in ui.Pie.

To make the pie chart paint a positive fill speed in red and a negative one in green, lines 63 and 64 sets the colors of the slices in the pie chart to black and red, and lines 66 and 67 modifies the second color to green in case of a negative speed. Because the graph only processes positive values, line 68 reverses the sign of the negative velocities after the color has been adjusted. The termui Render function paints all three widgets onto the terminal's canvas in lines 71 and 72, refreshing the display at intervals of one second.

Now how does the pie chart paint the speedometer reading based on a floating-point value for speed between 0 and 1? It does this by calling the fract() function starting in line 75, which – in turn – produces a fraction using the formula (1-val)/val, which yields the ratio of the speed's colored area (green or red) divided by the size of the black area. For percentage values, lines 77 and 78 also multiply the numerator and denominator values by 100.

In this way, for a floating-point value of 0.35, for example, fract() returns 0.65*100 and 0.35*100, (i.e., 65 and 35). Consequently, the red speedometer segment shown (see Figure 1) occupies about one third of the total circle, and the remaining two thirds are left black on the left side.

Build Time!

You can compile the whole enchilada with the calls from Listing 3, which retrieve the termui UI and its dependencies from GitHub and then go ahead to build the dftop binary. When invoking the finished program from the command line, Listing 3 produces the output from Figures 1 and 2. If you run dftop in a window in a corner of your desktop, you can keep an eye on your remaining hard disk capacity, and prevent overfilling before it's too late.

Listing 3

Compiling

$ go mod init dftop
$ go mod tidy
$ go build dftop.go ui.go

The Author

Mike Schilli works as a software engineer in the San Francisco Bay area, California. Each month in his column, which has been running since 1997, he researches practical applications of various programming languages. If you email him at mailto:mschilli@perlmeister.com he will gladly answer any questions.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Cave Painter

    While searching for a method to draw geodata right into the terminal, Mike Schilli discovers the wondrous world of map projections.

  • Ready to Rumble

    A Go program writes a downloaded ISO file to a bootable USB stick. To prevent it from accidentally overwriting the hard disk, Mike Schilli provides it with a user interface and security checks.

  • hdparm Drive Utility

    Hdparm is the tool to use when it comes to tuning your hard disk or DVD drive, but it can also measure read speed, deliver valuable information about the device, change important drive settings, and even erase SSDs securely.

  • Progress by Installments

    Desktop applications, websites, and even command-line tools routinely display progress bars to keep impatient users patient during time-consuming actions. Mike Schilli shows several programming approaches for handwritten tools.

  • At a Glance

    Using extensions in Go and Ruby, Mike Schilli adapts the WTF terminal dashboard tool to meet his personal needs.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News