Skip to content

Instantly share code, notes, and snippets.

@reubenmiller
Last active November 5, 2023 18:31
Show Gist options
  • Save reubenmiller/b476aec48bad01395e05d2586f88a59b to your computer and use it in GitHub Desktop.
Save reubenmiller/b476aec48bad01395e05d2586f88a59b to your computer and use it in GitHub Desktop.
thin-edge.io script to debug an issue with the tedge-mapper-c8y and jwt request
#!/bin/bash
########################################################################################################################
# Debug script to check the behaviour of the tedge-mapper-c8y/mosquitto and the responsiveness of the
# Cumulocity IoT token request/response topics (c8y/s/uat and c8y/s/dat).
#
# The script does the following:
# 1. Install a systemd service (and enables it)
# 2. Create mqtt subscriber to c8y/s/dat
# 3. Publish a mqtt message to c8y/s/uat
# 4. Wait for a message on the c8y/s/dat topic (give up after a few seconds)
# 5. Record the result (and increment the run counter)
# 6. Reboot the device/container (the script will run automatically on startup, and repeat the procedure MAX_RUN times)
# 7. When MAX_RUNS is reached, the service is disabled
# 8. Run the script again to print the results
#
#
# Usage
#
# Start the test and install the service if it does not already exist
# ./debug-qos-issue.sh
#
# Clean the results so the test procedure can be run again
# ./debug-qos-issue.sh clean
#
########################################################################################################################
set -e
MAX_RUNS=20
mkdir -p /etc/debug-qos-issue/
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
SCRIPT_FULL_PATH="$SCRIPTPATH/$(basename "$0")"
if [ "$1" = "clean" ]; then
echo "Cleaning all results"
rm -Rf /etc/debug-qos-issue
exit 0
fi
install_service() {
if [ -f /lib/systemd/system/debug-qos-issue.service ]; then
return 0
fi
echo "installing debug-qos-issue.service" >&2
cat <<EOT > /lib/systemd/system/debug-qos-issue.service
[Unit]
Description=Debug qos issue
After=mosquitto.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=$SCRIPT_FULL_PATH
[Install]
WantedBy=multi-user.target
EOT
systemctl daemon-reload ||:
systemctl enable debug-qos-issue.service ||:
}
RUN_NUM=
if [ -f /etc/debug-qos-issue/counter ]; then
RUN_NUM=$(head -n1 /etc/debug-qos-issue/counter)
fi
install_service
if [ -z "$RUN_NUM" ]; then
RUN_NUM=0
fi
if [ "$RUN_NUM" -ge "$MAX_RUNS" ]; then
echo "Max number of runs reached (disabling service)"
systemctl disable debug-qos-issue
PASSED_TOTAL=$(find /etc/debug-qos-issue -name "*.pass" | wc -l)
FAILED_TOTAL=$(find /etc/debug-qos-issue -name "*.fail" | wc -l)
TOTAL=$((PASSED_TOTAL + FAILED_TOTAL))
echo "$PASSED_TOTAL passed, $FAILED_TOTAL failed, $TOTAL total" > /etc/debug-qos-issue/result
cat /etc/debug-qos-issue/result
exit 0
fi
RUN_NUM=$((RUN_NUM + 1))
printf "%d" "$RUN_NUM" > /etc/debug-qos-issue/counter
printf "Run %s..." "$RUN_NUM"
# Wait for system to start and settle
sleep 10
{
sleep 1;
tedge mqtt pub c8y/s/uat '' -q 1;
} &
BACKGROUND_PID=$!
MESSAGES=$(mosquitto_sub -t c8y/s/dat -C 1 -W 3 2>/dev/null || :)
wait "$BACKGROUND_PID" ||:
PASSED=0
case "$MESSAGES" in
*71,*)
PASSED=1
;;
Timed\ out)
;;
esac
if [ $PASSED -eq 1 ]; then
echo "$MESSAGES" > /etc/debug-qos-issue/run.$RUN_NUM.pass
printf "PASS\n"
else
echo "message: $MESSAGES" > /etc/debug-qos-issue/run.$RUN_NUM.fail
journalctl -n 100 --no-pager -u tedge-mapper-c8y >> /etc/debug-qos-issue/run.$RUN_NUM.fail
printf "FAILED\n"
fi
sleep 5
echo "Shutting down"
shutdown -r now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment