BASH 146
Monitor LXC Guest on 30th April 2023 10:49:12 AM
  1. #!/usr/bin/env bash
  2.  
  3. clear
  4. cat <<"EOF"
  5.     __  ___            _ __                __   _  ________
  6.    /  |/  /___  ____  (_) /_____  _____   / /  | |/ / ____/
  7.   / /|_/ / __ \/ __ \/ / __/ __ \/ ___/  / /   |   / /
  8.  / /  / / /_/ / / / / / /_/ /_/ / /     / /___/   / /___
  9. /_/  /_/\____/_/ /_/_/\__/\____/_/     /_____/_/|_\____/
  10.  
  11. EOF
  12.  
  13. add() {
  14. while true; do
  15.   read -p "This script will add Monitor LXC to Proxmox VE. Proceed(y/n)?" yn
  16.   case $yn in
  17.   [Yy]*) break ;;
  18.   [Nn]*) exit ;;
  19.   *) echo "Please answer yes or no." ;;
  20.   esac
  21. done
  22.  
  23. echo '#!/usr/bin/env bash
  24.  
  25. # Read excluded containers from command line arguments
  26. excluded_containers=("$@")
  27. echo "Excluded containers: ${excluded_containers[@]}"
  28.  
  29. while true
  30. do
  31.  # Get the list of containers
  32.  containers=$(pct list | tail -n +2 | cut -f1 -d" ")
  33.  
  34.  for container in $containers
  35.  do
  36.    # Skip excluded containers
  37.    if [[ " ${excluded_containers[@]} " =~ " ${container} " ]]; then
  38.      continue
  39.    fi
  40.  
  41.    # Skip containers based on templates
  42.    template=$(pct config $container | grep -q "template:" && echo "true" || echo "false")
  43.    if [ "$template" == "true" ]; then
  44.      continue
  45.    fi
  46.  
  47.    # Get the IP address of the container
  48.    IP=$(pct exec $container ip a s dev eth0 | awk '\''/inet / {print $2}'\'' | cut -d/ -f1)
  49.  
  50.    # Ping the container
  51.    if ! ping -c 1 $IP >/dev/null 2>&1; then
  52.      # If the container can'\''t be pinged, stop and start it
  53.      echo "$(date): Container $container is not responding, restarting..."
  54.      pct stop $container >/dev/null 2>&1
  55.      sleep 5
  56.      pct start $container >/dev/null 2>&1
  57.    fi
  58.  done
  59.  
  60.  # Wait for 5 minutes. (Edit to your needs)
  61.  echo "$(date): Sleeping for 5 minutes..."
  62.  sleep 300
  63. done >> /var/log/ping-containers.log 2>&1' >/usr/local/bin/ping-containers.sh
  64.  
  65. # Change file permissions to executable
  66. chmod +x /usr/local/bin/ping-containers.sh
  67.  
  68. # Create ping-containers.service
  69. echo '[Unit]
  70. Description=Pings containers every 5 minutes and restarts if necessary
  71.  
  72. [Service]
  73. Type=simple
  74. # Include the container ID at the end of the line where ExecStart=/usr/local/bin/ping-containers.sh is specified,
  75. # to indicate which container should be excluded. Example: ExecStart=/usr/local/bin/ping-containers.sh 100 102
  76. ExecStart=/usr/local/bin/ping-containers.sh
  77. Restart=always
  78. StandardOutput=file:/var/log/ping-containers.log
  79. StandardError=file:/var/log/ping-containers.log
  80.  
  81. [Install]
  82. WantedBy=multi-user.target' >/etc/systemd/system/ping-containers.service
  83.  
  84. # Reload daemon, enable and start ping-containers.service
  85. systemctl daemon-reload
  86. systemctl enable -q --now ping-containers.service
  87. clear
  88. echo -e "\n To view Monitor LXC logs: cat /var/log/ping-containers.log"
  89. }
  90.  
  91. remove() {
  92.   systemctl stop ping-containers.service
  93.   systemctl disable ping-containers.service &>/dev/null
  94.   rm /etc/systemd/system/ping-containers.service
  95.   rm /usr/local/bin/ping-containers.sh
  96.   rm /var/log/ping-containers.log
  97.   echo "Removed Monitor LXC from Proxmox VE"
  98. }
  99.  
  100. if [ "$1" == "add" ]; then
  101.     add
  102. elif [ "$1" == "remove" ]; then
  103.     remove
  104. else
  105.     echo "Usage: $0 [ -s add | -s remove ]"
  106.     exit 1
  107. fi

paste.retronerd.at ist fuer Quelltexte und generellen Debugging Text.

Login oder Registrieren um zu bearbeiten, loeschen, um deine Pastes zu verfolgen und mehr.

Raw Paste

Login oder Registrieren um diesen Paste zu bearbeiten oder zu forken. Es ist kostenlos.