2022-02-04 14:40:59 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# ./vm_control.sh [start|stop]
|
|
|
|
|
|
|
|
# der Pfad im Web-Verzeichnis von vm_control.php ist anzupassen:
|
|
|
|
vm_control_php_path="/volume1/web/vm_control.php"
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# ab hier nichts mehr ändern
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
saved_IFS=$IFS
|
|
|
|
vm_state_path="$(dirname "$vm_control_php_path")/vm_state.php"
|
|
|
|
|
|
|
|
if [ ! $(which inotifywait) ]; then
|
|
|
|
echo "inotify-tools nicht installiert"
|
|
|
|
exit 1
|
|
|
|
elif [ $(whoami) != "root" ]; then
|
|
|
|
echo "ERROR: you are not root!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
inotify_start() {
|
|
|
|
PROZESSID=$(ps aux | grep -v "grep" | grep -E "inotifywait.*vm_state.php" | awk -F' ' '{print $2}')
|
2022-02-06 18:10:52 +00:00
|
|
|
echo "..."
|
2022-02-04 14:40:59 +00:00
|
|
|
if [[ -z $PROZESSID ]];then
|
|
|
|
echo "Überwachung von ${vm_state_path} wird gestartet"
|
|
|
|
|
2022-02-06 18:10:52 +00:00
|
|
|
# initial Web-Status auslesen:
|
|
|
|
web_state=$(cat "$vm_state_path" | grep "=>" "$vm_state_path" | grep -v "letzter Zugriff")
|
|
|
|
declare -A vm_states
|
|
|
|
while read line ; do
|
|
|
|
vm=$(awk -F\' '{print $2}' <<<"$line")
|
|
|
|
vms+=( "$vm" )
|
|
|
|
vm_states[$vm]=$(echo "$line" | grep "$vm" | awk -F\' '{print $4}')
|
|
|
|
done <<<"$web_state"
|
|
|
|
|
|
|
|
inotifywait "${vm_state_path}" --monitor --event modify --timeout -1 --format %e |
|
2022-02-04 14:40:59 +00:00
|
|
|
while read line ; do
|
2022-02-06 18:10:52 +00:00
|
|
|
|
|
|
|
unset vm_states_saved
|
|
|
|
declare -A vm_states_saved
|
|
|
|
|
|
|
|
# associative array umschreiben / umbenennen:
|
|
|
|
for key in "${!vm_states[@]}"; do # make sure you include the quotes there
|
|
|
|
vm_states_saved+=( ["$key"]="${vm_states["$key"]}" )
|
|
|
|
# or:
|
|
|
|
# vm_states_saved["$key"]="${vm_states["$key"]}"
|
|
|
|
done
|
|
|
|
|
|
|
|
unset vm_states
|
|
|
|
unset vms
|
|
|
|
|
|
|
|
# neu einlesen:
|
|
|
|
web_state=$(cat "$vm_state_path" | grep "=>" "$vm_state_path" | grep -v "letzter Zugriff")
|
|
|
|
declare -A vm_states
|
|
|
|
while read line ; do
|
2022-02-04 14:40:59 +00:00
|
|
|
vm=$(awk -F\' '{print $2}' <<<"$line")
|
2022-02-06 18:10:52 +00:00
|
|
|
vms+=( "$vm" )
|
|
|
|
vm_states[$vm]=$(echo "$line" | grep "$vm" | awk -F\' '{print $4}')
|
|
|
|
echo "Name: $vm Status: $(echo "$line" | grep "$vm" | awk -F\' '{print $4}')"
|
|
|
|
done <<<"$web_state"
|
|
|
|
|
|
|
|
for vm in "${vms[@]}"; do
|
2022-02-06 20:31:47 +00:00
|
|
|
vm_host_status=$(synowebapi --exec api=SYNO.Virtualization.API.Guest version=1 method=get runner=admin guest_name="$vm" 2>/dev/null | jq -r .data.status )
|
|
|
|
|
2022-02-06 18:10:52 +00:00
|
|
|
if [ "${vm_states[$vm]}" = "${vm_states_saved[$vm]}" ]; then
|
|
|
|
echo "keine Änderung für $vm (web_state online: ${vm_states[$vm]} / web_state saved: ${vm_states_saved[$vm]})"
|
2022-02-04 14:40:59 +00:00
|
|
|
continue
|
2022-02-06 18:10:52 +00:00
|
|
|
else
|
|
|
|
echo "Änderung für $vm (web_state online: ${vm_states[$vm]} / web_state saved: ${vm_states_saved[$vm]})"
|
2022-02-04 14:40:59 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# wenn Änderung gefunden wurde:
|
2022-02-06 18:10:52 +00:00
|
|
|
case "${vm_states[$vm]}" in
|
2022-02-04 14:40:59 +00:00
|
|
|
on)
|
2022-02-06 20:31:47 +00:00
|
|
|
if [ "$vm_host_status" = "shutdown" ]; then
|
|
|
|
echo "starte $vm ..."
|
2022-02-06 20:46:52 +00:00
|
|
|
synowebapi --exec api=SYNO.Virtualization.API.Guest.Action version=1 method=poweron runner=admin guest_name="$vm" > /dev/null 2>&1
|
2022-02-06 20:31:47 +00:00
|
|
|
else
|
|
|
|
echo "VM $vm hat folgenden Status und kann nicht gestartet werden: $vm_host_status"
|
|
|
|
fi
|
2022-02-04 14:40:59 +00:00
|
|
|
;;
|
|
|
|
off)
|
2022-02-06 20:31:47 +00:00
|
|
|
if [ "$vm_host_status" = "running" ]; then
|
|
|
|
echo "beende $vm ..."
|
2022-02-06 20:46:52 +00:00
|
|
|
synowebapi --exec api=SYNO.Virtualization.API.Guest.Action version=1 method=shutdown runner=admin guest_name="$vm" > /dev/null 2>&1
|
2022-02-06 20:31:47 +00:00
|
|
|
else
|
|
|
|
echo "VM $vm hat folgenden Status und kann nicht beendet werden: $vm_host_status"
|
|
|
|
fi
|
2022-02-04 14:40:59 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2022-02-06 18:10:52 +00:00
|
|
|
# done >/dev/null 2>&1 &
|
|
|
|
done &
|
2022-02-04 14:40:59 +00:00
|
|
|
else
|
|
|
|
echo "läuft bereits …"
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
inotify_stop()
|
|
|
|
{
|
|
|
|
# Überwachung stoppem:
|
|
|
|
# --------------------------------------------------------------
|
|
|
|
PROZESSID=$(ps aux | grep -v "grep" | grep -E "inotifywait.*vm_state.php" | awk -F' ' '{print $2}')
|
|
|
|
|
|
|
|
if [[ ! -z $PROZESSID ]];then
|
|
|
|
kill $PROZESSID
|
|
|
|
if [ $? = 0 ]; then
|
|
|
|
echo "Überwachung beendet"
|
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
echo "FEHLER beim Beenden der Überwachung!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "läuft nicht … "
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# start-stop-Überwachung:
|
|
|
|
case "$1" in
|
|
|
|
start)
|
|
|
|
inotify_start
|
|
|
|
echo "läuft … "
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
stop)
|
|
|
|
inotify_stop
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
exit 0
|