Skip to content

Example wesplot pipelines

Shuhao Wu edited this page Oct 15, 2023 · 15 revisions

All-core CPU usage via sar

You need to first install sar, which is available via apt install sysstat on Debian/Ubuntu. You also need GNU awk (with the package gawk) Then:

S_TIME_FORMAT=ISO sar 1 | \
  gawk '{ if ($NF ~ /^[0-9]+[.]?[0-9]*$/) print 100-$NF; fflush(); }' | \
  wesplot -t "CPU Utilization" -c "CPU%" -m 0 -M 100

Memory and swap plots via free

Confirm that the output of free -m is the following where the memory usage in MB is on the 3rd column of the 2nd line and swap usage in MB is the 3rd column of the 3rd line.

               total        used        free      shared  buff/cache   available
Mem:           31815       13002         972        3023       17840       15334
Swap:          10239         610        9629

Then:

max_memory=32000
{
  while true; do
    free -m \
      | gawk 'NR==2{ line=line " " $3 } NR==3{ line=line " " $3; print(line); fflush(); }'
    sleep 2
  done;
} | wesplot -t "Memory usage" -u "MB" -c "Memory" -c "Swap" -m 0 -M $max_memory

Disk read/write usage

device=nvme0n1
iostat -x 1 | \
  grep --line-buffered $device | \
  gawk '{ print $3, $9, $15; fflush(); }' | \
  wesplot -t "${device} usage" -c "Read KB/s" -c "Write KB/s" -c "Discard KB/s" -u "KB/s"

Network throughput

S_TIME_FORMAT=ISO sar -n DEV 1 | \
  gawk '$2 == "eth0" { print $5/125, $6/125; fflush(); }' | \
  wesplot -t "Network throughput" -u "Mbit/s" -c "Download" -c "Upload"

Current laptop battery capacity

{
  while true; do
    cat /sys/class/power_supply/BAT0/capacity
    sleep 30
  done
} | wesplot -t "Battery" -u "%" -m 0 -M 100

Current estimated laptop power consumption

{
  while true; do
    if [ -f /sys/class/power_supply/BAT0/power_now ]; then
      power_mw=$(cat /sys/class/power_supply/BAT0/power_now)
      power=$(bc <<< "scale=2; $power_mw / 1000000.0")
      echo $power
    else
      current=$(cat /sys/class/power_supply/BAT0/current_now)
      voltage=$(cat /sys/class/power_supply/BAT0/voltage_now)
      power=$(bc <<< "scale=2; $current * $voltage / 1000000000000.0")
      echo $power
    fi
    sleep 3
  done
} | wesplot -t "Power usage" -u "W" -m 0

Ping latency to 1.1.1.1

ip=1.1.1.1
ping $ip | sed -u 's/^.*time=//g; s/ ms//g' | wesplot -t "Ping to ${ip}" -u "ms"

CPU Package temperature with sensors

{ 
  while true; do 
    sensors | grep -oP 'Package.*?\+\K[0-9.]+'
    sleep 1
  done 
} | wesplot -t "CPU package temp" -u "°C" -m 20 -M 120

All core frequency plots

This also shows off gawk merging multiple lines into a single line. Need to modify this (the number 16 and the -c flags) for the number of CPUs you have.

{ 
  while true; do 
    cat /proc/cpuinfo \
      | grep "MHz" \
      | gawk -F: '{ print $2 }' \
      | gawk '{line=line " " $0} NR%16==0{print substr(line,2); line=""; fflush(); }'
    sleep 1
  done 
} | wesplot -t "CPU Freq" -c CPU0 -c CPU1 -c CPU2 -c CPU3 -c CPU4 -c CPU5 -c CPU6 -c CPU7 -c CPU8 -c CPU9 -c CPU10 -c CPU11 -c CPU12 -c CPU13 -c CPU14 -c CPU15 -u MHz