#!/usr/bin/env bash

MON=$(cat "$HOME/bench/ornith/LAST_MONITOR")

while true; do
    clear

    LINE=$(tail -n 1 "$MON/system.csv" 2>/dev/null)

    if [[ -z "$LINE" || "$LINE" == timestamp* ]]; then
        echo "Esperando datos..."
        sleep 1
        continue
    fi

    echo "$LINE" | awk -F',' '
    {
        printf "SYSTEM / PSI\n"
        printf "====================================\n"
        printf "CPU busy      %8s %%\n", $2
        printf "CPU iowait    %8s %%\n", $3
        printf "Load          %8s / %s / %s\n", $4,$5,$6
        printf "Running       %8s\n", $7
        printf "Blocked       %8s\n", $8
        printf "\n"
        printf "RAM used      %8s GiB\n", $9
        printf "RAM available %8s GiB\n", $10
        printf "Swap used     %8s GiB\n", $11
        printf "\n"
        printf "PSI CPU       %8s\n", $14
        printf "PSI MEM       %8s\n", $15
        printf "PSI IO        %8s\n", $16
        printf "\n"
        printf "Disk read     %8s MiB/s\n", $17
        printf "Disk write    %8s MiB/s\n", $18
        printf "Dirty         %8s MiB\n", $12
        printf "Writeback     %8s MiB\n", $13
        printf "\n"
        printf "Network RX    %8s MiB/s\n", $19
        printf "Network TX    %8s MiB/s\n", $20
        printf "\n%s\n", $1
    }'

    sleep 1
done
