simplesaml_version_check.sh 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 once a day to minimize processing cost.
  16. #
  17. # Also do not forget to make this file executable with chmod +x /usr/lib/check_mk_agent/local/simplesaml_version_check
  18. ##########################################################################
  19. # Global definitions for the script - change if needed to fit your needs #
  20. ##########################################################################
  21. # Define path to where SimpleSalmPHP is installed
  22. BASEPATH="/opt/simplesamlphp"
  23. # Version (json) file reference
  24. VERSION_FILE="composer.json"
  25. # url to check releases against
  26. RELEASEURL="https://api.github.com/repos/simplesamlphp/simplesamlphp/releases"
  27. # Service Name to be shown in Check_MK
  28. SERVICE="SimpleSAMLphp_Version"
  29. #####################################################################
  30. # DO NOT CHANGE BELOW UNLESS YOU EXACTLY KNOW WHAT YOU ARE DOING #
  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 presence of SimpleSAMLPhp and report if not found
  43. if [[ ! -f "$BASEPATH/$VERSION_FILE" ]]; then
  44. echo "3 \"$SERVICE\" - No local version information found in: $$BASEPATH/$VERSION_FILE check localcheck configuration."
  45. exit 0
  46. fi
  47. # Get installed version information via composer.json or version file
  48. if [ -f "$BASEPATH/$VERSION_FILE" ]; then
  49. INSTALLED_VERSION=$(jq -r '.version // empty' "$BASEPATH/$VERSION_FILE")
  50. INSTALLED_VERSION="${INSTALLED_VERSION#v}"
  51. else
  52. # Fallback to checking directory name if version isn't in composer.json
  53. INSTALLED_VERSION="Unknown"
  54. fi
  55. # Get latest avaiable version information from github.
  56. 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)
  57. # Error handling / Evaluate results
  58. if [ -z "$AVAILABLE_VERSION" ]; then
  59. # Issue Warn state when failing to fetch data from GitHub.
  60. echo "1 \"$SERVICE\" - Could not fetch the latest version from GitHub API."
  61. exit 0
  62. elif [ "$VERSION_FILE" = "Unknown" ]; then
  63. # Issue Warn state when local/installed version could not be determined.
  64. echo "1 \"$SERVICE\" - Cannot detect installed version at $VERSION_FILE."
  65. exit 0
  66. elif [ "$INSTALLED_VERSION" = "$AVAILABLE_VERSION" ]; then
  67. # Set OK state when installed and latest release version match.
  68. echo "0 \"$SERVICE\" - SimpleSAMLphp is up to date ($VERSION_FILE)."
  69. exit 0
  70. else
  71. # WARN: Update available
  72. echo "1 \"$SERVICE\" - Update available! Installed: $INSTALLED_VERSION, Available: $AVAILABLE_VERSION."
  73. exit 0
  74. fi