lassign does what I need. I now have the following:
#!/usr/bin/env tclsh
### Improvements
# Get database from conf-file
package require sqlite3
if {$argc != 1} {
error "Error: ${argv0} DATABASE"
}
sqlite db [lindex $argv 0]
db timeout 10000
set insertVmstat "
INSERT INTO vmstat (
runlength,
-- procs
runable, uninteruptable,
-- memory
swap, free, buffers, cache,
-- swap
swapIn, swapOut,
-- io
blockIn, blockOut,
-- system
interuptsPerSec, contextSwitchesPerSec,
-- cpu
userTime, systemTime, idleTime, waitTime, stolenTime
) VALUES (
:runLength,
:runable, :uninteruptable,
:swap, :free, :buffers, :cache,
:swapIn, :swapOut,
:blockIn, :blockOut,
:interuptsPerSec, :contextSwitchesPerSec,
:userTime, :systemTime, :idleTime, :waitTime, :stolenTime
);
"
set runLength 60
puts "Using an interval of ${runLength} seconds"
after [expr {1000 * (60 - [clock seconds] % 60)}]
set vmstat [open "|vmstat -n ${runLength}"]
# The first three lines need to be skipped
for {set i 0} {${i} < 3} {incr i} {
gets ${vmstat}
}
while {true} {
lassign [gets ${vmstat}] \
runable uninteruptable \
swap free buffers cache \
swapIn swapOut \
blockIn blockOut \
interuptsPerSec contextSwitchesPerSec \
userTime systemTime idleTime waitTime stolenTime
db eval ${insertVmstat}
}
# Not really necessary because the above loop never ends
# But I find this more clear and is robuster against change
close vmstat
db close