mirror of
https://github.com/tteck/Proxmox.git
synced 2024-12-28 11:03:57 +02:00
70ce72fcbf
final tweak
129 lines
3.9 KiB
Bash
129 lines
3.9 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
# Copyright (c) 2021-2023 tteck
|
|
# Author: tteck (tteckster)
|
|
# License: MIT
|
|
# https://github.com/tteck/Proxmox/raw/main/LICENSE
|
|
|
|
clear
|
|
cat <<"EOF"
|
|
__ ___ _ __ ___ ____
|
|
/ |/ /___ ____ (_) /_____ _____ / | / / /
|
|
/ /|_/ / __ \/ __ \/ / __/ __ \/ ___/ / /| | / / /
|
|
/ / / / /_/ / / / / / /_/ /_/ / / / ___ |/ / /
|
|
/_/ /_/\____/_/ /_/_/\__/\____/_/ /_/ |_/_/_/
|
|
|
|
EOF
|
|
|
|
add() {
|
|
while true; do
|
|
read -p "This script will add Monitor All to Proxmox VE. Proceed(y/n)?" yn
|
|
case $yn in
|
|
[Yy]*) break ;;
|
|
[Nn]*) exit ;;
|
|
*) echo "Please answer yes or no." ;;
|
|
esac
|
|
done
|
|
|
|
echo '#!/usr/bin/env bash
|
|
# Read excluded instances from command line arguments
|
|
excluded_instances=("$@")
|
|
echo "Excluded instances: ${excluded_instances[@]}"
|
|
|
|
while true; do
|
|
|
|
for instance in $(pct list | awk '\''{if(NR>1) print $1}'\''; qm list | awk '\''{if(NR>1) print $1}'\''); do
|
|
# Skip excluded instances
|
|
if [[ " ${excluded_instances[@]} " =~ " ${instance} " ]]; then
|
|
continue
|
|
fi
|
|
|
|
# Determine the type of the instance (container or virtual machine)
|
|
if pct status $instance >/dev/null 2>&1; then
|
|
# It is a container
|
|
config_cmd="pct config"
|
|
IP=$(pct exec $instance ip a s dev eth0 | awk '\''/inet / {print $2}'\'' | cut -d/ -f1)
|
|
else
|
|
# It is a virtual machine
|
|
config_cmd="qm config"
|
|
IP=$(qm guest cmd $instance network-get-interfaces | egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -E "192\.|10\.")
|
|
fi
|
|
|
|
# Skip instances based on templates
|
|
template=$($config_cmd $instance | grep template | grep -q "template:" && echo "true" || echo "false")
|
|
if [ "$template" == "true" ]; then
|
|
echo "Skipping $instance because it is a template"
|
|
continue
|
|
fi
|
|
|
|
# Ping the instance
|
|
if ! ping -c 1 $IP >/dev/null 2>&1; then
|
|
# If the instance can not be pinged, stop and start it
|
|
if pct status $instance >/dev/null 2>&1; then
|
|
# It is a container
|
|
echo "$(date): CT $instance is not responding, restarting..."
|
|
pct stop $instance >/dev/null 2>&1
|
|
sleep 5
|
|
pct start $instance >/dev/null 2>&1
|
|
else
|
|
# It is a virtual machine
|
|
if qm status $instance | grep -q "status: running"; then
|
|
echo "$(date): VM $instance is not responding, resetting..."
|
|
qm reset $instance >/dev/null 2>&1
|
|
else
|
|
qm start $instance >/dev/null 2>&1
|
|
echo "$(date): VM $instance is not running, starting..."
|
|
fi
|
|
fi
|
|
fi
|
|
done
|
|
|
|
# Wait for 5 minutes. (Edit to your needs)
|
|
echo "$(date): Pausing for 5 minutes..."
|
|
sleep 300
|
|
done >> /var/log/ping-instances.log 2>&1' >/usr/local/bin/ping-instances.sh
|
|
|
|
# Change file permissions to executable
|
|
chmod +x /usr/local/bin/ping-instances.sh
|
|
|
|
# Create ping-instances.service
|
|
echo '[Unit]
|
|
Description=Ping instances every 5 minutes and restarts if necessary
|
|
|
|
[Service]
|
|
Type=simple
|
|
# Include the CT/VM ID at the end of the line where ExecStart=/usr/local/bin/ping-instances.sh is specified,
|
|
# to indicate which CT/VN should be excluded. Example: ExecStart=/usr/local/bin/ping-instances.sh 100 102
|
|
ExecStart=/usr/local/bin/ping-instances.sh
|
|
Restart=always
|
|
StandardOutput=file:/var/log/ping-instances.log
|
|
StandardError=file:/var/log/ping-instances.log
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target' >/etc/systemd/system/ping-instances.service
|
|
|
|
# Reload daemon, enable and start ping-instances.service
|
|
systemctl daemon-reload
|
|
systemctl enable -q --now ping-instances.service
|
|
clear
|
|
echo -e "\n To view Monitor All logs: cat /var/log/ping-instances.log"
|
|
}
|
|
|
|
remove() {
|
|
systemctl stop ping-instances.service
|
|
systemctl disable ping-instances.service &>/dev/null
|
|
rm /etc/systemd/system/ping-instances.service
|
|
rm /usr/local/bin/ping-instances.sh
|
|
rm /var/log/ping-instances.log
|
|
echo "Removed Monitor All from Proxmox VE"
|
|
}
|
|
|
|
if [ "$1" == "add" ]; then
|
|
add
|
|
elif [ "$1" == "remove" ]; then
|
|
remove
|
|
else
|
|
echo "Usage: $0 [ -s add | -s remove ]"
|
|
exit 1
|
|
fi
|