-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (89 loc) · 2.37 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"embed"
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"os"
"time"
"gitlab.com/nest-machine/sysmonitor/core"
"gitlab.com/nest-machine/sysmonitor/features"
"gopkg.in/yaml.v3"
)
//go:embed res/*
var res embed.FS
func main() {
var err error
var report core.ReportContext
report.Now = time.Now()
report.Groups = make([]core.Group, 0)
var help bool
var confFileName string
// var dryRun bool
flag.BoolVar(&help, "h", false, "Print Help and Exit")
// flag.BoolVar(&dryRun, "n", false, "Dry Run")
flag.StringVar(&confFileName, "cf", "sysmonitor.yaml", "Configuration file name")
flag.Parse()
var conf = &core.Config{}
if help {
printHelp(conf)
return
}
if err = loadConfig(confFileName, conf); err != nil {
fmt.Printf("failed to load config file %s: %v", confFileName, err)
return
}
var templ *template.Template
if templ, err = template.New("report").ParseFS(res, "res/report.html"); err != nil {
fmt.Printf("failed to load template file res/report.html: %v", err)
return
}
if conf.FeatureEnabled(core.BtrfsFeature) {
report.Groups = append(report.Groups, features.BtrfsReport(conf)...)
}
if conf.FeatureEnabled(core.ZfsFeature) {
report.Groups = append(report.Groups, features.ZfsReport())
}
if conf.FeatureEnabled(core.JournalFeature) {
report.Groups = append(report.Groups, features.JournalReport())
}
if conf.FeatureEnabled(core.RsyncFeature) {
report.Groups = append(report.Groups, features.RsyncReport(conf)...)
}
if conf.FeatureEnabled(core.AppsFeature) {
report.Groups = append(report.Groups, features.AppsReport(conf)...)
}
var writer io.Writer
if writer, err = conf.OutputWriter(); err != nil {
fmt.Printf("failed to get output writer: %v", err)
return
}
defer conf.CloseOutputWriter()
if err = templ.ExecuteTemplate(writer, "report", &report); err != nil {
fmt.Println()
fmt.Println(err.Error())
fmt.Println("finished with error")
return
}
}
func printHelp(conf *core.Config) {
var err error
fmt.Printf("Usage: %s [OPTIONS]\n", os.Args[0])
flag.PrintDefaults()
fmt.Println("Config File Example:")
var content []byte
if content, err = yaml.Marshal(conf); err != nil {
panic(err)
}
os.Stdout.Write(content)
fmt.Println()
}
func loadConfig(fileName string, target *core.Config) error {
content, err := ioutil.ReadFile(fileName)
if err != nil {
return err
}
return yaml.Unmarshal(content, target)
}