-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
executable file
·124 lines (114 loc) · 2.66 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
var (
exportFolder string
logsFolder string
)
func main() {
fmt.Printf("Logging Main process \n")
//logsFolder = os.Getenv("LOGS_FOLDER")
//exportFolder = os.Getenv("EXPORT_FOLDER")
interval, err := strconv.Atoi(os.Getenv("EXEC_INTERVAL"))
if err != nil {
interval = 7
}
if logsFolder == "" {
logsFolder = "/var/log/containers"
}
if exportFolder == "" {
exportFolder = "/opt/logs"
}
go gather(interval)
runWebServer()
}
func runWebServer() {
http.Handle("/", http.FileServer(http.Dir(exportFolder)))
http.HandleFunc("/healthz", healthz)
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
func gather(interval int) {
for {
fmt.Println("Gathering logs...")
err2 := filepath.Walk(logsFolder, visit)
if err2 != nil {
fmt.Println("Error visiting file", err2)
}
fmt.Printf("Pausing the process during %d minutes \n", interval)
time.Sleep(time.Minute * time.Duration(interval))
}
}
func healthz(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
}
func visit(path string, f os.FileInfo, err error) error {
if err != nil {
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err)
return err
}
if f != nil && !strings.Contains(f.Name(), "POD") {
if f.IsDir() {
fmt.Println(path, "is a directory, ignore")
}
realPath, errSymLink := filepath.EvalSymlinks(path)
if errSymLink != nil {
fmt.Println(path, " is not a SymLink")
realPath = path
}
sName := strings.Split(f.Name(), "_")
if len(sName) == 3 {
if sName[1] != "kube-system" {
err = copy(realPath, exportFolder+"/"+sName[1]+"/"+sName[2])
if err != nil {
fmt.Println("Error copying file", realPath, err)
}
} else {
fmt.Println(sName[1], "Ignoring namespace")
return nil
}
}
}
return err
}
func copy(src, dst string) error {
fmt.Printf("Opening file to be copied %s \n", src)
in, err := os.Open(src)
if err != nil {
in.Close()
return err
}
//defer in.Close() ==>Avoiding to use defer in order to close files
fmt.Printf("Creating destination file %s \n", dst)
os.MkdirAll(filepath.Dir(dst), os.ModePerm)
out, err := os.Create(dst)
if err != nil {
in.Close()
out.Close()
return err
}
//defer out.Close() ==>Avoiding to use defer in order to close files
fmt.Printf("Copying from %s to %s\n", src, dst)
bytesWritten, err := io.Copy(out, in)
if err != nil {
in.Close()
out.Close()
return err
}
fmt.Printf("Copied %d bytes. \n", bytesWritten)
fmt.Printf("Copied from %s to %s\n", src, dst)
//cerr := out.Close()
//return cerr
in.Close()
out.Close()
return err
}