Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/api_topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func handleNode(rep Reporter, t topologyView, w http.ResponseWriter, r *http.Req
http.NotFound(w, r)
return
}
originHostFunc := func(id string) (OriginHost, bool) { return getOriginHost(rpt.HostMetadatas, id) }
originHostFunc := func(id string) (OriginHost, bool) { return getOriginHost(rpt.Host, id) }
originNodeFunc := func(id string) (OriginNode, bool) { return getOriginNode(t.selector(rpt), id) }
respondWith(w, http.StatusOK, APINode{Node: makeDetailed(node, originHostFunc, originNodeFunc)})
}
Expand Down
13 changes: 5 additions & 8 deletions app/detail_pane.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ outer:
Numeric: false,
Rows: []report.Row{
{"Hostname", host.Hostname, ""},
{"Load", fmt.Sprintf("%.2f %.2f %.2f", host.LoadOne, host.LoadFive, host.LoadFifteen), ""},
{"Load", host.Load, ""},
{"OS", host.OS, ""},
//{"Addresses", strings.Join(host.Addresses, ", "), ""},
{"ID", id, ""},
},
})
Expand All @@ -75,12 +74,10 @@ outer:

func unknownOriginHost(id string) OriginHost {
return OriginHost{
Hostname: fmt.Sprintf("[%s]", id),
OS: "unknown",
Addresses: []string{},
LoadOne: 0.0,
LoadFive: 0.0,
LoadFifteen: 0.0,
Hostname: fmt.Sprintf("[%s]", id),
OS: "unknown",
Networks: []string{},
Load: "",
}
}

Expand Down
30 changes: 16 additions & 14 deletions app/mock_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,23 @@ func (s StaticReport) Report() report.Report {
},
},

HostMetadatas: report.HostMetadatas{
"hostA": report.HostMetadata{
Hostname: "node-a.local",
LocalNets: []*net.IPNet{localNet},
OS: "Linux",
LoadOne: 3.1415,
LoadFive: 2.7182,
LoadFifteen: 1.6180,
},
"hostB": report.HostMetadata{
Hostname: "node-b.local",
LocalNets: []*net.IPNet{localNet},
OS: "Linux",
Host: report.Topology{
Adjacency: report.Adjacency{},
EdgeMetadatas: report.EdgeMetadatas{},
NodeMetadatas: report.NodeMetadatas{
report.MakeHostNodeID("hostA"): report.NodeMetadata{
"host_name": "node-a.local",
"os": "Linux",
"local_networks": localNet.String(),
"load": "3.14 2.71 1.61",
},
report.MakeHostNodeID("hostB"): report.NodeMetadata{
"host_name": "node-b.local",
"os": "Linux",
"local_networks": localNet.String(),
},
},
},
}
return testReport.SquashRemote()
return testReport.Squash()
}
32 changes: 12 additions & 20 deletions app/origin_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"net/http"
"strings"

"github.com/gorilla/mux"

Expand All @@ -12,32 +13,23 @@ import (
// some data in the system. The struct is returned by the /api/origin/{id}
// handler.
type OriginHost struct {
Hostname string `json:"hostname"`
OS string `json:"os"`
Addresses []string `json:"addresses"`
LoadOne float64 `json:"load_one"`
LoadFive float64 `json:"load_five"`
LoadFifteen float64 `json:"load_fifteen"`
Hostname string `json:"hostname"`
OS string `json:"os"`
Networks []string `json:"networks"`
Load string `json:"load"`
}

func getOriginHost(mds report.HostMetadatas, nodeID string) (OriginHost, bool) {
host, ok := mds[nodeID]
func getOriginHost(t report.Topology, nodeID string) (OriginHost, bool) {
host, ok := t.NodeMetadatas[nodeID]
if !ok {
return OriginHost{}, false
}

var addrs []string
for _, l := range host.LocalNets {
addrs = append(addrs, l.String())
}

return OriginHost{
Hostname: host.Hostname,
OS: host.OS,
Addresses: addrs,
LoadOne: host.LoadOne,
LoadFive: host.LoadFive,
LoadFifteen: host.LoadFifteen,
Hostname: host["host_name"],
OS: host["os"],
Networks: strings.Split(host["local_networks"], " "),
Load: host["load"],
}, true
}

Expand All @@ -48,7 +40,7 @@ func makeOriginHostHandler(rep Reporter) http.HandlerFunc {
vars = mux.Vars(r)
nodeID = vars["id"]
)
origin, ok := getOriginHost(rep.Report().HostMetadatas, nodeID)
origin, ok := getOriginHost(rep.Report().Host, nodeID)
if !ok {
http.NotFound(w, r)
return
Expand Down
10 changes: 2 additions & 8 deletions app/origin_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,15 @@ func TestAPIOriginHost(t *testing.T) {

{
// Origin
body := getRawJSON(t, ts, "/api/origin/host/hostA")
body := getRawJSON(t, ts, "/api/origin/host/hostA;<host>") // TODO MakeHostNodeID
var o OriginHost
if err := json.Unmarshal(body, &o); err != nil {
t.Fatalf("JSON parse error: %s", err)
}
if want, have := "Linux", o.OS; want != have {
t.Errorf("Origin error. Want %v, have %v", want, have)
}
if want, have := 3.1415, o.LoadOne; want != have {
t.Errorf("Origin error. Want %v, have %v", want, have)
}
if want, have := 2.7182, o.LoadFive; want != have {
t.Errorf("Origin error. Want %v, have %v", want, have)
}
if want, have := 1.6180, o.LoadFifteen; want != have {
if want, have := "3.14 2.71 1.61", o.Load; want != have {
t.Errorf("Origin error. Want %v, have %v", want, have)
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/report_lifo.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewReportLIFO(r reporter, maxAge time.Duration) *ReportLIFO {
select {
case report := <-r.Reports():
// Incoming report from the collecter.
report = report.SquashRemote() // TODO?: make this a CLI argument.
report = report.Squash() // TODO?: make this a CLI argument.
tr := timedReport{
Timestamp: time.Now(),
Report: report,
Expand Down
8 changes: 5 additions & 3 deletions app/scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,19 @@ func checkRequest(t *testing.T, ts *httptest.Server, method, path string, body [
func getRawJSON(t *testing.T, ts *httptest.Server, path string) []byte {
res, body := checkGet(t, ts, path)

_, file, line, _ := runtime.Caller(1)
file = filepath.Base(file)
if res.StatusCode != 200 {
t.Fatalf("Expected status %d, got %d. Path: %s", 200, res.StatusCode, path)
t.Fatalf("%s:%d: Expected status %d, got %d. Path: %s", file, line, 200, res.StatusCode, path)
}

foundCtype := res.Header.Get("content-type")
if foundCtype != "application/json" {
t.Errorf("Wrong Content-type for JSON: %s", foundCtype)
t.Errorf("%s:%d: Wrong Content-type for JSON: %s", file, line, foundCtype)
}

if len(body) == 0 {
t.Errorf("No response body")
t.Errorf("%s:%d: No response body", file, line)
}
// fmt.Printf("Body: %s", body)

Expand Down
2 changes: 1 addition & 1 deletion experimental/bridge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func discover(c collector, p publisher, fixed []string) {

var (
now = time.Now()
localNets = r.LocalNets()
localNets = r.LocalNetworks()
)

for _, adjacent := range r.Address.Adjacency {
Expand Down
10 changes: 5 additions & 5 deletions experimental/demoprobe/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ func DemoReport(nodeCount int) report.Report {
r.Address.Adjacency[nodeDstAddressID] = r.Address.Adjacency[nodeDstAddressID].Add(srcAddressID)

// Host data
r.HostMetadatas["hostX"] = report.HostMetadata{
Timestamp: time.Now().UTC(),
Hostname: "host-x",
LocalNets: []*net.IPNet{localNet},
OS: "linux",
r.Host.NodeMetadatas["hostX"] = report.NodeMetadata{
"ts": time.Now().UTC().Format(time.RFC3339Nano),
"host_name": "host-x",
"local_networks": localNet.String(),
"os": "linux",
}
}

Expand Down
10 changes: 5 additions & 5 deletions experimental/genreport/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ func DemoReport(nodeCount int) report.Report {
r.Address.Adjacency[nodeDstAddressID] = r.Address.Adjacency[nodeDstAddressID].Add(srcAddressID)

// Host data
r.HostMetadatas["hostX"] = report.HostMetadata{
Timestamp: time.Now().UTC(),
Hostname: "host-x",
LocalNets: []*net.IPNet{localNet},
OS: "linux",
r.Host.NodeMetadatas["hostX"] = report.NodeMetadata{
"ts": time.Now().UTC().Format(time.RFC3339Nano),
"host_name": "host-x",
"local_networks": localNet.String(),
"os": "linux",
}
}

Expand Down
2 changes: 1 addition & 1 deletion experimental/graphviz/report_lifo.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewReportLIFO(r reporter, maxAge time.Duration) *ReportLIFO {
for {
select {
case report := <-r.Reports():
report = report.SquashRemote()
report = report.Squash()
tr := timedReport{
Timestamp: time.Now(),
Report: report,
Expand Down
52 changes: 25 additions & 27 deletions probe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -79,8 +80,8 @@ func main() {
defer close(quit)
go func() {
var (
hostname = hostname()
nodeID = hostname // TODO: we should sanitize the hostname
hostName = hostname()
hostID = hostName // TODO: we should sanitize the hostname
pubTick = time.Tick(*publishInterval)
spyTick = time.Tick(*spyInterval)
r = report.MakeReport()
Expand All @@ -90,12 +91,12 @@ func main() {
select {
case <-pubTick:
publishTicks.WithLabelValues().Add(1)
r.HostMetadatas[nodeID] = hostMetadata(hostname)
r.Host = hostTopology(hostID, hostName)
publisher.Publish(r)
r = report.MakeReport()

case <-spyTick:
r.Merge(spy(hostname, hostname, *spyProcs))
r.Merge(spy(hostID, hostName, *spyProcs))
r = tag.Apply(r, taggers)
// log.Printf("merged report:\n%#v\n", r)

Expand All @@ -108,34 +109,31 @@ func main() {
log.Printf("%s", <-interrupt())
}

func interrupt() chan os.Signal {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
return c
}

// hostMetadata produces an instantaneous HostMetadata for this host. No need
// to do this more than once per published report.
func hostMetadata(hostname string) report.HostMetadata {
loadOne, loadFive, loadFifteen := getLoads()

host := report.HostMetadata{
Timestamp: time.Now().UTC(),
Hostname: hostname,
OS: runtime.GOOS,
LoadOne: loadOne,
LoadFive: loadFive,
LoadFifteen: loadFifteen,
}

// hostTopology produces a host topology for this host. No need to do this
// more than once per published report.
func hostTopology(hostID, hostName string) report.Topology {
var localCIDRs []string
if localNets, err := net.InterfaceAddrs(); err == nil {
// Not all networks are IP networks.
for _, localNet := range localNets {
if net, ok := localNet.(*net.IPNet); ok {
host.LocalNets = append(host.LocalNets, net)
if ipNet, ok := localNet.(*net.IPNet); ok {
localCIDRs = append(localCIDRs, ipNet.String())
}
}
}
t := report.NewTopology()
t.NodeMetadatas[report.MakeHostNodeID(hostID)] = report.NodeMetadata{
"ts": time.Now().UTC().Format(time.RFC3339Nano),
"host_name": hostName,
"local_networks": strings.Join(localCIDRs, " "),
"os": runtime.GOOS,
"load": getLoad(),
}
return t
}

return host
func interrupt() chan os.Signal {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
return c
}
32 changes: 10 additions & 22 deletions probe/system_darwin.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
package main

import (
"fmt"
"os/exec"
"strconv"
"strings"
"regexp"
)

func getLoads() (float64, float64, float64) {
var loadRe = regexp.MustCompile(`load average\: ([0-9\.]+), ([0-9\.]+), ([0-9\.]+)`)

func getLoad() string {
out, err := exec.Command("w").CombinedOutput()
if err != nil {
return -1, -1, -1
}
noCommas := strings.NewReplacer(",", "")
firstLine := strings.Split(string(out), "\n")[0]
toks := strings.Fields(firstLine)
if len(toks) < 5 {
return -1, -1, -1
}
one, err := strconv.ParseFloat(noCommas.Replace(toks[len(toks)-3]), 64)
if err != nil {
return -1, -1, -1
return "unknown"
}
five, err := strconv.ParseFloat(noCommas.Replace(toks[len(toks)-2]), 64)
if err != nil {
return -1, -1, -1
}
fifteen, err := strconv.ParseFloat(noCommas.Replace(toks[len(toks)-1]), 64)
if err != nil {
return -1, -1, -1
matches := loadRe.FindAllStringSubmatch(string(out), -1)
if matches == nil || len(matches) < 1 || len(matches[0]) < 4 {
return "unknown"
}
return one, five, fifteen
return fmt.Sprintf("%s %s %s", matches[0][1], matches[0][2], matches[0][3])
}
Loading