1
0

simplesaml_version_check.sh 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. # (c) Michael Honkoop <mhonkoop@comsolve.nl>
  3. # License: GNU General Public License v2
  4. # Version 0.1:
  5. # - initial release
  6. # Current active version of the script
  7. SCRIPT_VERSION=0.1
  8. # Installation:
  9. # This file should be placed on a/the host running SimpleSAMLPhp in /usr/lib/check_mk_agent/local
  10. #
  11. # (reccomended) if you want the check to run on a different schedule (like once per hour) put it in a subdirectory of above path
  12. # named to the number of seconds of the interval wanted, so 3600/ for once an hour, 86400/ for once per day.
  13. # Suggestion for this check is to run this once a day to minimize processing cost, github does not like it when you query every (default)
  14. # check-interval, and will return a http 403 -error.
  15. #
  16. # Also do not forget to make this file executable with chmod +x /usr/lib/check_mk_agent/local/simplesaml_version_check.sh
  17. ##########################################################################
  18. # Global definitions for the script - change if needed to fit your needs #
  19. ##########################################################################
  20. # Define path to where SimpleSAMLPhp is installed
  21. BASEPATH="/opt/simplesamlphp"
  22. # Version (json) file reference
  23. VERSION_FILE="composer.json"
  24. # url to check releases against
  25. RELEASEURL="https://api.github.com/repos/simplesamlphp/simplesamlphp/releases"
  26. # Service Name to be shown in Check_MK
  27. SERVICE="SimpleSAMLphp_Version"
  28. #####################################################################
  29. # DO NOT CHANGE BELOW UNLESS YOU EXACTLY KNOW WHAT YOU ARE DOING #
  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 "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 presence of SimpleSAMLPhp and report if not found
  42. if [[ ! -f "$BASEPATH/$VERSION_FILE" ]]; then
  43. echo "2 \"$SERVICE\" - No local version information found in: $$BASEPATH/$VERSION_FILE check localcheck configuration."
  44. exit 0
  45. fi
  46. # Get installed version information via composer.json or version file
  47. if [ -f "$BASEPATH/$VERSION_FILE" ]; then
  48. INSTALLED_VERSION=$(jq -r '.version // empty' "$BASEPATH/$VERSION_FILE")
  49. INSTALLED_VERSION="${INSTALLED_VERSION#v}"
  50. else
  51. # Fallback to checking directory name if version isn't in composer.json
  52. INSTALLED_VERSION="Unknown"
  53. fi
  54. # Get latest avaiable version information from github.
  55. AVAILABLE_VERSION=$(curl -fsSL -H 'Accept: application/vnd.github+json' --connect-timeout 5 --max-time 15 "$RELEASEURL" | jq -r '.[] | select(.draft == false and .prerelease == false) | .tag_name' | sed 's/^v//' | sort -V | tail -n1)
  56. # Error handling / Evaluate results
  57. if [ -z "$AVAILABLE_VERSION" ]; then
  58. # Issue Warn state when failing to fetch data from GitHub.
  59. echo "1 \"$SERVICE\" - Could not fetch the latest version from GitHub API."
  60. exit 0
  61. elif [ "$VERSION_FILE" = "Unknown" ]; then
  62. # Issue Warn state when local/installed version could not be determined.
  63. echo "1 \"$SERVICE\" - Cannot detect installed version at $VERSION_FILE."
  64. exit 0
  65. elif [ "$INSTALLED_VERSION" = "$AVAILABLE_VERSION" ]; then
  66. # Set OK state when installed and latest release versnion match.
  67. echo "0 \"$SERVICE\" - SimpleSAMLphp is up to date ($VERSION_FILE)."
  68. exit 0
  69. else
  70. # WARN: Update available
  71. echo "1 \"$SERVICE\" - Update available! Installed: $INSTALLED_VERSION, Available: $AVAILABLE_VERSION."
  72. exit 0
  73. fi