app works again
This commit is contained in:
parent
97f29457db
commit
b53e71ed9e
3
http.go
3
http.go
|
@ -40,7 +40,8 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
if route == "/machine" {
|
||||
handleMachine(w, hostname, msg)
|
||||
handleMachine(r, w, hostname, msg)
|
||||
return
|
||||
|
||||
var m *zoopb.Machine
|
||||
m = new(zoopb.Machine)
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func dumpRemoteAddr(r *http.Request) string {
|
||||
return r.RemoteAddr
|
||||
}
|
||||
|
||||
func dumpUserAgent(r *http.Request) string {
|
||||
var all string
|
||||
for param, values := range r.URL.Query() {
|
||||
for _, value := range values {
|
||||
all += fmt.Sprint(" Query:", param, value)
|
||||
}
|
||||
}
|
||||
// hostname := r.URL.Query().Get("hostname")
|
||||
return r.UserAgent() + all
|
||||
}
|
||||
|
||||
func dumpClient(r *http.Request) {
|
||||
/*
|
||||
var host, url, proto, addr, agent string
|
||||
|
||||
host = r.Host
|
||||
url = r.URL.String()
|
||||
proto = r.Proto
|
||||
addr = r.RemoteAddr
|
||||
agent = r.UserAgent()
|
||||
|
||||
log.Warn(host, proto, addr, url, agent)
|
||||
|
||||
fmt.Fprintln(accessf, time.Now(), host, proto, addr, url, agent)
|
||||
// return
|
||||
|
||||
fmt.Fprintln(clientf)
|
||||
fmt.Fprintln(clientf, time.Now())
|
||||
// Basic request information
|
||||
fmt.Fprintln(clientf, "Method:", r.Method)
|
||||
fmt.Fprintln(clientf, "URL:", r.URL)
|
||||
fmt.Fprintln(clientf, "Protocol:", r.Proto)
|
||||
fmt.Fprintln(clientf, "Host:", r.Host)
|
||||
fmt.Fprintln(clientf, "Remote Address:", r.RemoteAddr)
|
||||
|
||||
// Headers
|
||||
fmt.Fprintln(clientf, "Headers:")
|
||||
for name, values := range r.Header {
|
||||
for _, value := range values {
|
||||
fmt.Fprintln(clientf, "Headers:", name, value)
|
||||
}
|
||||
}
|
||||
|
||||
// Query parameters
|
||||
fmt.Fprintln(clientf, "Query Parameters:")
|
||||
for param, values := range r.URL.Query() {
|
||||
for _, value := range values {
|
||||
fmt.Fprintln(clientf, "Query:", param, value)
|
||||
}
|
||||
}
|
||||
|
||||
// User-Agent
|
||||
fmt.Fprintln(clientf, "User-Agent:", r.UserAgent())
|
||||
|
||||
// Content Length
|
||||
fmt.Fprintln(clientf, "Content Length:", r.ContentLength)
|
||||
|
||||
// Cookies
|
||||
fmt.Fprintln(clientf, "Cookies:")
|
||||
for _, cookie := range r.Cookies() {
|
||||
fmt.Fprintln(clientf, cookie.Name, cookie.Value)
|
||||
}
|
||||
|
||||
// Request Body (if applicable)
|
||||
if r.Body != nil {
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err == nil {
|
||||
fmt.Fprintln(clientf, "Body:", string(body))
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
23
machine.go
23
machine.go
|
@ -19,31 +19,42 @@ func rawGetHostname(data []byte) *zoopb.Machine {
|
|||
return newm
|
||||
}
|
||||
|
||||
func handleMachine(w http.ResponseWriter, hostname string, data []byte) {
|
||||
func handleMachine(r *http.Request, w http.ResponseWriter, hostname string, data []byte) {
|
||||
hostname = strings.TrimSpace(hostname)
|
||||
newm := rawGetHostname(data)
|
||||
if newm == nil {
|
||||
log.Info("unmarshal failed on data len =", len(data))
|
||||
}
|
||||
if hostname == newm.Hostname {
|
||||
log.Info("hostname mismatch", hostname, "vs", newm.Hostname)
|
||||
if hostname != newm.Hostname {
|
||||
// log.Info("hostname mismatch", hostname, "vs", newm.Hostname)
|
||||
hostname = newm.Hostname
|
||||
}
|
||||
|
||||
if hostname == "" {
|
||||
log.Info("hostname is blank even after unmarshal. data len =", len(data))
|
||||
ua := dumpUserAgent(r)
|
||||
ra := dumpRemoteAddr(r)
|
||||
log.Info("hostname is blank even after unmarshal. data len =", len(data), ra, ua, newm.Cpus, newm.Hostname)
|
||||
return
|
||||
}
|
||||
log.Info("lookoing for", hostname)
|
||||
// log.Info("lookoing for", hostname)
|
||||
m := me.machines.FindByHostname(hostname)
|
||||
if m == nil {
|
||||
am := new(zoopb.Machine)
|
||||
am.Hostname = newm.Hostname
|
||||
am.Memory = newm.Memory
|
||||
me.machines2.Append(am)
|
||||
me.machines.Append(newm)
|
||||
log.Info("new machine", am.Hostname, am.Memory)
|
||||
return
|
||||
}
|
||||
log.Info("not new machine", hostname)
|
||||
ua := dumpUserAgent(r)
|
||||
ra := dumpRemoteAddr(r)
|
||||
if m.UserAgent != ua {
|
||||
log.Info("hostname ua changed len =", len(data), ra, hostname, ua)
|
||||
m.UserAgent = ua
|
||||
}
|
||||
// log.Info("update machine protobuf", hostname)
|
||||
updateMachine(newm)
|
||||
}
|
||||
|
||||
// someone sent machine 'u' is it new?
|
||||
|
|
Loading…
Reference in New Issue