From 6afc8da6765a8742b8db45b772cd5ed2f547e482 Mon Sep 17 00:00:00 2001 From: AuxXxilium Date: Sat, 6 Jul 2024 14:07:42 +0200 Subject: [PATCH] ledcontrol: rework Signed-off-by: AuxXxilium --- disks/install.sh | 3 + ledcontrol/all/addons/led.conf | 5 + ledcontrol/all/usr/bin/ledcontrol.sh | 137 ++++++++++++++++++++++++++- ledcontrol/install.sh | 101 ++++++++++++++++++-- 4 files changed, 233 insertions(+), 13 deletions(-) create mode 100644 ledcontrol/all/addons/led.conf diff --git a/disks/install.sh b/disks/install.sh index 26fd00c..3b396e8 100755 --- a/disks/install.sh +++ b/disks/install.sh @@ -424,6 +424,7 @@ if [ "${1}" = "patches" ]; then echo "BOOTDISK=${BOOTDISK}" echo "BOOTDISK_PHYSDEVPATH=${BOOTDISK_PHYSDEVPATH}" + echo "${BOOTDISK}" >/usr/addons/bootdisk checkSynoboot @@ -464,4 +465,6 @@ elif [ "${1}" = "late" ]; then SUPPORT_M2_POOL=$(_get_conf_kv support_m2_pool) _set_conf_kv hd "supportnvme" "${SUPPORTNVME}" _set_conf_kv hd "support_m2_pool" "${SUPPORT_M2_POOL}" + # export Bootdisk for Ledcontrol + cp -vf /usr/addons/bootdisk /tmpRoot/usr/addons/bootdisk fi \ No newline at end of file diff --git a/ledcontrol/all/addons/led.conf b/ledcontrol/all/addons/led.conf new file mode 100644 index 0000000..b93cc13 --- /dev/null +++ b/ledcontrol/all/addons/led.conf @@ -0,0 +1,5 @@ +[brightness] +night_start=22:00 +night_end=07:00 +day_brightness=255 +night_brightness=64 diff --git a/ledcontrol/all/usr/bin/ledcontrol.sh b/ledcontrol/all/usr/bin/ledcontrol.sh index fc6b1ba..a5c6ffd 100755 --- a/ledcontrol/all/usr/bin/ledcontrol.sh +++ b/ledcontrol/all/usr/bin/ledcontrol.sh @@ -6,14 +6,141 @@ # See /LICENSE for more information. # +UGREEN_LEDS_CLI="/usr/bin/ugreen_leds_cli" +SMARTCTL="/usr/bin/smartctl" +HDPARM="/usr/bin/hdparm" +CONFIG_FILE="/usr/arc/addons/led.conf" + + +# Funktion, um den SMART-Status einer Festplatte zu überprüfen +function check_smart_status() { + local device=$1 + smartctl -H -n standby $device | grep -q "PASSED" + return $? +} + +# Funktion, um den Power-Status einer Festplatte zu überprüfen, ohne sie aufzuwecken +function check_power_status() { + local device=$1 + local status=$(hdparm -C $device | grep -oP '(?<=drive state is: ).*') + echo $status +} + +# Funktion, um den Load Average-Wert zu ermitteln +function get_load_average() { + load_average=$(uptime | awk -F'[a-z]:' '{print $2}' | awk -F',' '{print $1}' | tr -d '[:space:]') + echo $load_average +} + +# Funktion, um zu überprüfen, ob ein Netzwerkinterface verbunden ist +function check_network_interface() { + local interface=$1 + ip link show dev $interface | grep -q "state UP" + return $? +} + +# Funktion, um die aktuelle Helligkeit basierend auf der Konfigurationsdatei zu ermitteln +function get_brightness() { + local current_time=$(date +%H:%M) + local night_start=$(grep -Po '(?<=night_start=).*' $CONFIG_FILE) + local night_end=$(grep -Po '(?<=night_end=).*' $CONFIG_FILE) + local day_brightness=$(grep -Po '(?<=day_brightness=).*' $CONFIG_FILE) + local night_brightness=$(grep -Po '(?<=night_brightness=).*' $CONFIG_FILE) + + if [[ "$current_time" > "$night_start" || "$current_time" < "$night_end" ]]; then + echo $night_brightness + else + echo $day_brightness + fi +} + if [ "${1}" = "on" ]; then echo "Enable Ugreen LED" - ugreen_leds_cli all -on -color 255 255 255 -brightness 65 + $UGREEN_LEDS_CLI all -on -color 255 255 255 -brightness 65 elif [ "${1}" = "off" ]; then echo "Disable Ugreen LED" - ugreen_leds_cli all -off -elif [ "${1}" = "blink" ]; then - echo "Blink Ugreen LED" - ugreen_leds_cli all -blink -color 255 255 255 -brightness 65 + $UGREEN_LEDS_CLI all -off +elif [ "${1}" = "nic" ]; then + echo "NIC Ugreen LED" + # Get Brightness + brightness=$(get_brightness) + # NIC Status + interface_up=0 + interfaces=$(ls /sys/class/net/ 2>/dev/null | grep eth) + + for interface in "${interfaces[@]}"; do + if check_network_interface $interface; then + interface_up=1 + break + fi + done + + if [[ $interface_up -eq 1 ]]; then + $UGREEN_LEDS_CLI netdev -on -color 0 255 0 -brightness $brightness > /dev/null 2>&1 + else + $UGREEN_LEDS_CLI netdev -blink 400 400 -color 255 0 255 -brightness $brightness > /dev/null 2>&1 + fi +elif [ "${1}" = "disk" ]; then + # Get Disks + devices=$(ls -d /dev/sd[a-z] 2>/dev/null) + bootdisk=$(cat /usr/addons/bootdisk) + # Disks Smart Check + for i in "${!devices[@]}"; do + [ "${devices[$i]}" = "/dev/${bootdisk}" ] && continue + device=${devices[$i]} + disk_number=$((i + 1)) # Disknummer für die LED-Steuerung (disk1, disk2, etc.) + color="0 255 0" # Standardfarbe grün + + if check_smart_status $device; then + color="0 255 0" # Grün + else + color="255 0 0" # Rot + fi + + power_status=$(check_power_status $device) + + case $power_status in + "unknown") + $UGREEN_LEDS_CLI disk$disk_number -on -color $color -brightness $brightness > /dev/null 2>&1 + ;; + "active/idle") + $UGREEN_LEDS_CLI disk$disk_number -blink 500 500 -color $color -brightness $brightness > /dev/null 2>&1 + ;; + "standby") + $UGREEN_LEDS_CLI disk$disk_number -breath 1000 1000 -color $color -brightness $brightness > /dev/null 2>&1 + ;; + *) + $UGREEN_LEDS_CLI disk$disk_number -on -color $color -brightness $brightness > /dev/null 2>&1 + ;; + esac + done +elif [ "${1}" = "cpu" ]; then + # CPU Load Average + if awk "BEGIN {exit !($load_average < 1.0)}"; then + blink_on=2000 + blink_off=2000 + color="0 255 0" # Grün + elif awk "BEGIN {exit !($load_average < 2.0)}"; then + blink_on=1500 + blink_off=1500 + color="127 255 0" # Gelbgrün + elif awk "BEGIN {exit !($load_average < 3.0)}"; then + blink_on=1000 + blink_off=1000 + color="255 255 0" # Gelb + elif awk "BEGIN {exit !($load_average < 4.0)}"; then + blink_on=750 + blink_off=750 + color="255 127 0" # Orange + elif awk "BEGIN {exit !($load_average < 5.0)}"; then + blink_on=500 + blink_off=500 + color="255 0 0" # Rot + else + blink_on=250 + blink_off=250 + color="255 0 0" # Rot (schnelleres Blinken) + fi + $UGREEN_LEDS_CLI power -blink $blink_on $blink_off -color $color -brightness $brightness > /dev/null 2>&1 fi exit 0 \ No newline at end of file diff --git a/ledcontrol/install.sh b/ledcontrol/install.sh index b23d50c..cbab191 100755 --- a/ledcontrol/install.sh +++ b/ledcontrol/install.sh @@ -11,40 +11,125 @@ if [ "${1}" = "late" ]; then mkdir -p "/tmpRoot/usr/arc/addons/" cp -vf "${0}" "/tmpRoot/usr/arc/addons/" - cp -vf /usr/lib/modules/i2c* /tmpRoot/usr/lib/modules/ cp -vf /usr/bin/ledcontrol.sh /tmpRoot/usr/bin/ledcontrol.sh cp -vf /usr/bin/ugreen_leds_cli /tmpRoot/usr/bin/ugreen_leds_cli - modprobe i2c-algo-bit + cp -vf /usr/arc/addons/led.conf /tmpRoot/usr/arc/addons/led.conf modprobe i2c-smbus modprobe i2c-i801 mkdir -p "/tmpRoot/usr/lib/systemd/system" +# NIC DEST="/tmpRoot/usr/lib/systemd/system/ledcontrol.service" cat > ${DEST} < ${DEST} < ${DEST} < ${DEST} < ${DEST} </tmpRoot/usr/arc/revert.sh && chmod +x /tmpRoot/usr/arc/revert.sh echo "/usr/bin/ledcontrol.sh" >>/tmpRoot/usr/arc/revert.sh