nextcloud-updates.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/bin/bash
  2. # Version 1.5
  3. #
  4. # v1.5 CHANGES:
  5. # - added enhancement sugestion made by briand on checkmk forum to make the version-compare more granular
  6. # v1.4 CHANGES:
  7. # - added extra logic to deal with unresponsive or empty response from release-url
  8. # v1.3 CHANGES:
  9. # - Added check for configured apache-user and basepath exist.
  10. # v1.2 CHANGES:
  11. # - Updated nextcloud release check to actually get latest version from the release-url
  12. # v1.1 CHANGES:
  13. # - Updated query for installed version to versionstring instead of version
  14. #
  15. # WARNING: Current logic in the check will report wrong version as upgrade when a release is withdrawn from a/the release-url.
  16. # Installation:
  17. # This file should be place on a/the nextcloud host in /usr/lib/check_mk_agent/local
  18. #
  19. # (optional) if you want the check to run on a different schedule (like once per hour) put it in a subdirectory of above path
  20. # named to the number of seconds of the interval wanted, so 3600/ for once an hour, 86400 for once per day.
  21. #
  22. # Also do not forget to make this file executable with chmod +x /usr/lib/check_mk_agent/local/nextcloud-updates.sh
  23. # (c) Michael Honkoop <mhonkoop@comsolve.nl>
  24. # License: GNU General Public License v2
  25. RELEASEURL="https://download.nextcloud.com/server/releases/"
  26. #basepath of Nextcloud's occ executable
  27. BASEPATH="/var/www/html"
  28. # set the correct user for running the commands ( either apache or www-data, this depends on your Linux-Distribution)
  29. APACHEUSER="apache"
  30. # Set how exact the version-comparison should be - enhancement suggested by briand at checkmk forum
  31. #
  32. # how many “.”-separated fields to compare:
  33. # 3 → XX.YY.ZZ (full)
  34. # 2 → XX.YY (major+minor)
  35. # 1 → XX (major only)
  36. VERSION_FIELDS=2
  37. ######################
  38. # The code below should not be changed, unless you know EXACTLY what you are doing.
  39. # All confogureable parameters are above this section.
  40. ######################
  41. # check if jq package is present on the system
  42. /usr/bin/which jq >/dev/null 2>&1
  43. if [ $? -ne 0 ]; then
  44. echo "2 \"NextCloud\" - NextCloud localcheck requires jq package to be installed"
  45. echo "2 \"NextCloud Apps\" - NextCloud localcheck requires jq package to be installed"
  46. exit;
  47. fi
  48. # check if configured apache user exists
  49. if ! id -u "$APACHEUSER" >/dev/null 2>&1; then
  50. echo "2 \"NextCloud\" - NextCloud localcheck webserver user does not exist, check configuration!"
  51. echo "2 \"NextCloud Apps\" - NextCloud localcheck webserver user does not exist, check configuration!"
  52. exit;
  53. fi
  54. # check if configured basepath exists
  55. if [ ! -d "$BASEPATH" ]; then
  56. echo "2 \"NextCloud\" - NextCloud localcheck could not find configured basepath, check configuration!"
  57. echo "2 \"NextCloud Apps\" - NextCloud localcheck could not find configured basepath, check configuration!"
  58. exit;
  59. fi
  60. # Get current installed Nextcloud version-information
  61. # in v29.x.x version gives more detail, which is not given by the check on installed version, so switched to versionstring instead.
  62. NCINSTALLED=$(sudo -u $APACHEUSER php $BASEPATH/occ status --output=json | jq -r '.versionstring' | cut -d. -f1-"$VERSION_FIELDS")
  63. # Get latest avaiable version information
  64. # In v29.x.x this has changed outputs <name-of-instance> <version>,
  65. 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")
  66. # if the check on the release-url is not returning a version - report status
  67. if [ -z $NCAVAILABLE ]; then
  68. echo "1 \"NextCloud\" - No data received from release-URL, can not determine latest release-version"
  69. # compare resuts of installed and available version and report update available if they differ
  70. elif [ $NCINSTALLED != $NCAVAILABLE ]; then
  71. echo "1 \"NextCloud\" - A new NextCloud version ($NCAVAILABLE)is available, running version $NCINSTALLED please upgrade"
  72. else
  73. sudo -u $APACHEUSER php $BASEPATH/occ status -e
  74. if [ $? -eq 0 ]; then
  75. echo "0 \"NextCloud\" - NextCloud all up to date"
  76. elif [ $? -eq 1 ]; then
  77. echo "1 \"NextCloud\" - NextCloud maintenance-mode is enabled"
  78. elif [ $? -eq 2 ]; then
  79. echo "2 \"NextCloud\" - NextCloud Core needs updating"
  80. fi
  81. fi
  82. APPRESULT=$(sudo -u $APACHEUSER php $BASEPATH/occ app:update --showonly)
  83. if [[ $APPRESULT == *"All apps are up-to-date"* ]]; then
  84. echo "0 \"NextCloud Apps\" - NextCloud Apps all up to date"
  85. else
  86. UPDATECOUNT=$(echo "$APPRESULT" | wc -l)
  87. echo "1 \"NextCloud Apps\" - $UPDATECOUNT NextCloud Apps requires updates"
  88. fi