gogs-version.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash
  2. # Version 0.1 - initial start of this localcheck
  3. # Version 0.2 - introduced additional logic to also deal with a local installed version being higher then available from the release-url.
  4. # Current active version of the script
  5. SCRIPT_VERSION=0.2
  6. # Installation
  7. # This file should be place on a/the Gogs (https://gogs.io) host in /usr/lib/check_mk_agent/local
  8. #
  9. # (optional) if you want the check to run on a different schedule (like once per hour) put it in a subdirectory of above path
  10. # named to the number of seconds of the interval wanted, so 3600/ for once an hour, 86400/ for once per day.
  11. #
  12. # Also do not forget to make this file executable with chmod +x /usr/lib/check_mk_agent/local/gogs-version.sh
  13. # (c) Michael Honkoop <mhonkoop@comsolve.nl>
  14. # License: GNU General Public License v2
  15. ######################
  16. # Configurable Parameters
  17. ######################
  18. # url to check releases against
  19. RELEASEURL="https://api.github.com/repos/gogs/gogs/releases"
  20. # basepath of where gogs is installed (no trailing slash-forward)
  21. BASEPATH="/opt/gogs"
  22. # Gogs executable
  23. GOGS_BIN="${BASEPATH}/gogs"
  24. # Service name
  25. SERVICE="Gogs"
  26. ######################
  27. # The code below should not be changed, unless you know EXACTLY what you are doing.
  28. # All configureable parameters are above this section.
  29. ######################
  30. # Check if jq package is present on the system (required for this check)
  31. command -v jq >/dev/null 2>&1
  32. if [ $? -ne 0 ]; then
  33. echo "2 \"$SERVICE\" - $SERVICE localcheck requires jq package to be installed"
  34. exit 0
  35. fi
  36. # Check if curl package is present on the system (required for this check)
  37. if ! command -v curl >/dev/null 2>&1; then
  38. echo "2 \"$SERVICE\" - $SERVICE localcheck requires curl package to be installed"
  39. exit 0
  40. fi
  41. # Check Gogs binary presence and report if not found
  42. if [[ ! -x "$GOGS_BIN" ]]; then
  43. echo "2 \"$SERVICE\" - Gogs executable not found: $GOGS_BIN check localcheck configuration."
  44. exit 0
  45. fi
  46. # Get current installed Gogs version-information
  47. GOGS_INSTALLED=$("$GOGS_BIN" --version 2>/dev/null | sed -nE 's/.*version ([0-9]+\.[0-9]+\.[0-9]+).*/\1/p')
  48. if [[ -z "$GOGS_INSTALLED" ]]; then
  49. echo "3 \"$SERVICE\" - Gogs executable found, but returned no valid version"
  50. exit 0
  51. fi
  52. # Get latest avaiable version information.
  53. GOGS_AVAILABLE=$(curl -fsSL -H 'Accept: application/vnd.github+json' --connect-timeout 5 --max-time 15 "$RELEASEURL" | jq -er 'map(select(.draft == false and .prerelease == false)) | .[0].tag_name' | sed 's/^v//')
  54. # Return status when latest version information could not be retrieved.
  55. if [[ -z "$GOGS_AVAILABLE" || "$GOGS_AVAILABLE" == "null" ]]; then
  56. echo "3 \"$SERVICE\" - No data received from release-URL, can not determine latest release-version."
  57. exit 0
  58. fi
  59. # Compare installed version to available version
  60. if [[ "$GOGS_INSTALLED" == "$GOGS_AVAILABLE" ]]; then
  61. echo "0 \"$SERVICE\" - Gogs version $GOGS_INSTALLED is up to date."
  62. elif [[ "$(printf '%s\n' "$GOGS_INSTALLED" "$GOGS_AVAILABLE" | sort -V | tail -n1)" == "$GOGS_AVAILABLE" ]]; then
  63. echo "1 \"$SERVICE\" - Update available: $GOGS_INSTALLED -> $GOGS_AVAILABLE"
  64. exit 0
  65. else
  66. echo "0 \"$SERVICE\" - Gogs version $GOGS_INSTALLED is newer than latest release $GOGS_AVAILABLE."
  67. exit 0
  68. fi