Skip to content

Instantly share code, notes, and snippets.

@tve
Last active May 3, 2024 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tve/e36449b1ed8fb9cf5a0bf732c60f06b0 to your computer and use it in GitHub Desktop.
Save tve/e36449b1ed8fb9cf5a0bf732c60f06b0 to your computer and use it in GitHub Desktop.
Sensorgnome FUNcube check
#! /bin/bash
DEBUG=1 # 1=report whether error or not (recommended)
PASS=none
VAH=/tmp/VAH.sock
# fetch currently active ports from VAH (vamp-alsa-host)
date=$(date +%s)
data="$(echo list | nc -U -i 1 -q 1 $VAH)"
RE='"(p[0-9])":\{([^}]*)}(.*)'
FRAM_RE='"totalFrames":([0-9]+)'
FEAT_RE='"totalFeatures":([0-9]+)'
STATE=/tmp/fcd-state # state to keep from one iteration to the next
POST=/tmp/fcd-post # file that gets posted to server
ERRORS=
SGID=$(hostname)
[[ -f /etc/beaglebone_id ]] && SGID=SG-$(cat /etc/beaglebone_id)
[[ -f /etc/ctt/station-id ]] && SGID=CTT-$(cat /etc/ctt/station-id)
echo "$(date) ($date)"
# finish up: grab info frmo the machine and post everything to the server
function finish {
cat $STATE >$POST
ERRORS="${ERRORS//\"/\\\"}" # replace quotes by backslash-quotes
echo "ERRORS=\"$ERRORS\"" >>$POST
echo "==========" >>$POST
tail -300 /var/log/syslog | grep -v 'rsyslogd.*action 17' >>$POST
echo "==========" >>$POST
dmesg -H | tail -300 >>$POST
echo "==========" >>$POST
ls -l /etc/cron.hourly/fcd-check >>$POST
#[[ -d /etc/ctt ]] && ls /etc/ctt >>$POST
ls /dev/sensorgnome >>$POST
lsusb >>$POST
uptime >>$POST
cat /proc/version >>$POST
for i in /etc/ctt/station-[irs]*; do echo $i $(cat $i); done >>$POST
ls -ls /usr/bin/vamp-alsa-host >>$POST
ldd /usr/bin/vamp-alsa-host >>$POST
ps axl | grep '[v]amp' >>$POST
systemctl status sensorgnome >>$POST
echo "VAH DATA: $data" >>$POST
echo "Posting $(cat $POST | wc -l) lines"
logger -t fcd-check "Posting $(cat $POST | wc -l) lines"
wget -nv --no-check-certificate \
"--user=$SGID" "--password=$PASS" "--post-file=$POST" --auth-no-challenge \
--header="content-type: application/binary" \
https://www.sensorgnome.net/notify/fcd-check
if [[ -z "$ERRORS" ]]; then exit 0; else exit 1; fi
}
# use usbreset to reset a port, using the o_paths map from pX -> N:M (reall bus & dev)
function portreset {
local p=${o_paths[$1]}
if [[ -z "$p" ]]; then
echo "No path for port $1" | logger -t fcd-check
return
fi
if usbreset ${p/://}; then
echo "Reset port $1 ($p)" | logger -t fcd-check
else
echo "Failed to reset port $1 ($p)" | logger -t fcd-check
fi
}
#echo "#RAW: $data"
#echo
if [[ "$data" != {* ]]; then
ERRORS="Unexpectd or missing VAH output: $data"
goto error
fi
# parse the data from VAH
rest="$data"
declare -a ports
declare -A frames
declare -A features
while [[ "$rest" =~ $RE ]]; do
port="${BASH_REMATCH[1]}"
info="${BASH_REMATCH[2]}"
rest="${BASH_REMATCH[3]}"
echo $port $info
ports+=($port)
if [[ "$info" =~ $FRAM_RE ]]; then frames[$port]=${BASH_REMATCH[1]}; fi
if [[ "$info" =~ $FEAT_RE ]]; then features[$port]=${BASH_REMATCH[1]}; fi
done
# save the parsed data to the new/next state file
echo "o_date=$date" >$STATE.new
echo "o_ports=(${ports[@]})" >>$STATE.new
echo declare -A o_frames o_features o_paths >>$STATE.new
for p in "${ports[@]}"; do
echo "o_frames[$p]=${frames[$p]}" >>$STATE.new
echo "o_features[$p]=${features[$p]}" >>$STATE.new
done
# port to usb path mapping, really usb bus & device
/bin/ls /dev/sensorgnome | \
sed -E -e 's/^/#/' \
-e 's/.*port=([0-9]+).*usbPath=([0-9:]+).*/o_paths[p\1]=\2/' \
-e '/^#/d' >>$STATE.new
# swap state files, loading the old state
if ! [[ -f $STATE ]]; then
mv $STATE.new $STATE
exit 0
fi
source $STATE
mv $STATE.new $STATE
# compare the old and new port list
dt=$(( $date - $o_date ))
echo "last check was $dt seconds ago"
echo "${o_ports[*]} ?? ${ports[*]}"
if [[ "${o_ports[*]}" != "${ports[*]}" ]]; then
ERRORS="${ERRORS}Error: ports have changed: ${o_ports[@]} to ${ports[@]}; "
echo "Ports have changed"
fi
# iterate port by port and figure out details
for port in "${o_ports[@]}"; do
if [[ "${#frames[$port]}" == 0 ]]; then
ERRORS="${ERRORS}No more frames for port $port? (not resetting); "
#portreset $port
else
dfr=$(( ${frames[$port]} - ${o_frames[$port]} ))
rate=$(( $dfr / $dt ))
echo port $port rate $rate
if [[ $rate < 46000 || $rate > 50000 ]]; then
ERRORS="${ERRORS}Error: rate for port $port out of range: $rate (not resetting); "
#portreset $port
fi
fi
done
#if [[ -z "$ERRORS" ]]; then echo "ALL OK"; exit 0; fi
finish
#! /bin/bash
# Install fresh version of fcd-check (FUNcube checker)
cd /tmp
F=fcd-check
echo Fetching script $F
curl -sL -o $F https://gist.github.com/tve/e36449b1ed8fb9cf5a0bf732c60f06b0/raw/$F
if [[ -s $F ]]; then
chmod +x ./$F
echo Moving to cron.hourly
sudo chown root $F
sudo mv $F /etc/cron.hourly
fi
#
if ! [[ -f /etc/cron.daily/fcdc-install ]]; then
F=fcdc-install
echo Fetching script $F
curl -sL -o $F https://gist.github.com/tve/e36449b1ed8fb9cf5a0bf732c60f06b0/raw/$F
if [[ -s $F ]]; then
chmod +x ./$F
echo Moving to cron.daily
sudo chown root $F
sudo mv $F /etc/cron.daily
echo Running fcd-check script
sudo /etc/cron.hourly/fcd-check
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment