#!/usr/bin/perl -w ####################################### # gdf - graphically display disk fullness ####################################### use strict; use diagnostics; use Tk; my $debug_on = 0; use vars qw( $dfproc @disks @fs @capac @avail @fullness $bogus $percent @percents ); ######################## # attempt to find Berkely-style df (with percentages) - this is where the displayed info # comes from ######################## if ( -f "/usr/bin/bdf" ) { $dfproc = "/usr/bin/bdf"; } else { $dfproc = "/bin/df"; } if ($debug_on) { print "Using $dfproc\n"; } my $pid = open( DFPIPE, "$dfproc |" ); my $idx = -1; while () { if ( ! $_=~/%/ ) { die "Couldn't find Berkely-style `df' (with percentages)"; } chop; if ($idx >= 0) { ($disks[$idx],$capac[$idx],$bogus,$avail[$idx],$percent,$fs[$idx]) = split; $percents[$idx] = $percent; # convert percentage full to sweep of arc for pie chart chop $percent; $fullness[$idx] = $percent * 3.6; if ($debug_on) { print "$disks[$idx], $capac[$idx], $avail[$idx], $percent, $fullness[$idx], $fs[$idx]\n"; } } $idx++; } ####################################### # Create main window, frame, and canvas ####################################### my $main = MainWindow->new; $main->title('Graphical Disk Fullness'); my $w_frame = $main->Frame(); $w_frame->pack(-side => 'top', -fill => 'y', -expand => 'yes'); my $fullheight = ($idx * 3) + 3.2 . 'c'; my $canvas = $w_frame->Canvas( -scrollregion => ['0c', '0c', '12.1c', $fullheight ], -width => '12.1c', -height => '6c', -relief => 'sunken', -bd => 2 ); $canvas->pack(-expand => 'yes', -side => 'left', -fill => 'y'); my $w_frame_vscroll = $w_frame->Scrollbar(-command => [$canvas => 'yview']); $canvas->configure( -yscrollcommand => [$w_frame_vscroll => 'set']); $w_frame_vscroll->pack(-side => 'right', -fill => 'y'); ####################################### # Begin populating the canvas with interesting stuff. ####################################### use vars qw( $fullpart $emptypart $startpoint $x1 $y1 $x2 $y2 ); my $num_disks = $idx - 1; my $yoffset = 3; $x1 = 0.5; $x2 = 4; # iterate through all disks foreach $idx (0..$num_disks) { # compute arc empty/full proportions $fullpart = $fullness[$idx]; $startpoint = $fullpart; $emptypart = 360 - $fullness[$idx]; if ($debug_on) { print "Full: $fullpart Empty: $emptypart\n"; } #compute Y positioning for framing rect. $y1 = ($idx * $yoffset) + 0.2; $y2 = ($idx * $yoffset) + 2.2 + 1; # draw framing rectangle $canvas->create(("rect", "0.1c", $y1 . 'c', "12c", $y2 . 'c'), -width => 1); #compute Y positioning for pie chart $y1 = ($idx * $yoffset) + 0.6 + 0.6; $y2 = ($idx * $yoffset) + 2.15 + 0.6; # shadow oval beneath chart # 0.5c 0.6c 4c 2.15c $canvas->create(("arc", $x1 . 'c', $y1 . 'c', $x2 . 'c', $y2 . 'c'), -fill => "#000000", qw(-outline black -start 0 -extent 359 -style pieslice -tags item)); #compute Y positioning for pie chart $y1 = ($idx * $yoffset) + 0.5 + 0.6; $y2 = ($idx * $yoffset) + 2 + 0.6; # arc representing full part # 0.5c 0.5c 4c 2c $canvas->create(("arc", $x1 . 'c', $y1 . 'c', $x2 . 'c', $y2 . 'c'), -fill => "red", qw(-outline black -start 0 ), -extent => $fullpart-1, -style => "pieslice", -tags => "item"); # arc representing empty part # 0.5c 0.5c 4c 2c $canvas->create(("arc", $x1 . 'c', $y1 . 'c', $x2 . 'c', $y2 . 'c'), -fill => "green", qw(-outline black), -start => $startpoint, -extent => $emptypart, -style => "pieslice", -tags => "item"); $debug_on and print "$disks[$idx], $capac[$idx], $avail[$idx], $percent, $fullness[$idx], $fs[$idx]\n"; ################################ # print accompanying information ################################ $y1 = ($idx * $yoffset) + 0.6; $canvas->create(( "text", "1c", $y1 . 'c'), -text => "DISK: $disks[$idx]", -anchor => "w"); $canvas->create(( "text", "6c", $y1 . 'c'), -text => "Filesystem: $fs[$idx]", -anchor => "w"); $y1 = ($idx * $yoffset) + 1.1; $canvas->create(( "text", "6c", $y1 . 'c'), -text => "Capacity: $capac[$idx] KB", -anchor => "w"); $y1 = ($idx * $yoffset) + 1.6; $canvas->create(( "text", "6c", $y1 . 'c'), -text => "Available: $avail[$idx] KB", -anchor => "w"); $y1 = ($idx * $yoffset) + 2.1; $canvas->create(( "text", "6c", $y1 . 'c'), -text => "Usage is $percents[$idx]", -anchor => "w"); } # put the "Dismiss" button at the bottom my $btn_frame = $main->Frame(); $btn_frame->pack(-side => 'bottom', -fill => 'x', -expand => 'no'); # This is VERY important ^^^^^^^^^^^^^^^ my $w_dismiss = $btn_frame->Button( -text => 'Dismiss', -command => [$main => 'destroy']); $w_dismiss->pack(-side => 'bottom', -fill => 'x', -expand => 1); $main->resizable( 0,1 ); MainLoop;