„vm_control.sh“ ändern

This commit is contained in:
Stephan 2022-07-31 12:20:58 +00:00
parent 52bee6f494
commit ec6907e7ed

View file

@ -1,22 +1,21 @@
#!/bin/bash #!/bin/bash
# ./vm_control.sh [start|stop] # ./vm_control.sh [start|stop]
# DIESES SKRIPT KOMMT IN EIN PRIVATES VERZEICHNIS # DIESES SKRIPT KOMMT IN EIN PRIVATES VERZEICHNIS
# VMM verlangt root-Rechte, weshalb dieses Skript von root aufgerufen werden muss.
# der Pfad im Web-Verzeichnis von vm_control.php ist anzupassen: vm_control_php_path="/volume1/web/vm_control.php" # der Pfad im Web-Verzeichnis von vm_control.php
vm_control_php_path="/volume1/web/vm_control.php"
################################################################################ ################################################################################
# ab hier nichts mehr ändern # ab hier nichts mehr ändern
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
saved_IFS=$IFS
vm_state_path="$(dirname "$vm_control_php_path")/vm_state.php" vm_state_path="$(dirname "$vm_control_php_path")/vm_state.php"
if [ ! $(which inotifywait) ]; then if [ ! $(which inotifywait) ]; then
echo "inotify-tools nicht installiert" echo "inotify-tools nicht installiert"
exit 1 exit 1
elif [ $(whoami) != "root" ]; then elif [ $(whoami) != "root" ]; then
echo "ERROR: you are not root!" echo "ERROR: du bist nicht root!"
exit 1 exit 1
fi fi
@ -27,7 +26,7 @@ inotify_start() {
echo "Überwachung von ${vm_state_path} wird gestartet" echo "Überwachung von ${vm_state_path} wird gestartet"
# initial Web-Status auslesen: # initial Web-Status auslesen:
web_state=$(cat "$vm_state_path" | grep "=>" "$vm_state_path" | grep -v "letzter Zugriff") web_state=$(cat "$vm_state_path" | grep "=>" "$vm_state_path" | grep -v "LOG:")
declare -A vm_states declare -A vm_states
while read line ; do while read line ; do
vm=$(awk -F\' '{print $2}' <<<"$line") vm=$(awk -F\' '{print $2}' <<<"$line")
@ -35,7 +34,7 @@ inotify_start() {
vm_states[$vm]=$(echo "$line" | grep "$vm" | awk -F\' '{print $4}') vm_states[$vm]=$(echo "$line" | grep "$vm" | awk -F\' '{print $4}')
done <<<"$web_state" done <<<"$web_state"
inotifywait "${vm_state_path}" --monitor --event modify --timeout -1 --format %e | inotifywait "${vm_state_path}" --monitor --event close_write --timeout -1 --format %e |
while read line ; do while read line ; do
unset vm_states_saved unset vm_states_saved
@ -52,31 +51,45 @@ inotify_start() {
unset vms unset vms
# neu einlesen: # neu einlesen:
web_state=$(cat "$vm_state_path" | grep "=>" "$vm_state_path" | grep -v "letzter Zugriff") web_state=$(cat "$vm_state_path" | grep "=>" "$vm_state_path" | grep -v "LOG:")
declare -A vm_states declare -A vm_states
while read line ; do while read line ; do
vm=$(awk -F\' '{print $2}' <<<"$line") vm=$(awk -F\' '{print $2}' <<<"$line")
vms+=( "$vm" ) vms+=( "$vm" )
vm_states[$vm]=$(echo "$line" | grep "$vm" | awk -F\' '{print $4}') vm_states[$vm]=$(echo "$line" | grep "$vm" | awk -F\' '{print $4}')
echo "Name: $vm Status: $(echo "$line" | grep "$vm" | awk -F\' '{print $4}')" printf "%-25s %s\n" "Name: $vm" "Status: $(echo "$line" | grep "$vm" | awk -F\' '{print $4}')"
done <<<"$web_state" done <<<"$web_state"
echo "-----------------------------------------"
echo "MODIFIKATION ERKANNT ..."
echo "online Status:"
for i in "${!vm_states[@]}" ; do
printf "%-25s ➜ %s\n" "$i" "${vm_states[$i]}"
done
echo -e
echo "gespeicherte Status:"
for i in "${!vm_states_saved[@]}" ; do
printf "%-25s ➜ %s\n" "$i" "${vm_states_saved[$i]}"
done
echo "-----------------------------------------"
# Änderungen filtern / Aktion ausführen:
for vm in "${vms[@]}"; do for vm in "${vms[@]}"; do
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 ) 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 )
if [ "${vm_states[$vm]}" = "${vm_states_saved[$vm]}" ]; then 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]})" echo "keine Änderung für $vm (web_state online: $(echo "${vm_states[$vm]}" | awk -F_ '{print $1}') / web_state saved: $(echo "${vm_states_saved[$vm]}" | awk -F_ '{print $1}'))"
continue continue
else
echo "Änderung für $vm (web_state online: ${vm_states[$vm]} / web_state saved: ${vm_states_saved[$vm]})"
fi fi
# wenn Änderung gefunden wurde: # wenn Änderung gefunden wurde:
case "${vm_states[$vm]}" in echo "Änderung für $vm (web_state online: $(echo "${vm_states[$vm]}" | awk -F_ '{print $1}') / web_state saved: $(echo "${vm_states_saved[$vm]}" | awk -F_ '{print $1}'))"
case $(echo "${vm_states[$vm]}" | awk -F_ '{print $1}') in
on) on)
if [ "$vm_host_status" = "shutdown" ]; then if [ "$vm_host_status" = "shutdown" ]; then
echo "starte $vm ..." echo "starte $vm ..."
synowebapi --exec api=SYNO.Virtualization.API.Guest.Action version=1 method=poweron runner=admin guest_name="$vm" > /dev/null 2>&1 synowebapi --exec api=SYNO.Virtualization.API.Guest.Action version=1 method=poweron runner=admin guest_name="$vm" > /dev/null 2>&1
else else
echo "VM $vm hat folgenden Status und kann nicht gestartet werden: $vm_host_status" echo "VM $vm hat folgenden Status und kann nicht gestartet werden: $vm_host_status"
fi fi
@ -84,14 +97,13 @@ inotify_start() {
off) off)
if [ "$vm_host_status" = "running" ]; then if [ "$vm_host_status" = "running" ]; then
echo "beende $vm ..." echo "beende $vm ..."
synowebapi --exec api=SYNO.Virtualization.API.Guest.Action version=1 method=shutdown runner=admin guest_name="$vm" > /dev/null 2>&1 synowebapi --exec api=SYNO.Virtualization.API.Guest.Action version=1 method=shutdown runner=admin guest_name="$vm" > /dev/null 2>&1
else else
echo "VM $vm hat folgenden Status und kann nicht beendet werden: $vm_host_status" echo "VM $vm hat folgenden Status und kann nicht beendet werden: $vm_host_status"
fi fi
;; ;;
esac esac
done done
# done >/dev/null 2>&1 &
done & done &
else else
echo "läuft bereits …" echo "läuft bereits …"