simplesaml_version_check.sh 4.0 KB

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