monitor-all.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\." | head -n 1)
  42. fi
  43. # Skip instances based on onboot and templates
  44. onboot=$($config_cmd $instance | grep onboot | grep -q "onboot: 0" && echo "true" || echo "false")
  45. template=$($config_cmd $instance | grep template | grep -q "template:" && echo "true" || echo "false")
  46. if [ "$onboot" == "true" ]; then
  47. echo "Skipping $instance because it is set not to boot"
  48. continue
  49. elif [ "$template" == "true" ]; then
  50. echo "Skipping $instance because it is a template"
  51. continue
  52. fi
  53. # Ping the instance
  54. if ! ping -c 1 $IP >/dev/null 2>&1; then
  55. # If the instance can not be pinged, stop and start it
  56. if pct status $instance >/dev/null 2>&1; then
  57. # It is a container
  58. echo "$(date): CT $instance is not responding, restarting..."
  59. pct stop $instance >/dev/null 2>&1
  60. sleep 5
  61. pct start $instance >/dev/null 2>&1
  62. else
  63. # It is a virtual machine
  64. if qm status $instance | grep -q "status: running"; then
  65. echo "$(date): VM $instance is not responding, resetting..."
  66. qm reset $instance >/dev/null 2>&1
  67. else
  68. qm start $instance >/dev/null 2>&1
  69. echo "$(date): VM $instance is not running, starting..."
  70. fi
  71. fi
  72. fi
  73. done
  74. # Wait for 5 minutes. (Edit to your needs)
  75. echo "$(date): Pausing for 5 minutes..."
  76. sleep 300
  77. done >> /var/log/ping-instances.log 2>&1' >/usr/local/bin/ping-instances.sh
  78. # Change file permissions to executable
  79. chmod +x /usr/local/bin/ping-instances.sh
  80. # Create ping-instances.service
  81. echo '[Unit]
  82. Description=Ping instances every 5 minutes and restarts if necessary
  83. [Service]
  84. Type=simple
  85. # 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.
  86. # For example: ExecStart=/usr/local/bin/ping-instances.sh 100 102
  87. # Virtual machines without the QEMU guest agent installed must be excluded.
  88. ExecStart=/usr/local/bin/ping-instances.sh
  89. Restart=always
  90. StandardOutput=file:/var/log/ping-instances.log
  91. StandardError=file:/var/log/ping-instances.log
  92. [Install]
  93. WantedBy=multi-user.target' >/etc/systemd/system/ping-instances.service
  94. # Reload daemon, enable and start ping-instances.service
  95. systemctl daemon-reload
  96. systemctl enable -q --now ping-instances.service
  97. clear
  98. echo -e "\n To view Monitor All logs: cat /var/log/ping-instances.log"
  99. }
  100. remove() {
  101. systemctl stop ping-instances.service
  102. systemctl disable ping-instances.service &>/dev/null
  103. rm /etc/systemd/system/ping-instances.service
  104. rm /usr/local/bin/ping-instances.sh
  105. rm /var/log/ping-instances.log
  106. echo "Removed Monitor All from Proxmox VE"
  107. }
  108. # Define options for the whiptail menu
  109. OPTIONS=(Add "Add Monitor-All to Proxmox VE" \
  110. Remove "Remove Monitor-All from Proxmox VE")
  111. # Show the whiptail menu and save the user's choice
  112. CHOICE=$(whiptail --title "Monitor-All for Proxmox VE" --menu "Select an option:" 10 58 2 \
  113. "${OPTIONS[@]}" 3>&1 1>&2 2>&3)
  114. # Check the user's choice and perform the corresponding action
  115. case $CHOICE in
  116. "Add")
  117. add
  118. ;;
  119. "Remove")
  120. remove
  121. ;;
  122. *)
  123. echo "Exiting..."
  124. exit 0
  125. ;;
  126. esac