„vm_control.sh“ ändern
This commit is contained in:
parent
d066a56152
commit
ac853942ad
1 changed files with 44 additions and 48 deletions
|
@ -19,67 +19,71 @@ elif [ $(whoami) != "root" ]; then
|
|||
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}')
|
||||
|
||||
echo "..."
|
||||
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 |
|
||||
# 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_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"))"
|
||||
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 |
|
||||
while read line ; do
|
||||
|
||||
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
|
||||
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}')"
|
||||
done <<<"$web_state"
|
||||
|
||||
for vm in "${vms[@]}"; do
|
||||
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]})"
|
||||
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 "$web_state" in
|
||||
case "${vm_states[$vm]}" in
|
||||
on)
|
||||
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
|
||||
continue
|
||||
;;
|
||||
off)
|
||||
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
|
||||
continue
|
||||
;;
|
||||
esac
|
||||
synosetkeyvalue "$0" "web_$vm" "$web_state"
|
||||
done
|
||||
save_host_state
|
||||
done >/dev/null 2>&1 &
|
||||
# done >/dev/null 2>&1 &
|
||||
done &
|
||||
else
|
||||
echo "läuft bereits …"
|
||||
fi
|
||||
|
@ -111,7 +115,6 @@ inotify_stop()
|
|||
case "$1" in
|
||||
start)
|
||||
inotify_start
|
||||
# inotify_start &
|
||||
echo "läuft … "
|
||||
exit 0
|
||||
;;
|
||||
|
@ -122,10 +125,3 @@ inotify_stop()
|
|||
esac
|
||||
|
||||
exit 0
|
||||
|
||||
################################################################################
|
||||
# Nicht-Exekutiv-Bereich
|
||||
# ------------------------------------------------------------------------------
|
||||
#
|
||||
# dynamisch angepassten Werte:
|
||||
# ------------------------------------------------
|
||||
|
|
Loading…
Reference in a new issue