| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- #!/bin/bash
- # v1.0:
- # - Initial release of the script
- # v1.1 CHANGES:
- # - Updated query for installed version to versionstring instead of version
- # v1.2 CHANGES:
- # - Updated nextcloud release check to actually get latest version from the release-url
- # v1.3 CHANGES:
- # - Added check for configured apache-user and basepath exist.
- # v1.4 CHANGES:
- # - added extra logic to deal with unresponsive or empty response from release-url
- # v1.5 CHANGES:
- # - added enhancement suggestion made by briand on checkmk forum to make the version-compare more granular
- # v1.6 CHANGES:
- # - Restructured and refined variables, also added additional comments per line of code
- # v1.7 CHANGES:
- # - Updated logic to also deal with higher installed version compared to release-url
- # Current active version of the script
- SCRIPT_VERSION=1.7
- # Installation:
- # This file should be place on a/the nextcloud host in /usr/lib/check_mk_agent/local
- #
- # (optional) if you want the check to run on a different schedule (like once per hour) put it in a subdirectory of above path
- # named to the number of seconds of the interval wanted, so 3600/ for once an hour, 86400 for once per day.
- #
- # Also do not forget to make this file executable with chmod +x /usr/lib/check_mk_agent/local/nextcloud-updates.sh
- # (c) Michael Honkoop <mhonkoop@comsolve.nl>
- # License: GNU General Public License v2
- ######################
- # Configurable Parameters
- ######################
- # url to check releases against
- RELEASEURL="https://download.nextcloud.com/server/releases/"
- #basepath of Nextcloud's occ executable
- BASEPATH="/var/www/nextcloud"
- # set the correct user for running the commands (either apache or www-data, this depends on your Linux-Distribution)
- APACHEUSER="www-data"
- # Service name
- SERVICE="NextCloud"
- # Set how exact the version-comparison should be - enhancement suggested by briand at checkmk forum
- #
- # how many “.”-separated fields to compare:
- # 3 → XX.YY.ZZ (full)
- # 2 → XX.YY (major+minor)
- # 1 → XX (major only)
- VERSION_FIELDS=2
- ######################
- # The code below should not be changed, unless you know EXACTLY what you are doing.
- # All configureable parameters are above this section.
- ######################
- # Check availabillity of required commands or packages
- for COMMAND in sudo php jq curl; do
- if ! command -v "$COMMAND" >/dev/null 2>&1; then
- echo "2 \"$SERVICE\" - $SERVICE localcheck requires $COMMAND command to be installed"
- echo "2 \"$SERVICE Apps\" - $SERVICE localcheck requires $COMMAND command to be installed"
- exit 0
- fi
- done
- # Check if configured apache user exists
- if ! id -u "$APACHEUSER" >/dev/null 2>&1; then
- echo "2 \"$SERVICE\" - $SERVICE localcheck webserver user does not exist, check configuration!"
- echo "2 \"$SERVICE Apps\" - $SERVICE Apps localcheck webserver user does not exist, check configuration!"
- exit 0
- fi
- # Check if configured basepath exists
- if [ ! -d "$BASEPATH" ]; then
- echo "2 \"$SERVICE\" - $SERVICE localcheck could not find configured basepath, check configuration!"
- echo "2 \"$SERVICE Apps\" - $SERVICE Apps localcheck could not find configured basepath, check configuration!"
- exit 0
- fi
- # Get current installed Nextcloud version-information
- # in v29.x.x version gives more detail, which is not given by the check on installed version, so switched to versionstring instead.
- NCINSTALLED=$(sudo -u "$APACHEUSER" php "$BASEPATH"/occ status --output=json | jq -r '.versionstring // empty' | cut -d. -f1-"$VERSION_FIELDS")
- # Check output of installed version
- if [ -z "$NCINSTALLED" ]; then
- echo "3 \"$SERVICE\" - Unable to determine installed $SERVICE Core version"
- echo "3 \"$SERVICE Apps\" - Unable to determine installed $SERVICE Core version"
- exit 0
- fi
- # Get latest available (core) version information from release-url
- # In v29.x.x this has changed outputs <name-of-instance> <version>,
- NCRELEASEDATA=$(curl -fsS --connect-timeout 5 --max-time 10 "$RELEASEURL" 2>/dev/null)
- CURL_RC=$?
- # If curl failed, report UNKNOWN
- if [ "$CURL_RC" -ne 0 ]; then
- echo "3 \"$SERVICE\" - Unable to retrieve data from release-URL for $SERVICE Core (curl exit code $CURL_RC)"
- echo "3 \"$SERVICE Apps\" - No data received from release-URL, cannot determine latest release-version for $SERVICE Apps"
- exit 0
- fi
- # If the release URL returned an empty response, report UNKNOWN
- if [ -z "$NCRELEASEDATA" ]; then
- echo "3 \"$SERVICE\" - No data received from release-URL, cannot determine latest release-version for $SERVICE Core"
- echo "3 \"$SERVICE Apps\" - No data received from release-URL, cannot determine latest release-version for $SERVICE Apps"
- exit 0
- fi
- # Extract all available Nextcloud release versions from the release data,
- # sort them as versions and select the highest version.
- NCAVAILABLE=$(printf '%s\n' "$NCRELEASEDATA" | grep -oE 'nextcloud-[0-9]+\.[0-9]+\.[0-9]+\.zip' | sed -E 's/^nextcloud-([0-9]+\.[0-9]+\.[0-9]+)\.zip$/\1/' | sort -V | tail -n1)
- # Check output of release-version parsing
- if [ -z "$NCAVAILABLE" ]; then
- echo "3 \"$SERVICE\" - No valid release-version found in release-URL data for $SERVICE Core"
- echo "3 \"$SERVICE Apps\" - No valid release-version found in release-URL data for $SERVICE Apps"
- exit 0
- fi
- # Apply configured version-comparison granularity to the available version
- NCAVAILABLE=$(printf '%s\n' "$NCAVAILABLE" | cut -d. -f1-"$VERSION_FIELDS")
- # Compare installed and available versions.
- if [ "$NCINSTALLED" = "$NCAVAILABLE" ]; then
- # Versions match, so check the actual Nextcloud core status.
- sudo -u "$APACHEUSER" php "$BASEPATH"/occ status -e >/dev/null 2>&1
- STATUS_RC=$?
- if [ "$STATUS_RC" -eq 0 ]; then
- echo "0 \"$SERVICE\" - $SERVICE Core is latest version ($NCAVAILABLE)"
- elif [ "$STATUS_RC" -eq 1 ]; then
- echo "1 \"$SERVICE\" - $SERVICE maintenance-mode is enabled"
- elif [ "$STATUS_RC" -eq 2 ]; then
- echo "2 \"$SERVICE\" - $SERVICE Core needs updating"
- else
- echo "3 \"$SERVICE\" - Unable to determine $SERVICE Core status (occ status exit code $STATUS_RC)"
- fi
- # Installed version is lower than available version
- elif [ "$(printf '%s\n%s\n' "$NCINSTALLED" "$NCAVAILABLE" | sort -V | head -n1)" = "$NCINSTALLED" ]; then
- echo "1 \"$SERVICE\" - A new $SERVICE Core version ($NCAVAILABLE) is available, running Core version is: $NCINSTALLED, please upgrade"
- # Installed version is higher than available version (happens when a release is retracted)
- else
- echo "2 \"$SERVICE\" - $SERVICE Core version ($NCAVAILABLE) is lower than the running Core version $NCINSTALLED, please investigate."
- fi
- APPRESULT=$(sudo -u "$APACHEUSER" php "$BASEPATH"/occ app:update --showonly)
- APP_RC=$?
- # If occ failed, report UNKNOWN
- if [ "$APP_RC" -ne 0 ]; then
- echo "3 \"$SERVICE Apps\" - Unable to determine available app updates (occ app:update exit code $APP_RC)"
- exit 0
- fi
- # Check if all apps are up to date
- if [[ "$APPRESULT" == *"All apps are up-to-date"* ]]; then
- echo "0 \"$SERVICE Apps\" - $SERVICE Apps all up to date"
- exit 0
- else
- UPDATECOUNT=$(printf '%s\n' "$APPRESULT" | wc -l)
- echo "1 \"$SERVICE Apps\" - $UPDATECOUNT $SERVICE Apps require updates"
- exit 0
- fi
|