Fork me on GitHub
tclbsd
by flightaware
Tcl bindings for BSD system calls and library routines

Overview

This is tclbsd, a Tcl C extension written to provide Tcl bindings to various BSD UNIX system calls and library routines.

tclbsd provides interfaces to get the system load average (getloadavg), get and set hard and soft resource limits (rlimit), obtain the resource usage for the current process or its children (rusage), set the process title (setproctitle), get information about mounted filesystems (statfs and getfsstat), log to the system log (syslog), abort the process (abort), and obtain system clock counts (cptime).

Code

Get it on github: http://github.com/flightaware/tclbsd

Support

You can sometimes find tclbsd on IRC in the #tcl channel on the Freenode network or you can file issues with github here.

Download

You can also clone the project with Git by running:

$ git clone git://github.com/flightaware/tclbsd

Using tclbsd

package require BSD
    
Get load average. Three elements are returned, each representing the average number of processes is the system queue averaged over one, five and fifteen minutes, respectively.
lassign [::bsd::getloadavg] load1 load5 load15
    
Get resource usage. A list of key-value pairs is returned.
array set rusage [::bsd::rusage]
parray rusage
    
...yielding...
rusage(fileSystemInputs)           = 0
rusage(fileSystemOutputs)          = 0
rusage(involuntaryContextSwitches) = 1
rusage(ipcMessagesReceived)        = 0
rusage(ipcMessagesSent)            = 0
rusage(maxResidentSetSizeKbytes)   = 4116
rusage(nSignalsDelivered)          = 0
rusage(nTimesSwapped)              = 0
rusage(pageFaults)                 = 0
rusage(pageReclaims)               = 546
rusage(sharedTextKbyteSeconds)     = 0.25
rusage(stackKbyteSeconds)          = 8.0
rusage(systemTimeUsed)             = 0.01409
rusage(unsharedDataKbyteSeconds)   = 64.0
rusage(userTimeUsed)               = 0.042272
rusage(voluntaryContextSwitches)   = 89
	
Set process title. Pass an argument, pretty simple.
::bsd::setproctitle "this will show up in ps but usually not top"
yields something like
  PID  TT  STAT      TIME COMMAND
20338   2  IWs    0:00.00 /usr/local/bin/bash
16678   4  Is+    0:00.12 -bash (bash)
70797   8  I+     0:00.95 vim index.html
71288  10  S+     0:00.06 this will show up in ps but usually not top (tclsh8.5)
71431  10  R+     0:00.00 /bin/ps
98222  17- I     28:21.37 peekaboo
statfs returns information about a mounted filesystem...
array set statfs [::bsd::statfs /usr]
parray statfs
yielding
statfs(availableFreeBlocks)            = 248899883
statfs(fileSystemType)                 = ufs
statfs(flags)                          = softUpdates filesystemStoredLocally
statfs(freeBlocks)                     = 280057534
statfs(freeFileNodes)                  = 96751664
statfs(fundamentalFileSystemBlockSize) = 2048
statfs(mountPoint)                     = /usr
statfs(mountedFileSystem)              = /dev/mfid0s1f
statfs(totalDataBlocks)                = 389470645
statfs(totalFileNodes)                 = 100708350

Likewise, ::bsd::getfsstat returns a list of lists for all mounted filesystems. Note you can past getfsstat a -wait or -nowait argument -- check the manpage for details.

The rlimit, syslog, abort and cptime commands are covered in the manpage.

License

This software is copyrighted by FlightAware LLC, 
and includes modified sample extension code copyright Scriptics 
corporation, and other parties.  The following terms apply to all files 
associated with the software unless explicitly disclaimed in individual 
files.

$Id: license.terms,v 1.2 2005-11-21 11:38:11 karl Exp $

The author hereby grants permission to use, copy, modify, distribute,
and license this software and its documentation for any purpose, provided
that existing copyright notices are retained in all copies and that this
notice is included verbatim in any distributions. No written agreement,
license, or royalty fee is required for any of the authorized uses.
Modifications to this software may be copyrighted by their authors
and need not follow the licensing terms described here, provided that
the new terms are clearly indicated on the first page of each file where
they apply.

IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
MODIFICATIONS.

GOVERNMENT USE: If you are acquiring this software on behalf of the
U.S. government, the Government shall have only "Restricted Rights"
in the software and related documentation as defined in the Federal
Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you
are acquiring the software on behalf of the Department of Defense, the
software shall be classified as "Commercial Computer Software" and the
Government shall have only "Restricted Rights" as defined in Clause
252.227-7013 (c) (1) of DFARs.  Notwithstanding the foregoing, the
authors grant the U.S. Government and others acting in its behalf
permission to use and distribute the software in accordance with the
terms specified in this license.