1
0

directadmin.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. # - Updated output to have updateable compontents comma-sepoarated.
  8. # Version 0.3:
  9. # - Removed a duplicate routine, and added more comments
  10. # Version 0.4:
  11. # - Added check for DirectAdmin Core => Rediscovery is required if you had a previous version installed !
  12. # Version 0.5:
  13. # - Updates status to be reported in CheckMK when prerequisites are missing.
  14. # Current active version of the script
  15. SCRIPT_VERSION=0.5
  16. # Installation:
  17. # This file should be placed on a/the DirectAdmin host in /usr/lib/check_mk_agent/local
  18. #
  19. # (optional/reccomended) if you want the check to run on a different schedule (like once per hour) put it in a subdirectory of above path
  20. # named to the number of seconds of the interval wanted, so 3600/ for once an hour, 86400/ for once per day.
  21. # Suggestion for this check is to run this once a day to minimize processing cost.
  22. #
  23. # Also do not forget to make this file executable with chmod +x /usr/lib/check_mk_agent/local/direactadmin.sh
  24. ##########################################################################
  25. # Global definitions for the script - change if needed to fit your needs #
  26. ##########################################################################
  27. # Custombuild switch , set to 1 if you are using it.
  28. CustomBuild=1
  29. # Service Name
  30. SERVICE="DirectAdmin"
  31. #####################################################################
  32. # DO NOT CHANGE BELOW UNLESS YOU EXACTLY KNOW WHAT YOU ARE DOING #
  33. #####################################################################
  34. # Check if da command is present
  35. if ! command -v da >/dev/null 2>&1; then
  36. echo "3 \"$SERVICE Core\" - $SERVICE executable was not found."
  37. fi
  38. # Read installed DirectAdmin (core) version
  39. DA_INSTALLED=$(da version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+(\.[0-9]+)?' | head -n1)
  40. # Check if a version was returned
  41. if [[ -z "$DA_INSTALLED" ]]; then
  42. echo "3 \"$SERVICE Core\" - Unable to determine DirectAdmin Core version"
  43. fi
  44. # Read latest available DirectAdmin (core) version
  45. DA_AVAILABLE=$(da update --check 2>&1 | grep -oE '[0-9]+\.[0-9]+(\.[0-9]+)?' | head -n1)
  46. # Check if a version was returned
  47. if [[ -z "$DA_AVAILABLE" ]]; then
  48. echo "3 \"$SERVICE Core\" - Unable to determine available $SERVICE Core version"
  49. fi
  50. # Compare installed DirectAdmin version to available version
  51. if [[ "$DA_INSTALLED" = "$DA_AVAILABLE" ]]; then
  52. echo "0 \"$SERVICE Core\" - $SERVICE Core is up to date: ($DA_INSTALLED)"
  53. elif [[ "$DA_INSTALLED" < "$DA_AVAILABLE" ]]; then
  54. echo "1 \"$SERVICE Core\" - $SERVICE Core Update available: ($DA_AVAILABLE), Installed: ($DA_INSTALLED)"
  55. elif [[ "$DA_INSTALLED" > "$DA_AVAILABLE" ]]; then
  56. echo "2 \"$SERVICE Core\" - $SERVICE Core installed: ($DA_INSTALLED) is higher then available: ($DA_AVAILABLE)"
  57. fi
  58. # Define global path of directAdmin
  59. if [[ "$CustomBuild" -eq 1 ]]
  60. then
  61. DA_PATH="/usr/local/directadmin/custombuild"
  62. else
  63. DA_PATH="/usr/local/directadmin"
  64. fi
  65. # store version information from DirectAdmin packages
  66. DirectAdmin_Versions_Update=$("$DA_PATH"/build versions)
  67. # Define updates Array
  68. updates=()
  69. # Iterate over the collected data to find possible updates
  70. while read -r line; do
  71. if [[ "$line" =~ ^Latest\ version\ of\ (.+):\ (.+)$ ]]; then
  72. component="${BASH_REMATCH[1]}"
  73. latest="${BASH_REMATCH[2]}"
  74. elif [[ "$line" =~ ^Installed\ version\ of\ (.+):\ (.+)$ ]]; then
  75. installed="${BASH_REMATCH[2]}"
  76. # Compare versions using sort -V
  77. if [[ "$(printf '%s\n%s\n' "$installed" "$latest" | sort -V | head -n1)" != "$latest" ]]; then
  78. updates+=("$component")
  79. fi
  80. fi
  81. done <<< "$DirectAdmin_Versions_Update"
  82. # Report findings back to CheckMK
  83. if [[ ${#updates[@]} -eq 0 ]]; then
  84. echo "0 \"$SERVICE Components\" - All $SERVICE Components are up to date"
  85. else
  86. update_list=$(IFS=,; echo "${updates[*]}")
  87. echo "1 \"$SERVICE Components\" - $SERVICE Components ${update_list} have updates"
  88. fi