132 lines
4.5 KiB
Bash
132 lines
4.5 KiB
Bash
#!/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
|
|
|
|
save_web_state() {
|
|
# aktuelle Einstellungen aus Statusfile auslesen und für den späteren Vergleich in diesem Skript speichern:
|
|
vm_web_list=$(grep "=>" "$vm_state_path" | grep -v "letzter Zugriff")
|
|
IFS=$'\012'
|
|
for line in $vm_web_list; do
|
|
IFS=$saved_IFS
|
|
synosetkeyvalue "$0" "web_$(awk -F\' '{print $2}' <<<"$line")" "$(awk -F\' '{print $4}' <<<"$line")"
|
|
done
|
|
}
|
|
|
|
save_host_state() {
|
|
# VMs initialisieren - aktuelle Status der VMs auslesen und in diesem Skript speichern:
|
|
vm_list=$(synowebapi --exec api=SYNO.Virtualization.API.Guest method=list version=1 | jq -r .data.guests[].guest_name 2>/dev/null)
|
|
echo "vm_list: $vm_list"
|
|
IFS=$'\012'
|
|
for line in $vm_list; do
|
|
IFS=$saved_IFS
|
|
vm_status=$(synowebapi --exec api=SYNO.Virtualization.API.Guest version=1 method=get runner=admin guest_name="$line" | jq -r .data.status 2>/dev/null)
|
|
echo "status: $vm_status"
|
|
synosetkeyvalue "$0" "host_${line}" "${vm_status}"
|
|
done
|
|
}
|
|
|
|
inotify_start() {
|
|
PROZESSID=$(ps aux | grep -v "grep" | grep -E "inotifywait.*vm_state.php" | awk -F' ' '{print $2}')
|
|
|
|
if [[ -z $PROZESSID ]];then
|
|
echo "Überwachung von ${vm_state_path} wird gestartet"
|
|
save_host_state
|
|
save_web_state
|
|
|
|
inotifywait "${vm_state_path}" --event create,modify --monitor --timeout -1 |
|
|
while read line ; do
|
|
vm_web_list=$(grep "=>" "$vm_state_path" | grep -v "letzter Zugriff")
|
|
IFS=$'\012'
|
|
for line in $vm_web_list; do
|
|
IFS=$saved_IFS
|
|
vm=$(awk -F\' '{print $2}' <<<"$line")
|
|
web_state=$(awk -F\' '{print $4}' <<<"$line")
|
|
if [ "$(get_key_value "$0" "web_$vm")" = "$web_state" ]; then
|
|
echo "keine Änderung für $vm (web_state online $web_state / web_state saved $(get_key_value "$0" "web_$vm"))"
|
|
continue
|
|
fi
|
|
|
|
# wenn Änderung gefunden wurde:
|
|
case "$web_state" in
|
|
on)
|
|
echo "starte $vm"
|
|
synowebapi --exec api=SYNO.Virtualization.API.Guest.Action version=1 method=poweron runner=admin guest_name="$vm" >/dev/null 2>&1
|
|
continue
|
|
;;
|
|
off)
|
|
echo "beende $vm"
|
|
synowebapi --exec api=SYNO.Virtualization.API.Guest.Action version=1 method=shutdown runner=admin guest_name="$vm" >/dev/null 2>&1
|
|
continue
|
|
;;
|
|
esac
|
|
synosetkeyvalue "$0" "web_$vm" "$web_state"
|
|
done
|
|
save_host_state
|
|
done >/dev/null 2>&1 &
|
|
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
|
|
# inotify_start &
|
|
echo "läuft … "
|
|
exit 0
|
|
;;
|
|
stop)
|
|
inotify_stop
|
|
exit 0
|
|
;;
|
|
esac
|
|
|
|
exit 0
|
|
|
|
################################################################################
|
|
# Nicht-Exekutiv-Bereich
|
|
# ------------------------------------------------------------------------------
|
|
#
|
|
# dynamisch angepassten Werte:
|
|
# ------------------------------------------------
|