136 lines
3.1 KiB
Bash
Executable File
136 lines
3.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# nextcloud_healthcheck.sh
|
|
# Comprehensive health check for a Docker-based Nextcloud installation.
|
|
#
|
|
# Requirements:
|
|
# - Nextcloud running in a container named "nextcloud"
|
|
# - Access to the docker CLI
|
|
# - curl installed
|
|
#
|
|
# Author: Lars Klemstein (preferred setup preserved)
|
|
|
|
set -euo pipefail
|
|
|
|
readonly CONTAINER="nextcloud-fpm-v1"
|
|
readonly NC_URL="https://nextcloud.knusperkerne.de" # adjust if needed
|
|
|
|
log() {
|
|
printf "[%s] %s\n" "$(date +'%Y-%m-%d %H:%M:%S')" "$1"
|
|
}
|
|
|
|
err() {
|
|
printf "\033[31m[%s] ERROR: %s\033[0m\n" "$(date +'%Y-%m-%d %H:%M:%S')" "$1"
|
|
}
|
|
|
|
check_container() {
|
|
if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER}$"; then
|
|
err "Container '${CONTAINER}' not running."
|
|
exit 1
|
|
fi
|
|
log "Container '${CONTAINER}' is running."
|
|
}
|
|
|
|
occ() {
|
|
docker exec -u www-data "${CONTAINER}" php occ "$@"
|
|
}
|
|
|
|
check_occ_status() {
|
|
log "Checking Nextcloud status via OCC..."
|
|
if ! occ status >/tmp/nc_status.txt 2>/tmp/nc_status_err.txt; then
|
|
err "OCC status failed. See /tmp/nc_status_err.txt"
|
|
exit 1
|
|
fi
|
|
|
|
cat /tmp/nc_status.txt
|
|
}
|
|
|
|
|
|
check_integrity() {
|
|
log "Running integrity check..."
|
|
|
|
TMP_OUT="$(mktemp -t nc_integrity_out.XXXXXX)"
|
|
TMP_ERR="$(mktemp -t nc_integrity_err.XXXXXX)"
|
|
|
|
# execute OCC integrity check
|
|
if docker exec -u www-data "${CONTAINER}" php occ integrity:check-core >"$TMP_OUT" 2>"$TMP_ERR"; then
|
|
log "Integrity OK."
|
|
else
|
|
err "Integrity check failed."
|
|
err "Output: $TMP_OUT"
|
|
err "Errors: $TMP_ERR"
|
|
exit 1
|
|
fi
|
|
|
|
rm -f "$TMP_OUT" "$TMP_ERR"
|
|
}
|
|
|
|
check_updates() {
|
|
log "Checking for available app updates..."
|
|
occ app:update --all >/tmp/nc_app_update.txt 2>/tmp/nc_app_update_err.txt || true
|
|
log "App update check completed."
|
|
}
|
|
|
|
check_http() {
|
|
log "Checking HTTP endpoint ${NC_URL}..."
|
|
HTTP_CODE=$(curl -k -s -o /dev/null -w "%{http_code}" "${NC_URL}")
|
|
|
|
case "$HTTP_CODE" in
|
|
200|301|302)
|
|
log "HTTP check OK (${HTTP_CODE})."
|
|
;;
|
|
*)
|
|
err "HTTP check returned ${HTTP_CODE}."
|
|
exit 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
check_cron() {
|
|
log "Checking background jobs mode..."
|
|
occ background:status >/tmp/nc_bg.txt 2>/tmp/nc_bg_err.txt
|
|
|
|
MODE=$(grep "Background mode" /tmp/nc_bg.txt | awk -F: '{print $2}' | xargs)
|
|
log "Background mode: ${MODE}"
|
|
}
|
|
|
|
check_db() {
|
|
log "Checking database schema..."
|
|
occ db:convert-filecache-bigint --dry-run >/tmp/nc_db.txt 2>/tmp/nc_db_err.txt || true
|
|
|
|
if grep -q "All tables already up to date" /tmp/nc_db.txt; then
|
|
log "Database schema OK."
|
|
else
|
|
log "Database schema check: see /tmp/nc_db.txt (not an error, just info)."
|
|
fi
|
|
}
|
|
|
|
main() {
|
|
log "Starting Nextcloud health check..."
|
|
|
|
check_container
|
|
check_http
|
|
check_occ_status
|
|
check_integrity
|
|
check_cron
|
|
check_updates
|
|
check_db
|
|
|
|
log "Health check completed successfully."
|
|
}
|
|
|
|
final_status_ok=true
|
|
|
|
final_ok() {
|
|
printf "\033[32m[FINAL] OK\033[0m\n"
|
|
}
|
|
|
|
final_not_ok() {
|
|
printf "\033[31m[FINAL] NOT_OK\033[0m\n"
|
|
}
|
|
|
|
trap 'if [ "$final_status_ok" = true ]; then final_ok; else final_not_ok; fi' EXIT
|
|
|
|
|
|
main "$@"
|