~kris/dots

srice

ref: e9b48d06a8541f3eda5c4db90382ab3c77183afb srice/.config/eww/scripts/sys_info.sh -rwxr-xr-x 2.0 KiB
e9b48d06 — Kris Yotam xprofile: systemd-aware pipewire start + blueman-applet; sb-internet: tolerate missing /proc/net/wireless 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash

# --- FUNGSI PINTAR UNTUK SUHU ---
get_cpu_temp() {
    if [ -f /sys/class/thermal/thermal_zone0/temp ]; then
        val=$(cat /sys/class/thermal/thermal_zone0/temp)
        if [ "$val" -gt 1000 ]; then
            echo $((val / 1000))
            return
        fi
    fi

    for file in /sys/class/hwmon/hwmon*/temp*_input; do
        if [ -f "$file" ]; then
            label_file="${file%_input}_label"
            if [ -f "$label_file" ]; then
                label=$(cat "$label_file")
                if [[ "$label" =~ "Core" || "$label" =~ "Package" || "$label" =~ "Tctl" ]]; then
                    val=$(cat "$file")
                    echo $((val / 1000))
                    return
                fi
            fi
        fi
    done

    # Metode 3: Fallback ke command 'sensors' 
    temp=$(sensors 2>/dev/null | grep -E "Package id 0|Core 0|Tctl" | head -1 | awk '{print $2}' | tr -d '+°C' | cut -d. -f1)
    
    if [ -n "$temp" ]; then
        echo "$temp"
    else
        echo "0"
    fi
}

# --- EXECUTION ---

# 1. Ambil Suhu pakai fungsi di atas
cpu_temp=$(get_cpu_temp)

# 2. CPU Usage (Top method)
cpu_usage=$(top -bn1 | grep 'Cpu(s)' | awk '{print 100 - $8}' | cut -d. -f1)

# 3. RAM (Read once)
# Output free: total used free ...
read -r mem_total_str mem_used_str <<< $(free -h | awk '/^Mem/ {print $2, $3}' | sed 's/i//g')
# Hitung persen pakai integer murni
read -r mem_total_int mem_used_int <<< $(free | awk '/^Mem/ {print $2, $3}')
if [ "$mem_total_int" -gt 0 ]; then
    mem_perc=$(( 100 * mem_used_int / mem_total_int ))
else
    mem_perc=0
fi

# 4. DISK (Read once)
read -r disk_total disk_used disk_perc <<< $(df -h / | awk 'NR==2 {print $2, $3, $5}' | tr -d '%')

# --- OUTPUT JSON FINAL ---
echo "{
    \"cpu_usage\": \"$cpu_usage\",
    \"cpu_temp\": \"$cpu_temp\",
    \"mem_used\": \"$mem_used_str\",
    \"mem_total\": \"$mem_total_str\",
    \"mem_perc\": \"$mem_perc\",
    \"disk_used\": \"$disk_used\",
    \"disk_total\": \"$disk_total\",
    \"disk_perc\": \"$disk_perc\"
}"