1
0

gogs-version.sh 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. # 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. if ! command -v jq >/dev/null 2>&1; then
  32. echo "2 \"$SERVICE\" - $SERVICE localcheck requires jq package to be installed"
  33. exit 0
  34. fi
  35. # Check if curl package is present on the system (required for this check)
  36. if ! command -v curl >/dev/null 2>&1; then
  37. echo "2 \"$SERVICE\" - $SERVICE localcheck requires curl package to be installed"
  38. exit 0
  39. fi
  40. # Check Gogs binary presence and report if not found
  41. if [[ ! -x "$GOGS_BIN" ]]; then
  42. echo "2 \"$SERVICE\" - Gogs executable not found: $GOGS_BIN check localcheck configuration."
  43. exit 0
  44. fi
  45. # Get current installed Gogs version-information
  46. GOGS_INSTALLED=$("$GOGS_BIN" --version 2>/dev/null | sed -nE 's/.*version ([0-9]+\.[0-9]+\.[0-9]+).*/\1/p')
  47. if [[ -z "$GOGS_INSTALLED" ]]; then
  48. echo "3 \"$SERVICE\" - Gogs executable found, but returned no valid version"
  49. exit 0
  50. fi
  51. # Get latest avaiable version information.
  52. 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//')
  53. # Return status when latest version information could not be retrieved.
  54. if [[ -z "$GOGS_AVAILABLE" || "$GOGS_AVAILABLE" == "null" ]]; then
  55. echo "3 \"$SERVICE\" - No data received from release-URL, can not determine latest release-version."
  56. exit 0
  57. fi
  58. # Validate the version returned by GitHub.
  59. if ! [[ "$GOGS_AVAILABLE" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  60. echo "3 \"$SERVICE\" - Invalid Gogs version received from release-URL: $GOGS_AVAILABLE"
  61. exit 0
  62. fi
  63. # Compare installed version to available version
  64. if [[ "$GOGS_INSTALLED" == "$GOGS_AVAILABLE" ]]; then
  65. echo "0 \"$SERVICE\" - Gogs version $GOGS_INSTALLED is up to date."
  66. elif [[ "$(printf '%s\n' "$GOGS_INSTALLED" "$GOGS_AVAILABLE" | sort -V | tail -n1)" == "$GOGS_AVAILABLE" ]]; then
  67. echo "1 \"$SERVICE\" - Update available: $GOGS_INSTALLED -> $GOGS_AVAILABLE"
  68. exit 0
  69. else
  70. echo "0 \"$SERVICE\" - Gogs version $GOGS_INSTALLED is newer than latest release $GOGS_AVAILABLE."
  71. exit 0
  72. fi