monitor-all.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2021-2023 tteck
  3. # Author: tteck (tteckster)
  4. # License: MIT
  5. # https://github.com/tteck/Proxmox/raw/main/LICENSE
  6. clear
  7. cat <<"EOF"
  8. __ ___ _ __ ___ ____
  9. / |/ /___ ____ (_) /_____ _____ / | / / /
  10. / /|_/ / __ \/ __ \/ / __/ __ \/ ___/ / /| | / / /
  11. / / / / /_/ / / / / / /_/ /_/ / / / ___ |/ / /
  12. /_/ /_/\____/_/ /_/_/\__/\____/_/ /_/ |_/_/_/
  13. EOF
  14. add() {
  15. while true; do
  16. read -p "This script will add Monitor All to Proxmox VE. Proceed(y/n)?" yn
  17. case $yn in
  18. [Yy]*) break ;;
  19. [Nn]*) exit ;;
  20. *) echo "Please answer yes or no." ;;
  21. esac
  22. done
  23. echo '#!/usr/bin/env bash
  24. # Read excluded instances from command line arguments
  25. excluded_instances=("$@")
  26. echo "Excluded instances: ${excluded_instances[@]}"
  27. while true; do
  28. for instance in $(pct list | awk '\''{if(NR>1) print $1}'\''; qm list | awk '\''{if(NR>1) print $1}'\''); do
  29. # Skip excluded instances
  30. if [[ " ${excluded_instances[@]} " =~ " ${instance} " ]]; then
  31. continue
  32. fi
  33. # Determine the type of the instance (container or virtual machine)
  34. if pct status $instance >/dev/null 2>&1; then
  35. # It is a container
  36. config_cmd="pct config"
  37. IP=$(pct exec $instance ip a s dev eth0 | awk '\''/inet / {print $2}'\'' | cut -d/ -f1)
  38. else
  39. # It is a virtual machine
  40. config_cmd="qm config"
  41. IP=$(qm guest cmd $instance network-get-interfaces | egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -E "192\.|10\.")
  42. fi
  43. # Skip instances based on templates
  44. template=$($config_cmd $instance | grep template | grep -q "template:" && echo "true" || echo "false")
  45. if [ "$template" == "true" ]; then
  46. echo "Skipping $instance because it is a template"
  47. continue
  48. fi
  49. # Ping the instance
  50. if ! ping -c 1 $IP >/dev/null 2>&1; then
  51. # If the instance can not be pinged, stop and start it
  52. if pct status $instance >/dev/null 2>&1; then
  53. # It is a container
  54. echo "$(date): CT $instance is not responding, restarting..."
  55. pct stop $instance >/dev/null 2>&1
  56. sleep 5
  57. pct start $instance >/dev/null 2>&1
  58. else
  59. # It is a virtual machine
  60. if qm status $instance | grep -q "status: running"; then
  61. echo "$(date): VM $instance is not responding, resetting..."
  62. qm reset $instance >/dev/null 2>&1
  63. else
  64. qm start $instance >/dev/null 2>&1
  65. echo "$(date): VM $instance is not running, starting..."
  66. fi
  67. fi
  68. fi
  69. done
  70. # Wait for 5 minutes. (Edit to your needs)
  71. echo "$(date): Pausing for 5 minutes..."
  72. sleep 300
  73. done >> /var/log/ping-instances.log 2>&1' >/usr/local/bin/ping-instances.sh
  74. # Change file permissions to executable
  75. chmod +x /usr/local/bin/ping-instances.sh
  76. # Create ping-instances.service
  77. echo '[Unit]
  78. Description=Ping instances every 5 minutes and restarts if necessary
  79. [Service]
  80. Type=simple
  81. # Include the CT/VM ID at the end of the line where ExecStart=/usr/local/bin/ping-instances.sh is specified,
  82. # to indicate which CT/VN should be excluded. Example: ExecStart=/usr/local/bin/ping-instances.sh 100 102
  83. ExecStart=/usr/local/bin/ping-instances.sh
  84. Restart=always
  85. StandardOutput=file:/var/log/ping-instances.log
  86. StandardError=file:/var/log/ping-instances.log
  87. [Install]
  88. WantedBy=multi-user.target' >/etc/systemd/system/ping-instances.service
  89. # Reload daemon, enable and start ping-instances.service
  90. systemctl daemon-reload
  91. systemctl enable -q --now ping-instances.service
  92. clear
  93. echo -e "\n To view Monitor All logs: cat /var/log/ping-instances.log"
  94. }
  95. remove() {
  96. systemctl stop ping-instances.service
  97. systemctl disable ping-instances.service &>/dev/null
  98. rm /etc/systemd/system/ping-instances.service
  99. rm /usr/local/bin/ping-instances.sh
  100. rm /var/log/ping-instances.log
  101. echo "Removed Monitor All from Proxmox VE"
  102. }
  103. # Define options for the whiptail menu
  104. OPTIONS=(Add "Add Monitor-All to Proxmox VE" \
  105. Remove "Remove Monitor-All from Proxmox VE")
  106. # Show the whiptail menu and save the user's choice
  107. CHOICE=$(whiptail --title "Monitor-All for Proxmox VE" --menu "Select an option:" 10 58 2 \
  108. "${OPTIONS[@]}" 3>&1 1>&2 2>&3)
  109. # Check the user's choice and perform the corresponding action
  110. case $CHOICE in
  111. "Add")
  112. add
  113. ;;
  114. "Remove")
  115. remove
  116. ;;
  117. *)
  118. echo "Exiting..."
  119. exit 0
  120. ;;
  121. esac