Perl Programming


HOME HTML Tree Enhancing vi Super Sorts Search Subdirs CPU Hogs

INDEX

HOME
Content...
About this site...
HTML Tree
Super Sorts
Enhancing vi
Search Subdirs
CPU Hogs

Graphical df using Perl/Tk


I've been working on this script in various versions since 1996, but here at last is one I feel is sufficiently refined to make available. At the time I wrote this I was responsible for a corporate database and webserver, and needed to be able to tell at a glance if one of the several system disks was getting full.

Granted, Berkeley df does a pretty good job of giving percentages in a hurry, but then, I also wanted to get a handle on Perl/Tk, a wonderful porting of the popular Tcl/Tk toolkit to Perl by Nick Ing-Simmons.

Compared to building GUI code with an X11 library such as Athena or Motif, GUI development in a scripted language is a breeze. I highly recommend it for all small to medium-scale GUI applications. Using the object oriented features of Perl 5, even large-scale GUI applications are within reach. I decided to start small (and useful) however, with a pie-chart display of Disk Fullness.

So How Does it Work?

gdf doesn't do anything terribly sophisticated for a Perl GUI application. It creates a scrolled canvas. It then runs the Berkeley version of df on your system, and captures the output. Each line returned is formatted into a new "segment" on the display. A segment consists of the following widgets:

  • A framing rectangle for the disk's information
  • A "shadow oval" to give the pie chart some relief
  • A green "filled arc" to represent the empty portion
  • A red filled arc to represent the full portion
  • Text widgets to list the disk name, filesystem name, and useage stats

The Basics

Perl/Tk is another name for a module for Perl called Tk.pm. There are several versions available. Jump to the CPAN Tk Module Directory to get a look at what's available. I recommend the latest version of Tk800 to get the full functionality of the graphics toolkit.

The quickest way to get Tk installed is using the "-MCPAN" option for Perl. This tells Perl to download and install the module you need directly from CPAN. The only drawback is that if you're on an RPM dependent system, you'll have to remember to perform this command after restoring or reinstalling Linux. The command is:

perl -MCPAN -e 'install Tk'

If you haven't used -MCPAN with Perl before, you will be prompted with several questions at first. Most of them can be answered with the enter key (to accept the default).

Programming with Perl/Tk is more straightforward than writing X11 graphics in C/C++ for several reasons. Here's a few of them:

  • Instant feedback - Scripting your GUI in perl skips the time-consuming compile/link/execute cycle
  • Simplified API - Much more of the UI functionality is managed behind the scenes
  • Perl's Richness - Hundreds of pre-written libraries are freely available
  • Good Documentation - Just about everything you need is there, for free

Here's some sources for Perl/Tk documentation.

  1. The pod/html documentation that comes with Tk800. To get it, download and install Tk, such as Tk800.022.tar.gz. Read the installation instructions and perform the installation. Once there, point your browser to the html directory, and start reading. This will be in ./pod/html from the installation's top level directory.
  2. The Perl/Tk FAQ. This can be found online at www.cpan.org.
  3. The book Learning Perl/Tk by Nancy Walsh. O'reilly, 1st Ed. January 1999, ISBN 1-56592-314-6.

To Do...

A couple of people who like gdf have requested that I include a button for updating the displayed information, or maybe have it periodically refresh the data it displays.

I had not thought of making it work this way initially since it more or less duplicates the behavior of the df command graphically: run it once, get one set of output. However, I do see the point of allowing updates. If one leaves gdf on the display for a long time, it will cease to show up-to-date information.

Addressing this requirement will be a significant update. The code which generates the pie arcs and text labels on the canvas will need to be encapsulated and moved to a separate subroutine (or callback) which can be executed independently of the initialization. In addition, "erase" routines may need to be added. These would be needed if simply re-drawing the pie arcs and labels is not sufficient to hide the remnants of the previous set.

Download gdf

Click here to download gdf.


The above text and scripts are Copyright 2000 © by Scott Chilcote.

HOME HTML Tree Enhancing vi Super Sorts Search Subdirs CPU Hogs