1
0

gogs-version.sh 2.6 KB

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