fix: guard grep -c || echo 0 gaf 0\n0 bij nul matches -> integer-fout; vervangen door grep -q + ssh-exitcode-logica

This commit is contained in:
Eddy de Vink 2026-10-19 10:48:12 +02:00
commit f308484f4d

View file

@ -64,23 +64,27 @@ if ! ssh_nc 'true' >/dev/null 2>&1; then
fi
# 2) netcup-multiplexer al op STANDBY?
nc_st=$(ssh_nc "grep -c '^Environment=STANDBY=1' $UNIT 2>/dev/null || echo 0") || exit 0
if [ "${nc_st:-0}" -ge 1 ]; then
if ssh_nc "grep -q '^Environment=STANDBY=1' $UNIT 2>/dev/null"; then
[ "$(state)" = "flipped" ] || log "netcup-multiplexer staat al op STANDBY — niets te doen"
echo flipped >"$STATEFILE"
exit 0
fi
# 3) nextcloud moet actief zijn; anders is netcup-actief een bewuste failover
nccl_st=$(ssh_nccl "grep -c '^Environment=STANDBY=1' $UNIT 2>/dev/null || echo 0")
case "${nccl_st:-}" in
''|*[!0-9]*) log "nextcloud-unit onleesbaar — netcup niet aanraken (kan bewuste failover zijn)"; echo unknown >"$STATEFILE"; exit 0;;
esac
if [ "$nccl_st" -ge 1 ]; then
# 3) nextcloud moet actief zijn; anders is netcup-actief een bewuste failover.
# rc: 0=STANDBY aanwezig, 1=bereikbaar+zonder STANDBY (actief), 255=onbereikbaar, 2=unit onleesbaar
ssh_nccl "grep -q '^Environment=STANDBY=1' $UNIT 2>/dev/null"
nccl_rc=$?
if [ "$nccl_rc" -eq 0 ]; then
[ "$(state)" = "deliberate" ] || log "nextcloud staat op STANDBY -> netcup actief is de bewuste richting; geen flip"
echo deliberate >"$STATEFILE"
exit 0
fi
if [ "$nccl_rc" -ne 1 ]; then
[ "$(state)" = "unknown" ] || log "nextcloud niet bevestigd actief (ssh rc=$nccl_rc) — netcup niet aanraken (kan bewuste failover zijn)"
echo unknown >"$STATEFILE"
exit 0
fi
# rc==1: nextcloud bereikbaar + géén STANDBY = actief
# 4) FLIP: netcup zonder STANDBY terwijl nextcloud actief is
log "FLIP: netcup terug + nextcloud actief -> netcup-multiplexer op STANDBY zetten"