1
0

nextcloud-updates.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 sugestion made by briand on checkmk forum to make the version-compare more granular
  14. # Version 1.6 CHANGES:
  15. # - Restructured and refined variables, also added additional comments per line of code
  16. #
  17. # WARNING: Current logic in the check will report wrong version as upgrade when a release is withdrawn from a/the release-url.
  18. # Current active version of the script
  19. SCRIPT_VERSION=1.6
  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. # Service name
  48. SERVICE="NextCloud"
  49. ######################
  50. # The code below should not be changed, unless you know EXACTLY what you are doing.
  51. # All configureable parameters are above this section.
  52. ######################
  53. for COMMAND in sudo php jq curl; do
  54. if ! command -v "$COMMAND" >/dev/null 2>&1; then
  55. echo "2 \"$SERVICE\" - $SERVICE localcheck requires $COMMAND command to be installed"
  56. echo "2 \"$SERVICE Apps\" - $SERVICE localcheck requires $COMMAND command to be installed"
  57. exit 0
  58. fi
  59. done
  60. # check if configured apache user exists
  61. if ! id -u "$APACHEUSER" >/dev/null 2>&1; then
  62. echo "2 \"$SERVICE\" - $SERVICE localcheck webserver user does not exist, check configuration!"
  63. echo "2 \"$SERVICE Apps\" - $SERVICE Apps localcheck webserver user does not exist, check configuration!"
  64. exit 0
  65. fi
  66. # check if configured basepath exists
  67. if [ ! -d "$BASEPATH" ]; then
  68. echo "2 \"$SERVICE\" - $SERVICE localcheck could not find configured basepath, check configuration!"
  69. echo "2 \"$SERVICE Apps\" - $SERVICE Apps localcheck could not find configured basepath, check configuration!"
  70. exit 0
  71. fi
  72. # Get current installed Nextcloud version-information
  73. # in v29.x.x version gives more detail, which is not given by the check on installed version, so switched to versionstring instead.
  74. NCINSTALLED=$(sudo -u "$APACHEUSER" php "$BASEPATH"/occ status --output=json | jq -r '.versionstring // empty' | cut -d. -f1-"$VERSION_FIELDS")
  75. # Get latest avaiable (core) version information from release-url
  76. # In v29.x.x this has changed outputs <name-of-instance> <version>,
  77. NCRELEASEDATA=$(curl -fsS --connect-timeout 5 --max-time 10 "$RELEASEURL" 2>/dev/null)
  78. CURL_RC=$?
  79. # If curl failed, report UNKNOWN
  80. if [ "$CURL_RC" -ne 0 ]; then
  81. echo "3 \"$SERVICE\" - Unable to retrieve data from release-URL for $SERVICE Core (curl exit code $CURL_RC)"
  82. echo "3 \"$SERVICE Apps\" - No data received from release-URL, cannot determine latest release-version for $SERVICE Apps"
  83. exit 0
  84. fi
  85. # If the release URL returned an empty response, report UNKNOWN
  86. if [ -z "$NCRELEASEDATA" ]; then
  87. echo "3 \"$SERVICE\" - No data received from release-URL, cannot determine latest release-version for $SERVICE Core"
  88. echo "3 \"$SERVICE Apps\" - No data received from release-URL, cannot determine latest release-version for $SERVICE Apps"
  89. exit 0
  90. fi
  91. # Extract all available Nextcloud release versions from the release data,
  92. # sort them as versions and select the highest version.
  93. 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)
  94. # Apply configured version-comparison granularity to the available version
  95. NCAVAILABLE=$(printf '%s\n' "$NCAVAILABLE" | cut -d. -f1-"$VERSION_FIELDS")
  96. # Compare installed and available versions.
  97. if [ "$NCINSTALLED" = "$NCAVAILABLE" ]; then
  98. # Versions match, so check the actual Nextcloud core status.
  99. sudo -u "$APACHEUSER" php "$BASEPATH"/occ status -e >/dev/null 2>&1
  100. STATUS_RC=$?
  101. if [ "$STATUS_RC" -eq 0 ]; then
  102. echo "0 \"$SERVICE\" - $SERVICE Core is latest version ($NCAVAILABLE)"
  103. elif [ "$STATUS_RC" -eq 1 ]; then
  104. echo "1 \"$SERVICE\" - $SERVICE maintenance-mode is enabled"
  105. elif [ "$STATUS_RC" -eq 2 ]; then
  106. echo "2 \"$SERVICE\" - $SERVICE Core needs updating"
  107. else
  108. echo "3 \"$SERVICE\" - Unable to determine $SERVICE Core status (occ status exit code $STATUS_RC)"
  109. fi
  110. # Installed version is lower than available version
  111. elif [ "$(printf '%s\n%s\n' "$NCINSTALLED" "$NCAVAILABLE" | sort -V | head -n1)" = "$NCINSTALLED" ]; then
  112. echo "1 \"$SERVICE\" - A new $SERVICE Core version ($NCAVAILABLE) is available, running Core version is: $NCINSTALLED, please upgrade"
  113. # Installed version is higher than available version (happens when a release is retracted)
  114. else
  115. echo "2 \"$SERVICE\" - $SERVICE Core version ($NCAVAILABLE) is lower than the running Core version $NCINSTALLED, please investigate."
  116. fi
  117. APPRESULT=$(sudo -u "$APACHEUSER" php "$BASEPATH"/occ app:update --showonly)
  118. APP_RC=$?
  119. # If occ failed, report UNKNOWN
  120. if [ "$APP_RC" -ne 0 ]; then
  121. echo "3 \"$SERVICE Apps\" - Unable to determine available app updates (occ app:update exit code $APP_RC)"
  122. exit 0
  123. fi
  124. # Check if all apps are up to date
  125. if [[ "$APPRESULT" == *"All apps are up-to-date"* ]]; then
  126. echo "0 \"$SERVICE Apps\" - $SERVICE Apps all up to date"
  127. exit 0
  128. else
  129. UPDATECOUNT=$(printf '%s\n' "$APPRESULT" | wc -l)
  130. echo "1 \"$SERVICE Apps\" - $UPDATECOUNT $SERVICE Apps require updates"
  131. exit 0
  132. fi