gogs-version.sh 3.6 KB

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