„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
# ./vm_control.sh [start|stop]
# 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"
vm_control_php_path="/volume1/web/vm_control.php" # der Pfad im Web-Verzeichnis von 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!"
echo "ERROR: du bist nicht root!"
exit 1
fi
@ -27,7 +26,7 @@ inotify_start() {
echo "Überwachung von ${vm_state_path} wird gestartet"
# 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
while read line ; do
vm=$(awk -F\' '{print $2}' <<<"$line")
@ -35,7 +34,7 @@ inotify_start() {
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 |
inotifywait "${vm_state_path}" --monitor --event close_write --timeout -1 --format %e |
while read line ; do
unset vm_states_saved
@ -52,27 +51,41 @@ inotify_start() {
unset vms
# 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
while read line ; do
vm=$(awk -F\' '{print $2}' <<<"$line")
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}')"
printf "%-25s %s\n" "Name: $vm" "Status: $(echo "$line" | grep "$vm" | awk -F\' '{print $4}')"
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
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
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
else
echo "Änderung für $vm (web_state online: ${vm_states[$vm]} / web_state saved: ${vm_states_saved[$vm]})"
fi
# 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)
if [ "$vm_host_status" = "shutdown" ]; then
echo "starte $vm ..."
@ -91,7 +104,6 @@ inotify_start() {
;;
esac
done
# done >/dev/null 2>&1 &
done &
else
echo "läuft bereits …"