NASutils Knowledge BaseUnofficialPolski

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

Functions read the limit, the volumes, the disks and the log; the report goes to the screen and to report.txt. The exit code is 1 if a line needs a look.

Programming on the NAS › Bash in 30 lessons

30-report.sh Download 30-report.sh

#!/usr/app/TurboShell/bin/bash
# A small tool: the state of volumes, disks and the log, on the screen and in report.txt
set -euo pipefail

limit=$(sed -n 's/^limit=//p' config.ini)
title=$(sed -n 's/^title=//p' config.ini)
bad=0

say() { # OK-OR-LOOK TEXT
  printf '  %-4s  %s\n' "$1" "$2"
  if [[ $1 == LOOK ]]; then
    bad=$((bad + 1))
  fi
}

report() {
  local percent mount name raw errors
  echo "$title"
  echo "Volumes"
  while read -r _ _ _ _ percent mount; do
    [[ $percent == *% ]] || continue
    if ((${percent%\%} >= limit)); then
      say LOOK "$mount $percent used"
    else
      say ok "$mount $percent used"
    fi
  done <df.txt

  echo "Disks"
  while read -r _ name _ _ _ _ _ _ _ raw _; do
    case $name in
      Reallocated_Sector_Ct | Current_Pending_Sector)
        if ((raw > 0)); then say LOOK "$name $raw"; else say ok "$name $raw"; fi
        ;;
    esac
  done <smartctl.txt

  echo "Log"
  errors=$(grep -c ' ERROR ' nas.log || true)
  if ((errors > 0)); then say LOOK "$errors errors"; else say ok "no errors"; fi
}

report >report.txt
cat report.txt
echo "$bad lines need a look"
((bad == 0))

Terminal

$ bash 30-report.sh; echo "exit code $?"
Morning check
Volumes
  ok    /volume/data 75% used
  LOOK  /volume/media 93% used
  ok    /volume/fast 20% used
Disks
  LOOK  Reallocated_Sector_Ct 8
  ok    Current_Pending_Sector 0
Log
  LOOK  3 errors
3 lines need a look
exit code 1
$ grep LOOK report.txt | wc -l
3

Exercise

Add the temperature of the disks.