1
0

directadmin.sh 4.3 KB

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