forked from steffenfritz/FileTrove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.go
105 lines (87 loc) · 2.62 KB
/
debug.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
101
102
103
104
105
package filetrove
import (
info "github.com/elastic/go-sysinfo"
"os"
"runtime"
"strconv"
)
// DebugCreateDebugPackage creates the file for compiling information into a debug package
func DebugCreateDebugPackage() (os.File, error) {
file, err := os.OpenFile("debug_ftrove", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return os.File{}, err
}
// We don't close the file handle here, this must be done after all information are gathered
file.WriteString("BEGIN FILETROVE DEBUG\n")
return *file, err
}
// DebugCheckInstalled checks if FileTrove is installed by checking if the database exists
func DebugCheckInstalled(fd os.File) error {
filePath := "db/filetrove.db"
_, err := fd.WriteString("\nBEGIN INSTALLED\n")
_, err = os.Stat(filePath)
if err == nil {
fd.WriteString("FileTrove installed: True")
fd.WriteString("\nEND INSTALLED\n")
return nil
}
if os.IsNotExist(err) {
fd.WriteString("FileTrove installed: False")
fd.WriteString("\nEND INSTALLED\n")
return nil
}
return err
}
// DebugHostinformation writes host stats and returns on error
func DebugHostinformation(fd os.File) error {
_, err := fd.WriteString("\nBEGIN HOST INFORMATION\n")
infoOnHost, err := info.Host()
if err != nil {
return err
}
fd.WriteString("OSName: " + infoOnHost.Info().OS.Name + "\n")
fd.WriteString("OSVersion: " + infoOnHost.Info().OS.Version + "\n")
fd.WriteString("Platform: " + infoOnHost.Info().OS.Platform + "\n")
mem, err := infoOnHost.Memory()
if err != nil {
fd.WriteString("Mem: " + err.Error())
return err
}
fd.WriteString("Memory Total: " + strconv.Itoa(int(mem.Total)) + "\n")
fd.WriteString("Memory Used: " + strconv.Itoa(int(mem.Used)) + "\n")
fd.WriteString("Go Runtime: " + runtime.Version() + "\n")
fd.WriteString("END HOST INFORMATION\n")
return nil
}
// DebugWriteFlags takes parsed flags from main and writes them to the diag file
func DebugWriteFlags(fd os.File, args []string) error {
_, err := fd.WriteString("\nBEGIN FLAGS\n")
if err != nil {
return err
}
for k, v := range args {
_, err = fd.WriteString(strconv.Itoa(k) + ": " + v + "\n")
}
fd.WriteString("END FLAGS\n")
return err
}
func DebugWriteFileList(fd os.File, filelist []string, dirlist []string) error {
fd.WriteString("\nBEGIN FILELIST\n")
for _, file := range filelist {
_, err := fd.WriteString(file + "\n")
if err != nil {
return err
}
}
fd.WriteString("END FILELIST\n")
fd.WriteString("\nBEGIN DIRLIST\n")
for _, dir := range dirlist {
_, err := fd.WriteString(dir + "\n")
if err != nil {
return err
}
}
fd.WriteString("END DIRLIST\n")
return nil
}
// DebugCollectLogs collects log files