Baza wiedzy NASutilsNieoficjalnaEnglish

BASIC · lekcja 30 z 30 · Narzędzia dla NAS-aDokończ narzędzie

Jeden program czyta woluminy, dyski i log, zapisuje report.txt i kończy z kodem. yabasic --bind robi z niego jeden plik, który uruchamiasz po nazwie.

Programowanie na NAS-ie › BASIC w 30 lekcjach

30-report.yab Pobierz 30-report.yab

rem Małe narzędzie: stan woluminów, dysków i logu w jednym pliku
limit = 90
bad = 0
dim field$(1)

open #2, "report.txt", "w"
section("Woluminy")
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) + ": zajęte w " + str$(used) + " procentach", used >= limit)
  endif
wend
close #1

section("Dyski")
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("błędy w logu: " + str$(errors), errors > 0)
close #2

print system$("cat report.txt");
print
print "Wiersze do sprawdzenia: ", bad
if bad > 0 exit 1

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

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

Terminal

$ yabasic 30-report.yab; echo "kod wyjścia $?"
Woluminy
  ok       /volume/data: zajęte w 75 procentach
  SPRAWDŹ  /volume/media: zajęte w 93 procentach
  ok       /volume/fast: zajęte w 20 procentach
Dyski
  SPRAWDŹ  Reallocated_Sector_Ct 8
  ok       Temperature_Celsius 38
  ok       Current_Pending_Sector 0
Log
  SPRAWDŹ  błędy w logu: 3

Wiersze do sprawdzenia: 3
kod wyjścia 1
$ yabasic --bind report 30-report.yab 2>/dev/null; chmod +x report; ./report | tail -n 1
Wiersze do sprawdzenia: 3

Ćwiczenie

Odczytaj limit z config.ini.