directadmin.sh 3.9 KB

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