1
0

gogs-version.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 than available from the release-url.
  4. # Version 0.3 - changed status reporting in CheckMK when prerequisites for the Localcheck are not met.
  5. # Current active version of the script
  6. SCRIPT_VERSION=0.3
  7. # Installation
  8. # This file should be place on a/the Gogs (https://gogs.io) host in /usr/lib/check_mk_agent/local
  9. #
  10. # (optional) if you want the check to run on a different schedule (like once per hour) put it in a subdirectory of above path
  11. # named to the number of seconds of the interval wanted, so 3600/ for once an hour, 86400/ for once per day.
  12. #
  13. # Also do not forget to make this file executable with chmod +x /usr/lib/check_mk_agent/local/gogs-version.sh
  14. # (c) Michael Honkoop <mhonkoop@comsolve.nl>
  15. # License: GNU General Public License v2
  16. ######################
  17. # Configurable Parameters
  18. ######################
  19. # url to check releases against
  20. RELEASEURL="https://api.github.com/repos/gogs/gogs/releases"
  21. # basepath of where gogs is installed (no trailing slash-forward)
  22. BASEPATH="/opt/gogs"
  23. # Gogs executable
  24. GOGS_BIN="${BASEPATH}/gogs"
  25. # Service name
  26. SERVICE="Gogs"
  27. ######################
  28. # The code below should not be changed, unless you know EXACTLY what you are doing.
  29. # All configureable parameters are above this section.
  30. ######################
  31. # Check if jq package is present on the system (required for this check)
  32. if ! command -v jq >/dev/null 2>&1; then
  33. echo "3 \"$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 "3 \"$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 "3 \"$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. # Validate the version returned by GitHub.
  60. if ! [[ "$GOGS_AVAILABLE" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  61. echo "3 \"$SERVICE\" - Invalid Gogs version received from release-URL: $GOGS_AVAILABLE"
  62. exit 0
  63. fi
  64. # Compare installed version to available version
  65. if [[ "$GOGS_INSTALLED" == "$GOGS_AVAILABLE" ]]; then
  66. echo "0 \"$SERVICE\" - Gogs version $GOGS_INSTALLED is up to date."
  67. elif [[ "$(printf '%s\n' "$GOGS_INSTALLED" "$GOGS_AVAILABLE" | sort -V | tail -n1)" == "$GOGS_AVAILABLE" ]]; then
  68. echo "1 \"$SERVICE\" - Update available: $GOGS_INSTALLED -> $GOGS_AVAILABLE"
  69. exit 0
  70. else
  71. echo "0 \"$SERVICE\" - Gogs version $GOGS_INSTALLED is newer than latest release $GOGS_AVAILABLE."
  72. exit 0
  73. fi