monitor-all.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. echo "Skipping $instance because it is excluded"
  32. continue
  33. fi
  34. # Determine the type of the instance (container or virtual machine)
  35. if pct status $instance >/dev/null 2>&1; then
  36. # It is a container
  37. config_cmd="pct config"
  38. IP=$(pct exec $instance ip a s dev eth0 | awk '\''/inet / {print $2}'\'' | cut -d/ -f1)
  39. else
  40. # It is a virtual machine
  41. config_cmd="qm config"
  42. IP=$(qm guest cmd $instance network-get-interfaces | egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -E "192\.|10\." | head -n 1)
  43. fi
  44. # Skip instances based on onboot and templates
  45. onboot=$($config_cmd $instance | grep -q "onboot: 0" || ( ! $config_cmd $instance | grep -q "onboot" ) && echo "true" || echo "false")
  46. template=$($config_cmd $instance | grep template | grep -q "template:" && echo "true" || echo "false")
  47. if [ "$onboot" == "true" ]; then
  48. echo "Skipping $instance because it is set not to boot"
  49. continue
  50. elif [ "$template" == "true" ]; then
  51. echo "Skipping $instance because it is a template"
  52. continue
  53. fi
  54. # Ping the instance
  55. if ! ping -c 1 $IP >/dev/null 2>&1; then
  56. # If the instance can not be pinged, stop and start it
  57. if pct status $instance >/dev/null 2>&1; then
  58. # It is a container
  59. echo "$(date): CT $instance is not responding, restarting..."
  60. pct stop $instance >/dev/null 2>&1
  61. sleep 5
  62. pct start $instance >/dev/null 2>&1
  63. else
  64. # It is a virtual machine
  65. if qm status $instance | grep -q "status: running"; then
  66. echo "$(date): VM $instance is not responding, restarting..."
  67. qm stop $instance >/dev/null 2>&1
  68. sleep 5
  69. else
  70. echo "$(date): VM $instance is not running, starting..."
  71. fi
  72. qm start $instance >/dev/null 2>&1
  73. fi
  74. fi
  75. done
  76. # Wait for 5 minutes. (Edit to your needs)
  77. echo "$(date): Pausing for 5 minutes..."
  78. sleep 300
  79. done >/var/log/ping-instances.log 2>&1' >/usr/local/bin/ping-instances.sh
  80. touch /var/log/ping-instances.log
  81. # Change file permissions to executable
  82. chmod +x /usr/local/bin/ping-instances.sh
  83. cat <<EOF >/etc/systemd/system/ping-instances.timer
  84. [Unit]
  85. Description=Delay ping-instances.service by 5 minutes
  86. [Timer]
  87. OnBootSec=300
  88. OnUnitActiveSec=300
  89. [Install]
  90. WantedBy=timers.target
  91. EOF
  92. # Create ping-instances.service
  93. cat <<EOF >/etc/systemd/system/ping-instances.service
  94. [Unit]
  95. Description=Ping instances every 5 minutes and restarts if necessary
  96. After=ping-instances.timer
  97. Requires=ping-instances.timer
  98. [Service]
  99. Type=simple
  100. # To specify which CT/VM should be excluded, add the CT/VM ID at the end of the line where ExecStart=/usr/local/bin/ping-instances.sh is specified.
  101. # For example: ExecStart=/usr/local/bin/ping-instances.sh 100 102
  102. # Virtual machines without the QEMU guest agent installed must be excluded.
  103. ExecStart=/usr/local/bin/ping-instances.sh
  104. Restart=always
  105. StandardOutput=file:/var/log/ping-instances.log
  106. StandardError=file:/var/log/ping-instances.log
  107. [Install]
  108. WantedBy=multi-user.target
  109. EOF
  110. # Reload daemon, enable and start ping-instances.service
  111. systemctl daemon-reload
  112. systemctl enable -q --now ping-instances.timer
  113. systemctl enable -q --now ping-instances.service
  114. clear
  115. echo -e "\n To view Monitor All logs: cat /var/log/ping-instances.log"
  116. }
  117. remove() {
  118. systemctl disable -q --now ping-instances.timer
  119. systemctl disable -q --now ping-instances.service
  120. rm /etc/systemd/system/ping-instances.service /etc/systemd/system/ping-instances.timer /usr/local/bin/ping-instances.sh /var/log/ping-instances.log
  121. echo "Removed Monitor All from Proxmox VE"
  122. }
  123. # Define options for the whiptail menu
  124. OPTIONS=(Add "Add Monitor-All to Proxmox VE" \
  125. Remove "Remove Monitor-All from Proxmox VE")
  126. # Show the whiptail menu and save the user's choice
  127. CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Monitor-All for Proxmox VE" --menu "Select an option:" 10 58 2 \
  128. "${OPTIONS[@]}" 3>&1 1>&2 2>&3)
  129. # Check the user's choice and perform the corresponding action
  130. case $CHOICE in
  131. "Add")
  132. add
  133. ;;
  134. "Remove")
  135. remove
  136. ;;
  137. *)
  138. echo "Exiting..."
  139. exit 0
  140. ;;
  141. esac