shithub: scripts

ref: 2c27c3c16d0d97e89f2d121f3fa78b9ffe8cd969
dir: /bin/rc/txcheck/

View raw version
#!/bin/rc

flagfmt='r procroot, u unit, m maxsize, D delay'
args='txpid txfd'

# variables and functions

txpid=()
txfd=()

pidfile=()
delay=()
fdpath=()
maxsz=()
unitfrac=1

fn usage {
	aux/usage
	exit usage
}

fn fatal {
	echo $* >[1=2]
	exit $"*
}

fn isnum {
	echo $1 | grep -s '^[0-9]+$'
}

fn isfloat {
	echo $1 | grep -s '^[0-9]+\.[0-9]+$'
}

# argument parsing

if(! eval `{aux/getflags -r /proc -u b -D 3 $*})
	usage

txpid=$1
txfd=$2

if(~ $#txpid 0 || ~ $#txfd 0)
	usage

# util and sanity checks

fdpath=$flagr^'/'^$txpid^'/fd'
if(! test -r $fdpath)
	fatal 'unable to read' $fdpath

switch($flagu) {
case b or B
	unitfrac='1'
case kb or KB
	unitfrac='1024'
case mb or MB
	unitfrac='1024 * 1024'
case *
	if(! isnum $flagu)
		fatal 'unknown unit or not a number'
	unitfrac=$flagu
}

if(! isnum $flagD)
	fatal 'delay is not a number'
delay=$flagD

if(! isfloat $flagm)
	fatal 'maximum size is not a float'
maxsz=$flagm

pidfile=/tmp/txcheck.$txpid
touch $pidfile

while(test -f $pidfile) {
	fdamt=`{cat $fdpath | grep '^  '^$txfd | awk '{print $9}'}
	amt=`{echo 'scale = 2;' $fdamt '/ (' $unitfrac ')' | bc}
	echo ($amt $maxsz)
	sleep $delay
}