NASutils Knowledge BaseUnofficialPolski

BASIC · lesson 30 of 30 · Tools for the NASFinish a tool

One program reads volumes, disks and a log, writes report.txt and ends with a code. yabasic --bind turns it into one file you start by name.

Programming on the NAS › BASIC in 30 lessons

30-report.yab Download 30-report.yab

rem A small tool: the state of volumes, disks and the log in one file
limit = 90
bad = 0
dim field$(1)

open #2, "report.txt", "w"
section("Volumes")
open #1, "df.txt", "r"
line input #1 header$
while not eof(1)
  line input #1 line$
  if token(line$, field$(), " ") = 6 then
    used = val(field$(5))
    say(field$(6) + " " + str$(used) + " percent used", used >= limit)
  endif
wend
close #1

section("Disks")
open #1, "smartctl.txt", "r"
while not eof(1)
  line input #1 line$
  if token(line$, field$(), " ") >= 10 then
    raw = val(field$(10))
    switch field$(2)
      case "Reallocated_Sector_Ct"
      case "Current_Pending_Sector"
        say(field$(2) + " " + str$(raw), raw > 0)
        break
      case "Temperature_Celsius"
        say(field$(2) + " " + str$(raw), raw >= 50)
        break
    end switch
  endif
wend
close #1

section("Log")
errors = val(system$("grep -c ' ERROR ' nas.log"))
say(str$(errors) + " errors", errors > 0)
close #2

print system$("cat report.txt");
print
print bad, " lines need a look"
if bad > 0 exit 1

sub section(title$)
  print #2, title$
end sub

sub say(text$, wrong)
  if wrong then
    print #2, "  LOOK  " + text$
    bad = bad + 1
  else
    print #2, "  ok    " + text$
  endif
end sub

Terminal

$ yabasic 30-report.yab; echo "exit code $?"
Volumes
  ok    /volume/data 75 percent used
  LOOK  /volume/media 93 percent used
  ok    /volume/fast 20 percent used
Disks
  LOOK  Reallocated_Sector_Ct 8
  ok    Temperature_Celsius 38
  ok    Current_Pending_Sector 0
Log
  LOOK  3 errors

3 lines need a look
exit code 1
$ yabasic --bind report 30-report.yab 2>/dev/null; chmod +x report; ./report | tail -n 1
3 lines need a look

Exercise

Read the limit from config.ini instead of the program.