nextcloud-updates.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/bin/bash
  2. # v1.0:
  3. # - Initial release of the script
  4. # v1.1 CHANGES:
  5. # - Updated query for installed version to versionstring instead of version
  6. # v1.2 CHANGES:
  7. # - Updated nextcloud release check to actually get latest version from the release-url
  8. # v1.3 CHANGES:
  9. # - Added check for configured apache-user and basepath exist.
  10. # v1.4 CHANGES:
  11. # - added extra logic to deal with unresponsive or empty response from release-url
  12. # v1.5 CHANGES:
  13. # - added enhancement suggestion made by briand on checkmk forum to make the version-compare more granular
  14. # v1.6 CHANGES:
  15. # - Restructured and refined variables, also added additional comments per line of code
  16. # v1.7 CHANGES:
  17. # - Updated logic to also deal with higher installed version compared to release-url
  18. # Current active version of the script
  19. SCRIPT_VERSION=1.7
  20. # Installation:
  21. # This file should be place on a/the nextcloud host in /usr/lib/check_mk_agent/local
  22. #
  23. # (optional) if you want the check to run on a different schedule (like once per hour) put it in a subdirectory of above path
  24. # named to the number of seconds of the interval wanted, so 3600/ for once an hour, 86400 for once per day.
  25. #
  26. # Also do not forget to make this file executable with chmod +x /usr/lib/check_mk_agent/local/nextcloud-updates.sh
  27. # (c) Michael Honkoop <mhonkoop@comsolve.nl>
  28. # License: GNU General Public License v2
  29. ######################
  30. # Configurable Parameters
  31. ######################
  32. # url to check releases against
  33. RELEASEURL="https://download.nextcloud.com/server/releases/"
  34. #basepath of Nextcloud's occ executable
  35. BASEPATH="/var/www/nextcloud"
  36. # set the correct user for running the commands (either apache or www-data, this depends on your Linux-Distribution)
  37. APACHEUSER="www-data"
  38. # Service name
  39. SERVICE="NextCloud"
  40. # Set how exact the version-comparison should be - enhancement suggested by briand at checkmk forum
  41. #
  42. # how many “.”-separated fields to compare:
  43. # 3 → XX.YY.ZZ (full)
  44. # 2 → XX.YY (major+minor)
  45. # 1 → XX (major only)
  46. VERSION_FIELDS=2
  47. ######################
  48. # The code below should not be changed, unless you know EXACTLY what you are doing.
  49. # All configureable parameters are above this section.
  50. ######################
  51. # Check availabillity of required commands or packages
  52. for COMMAND in sudo php jq curl; do
  53. if ! command -v "$COMMAND" >/dev/null 2>&1; then
  54. echo "2 \"$SERVICE\" - $SERVICE localcheck requires $COMMAND command to be installed"
  55. echo "2 \"$SERVICE Apps\" - $SERVICE localcheck requires $COMMAND command to be installed"
  56. exit 0
  57. fi
  58. done
  59. # Check if configured apache user exists
  60. if ! id -u "$APACHEUSER" >/dev/null 2>&1; then
  61. echo "2 \"$SERVICE\" - $SERVICE localcheck webserver user does not exist, check configuration!"
  62. echo "2 \"$SERVICE Apps\" - $SERVICE Apps localcheck webserver user does not exist, check configuration!"
  63. exit 0
  64. fi
  65. # Check if configured basepath exists
  66. if [ ! -d "$BASEPATH" ]; then
  67. echo "2 \"$SERVICE\" - $SERVICE localcheck could not find configured basepath, check configuration!"
  68. echo "2 \"$SERVICE Apps\" - $SERVICE Apps localcheck could not find configured basepath, check configuration!"
  69. exit 0
  70. fi
  71. # Get current installed Nextcloud version-information
  72. # in v29.x.x version gives more detail, which is not given by the check on installed version, so switched to versionstring instead.
  73. NCINSTALLED=$(sudo -u "$APACHEUSER" php "$BASEPATH"/occ status --output=json | jq -r '.versionstring // empty' | cut -d. -f1-"$VERSION_FIELDS")
  74. # Check output of installed version
  75. if [ -z "$NCINSTALLED" ]; then
  76. echo "3 \"$SERVICE\" - Unable to determine installed $SERVICE Core version"
  77. echo "3 \"$SERVICE Apps\" - Unable to determine installed $SERVICE Core version"
  78. exit 0
  79. fi
  80. # Get latest available (core) version information from release-url
  81. # In v29.x.x this has changed outputs <name-of-instance> <version>,
  82. NCRELEASEDATA=$(curl -fsS --connect-timeout 5 --max-time 10 "$RELEASEURL" 2>/dev/null)
  83. CURL_RC=$?
  84. # If curl failed, report UNKNOWN
  85. if [ "$CURL_RC" -ne 0 ]; then
  86. echo "3 \"$SERVICE\" - Unable to retrieve data from release-URL for $SERVICE Core (curl exit code $CURL_RC)"
  87. echo "3 \"$SERVICE Apps\" - No data received from release-URL, cannot determine latest release-version for $SERVICE Apps"
  88. exit 0
  89. fi
  90. # If the release URL returned an empty response, report UNKNOWN
  91. if [ -z "$NCRELEASEDATA" ]; then
  92. echo "3 \"$SERVICE\" - No data received from release-URL, cannot determine latest release-version for $SERVICE Core"
  93. echo "3 \"$SERVICE Apps\" - No data received from release-URL, cannot determine latest release-version for $SERVICE Apps"
  94. exit 0
  95. fi
  96. # Extract all available Nextcloud release versions from the release data,
  97. # sort them as versions and select the highest version.
  98. 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)
  99. # Check output of release-version parsing
  100. if [ -z "$NCAVAILABLE" ]; then
  101. echo "3 \"$SERVICE\" - No valid release-version found in release-URL data for $SERVICE Core"
  102. echo "3 \"$SERVICE Apps\" - No valid release-version found in release-URL data for $SERVICE Apps"
  103. exit 0
  104. fi
  105. # Apply configured version-comparison granularity to the available version
  106. NCAVAILABLE=$(printf '%s\n' "$NCAVAILABLE" | cut -d. -f1-"$VERSION_FIELDS")
  107. # Compare installed and available versions.
  108. if [ "$NCINSTALLED" = "$NCAVAILABLE" ]; then
  109. # Versions match, so check the actual Nextcloud core status.
  110. sudo -u "$APACHEUSER" php "$BASEPATH"/occ status -e >/dev/null 2>&1
  111. STATUS_RC=$?
  112. if [ "$STATUS_RC" -eq 0 ]; then
  113. echo "0 \"$SERVICE\" - $SERVICE Core is latest version ($NCAVAILABLE)"
  114. elif [ "$STATUS_RC" -eq 1 ]; then
  115. echo "1 \"$SERVICE\" - $SERVICE maintenance-mode is enabled"
  116. elif [ "$STATUS_RC" -eq 2 ]; then
  117. echo "2 \"$SERVICE\" - $SERVICE Core needs updating"
  118. else
  119. echo "3 \"$SERVICE\" - Unable to determine $SERVICE Core status (occ status exit code $STATUS_RC)"
  120. fi
  121. # Installed version is lower than available version
  122. elif [ "$(printf '%s\n%s\n' "$NCINSTALLED" "$NCAVAILABLE" | sort -V | head -n1)" = "$NCINSTALLED" ]; then
  123. echo "1 \"$SERVICE\" - A new $SERVICE Core version ($NCAVAILABLE) is available, running Core version is: $NCINSTALLED, please upgrade"
  124. # Installed version is higher than available version (happens when a release is retracted)
  125. else
  126. echo "2 \"$SERVICE\" - $SERVICE Core version ($NCAVAILABLE) is lower than the running Core version $NCINSTALLED, please investigate."
  127. fi
  128. APPRESULT=$(sudo -u "$APACHEUSER" php "$BASEPATH"/occ app:update --showonly)
  129. APP_RC=$?
  130. # If occ failed, report UNKNOWN
  131. if [ "$APP_RC" -ne 0 ]; then
  132. echo "3 \"$SERVICE Apps\" - Unable to determine available app updates (occ app:update exit code $APP_RC)"
  133. exit 0
  134. fi
  135. # Check if all apps are up to date
  136. if [[ "$APPRESULT" == *"All apps are up-to-date"* ]]; then
  137. echo "0 \"$SERVICE Apps\" - $SERVICE Apps all up to date"
  138. exit 0
  139. else
  140. UPDATECOUNT=$(printf '%s\n' "$APPRESULT" | wc -l)
  141. echo "1 \"$SERVICE Apps\" - $UPDATECOUNT $SERVICE Apps require updates"
  142. exit 0
  143. fi