#!/bin/bash configuration_file="${1:-#ETC#/colocation-client.conf}" jq -c '{ colocation_server, machine: .machines[] } | { url: ( .colocation_server + "update.php" + ( "?machine=" + .machine.machine + "&key=" + .machine.key ) ), test: ( .machine.values | to_entries | .[] | { state: .key, command: (.value.command // "true"), result: (.value.result // "exitcode") } ) } | { url: ( .url + "&" + .test.state + "=" ), command: .test.command, result: .test.result }' \ <"${configuration_file}" \ | sed ' s@^{"url":"\([^"]\+\)","command":"\([^"]\+\)","result":"\([^"]\+\)"}$@\1\n\2\n\3@ t d ' \ | while read -r url && read -r command && read -r result_strategy; do case "${result_strategy}" in 'exitcode') ${command} 2>/dev/null >/dev/null result=$? ;; 'stderr') result=$(${command} >/dev/null 2>&1) ;; 'stdout') result=$(${command} 2>/dev/null) ;; *) >&2 printf 'unknown result strategy "%s"\n' "${result_strategy}" exit 1 ;; esac if printf '%s\n' "${result}" | grep -vxF '0' | grep -qvxF '1'; then >&2 printf 'result must be 0 or 1, but is "%s" for command "%s" with result strategy "%s"\n' "${result}" "${command}" "${result_strategy}" exit 1 fi curl -Ss "${url}${result}" done