callgraph/sysdig/graph.go

42 lines
856 B
Go
Raw Normal View History

2016-04-13 12:17:46 +00:00
package sysdig
import (
"fmt"
"net"
)
type Records []Record
type Service struct {
Name string
Ips []net.IP
}
type Connection struct {
From string
To string
}
func (records Records) PrintGraph(services []Service) {
serviceMap := make(map[string]string)
for _, service := range services {
for _, ip := range service.Ips {
serviceMap[ip.String()] = service.Name
}
}
connections := make(map[string]Connection)
for _, r := range records {
if clientService, ok := serviceMap[r.ClientIp.String()]; ok {
if serverService, ok := serviceMap[r.ServerIp.String()]; ok {
connections[clientService+":"+serverService] = Connection{clientService, serverService}
}
}
}
fmt.Printf("digraph G {\n")
for _, connection := range connections {
fmt.Printf("\"%s\" -> \"%s\"\n", connection.From, connection.To)
}
fmt.Printf("}\n")
}