#!/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}') echo "..." if [[ -z $PROZESSID ]];then 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") 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 | 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 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]})" 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 on) if [ "$vm_host_status" = "shutdown" ]; then echo "starte $vm ..." synowebapi --exec api=SYNO.Virtualization.API.Guest.Action version=1 method=poweron runner=admin guest_name="$vm" > /dev/null 2>&1 else echo "VM $vm hat folgenden Status und kann nicht gestartet werden: $vm_host_status" fi ;; off) if [ "$vm_host_status" = "running" ]; then echo "beende $vm ..." synowebapi --exec api=SYNO.Virtualization.API.Guest.Action version=1 method=shutdown runner=admin guest_name="$vm" > /dev/null 2>&1 else echo "VM $vm hat folgenden Status und kann nicht beendet werden: $vm_host_status" fi ;; esac done # done >/dev/null 2>&1 & done & 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