fix issues with track_anything.sh

args were broken due to shift and other problems
-1 didn't work because of the subshell
output filtering didn't work with uniq.  this was resolved by adding an
unbuf function which uses stdbuf to fix buffering.
This commit is contained in:
James Andariese 2024-03-30 16:03:17 -05:00
parent 74a967eb10
commit aaa4cd251d

View File

@ -1,11 +1,12 @@
# shellcheck shell=bash
export ZERO="$0" export ZERO="$0"
HELP="$ZERO: tracker [-1] [-v [-v ...]] [q [-q ...]] HELP="$ZERO: tracker [-1] [-v [-v ...]] [q [-q ...]]
-1 run once and exit -1 run once and exit
-v increase verbosity (default >= warn) -v increase verbosity (default >= warn)
-q decrease verbosity -q decrease verbosity
" "
EXITING=0 trap 'kill -15 $$' INT
trap 'EXITING=1' INT
trace() { (( LOG_LEVEL > 2 )) && (exec 1>&2; builtin echo "$@"|grep .||true); } trace() { (( LOG_LEVEL > 2 )) && (exec 1>&2; builtin echo "$@"|grep .||true); }
debug() { (( LOG_LEVEL > 1 )) && (exec 1>&2; builtin echo "$@"|grep .||true); } debug() { (( LOG_LEVEL > 1 )) && (exec 1>&2; builtin echo "$@"|grep .||true); }
@ -15,6 +16,8 @@ error() { (( LOG_LEVEL > -2 )) && (exec 1>&2; builtin echo "$@"|grep .||true); }
fatal() { (exec 1>&2; builtin echo "$@"|grep .||true); exit 4; } fatal() { (exec 1>&2; builtin echo "$@"|grep .||true); exit 4; }
log() { info "$@"; } log() { info "$@"; }
unbuf() { SCRIPT="$1"; shift ; stdbuf -oL -eL bash -c "$SCRIPT" - "$@" ; }
start() { start() {
if ! options="$(getopt vq1 -- "$@")";then if ! options="$(getopt vq1 -- "$@")";then
fatal "$HELP" fatal "$HELP"
@ -24,17 +27,18 @@ start() {
while [ $# -gt 0 ];do while [ $# -gt 0 ];do
case "$1" in case "$1" in
-1) shift;ONCE=1 ;; -1) ONCE=1 ;;
-h) echo "requesting help" ; help; exit 0 ;; -h) info "$HELP"; exit 0 ;;
-v) LOG_LEVEL=$(( LOG_LEVEL + 1 )) ;echo increased log level ;; -v) LOG_LEVEL=$(( LOG_LEVEL + 1 )) ;;
-q) LOG_LEVEL=$(( LOG_LEVEL - 1 )) ;; -q) LOG_LEVEL=$(( LOG_LEVEL - 1 )) ;;
--) shift;break;; # we're _explicitly_ done with options --) shift;break;; # we're _explicitly_ done with options
-*) error "invalid option $1"; fatal "$HELP" ;;
*) break;; # we're done with options. *) break;; # we're done with options.
esac esac
shift shift
done done
if [ $VALIDATION ];then if [ "$VALIDATION" ];then
# fix up the validation regex to not search. # fix up the validation regex to not search.
VALIDATION="${VALIDATION#^}" VALIDATION="${VALIDATION#^}"
VALIDATION="${VALIDATION%'$'}" VALIDATION="${VALIDATION%'$'}"
@ -48,36 +52,33 @@ start() {
debug "POLL_INTERVAL: $POLL_INTERVAL" debug "POLL_INTERVAL: $POLL_INTERVAL"
debug " watch_cmd: $watch_cmd" debug " watch_cmd: $watch_cmd"
if [ $(echo "$POLL_INTERVAL < $RATE_LIMIT"|bc) -eq 1 ];then if [ "$(echo "$POLL_INTERVAL < $RATE_LIMIT"|bc)" = 1 ];then
warn "POLL_INTERVAL cannot be less than RATE_LIMIT." warn "POLL_INTERVAL cannot be less than RATE_LIMIT."
info "increasing POLL_INTERVAL to $RATE_LIMIT." info "increasing POLL_INTERVAL to $RATE_LIMIT."
POLL_INTERVAL=$RATE_LIMIT POLL_INTERVAL=$RATE_LIMIT
fi fi
while ! (( EXITING ));do while true;do
(
_poll || exit $? _poll || exit $?
(( ONCE )) && EXITING=1 (( ONCE )) && exit 0
$watch_cmd | while ! (( EXITING ));do
sleep $RATE_LIMIT
$watch_cmd | while sleep $RATE_LIMIT;do
# read with a maximum of the poll interval minus the previous rate limit. # read with a maximum of the poll interval minus the previous rate limit.
# rounding _up_ to the nearest second. (bc floors when scale is unset.) # rounding _up_ to the nearest second. (bc floors when scale is unset.)
if read -t $( echo "$POLL_INTERVAL - $RATE_LIMIT + .5 " | bc );then if read -r -t "$( echo "$POLL_INTERVAL - $RATE_LIMIT + .5 " | bc )";then
debug "received poll. removing other poll requests." debug "received poll. removing other poll requests."
else else
debug "reached the polling interval. forcing poll." debug "reached the polling interval. forcing poll."
fi fi
N=0 N=0
while read -t 0;do read; N=$((N + 1));done while read -t 0;do read -r; N=$((N + 1));done
debug "removed $N extra events" debug "removed $N extra events"
_poll _poll
debug "sleeping for $RATE_LIMIT" debug "sleeping for $RATE_LIMIT"
done done
) | eval "$_filter" warn "tracker died. restarting after $RATE_LIMIT seconds."
(( EXITING == 0 )) && (warn "tracker died. restarting after $RATE_LIMIT seconds.";sleep $RATE_LIMIT) sleep $RATE_LIMIT
done done | unbuf "$_filter"
info "bye" info "bye"
} }
@ -90,10 +91,10 @@ _poll() {
done done
local failed=0 local failed=0
if [ $JQ_VALIDATION ];then if [ "$JQ_VALIDATION" ];then
echo "$NEWVAL" | jq -e "$JQ_VALIDATION" > /dev/null || failed=1 echo "$NEWVAL" | jq -e "$JQ_VALIDATION" > /dev/null || failed=1
fi fi
if [ $VALIDATION ];then if [ "$VALIDATION" ];then
echo "$NEWVAL" | grep -qE "$VALIDATION" || failed=1 echo "$NEWVAL" | grep -qE "$VALIDATION" || failed=1
fi fi
if (( failed ));then if (( failed ));then
@ -115,7 +116,7 @@ poll_interval() {
POLL_INTERVAL=$1 POLL_INTERVAL=$1
} }
_filter=uniq _filter="uniq"
filter() { filter() {
_filter="$(printf " %q" "$@")" _filter="$(printf " %q" "$@")"
} }