1
0

simplesaml_version_check.sh 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. # Version 0.2
  7. # - adapted state for checking required packages for the check
  8. # Current active version of the script
  9. SCRIPT_VERSION=0.2
  10. # Installation:
  11. # This file should be placed on a/the host running SimpleSamlPHP in /usr/lib/check_mk_agent/local
  12. #
  13. # (reccomended) 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. # Suggestion for this check is to run this at least once an hour, preferrably once a day to minimize processing cost.
  16. # ..and to avoid a http 403 from querying github too often.
  17. #
  18. # Also do not forget to make this file executable with chmod +x /usr/lib/check_mk_agent/local/simplesaml_version_check
  19. ##########################################################################
  20. # Global definitions for the script - change if needed to fit your needs #
  21. ##########################################################################
  22. # Define path to where SimpleSalmPHP is installed
  23. BASEPATH="/opt/simplesamlphp"
  24. # Version (json) file reference
  25. VERSION_FILE="composer.json"
  26. # url to check releases against
  27. RELEASEURL="https://api.github.com/repos/simplesamlphp/simplesamlphp/releases"
  28. # Service Name to be shown in Check_MK
  29. SERVICE="SimpleSAMLphp_Version"
  30. #####################################################################
  31. # DO NOT CHANGE BELOW UNLESS YOU EXACTLY KNOW WHAT YOU ARE DOING #
  32. #####################################################################
  33. # Check if jq package is present on the system (required for this check)
  34. if ! command -v jq >/dev/null 2>&1; then
  35. echo "3 \"$SERVICE\" - $SERVICE localcheck requires jq package to be installed"
  36. exit 0
  37. fi
  38. # Check if curl package is present on the system (required for this check)
  39. if ! command -v curl >/dev/null 2>&1; then
  40. echo "3 \"$SERVICE\" - $SERVICE localcheck requires curl package to be installed"
  41. exit 0
  42. fi
  43. # Check presence of SimpleSAMLPhp and report if not found
  44. if [[ ! -f "$BASEPATH/$VERSION_FILE" ]]; then
  45. echo "3 \"$SERVICE\" - No local version information found in: $$BASEPATH/$VERSION_FILE check localcheck configuration."
  46. exit 0
  47. fi
  48. # Get installed version information via composer.json or version file
  49. if [ -f "$BASEPATH/$VERSION_FILE" ]; then
  50. INSTALLED_VERSION=$(jq -r '.version // empty' "$BASEPATH/$VERSION_FILE")
  51. INSTALLED_VERSION="${INSTALLED_VERSION#v}"
  52. else
  53. # Fallback to checking directory name if version isn't in composer.json
  54. INSTALLED_VERSION="Unknown"
  55. fi
  56. # Get latest avaiable version information from github.
  57. 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)
  58. # Error handling / Evaluate results
  59. if [ -z "$AVAILABLE_VERSION" ]; then
  60. # Issue Warn state when failing to fetch data from GitHub.
  61. echo "1 \"$SERVICE\" - Could not fetch the latest version from GitHub API."
  62. exit 0
  63. elif [ "$VERSION_FILE" = "Unknown" ]; then
  64. # Issue Warn state when local/installed version could not be determined.
  65. echo "1 \"$SERVICE\" - Cannot detect installed version at $VERSION_FILE."
  66. exit 0
  67. elif [ "$INSTALLED_VERSION" = "$AVAILABLE_VERSION" ]; then
  68. # Set OK state when installed and latest release version match.
  69. echo "0 \"$SERVICE\" - SimpleSAMLphp is up to date ($VERSION_FILE)."
  70. exit 0
  71. else
  72. # WARN: Update available
  73. echo "1 \"$SERVICE\" - Update available! Installed: $INSTALLED_VERSION, Available: $AVAILABLE_VERSION."
  74. exit 0
  75. fi