/
handlers.go
121 lines (106 loc) · 2.77 KB
/
handlers.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
package main
import (
"encoding/json"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"html/template"
"io/ioutil"
"net/http"
)
type AllHostDataPage struct {
Queries []HttpPost
Host string
}
type LatestHostDataPage struct {
Data HttpPost
Host string
}
type MainPage struct {
AvailableHosts []string
HostHits map[string]int
}
type HttpPost struct {
Host string
Data map[string]interface{}
Time string
}
// The container index
func Agent(w http.ResponseWriter, r *http.Request) {
log.Debug("/agent POST")
// Make a channel to dump our requests asynchronously
respCh := make(chan *HttpPost)
// Make an array of hostData to feed into
hostDataArry := []*HttpPost{}
// Spawn a proc to dump the data into our channel
go func(r *http.Request) {
var newData HttpPost
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Error(err)
}
// Unmarshal the POST into .Data
err = json.Unmarshal(body, &newData.Data)
// Type assert our way to the hostname
newData.Host = newData.Data["host"].(map[string]interface{})["hostname"].(string)
//newData.Time = string(time.Now().Format("2006010215040500"))
respCh <- &newData
}(r)
// Check the channel for a resp
select {
case r := <-respCh:
// log.Debug("New data from ", r.Host, "@", r.Time)
log.Debug("New data from ", r.Host)
log.Debug(r.Data)
hostDataArry = append(hostDataArry, r)
dumpToElastic(hostDataArry)
}
}
// The host index
func Console(w http.ResponseWriter, r *http.Request) {
log.Debug("/ GET")
var p MainPage
p.HostHits = make(map[string]int)
results := queryAllHosts(500)
for k, v := range results {
p.HostHits[k] = len(v)
p.AvailableHosts = append(p.AvailableHosts, k)
for key, value := range v {
log.Debug("Host ", k, " ", key, ": ", value)
}
}
log.Debug("Host hits: ", p.HostHits)
log.Debug("Available: ", p.AvailableHosts)
t, _ := template.ParseFiles("views/MainPage.html")
t.Execute(w, p)
}
func ConsoleHostnameLatest(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
hostname := vars["hostname"]
results := queryHostnameLatest(hostname)
log.Debug("New results for ", hostname, ":")
fmt.Println(results.Data)
// Latest data struct
var p LatestHostDataPage
// Execute template
p.Data = results
p.Host = hostname
t, _ := template.ParseFiles("views/LatestHostData.html")
t.Execute(w, p)
}
func ConsoleHostnameRoot(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
hostname := vars["hostname"]
results := queryHostnameAll(hostname)
log.Debug("Queried all results for ", hostname)
fmt.Println(results)
// New data page
var p AllHostDataPage
p.Queries = results
p.Host = vars["hostname"]
log.Debug("RESULTS ", results)
log.Debug(p)
// Parse Template
t, _ := template.ParseFiles("views/AllHostData.html")
t.Execute(w, p)
}