As promised, before we leave the world of Tk, we’ll create our own small application for measuring Oracle table space usage. Building this tool, which we’ll call TableSpacer, will extend our use of Tk beyond using downloaded applications as is. This example builds on two excellent Tcl/Tk products: George Howlett’s BLT, described in this section, and Tom Poindexter’s Oratcl, already described in Chapter 3.
BLT is an extension to Tcl/Tk that adds combinations of plotting widgets (graphs, barcharts, etc.) to Tk canvases, saving you hundreds—possibly thousands—of lines of code when creating a typical on-the-fly graph within a Tk canvas object. (Not only is BLT one of the best chart-making packages around, but also the letters BLT can apparently stand for anything you want them to.)
The following are the main web sites for BLT:
- http://www.tcltk.com/blt
General information on the BLT package, upgrades, latest news, and so on.
- ftp://ftp.tcltk.com/pub/blt/
Central download site for the latest BLT code releases and self-installing pre-compiled Windows executable files.
- ftp://tcltk.sourceforge.net/pub/tcltk/blt/
SourceForge site for all things Tcl/Tk. In this directory you will find two very useful PDF tutorial files (it’s the same tutorial in different layouts), handouts.pdf and slides.pdf.
BLT is also described in Paul Raines and Jeff Tranter’s Tcl/Tk in a Nutshell, referenced in Appendix C.
Before you can install BLT, you must have the basic Tcl/Tk environment set up, as we described in Chapter 3. Then follow these steps:
Download BLT from the download site listed earlier in this section.
For the standard installation, issue these commands:
$ gzip -d bltx.y.tar.gz $ tar xvf bltx.y.tar (this will create the bltx.y directory) $ cd bltx.y $ ./configure --with-tcl=/usr/local/tcl -with-tk=/usr/local/tk
There are further switches available for the configuration process. The two shown above assume that you have your previous Tcl and Tk installations in their own directories. (For customized installations, check out the
README
andINSTALL
files.)Carrying on with the basic installation, run make and change directory as follows:
$ make $ cd demos
Before final installation, check that BLT has compiled correctly by running one or two of the example scripts. For example:
$ ./graph3.tcl
In Figure 4-30, we’ve run this demo graph under Windows. If this works out OK for you too, carry out the final step:
$ make install
This final step will install the necessary files in the
/usr/local/blt
directory (unless directed otherwise with a customized configuration -prefix=my_dir
switch).
Windows users can visit the http://www.tcltk.com site to find self-installing,
precompiled versions of BLT, such as
blt2.4u-for-8.3.exe.
You can also compile BLT
under Windows using various compilers, such as Microsoft VC++ 6.0
(see the INSTALL
file again for more details).
Before installing BLT on Win32, you must install Tcl/Tk for Win32
first. This is because the key BLT chart-generating program,
bltwish
, relies upon various Win32 DLL files
already supplied by Tcl/Tk. If these DLL files are not present, BLT
will simply not work.
In Figure 4-30, we’ve installed BLT under
Win32 and then run graph3.tcl
from the
/demos
directory of the standard source code
installation tarball, which you may wish to download separately. You
can also find these Windows self-installers at:
ftp://tcltk.sourceforge.net/pub/tcltk/blt/ |
If you can think of a graph, BLT can probably produce it. BLT’s capabilities range from the most complex NASA Mars mission data charts to the simplest tablespace barcharts. However, BLT isn’t just about graphs. Figure 4-31 demonstrates what typical spline and barchart graphs look like, and Table 4-4 covers the Tcl extension capabilities of BLT. We’re only touching quickly on its capabilities here, though, and we urge you to learn more about this tool.
Table 4-4. BLT Command Summary
In building the TableSpacer code in Example 4-4, we wanted a simple program based on Oratcl that would provide us with important at-a-glance information on tablespace usage within our database. If possible, we also wanted to be able to drill down on this data. To provide this functionality, we coded the program shown here (the corresponding comments should help you navigate your way through the code). The program performs the following steps:
The necessary Tcl extensions are picked up (e.g., Oratcl and BLT are both available under Unix and Win32).
Oratcl is used to log into the target database.
A SQL cursor supplies the required graphical information.
The cursed output information is plugged into graphical elements via BLT.
Finally, the graph is displayed.
Magical BLT graphical widgets are used to provide zooming capabilities.
Example 4-4. TableSpacer.tcl
#!/usr/local/bin/wish8.4 package require Tcl package require Tk# Step 1 - Pick up the required packages Oratcl and BLT
package require Oratcl
package require BLT
# If we're on Tcl8.x, make use of the namespaces if { $tcl_version >= 8.0 } { namespace import blt::* namespace import -force blt::tile::* }# Step 2 - Log on to the database
set userid "system/manager"
set env(ORACLE_SID) "orcl"
set handle [oralogon $userid]
set retcode [catch {set handle [oralogon $userid]}]
# Use BLT to initialize our graph. set graph .graph option add *graph.xTitle "Percentage Used" option add *graph.yTitle "Tablespace" option add *graph.title \ "$env(ORACLE_SID) Tablespace Space Usage Percentages" option add *graph.elemBackground white option add *graph.elemRelief raised set visual [winfo screenvisual .] option add *print.background white # Now create the barchart, and configure the labels barchart $graph $graph xaxis configure -command FormatLabel -descending true $graph legend configure -hide yes # Build our Oracle SQL cursor set query_cursor [oraopen $handle] orasql $query_cursor {select a.tablespace_name,
round((nvl(100-(sum(nvl(a.bytes,0))/
(sum(nvl(b.bytes,0))/count(*)))*100,0)),0)
from dba_free_space a, dba_data_files b
where a.tablespace_name = b.tablespace_name and
a.file_id = b.file_id
group by a.tablespace_name, a.file_id
order by 1
} set row [orafetch $query_cursor] global names( ) global values( ) set i 0# Step 3 - Run through the cursor and fill various arrays
while {$oramsg(rc) == 0} {set names($i) [lindex $row 0]
set values($i) [lindex $row 1]
puts [format "\n%20s %s" $names($i) $values($i)] set row [orafetch $query_cursor] incr i } global numTabsps set numTabsps [ array size names ]# Step 4 - Arrays now filled, build up the graph.
for { set i 0} { $i < $numTabsps } { incr i } {$graph element create $i \
-data { { $numTabsps - $i } $values($i) } \
-bg black \
-relief raised \
-bd 2
}# Step 5 - Finish off the graph
table . \
.graph 1,0 -fill both
table configure . r0 r2 -resize none
wm min . 0 0
# Step 6 - Add several BLT Graphical goodies :-)
Blt_ZoomStack $graph
Blt_Crosshairs $graph
Blt_ActiveLegend $graph
Blt_ClosestPoint $graph
# Format the column labels and stop them being numeric proc FormatLabel { w value } { global names global numTabsps set i [expr $numTabsps - $value] return $names($i) } # That's all there is to it!!! 8-)
Figure 4-32 shows TableSpacer in action.
Before running the program, make
sure that the standard
wish
Tcl/Tk you’re using is able to
display the chart, and also be sure that Oratcl can link up to the
required Oracle libraries, as described earlier with Oddis:
$ DISPLAY=:0.0 $ export DISPLAY $ LD_LIBRARY_PATH=/usr/local/lib:$ORACLE_HOME/lib $ export LD_LIBRARY_PATH
Under Unix (or Linux), the program should now run properly:
$ ./TableSpacer.tcl
On Win32 systems, the same code will produce
similar results (remember
to use the bltwish
program installed earlier, because the
standard wish
program will simply report
errors):
C:\Temp>bltwish
TableSpacer.tcl
You can see the final TableSpacer program in action in Figure 4-32 for both Linux and Win32. Although the code in Example 4-4 is slightly involved, you may agree with us that this is a lot of bangs per buck. However the real Merlinesque magic of BLT is contained within the following program calls:
Blt_ZoomStack
|
Blt_Crosshairs
|
Blt_ActiveLegend
|
Blt_ClosestPoint
|
In Figure 4-33, we’ve demonstrated how, among many other things, these can give you the possibility of zooming in and out of your on-the-fly graphs ad infinitum via either right-clicks or left-clicks of your mouse.
Get Oracle and Open Source now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.