NASutils Knowledge BaseUnofficialPolski

BASIC · projectA NAS summary on one screen in yabasic

This program shows the state of the NAS on one screen: the CPU with its cores and logical processors, the CPU and system temperatures, the load, the memory, each data volume with a bar and the bytes each network port has received and sent. It prints once and ends.

Programming on the NAS › BASIC in 30 lessons

 ╔═ NAS summary ═════════════════════════════════════════════════ TBS-h574TX ═╗
 ║ CPU             12th Gen Intel(R) Core(TM) i5-1235U                        ║
 ║                 10 cores, 12 logical processors                            ║
 ║ Temperature     CPU 53 °C   system 34 °C                                   ║
 ║ Load            1.17  0.90  0.88              ██░░░░░░░░░░░░░░░░░░  10 %   ║
 ║ Memory          5.6 GiB of 15.4 GiB used      ███████░░░░░░░░░░░░░  36 %   ║
 ╟─ Volumes ──────────────────────────────────────────────────────────────────╢
 ║ CACHEDEV1_DATA  281.8 GiB of 1.3 TiB used     ████░░░░░░░░░░░░░░░░  21 %   ║
 ║ CACHEDEV2_DATA  177.1 GiB of 1.2 TiB used     ███░░░░░░░░░░░░░░░░░  14 %   ║
 ╟─ Network ──────────────────────────────────────────────────────────────────╢
 ║ eth0            received 51.4 GiB     sent 10.2 GiB                        ║
 ║ eth1            received 567.3 KiB    sent 11.4 MiB                        ║
 ╚════════════════════════════════════════════════════════════════════════════╝
The summary of the sample NAS: the i5-1235U with 10 cores and 12 logical processors, CPU 53 °C in amber, load 1.17 with a bar at 10 %, 5.6 GiB of 15.4 GiB memory used, two data volumes, and the traffic of eth0 and eth1.

Download it on the NAS

curl -fsSLO https://kb.nasutils.net/projects/basic/stats.yab

stats.yab Download stats.yab

rem NAS summary: CPU, temperature, load, memory, volumes and network on one screen, with bars.
rem Run: yabasic stats.yab   (prints once and ends)
rem Facts come from $PROC (default /proc), $DF (a saved df -k, else df itself) and getsysinfo.

proc$ = peek$("env", "PROC") : if proc$ = "" proc$ = "/proc"
dim line$(1), w$(20)
model$ = system$("getsysinfo model 2>/dev/null") : if model$ = "" model$ = "NAS"
title$ = "NAS summary"
print e$("33") + " ╔═" + e$("1") + " " + title$ + " " + e$("0;33") + rep$("═", 70 - width(title$) - width(model$)) + e$("1") + " " + model$ + " " + e$("0;33") + "═╗"

rem The CPU: logical processors are "processor" lines, cores are distinct (physical id, core id) pairs
n = token(readall$(proc$ + "/cpuinfo"), line$(), chr$(10))
for i = 1 to n
  key$ = trim$(left$(line$(i), instr(line$(i) + ":", ":") - 1)) : value$ = trim$(mid$(line$(i), instr(line$(i), ":") + 1))
  if key$ = "processor" logical = logical + 1
  if key$ = "model name" cpu$ = value$
  if key$ = "physical id" chip$ = value$
  if key$ = "core id" and not instr(seen$, "|" + chip$ + "/" + value$ + "|") then
    seen$ = seen$ + "|" + chip$ + "/" + value$ + "|" : cores = cores + 1
  endif
next i
row("CPU", cpu$)
row("", str$(cores) + " cores, " + str$(logical) + " logical processors")
cputmp = val(system$("getsysinfo cputmp 2>/dev/null")) : systmp = val(system$("getsysinfo systmp 2>/dev/null"))
if cputmp > 0 row("Temperature", "CPU " + temp$(cputmp) + "   system " + temp$(systmp))

n = token(readall$(proc$ + "/loadavg"), w$(), " ")
row("Load", pad$(w$(1) + "  " + w$(2) + "  " + w$(3), 30) + bar$(val(w$(1)) / logical))
total = meminfo("MemTotal") : used = total - meminfo("MemAvailable")
row("Memory", pad$(size$(used) + " of " + size$(total) + " used", 30) + bar$(used / total))

section("Volumes")
a$ = peek$("env", "DF")
if a$ = "" then a$ = system$("df -kP 2>/dev/null") else a$ = readall$(a$) : endif
for i = 2 to token(a$, line$(), chr$(10))
  if token(line$(i), w$(), " ") = 6 and left$(w$(6), 15) = "/share/CACHEDEV" then
    row(mid$(w$(6), 8), pad$(size$(val(w$(3))) + " of " + size$(val(w$(2))) + " used", 30) + bar$(val(w$(3)) / val(w$(2))))
  endif
next i

section("Network")
n = token(readall$(proc$ + "/net/dev"), line$(), chr$(10))
for i = 3 to n
  if token(change$(line$(i), ":", " "), w$(), " ") > 9 and left$(w$(1), 3) = "eth" then
    row(w$(1), "received " + e$("32") + pad$(size$(val(w$(2)) / 1024), 13) + e$("0") + "sent " + e$("32") + size$(val(w$(10)) / 1024) + e$("0"))
  endif
next i
print e$("33") + " ╚" + rep$("═", 76) + "╝" + e$("0")

sub row(label$, text$)
  print e$("33") + " ║ " + e$("1") + pad$(label$, 16) + e$("0") + pad$(text$, 59) + e$("33") + "║" + e$("0")
end sub

sub section(title$)
  print e$("33") + " ╟─" + e$("1") + " " + title$ + " " + e$("0;33") + rep$("─", 73 - width(title$)) + "╢" + e$("0")
end sub

sub bar$(part)
  local n, c$
  if part > 1 part = 1
  n = int(part * 20 + 0.5)
  c$ = "32"
  if part > 0.7 c$ = "33"
  if part > 0.9 c$ = "31"
  return e$(c$) + rep$("█", n) + e$("0") + rep$("░", 20 - n) + " " + str$(int(part * 100 + 0.5), "%3.0f") + " %"
end sub

sub temp$(c)
  local c$
  c$ = "32"
  if c >= 45 c$ = "33"
  if c >= 55 c$ = "31"
  return e$(c$) + str$(c) + " °C" + e$("0")
end sub

sub size$(kb)
  local u$, i
  u$ = "KiBMiBGiBTiB" : i = 1
  while kb >= 1024 and i < 4 : kb = kb / 1024 : i = i + 1 : wend
  return str$(kb, "%.1f") + " " + mid$(u$, i * 3 - 2, 3)
end sub

sub meminfo(key$)
  local i
  for i = 1 to token(readall$(proc$ + "/meminfo"), line$(), chr$(10))
    if left$(line$(i), len(key$) + 1) = key$ + ":" return val(mid$(line$(i), len(key$) + 2))
  next i
end sub

sub readall$(file$)
  local a$, s$
  if open(#1, file$, "r") then
    while not eof(#1) : line input #1 a$ : s$ = s$ + a$ + chr$(10) : wend
    close #1
  endif
  return s$
end sub

sub change$(s$, a$, b$)
  local i
  i = instr(s$, a$)
  while i : s$ = left$(s$, i - 1) + b$ + mid$(s$, i + len(a$)) : i = instr(s$, a$) : wend
  return s$
end sub

rem Width on the screen: UTF-8 letters count once, colour codes (ESC [ ... m) not at all
sub width(s$)
  local i, n, c, skip
  for i = 1 to len(s$)
    c = asc(mid$(s$, i, 1))
    if c = 27 skip = 1
    if not skip and (c < 128 or c >= 192) n = n + 1
    if skip and c = 109 skip = 0
  next i
  return n
end sub

sub pad$(s$, n) : return s$ + rep$(" ", n - width(s$)) : end sub
sub rep$(s$, n) : local r$, i : for i = 1 to n : r$ = r$ + s$ : next i : return r$ : end sub
sub e$(code$) : return chr$(27) + "[" + code$ + "m" : end sub

What you learn

  • readall$ reads a whole file into one string, and token(..., chr$(10)) cuts it into lines.
  • A string such as |0/4|0/8| remembers the pairs already seen, and instr() checks for a pair, so the program counts distinct cores.
  • width skips colour codes (ESC [ ... m), so pad$ can line up text that is already coloured.
  • getsysinfo cputmp answers "53 C/127 F", and val() takes the 53 from the front.

How it works

The top edge carries the title on the left and the model from getsysinfo model on the right. Each following line comes from row, which prints a bold label in 16 columns, the text in 59 and the right edge. section draws a divider with a title.

The CPU part reads /proc/cpuinfo line by line. Each processor line is one logical processor. Each core id is paired with the physical id above it, and a pair the program has not seen yet adds one core. On the sample NAS that gives 10 cores and 12 logical processors, because only some of the cores of the i5-1235U run two threads.

The load bar shows the 1-minute load divided by the logical processors. The memory bar shows MemTotal minus MemAvailable. The volumes are the mounts /share/CACHEDEV... of df -kP. The network part reads /proc/net/dev, turns the colon after the port name into a space and keeps the ports whose name starts with eth. Field 2 is the bytes received, field 10 the bytes sent.

Notes to the lines

  • 7On a PC without getsysinfo the model is "NAS".
  • 9The title and the model are measured with width, so the row of ═ between them fills the line to column 79.
  • 14The name of a field ends at the colon; trim$ takes away the tabs and spaces around both parts.
  • 18–20A new pair of physical id and core id goes into seen$ and counts as a core.
  • 24–25The temperature line appears only when getsysinfo answered.
  • 28bar$ gets the 1-minute load per logical processor: 1.17 on 12 makes about 10 %.
  • 36–37One row per data volume: used space, size and the bar of the used part.
  • 44After the colon turns into a space, a port line has 17 fields; eth keeps the real ports and skips lo and the virtual switch.
  • 45/proc/net/dev counts bytes, and size$ takes KiB, so the count is divided by 1024 first.
  • 58–66bar$ draws 20 cells and the percentage; it stops at 100 % when the load is higher than the processors.
  • 68–74temp$ colours a temperature: green below 45 °C, amber below 55 °C, red from 55 °C.
  • 76–81size$ divides by 1024 until the number is below 1024 and takes the unit from the string u$.
  • 107–116width stops counting at the Escape byte (27) and starts again after the m (109) that ends the code.

Run it

yabasic stats.yab

The program prints the summary and ends.

Try it yourself

Add a row "Swap" with SwapTotal minus SwapFree from meminfo and a bar of it. On line 44, change left$(w$(1), 3) = "eth" to w$(1) <> "lo" and see the virtual switch appear.

More projects in BASIC