nextcloud-updates.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. # check if jq package is present on the system
  54. if ! command -v curl >/dev/null 2>&1; then
  55. echo "2 \"$SERVICE\" - $SERVICE localcheck requires jq package to be installed"
  56. echo "2 \"$SERVICE Apps\" - $SERVICE Apps localcheck requires jq package to be installed"
  57. exit 0
  58. fi
  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' | cut -d. -f1-"$VERSION_FIELDS")
  74. # Get latest avaiable (core) version information
  75. # In v29.x.x this has changed outputs <name-of-instance> <version>,
  76. NCAVAILABLE=$(curl -s --connect-timeout 5 --fail-with-body "$RELEASEURL" | sed -e 's/<[^>]*>//g' | grep -o "^nextcloud.*.zip" | tail -n1 | cut -d'-' -f 2 | sed 's/.\{4\}$//' | cut -d. -f1-"$VERSION_FIELDS")
  77. # if the check on the release-url is not returning a version - report status
  78. if [ -z "$NCAVAILABLE" ]; then
  79. echo "1 \"$SERVICE\" - No data received from release-URL, can not determine latest release-version for $SERVICE Core"
  80. # compare resuts of installed and available version and report update available if they differ
  81. elif [ "$NCINSTALLED" != "$NCAVAILABLE" ]; then
  82. echo "1 \"$SERVICE\" - A new $SERVICE Core version ($NCAVAILABLE) is available, running Core version is: $NCINSTALLED please upgrade"
  83. else
  84. sudo -u "$APACHEUSER" php "$BASEPATH"/occ status -e
  85. if [ $? -eq 0 ]; then
  86. echo "0 \"$SERVICE\" - $SERVICE all up to date"
  87. elif [ $? -eq 1 ]; then
  88. echo "1 \"$SERVICE\" - $SERVICE maintenance-mode is enabled"
  89. elif [ $? -eq 2 ]; then
  90. echo "2 \"$SERVICE\" - $SERVICE Core needs updating"
  91. fi
  92. fi
  93. APPRESULT=$(sudo -u "$APACHEUSER" php "$BASEPATH"/occ app:update --showonly)
  94. if [[ "$APPRESULT" == *"All apps are up-to-date"* ]]; then
  95. echo "0 \"$SERVICE Apps\" - $SERVICE Apps all up to date"
  96. exit 0
  97. else
  98. UPDATECOUNT=$(echo "$APPRESULT" | wc -l)
  99. echo "1 \"$SERVICE Apps\" - $UPDATECOUNT $SERVICE Apps requires updates"
  100. exit 0
  101. fi