-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
186 lines (138 loc) · 3.82 KB
/
server.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
yaml "gopkg.in/yaml.v2"
)
//page Estrutura da Resposta
type page struct {
Title string
Body []byte
}
//Report Struct do Report
type Report struct {
Node string
Testes []Teste
}
//Teste Struct do Teste
type Teste struct {
Item string
Desc string
OcorrenciasAdicionais int //OcorrenciasAdicionais Adicionais
}
//Config struct da Configuracao
type Config struct {
Dir string `yaml:"dir"`
Port string `yaml:"port"`
Ipaddr string `yaml:"ipaddr"`
}
var config Config
func check(e error) {
if e != nil {
panic(e)
}
}
func (p *page) save() error {
filename := p.Title + ".txt"
return ioutil.WriteFile(filename, p.Body, 0600)
}
func loadpage(title string) (*page, error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
check(err)
return &page{Title: title, Body: body}, nil
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[len("/view/"):]
p, _ := loadpage(title)
fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.Title, p.Body)
}
func reportHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("reportHandler - ", config.Dir)
node := r.URL.Path[len("/cisreport/"):]
p := extractReportFromFile(node, config.Dir)
//json.NewEncoder(w).Encode//(p) //write json to
//extractReportFromFile(node,)
w.Header().Set("Content-Type", "application/json")
w.Write(p)
//fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.Title, p.Body)
}
func loadConfig(filename string) {
fmt.Println("Carregando Configuração...")
file, _ := filepath.Abs(filename)
fmt.Println("File: ", file)
yamlFile, err := ioutil.ReadFile(file)
//fmt.Println("loadconfig y: ", config.Dir)
check(err)
err = yaml.Unmarshal(yamlFile, &config)
fmt.Println("loadconfig config dir: ", config.Dir)
check(err)
}
func extractReportFromFile(k8snode string, dir string) []byte {
var filename string
if k8snode == "all" {
filename = "cis-docker-all.txt"
} else if strings.Contains(k8snode, "spessrvvpkn") {
filename = dir + "/cis-docker-" + k8snode + ".estaleiro.serpro.txt"
}
fileHandle, err := os.Open(filename)
check(err)
fscanner := bufio.NewScanner(fileHandle)
var qtde = 0
//var linha string
re := regexp.MustCompile(`\d{1,2}[\.]\d{1,2}`)
//flag := false
var report Report
report = Report{Node: k8snode}
var item string
var descricao string
var linhas []string
//Report.Node = k8snode
for fscanner.Scan() {
linetmp := fscanner.Text()
if strings.Contains(linetmp, "[WARN]") {
ind := strings.Index(linetmp, "0m")
linha := linetmp[ind+2:]
linhas = append(linhas, linha)
}
}
var indexItem int
for index, linha := range linhas {
item = re.FindString(linha)
//fmt.Println("index: ", index, " item: ", item, "linha: ", linha)
if len(item) > 0 && strings.Index(linha, "*") < 0 { //WARN com item e descricao
descricao = linha[strings.Index(linha, "-"):len(linha)]
//fmt.Println("Desc: ", descricao)
qtde = 0
report.Testes = append(report.Testes, Teste{Item: item, Desc: descricao})
indexItem = len(report.Testes) - 1
//fmt.Println("indexItem: ", indexItem)
} else if strings.Index(linha, "*") > 0 { //ocorrencia adicional do item
qtde++
if index == len(linhas)-1 || strings.Index(linhas[index+1], "*") < 0 { //se a proxima linha nao contem *
report.Testes[indexItem].OcorrenciasAdicionais = qtde
}
} else { //Linha mal formada
panic("fudeu")
}
}
jsonreport, err := json.Marshal(report)
check(err)
//os.Stdout.Write(b)
return jsonreport
}
func main() {
fmt.Println("Iniciando...")
loadConfig("config.yml")
//fmt.Println("config dir: ", config.Dir)
//http.HandleFunc("/view/", viewHandler)
http.HandleFunc("/cisreport/", reportHandler)
http.ListenAndServe("0.0.0.0:6669", nil)
}