2011/9/27 Patrick Proniewski <pat...@patpro.net>:
> Hello,
>
> I'm facing a challenging problem. I want to log some data into an SQLite3 DB. 
> Data come from a system command (iostat) in an endless steam, one row every X 
> seconds:
>
>         disk0
>   KB/t tps  MB/s
>   4.02 2318 9.09
>   4.00 1237  4.83
>   6.63 979  6.34
>  46.30  15  0.69
>  30.58  23  0.69
>  12.90  32  0.41
>  107.85  55  5.75
>
> I though I could easily pipe data into SQLite:
>
> iostat -d -w 10 disk0 |\
> awk '!/[a-zA-Z]/ {print "INSERT INTO io 
> VALUES(datetime(\"now\",\"localtime\"),"$1","$2","$3");"}' |\
> sqlite3 iostat.db
>
> but it won't work, because sqlite3 won't record any data until the iostat 
> command ends. And of course, this iostat command will never end.
> So I'm stuck with a working but very ugly script:
>
> while true; do
>  iostat -c 2 -d -w 10 disk0 |\
>  tail -1 |\
>  awk '!/[a-zA-Z]/ {print "INSERT INTO io 
> VALUES(datetime(\"now\",\"localtime\"),"$1","$2","$3");"}' |\
>  sqlite3 iostat.db
> done
>
> endless loop, forking iostat for 2 rows of data (-c 2), keeping only the last 
> row because the first one is an artifact (tail -1).
> I've tried various solutions with named pipes, file descriptors redirections… 
> but none worked, because they all seem to require the data steam to end 
> before feeding data into the DB.
>
> Any idea?
>
> regards,
> patpro
> _______________________________________________
> sqlite-users mailing list
> sqlite-users@sqlite.org
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>

You don't need awk :)


iostat -d -w 10 disk0 | while read a b c; do case $a in *[a-zA-Z]*)
continue ;; *) sqlite3 iostat.db "INSERT INTO io
VALUES(datetime('now', 'localtime'), \"$a\", \"$b\", \"$c\");" ;;
esac; done

(the mail can have beed splitted, but the above code should be one
single line :)

regards,
Bapt
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to