simplesaml_version_check.sh 3.8 KB

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