monitor-all.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 onboot | grep -q "onboot: 0" && 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. # Change file permissions to executable
  81. chmod +x /usr/local/bin/ping-instances.sh
  82. # Create ping-instances.service
  83. echo '[Unit]
  84. Description=Ping instances every 5 minutes and restarts if necessary
  85. [Service]
  86. Type=simple
  87. # 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.
  88. # For example: ExecStart=/usr/local/bin/ping-instances.sh 100 102
  89. # Virtual machines without the QEMU guest agent installed must be excluded.
  90. # Sleep for 300 seconds (5 minutes)
  91. ExecStartPre=/usr/bin/sleep 300
  92. ExecStart=/usr/local/bin/ping-instances.sh
  93. Restart=always
  94. StandardOutput=file:/var/log/ping-instances.log
  95. StandardError=file:/var/log/ping-instances.log
  96. [Install]
  97. WantedBy=multi-user.target' >/etc/systemd/system/ping-instances.service
  98. # Reload daemon, enable and start ping-instances.service
  99. systemctl daemon-reload
  100. systemctl enable -q --now ping-instances.service
  101. clear
  102. echo -e "\n To view Monitor All logs: cat /var/log/ping-instances.log"
  103. }
  104. remove() {
  105. systemctl stop ping-instances.service
  106. systemctl disable ping-instances.service &>/dev/null
  107. rm /etc/systemd/system/ping-instances.service
  108. rm /usr/local/bin/ping-instances.sh
  109. rm /var/log/ping-instances.log
  110. echo "Removed Monitor All from Proxmox VE"
  111. }
  112. # Define options for the whiptail menu
  113. OPTIONS=(Add "Add Monitor-All to Proxmox VE" \
  114. Remove "Remove Monitor-All from Proxmox VE")
  115. # Show the whiptail menu and save the user's choice
  116. CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Monitor-All for Proxmox VE" --menu "Select an option:" 10 58 2 \
  117. "${OPTIONS[@]}" 3>&1 1>&2 2>&3)
  118. # Check the user's choice and perform the corresponding action
  119. case $CHOICE in
  120. "Add")
  121. add
  122. ;;
  123. "Remove")
  124. remove
  125. ;;
  126. *)
  127. echo "Exiting..."
  128. exit 0
  129. ;;
  130. esac