Go · lekcja 30 z 30 · Narzędzia dla NAS-aDokończ narzędzie
Program czyta woluminy, dyski i log, a potem wypisuje tekst albo stronę WWW zrobioną z szablonu.
30-report.go Pobierz 30-report.go
package main
import (
"bufio"
"flag"
"fmt"
"html/template"
"os"
"strconv"
"strings"
)
type Line struct {
Text string
Look bool
}
type Section struct {
Title string
Lines []Line
}
// fields wywołuje each ze słowami każdego wiersza pliku
func fields(name string, each func(part []string)) error {
f, err := os.Open(name)
if err != nil {
return err
}
defer f.Close()
in := bufio.NewScanner(f)
for in.Scan() {
each(strings.Fields(in.Text()))
}
return in.Err()
}
func collect(limit int) ([]Section, error) {
var volumes, disks []Line
err := fields("df.txt", func(part []string) {
if len(part) != 6 || part[0] == "Filesystem" {
return
}
used, _ := strconv.Atoi(strings.TrimSuffix(part[4], "%"))
volumes = append(volumes, Line{fmt.Sprintf("%s zajęte %d%%", part[5], used), used >= limit})
})
if err != nil {
return nil, err
}
err = fields("smartctl.txt", func(part []string) {
if len(part) < 10 {
return
}
raw, _ := strconv.Atoi(part[9])
text := fmt.Sprintf("%s %d", part[1], raw)
switch part[1] {
case "Reallocated_Sector_Ct", "Current_Pending_Sector":
disks = append(disks, Line{text, raw > 0})
case "Temperature_Celsius":
disks = append(disks, Line{text, raw >= 50})
}
})
if err != nil {
return nil, err
}
errors := 0
err = fields("nas.log", func(part []string) {
if len(part) > 2 && part[2] == "ERROR" {
errors++
}
})
log := []Line{{fmt.Sprintf("błędy: %d", errors), errors > 0}}
return []Section{{"Woluminy", volumes}, {"Dyski", disks}, {"Log", log}}, err
}
const page = `<h1>Raport NAS-a</h1>
{{range .}}<h2>{{.Title}}</h2>
{{range .Lines}}<p{{if .Look}} class="look"{{end}}>{{.Text}}</p>
{{end}}{{end}}`
func main() {
limit := flag.Int("limit", 90, "procent, od którego wolumin jest pełny")
html := flag.Bool("html", false, "wypisz stronę WWW")
flag.Parse()
sections, err := collect(*limit)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
if *html {
if err := template.Must(template.New("page").Parse(page)).Execute(os.Stdout, sections); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
}
bad := 0
for _, s := range sections {
if !*html {
fmt.Println(s.Title)
}
for _, l := range s.Lines {
mark := "ok "
if l.Look {
mark = "UWAGA"
bad++
}
if !*html {
fmt.Println(" ", mark, l.Text)
}
}
}
if !*html {
fmt.Println("do sprawdzenia:", bad)
}
if bad > 0 {
os.Exit(1)
}
}
Terminal
$ ./30-report; echo "kod wyjścia $?"
Woluminy
ok /volume/data zajęte 75%
UWAGA /volume/media zajęte 93%
ok /volume/fast zajęte 20%
Dyski
UWAGA Reallocated_Sector_Ct 8
ok Temperature_Celsius 38
ok Current_Pending_Sector 0
Log
UWAGA błędy: 3
do sprawdzenia: 3
kod wyjścia 1
$ ./30-report -limit 95 | tail -n 1
do sprawdzenia: 2
$ ./30-report -html | head -n 4
<h1>Raport NAS-a</h1>
<h2>Woluminy</h2>
<p>/volume/data zajęte 75%</p>
<p class="look">/volume/media zajęte 93%</p>Ćwiczenie
Dodaj -json i wypisz sekcje jako JSON.
TurboShell nie zawiera Go. Te programy powstają w Bucket & Code, w jego środowisku Go, a gotowy plik działa na NAS-ie bez Go.