Serge Bazanski | 8d7843c | 2018-10-04 10:37:36 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
Serge Bazanski | 4676508 | 2018-10-06 12:32:01 +0100 | [diff] [blame] | 3 | import ( |
Serge Bazanski | c7be4a1 | 2018-10-06 13:18:05 +0100 | [diff] [blame] | 4 | "context" |
| 5 | "fmt" |
Serge Bazanski | 4676508 | 2018-10-06 12:32:01 +0100 | [diff] [blame] | 6 | "net/http" |
Serge Bazanski | 69518ab | 2018-10-06 17:48:23 +0100 | [diff] [blame] | 7 | "sort" |
Serge Bazanski | de869df | 2018-10-06 13:55:49 +0100 | [diff] [blame] | 8 | "strings" |
Serge Bazanski | 8d7843c | 2018-10-04 10:37:36 +0100 | [diff] [blame] | 9 | |
Serge Bazanski | ab55cca | 2018-10-25 12:35:55 +0100 | [diff] [blame] | 10 | "code.hackerspace.pl/hscloud/go/mirko" |
Serge Bazanski | c7be4a1 | 2018-10-06 13:18:05 +0100 | [diff] [blame] | 11 | "github.com/gobuffalo/packr" |
Serge Bazanski | c7be4a1 | 2018-10-06 13:18:05 +0100 | [diff] [blame] | 12 | "github.com/q3k/statusz" |
Serge Bazanski | 69518ab | 2018-10-06 17:48:23 +0100 | [diff] [blame] | 13 | "vbom.ml/util/sortorder" |
Serge Bazanski | a758ef5 | 2018-10-06 17:54:25 +0100 | [diff] [blame] | 14 | |
Serge Bazanski | ab55cca | 2018-10-25 12:35:55 +0100 | [diff] [blame] | 15 | "code.hackerspace.pl/hscloud/go/svc/topo/graph" |
| 16 | "code.hackerspace.pl/hscloud/go/svc/topo/state" |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 17 | |
Serge Bazanski | 26f1ee8 | 2018-10-25 12:45:00 +0100 | [diff] [blame] | 18 | ipb "code.hackerspace.pl/hscloud/go/proto/infra" |
Serge Bazanski | 4676508 | 2018-10-06 12:32:01 +0100 | [diff] [blame] | 19 | ) |
| 20 | |
Serge Bazanski | 4676508 | 2018-10-06 12:32:01 +0100 | [diff] [blame] | 21 | type Service struct { |
Serge Bazanski | fa5d556 | 2018-10-14 08:39:30 -0700 | [diff] [blame] | 22 | gr *graph.Graph |
| 23 | stm *state.StateManager |
Serge Bazanski | 4676508 | 2018-10-06 12:32:01 +0100 | [diff] [blame] | 24 | } |
| 25 | |
Serge Bazanski | fa5d556 | 2018-10-14 08:39:30 -0700 | [diff] [blame] | 26 | func NewService(gr *graph.Graph, stm *state.StateManager) *Service { |
Serge Bazanski | 4676508 | 2018-10-06 12:32:01 +0100 | [diff] [blame] | 27 | return &Service{ |
Serge Bazanski | fa5d556 | 2018-10-14 08:39:30 -0700 | [diff] [blame] | 28 | gr: gr, |
| 29 | stm: stm, |
Serge Bazanski | 4676508 | 2018-10-06 12:32:01 +0100 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
Serge Bazanski | c7be4a1 | 2018-10-06 13:18:05 +0100 | [diff] [blame] | 33 | const topologyFragment = ` |
| 34 | <script src="/assets/viz.js"></script> |
| 35 | <script> |
| 36 | |
| 37 | var viz = new Viz({ workerURL: '/assets/full.render.js' }); |
| 38 | var xmlhttp = new XMLHttpRequest(); |
| 39 | xmlhttp.onreadystatechange = function() { |
| 40 | if (this.readyState == 4 && this.status == 200) { |
| 41 | var dot = this.responseText; |
| 42 | viz.renderSVGElement(dot) |
| 43 | .then(function(element) { |
Serge Bazanski | 69518ab | 2018-10-06 17:48:23 +0100 | [diff] [blame] | 44 | document.getElementById("graph").appendChild(element); |
Serge Bazanski | c7be4a1 | 2018-10-06 13:18:05 +0100 | [diff] [blame] | 45 | }); |
| 46 | } |
| 47 | }; |
| 48 | xmlhttp.open('GET', '/debug/graphviz'); |
| 49 | xmlhttp.send(); |
| 50 | |
| 51 | </script> |
Serge Bazanski | 69518ab | 2018-10-06 17:48:23 +0100 | [diff] [blame] | 52 | <div id="graph" style="text-align: center;"></div> |
Serge Bazanski | c7be4a1 | 2018-10-06 13:18:05 +0100 | [diff] [blame] | 53 | ` |
| 54 | |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 55 | const switchportsFragment = ` |
| 56 | <style type="text/css"> |
| 57 | .table td,th { |
| 58 | background-color: #eee; |
| 59 | padding: 0.2em 0.4em 0.2em 0.4em; |
| 60 | } |
| 61 | .table th { |
| 62 | background-color: #c0c0c0; |
| 63 | } |
| 64 | .table { |
| 65 | background-color: #fff; |
| 66 | border-spacing: 0.2em; |
| 67 | margin-left: auto; |
| 68 | margin-right: auto; |
| 69 | } |
| 70 | </style> |
| 71 | <div> |
| 72 | <table class="table"> |
| 73 | <tr> |
| 74 | <th>Switch</th> |
| 75 | <th>Port</th> |
| 76 | <th>Link State</th> |
| 77 | <th>Port Mode</th> |
| 78 | <th>MTU</th> |
| 79 | <th>Sync Status</th> |
| 80 | </tr> |
| 81 | {{range .Ports }} |
| 82 | {{ if .Managed }} |
| 83 | <tr> |
| 84 | {{ else }} |
| 85 | <tr style="opacity: 0.5"> |
| 86 | {{ end}} |
| 87 | <td style="text-align: right;">{{ .Switch }}</td> |
| 88 | <td>{{ .Name }}</td> |
| 89 | {{ if eq .State "DOWN" }} |
| 90 | <td style="background-color: #ff3030;">{{ .State }}</td> |
| 91 | {{ else }} |
| 92 | <td>{{ .State }}</td> |
| 93 | {{ end }} |
| 94 | <td>{{ .Mode }}</td> |
| 95 | <td>{{ .MTU }}</td> |
| 96 | {{ if .Managed }} |
| 97 | <td style="background-color: #30ff30;">OK</td> |
| 98 | {{ else }} |
| 99 | <td><i>Unmanaged</i></td> |
| 100 | {{ end }} |
| 101 | </tr> |
| 102 | {{end}} |
| 103 | </table> |
| 104 | </div> |
| 105 | ` |
| 106 | |
Serge Bazanski | fa5d556 | 2018-10-14 08:39:30 -0700 | [diff] [blame] | 107 | func (s *Service) Setup(m *mirko.Mirko) { |
Serge Bazanski | c7be4a1 | 2018-10-06 13:18:05 +0100 | [diff] [blame] | 108 | assets := packr.NewBox("./assets") |
Serge Bazanski | fa5d556 | 2018-10-14 08:39:30 -0700 | [diff] [blame] | 109 | m.HTTPMux().Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(assets))) |
| 110 | m.HTTPMux().HandleFunc("/debug/graphviz", s.httpHandleGraphviz) |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 111 | statusz.AddStatusPart("Switch Ports", switchportsFragment, s.statusHandleSwitchports) |
Serge Bazanski | c7be4a1 | 2018-10-06 13:18:05 +0100 | [diff] [blame] | 112 | statusz.AddStatusPart("Topology", topologyFragment, func(ctx context.Context) interface{} { |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 113 | return nil |
Serge Bazanski | c7be4a1 | 2018-10-06 13:18:05 +0100 | [diff] [blame] | 114 | }) |
Serge Bazanski | 8d7843c | 2018-10-04 10:37:36 +0100 | [diff] [blame] | 115 | } |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 116 | |
| 117 | func (s *Service) statusHandleSwitchports(ctx context.Context) interface{} { |
| 118 | managedPorts := make(map[string]bool) |
| 119 | s.gr.Mu.RLock() |
| 120 | for _, sw := range s.gr.Switches { |
| 121 | for _, port := range sw.Ports { |
| 122 | managedPorts[sw.Name+"|"+port.Name] = true |
| 123 | } |
| 124 | } |
| 125 | s.gr.Mu.RUnlock() |
| 126 | |
| 127 | s.stm.Mu.RLock() |
| 128 | defer s.stm.Mu.RUnlock() |
| 129 | |
| 130 | res := struct { |
| 131 | Ports []*struct { |
| 132 | Switch string |
| 133 | Name string |
| 134 | State string |
| 135 | Mode string |
| 136 | Managed bool |
| 137 | MTU string |
| 138 | } |
| 139 | }{} |
| 140 | for _, sw := range s.stm.Switches { |
| 141 | for _, po := range sw.Ports { |
| 142 | state := "INVALID" |
| 143 | switch po.Proto.LinkState { |
Serge Bazanski | 26f1ee8 | 2018-10-25 12:45:00 +0100 | [diff] [blame] | 144 | case ipb.SwitchPort_LINKSTATE_DOWN: |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 145 | state = "DOWN" |
Serge Bazanski | 26f1ee8 | 2018-10-25 12:45:00 +0100 | [diff] [blame] | 146 | case ipb.SwitchPort_LINKSTATE_UP: |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 147 | state = "UP" |
| 148 | } |
| 149 | mode := "INVALID" |
| 150 | switch po.Proto.PortMode { |
Serge Bazanski | 26f1ee8 | 2018-10-25 12:45:00 +0100 | [diff] [blame] | 151 | case ipb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED: |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 152 | mode = fmt.Sprintf("UNTAGGED (%d)", po.Proto.VlanNative) |
Serge Bazanski | 26f1ee8 | 2018-10-25 12:45:00 +0100 | [diff] [blame] | 153 | case ipb.SwitchPort_PORTMODE_SWITCHPORT_TAGGED: |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 154 | mode = fmt.Sprintf("TAGGED (%v)", po.Proto.VlanTagged) |
Serge Bazanski | 26f1ee8 | 2018-10-25 12:45:00 +0100 | [diff] [blame] | 155 | case ipb.SwitchPort_PORTMODE_SWITCHPORT_GENERIC: |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 156 | mode = "GENERIC" |
Serge Bazanski | 26f1ee8 | 2018-10-25 12:45:00 +0100 | [diff] [blame] | 157 | case ipb.SwitchPort_PORTMODE_ROUTED: |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 158 | mode = "ROUTED" |
Serge Bazanski | 26f1ee8 | 2018-10-25 12:45:00 +0100 | [diff] [blame] | 159 | case ipb.SwitchPort_PORTMODE_MANGLED: |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 160 | mode = "MANGLED" |
| 161 | } |
| 162 | |
| 163 | managed := managedPorts[sw.Name+"|"+po.Proto.Name] |
| 164 | res.Ports = append(res.Ports, &struct { |
| 165 | Switch string |
| 166 | Name string |
| 167 | State string |
| 168 | Mode string |
| 169 | Managed bool |
| 170 | MTU string |
| 171 | }{ |
| 172 | Switch: sw.Name, |
| 173 | Name: po.Proto.Name, |
| 174 | State: state, |
| 175 | Mode: mode, |
| 176 | Managed: managed, |
| 177 | MTU: fmt.Sprintf("%d", po.Proto.Mtu), |
| 178 | }) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return res |
| 183 | } |
| 184 | |
| 185 | func (s *Service) httpHandleGraphviz(w http.ResponseWriter, r *http.Request) { |
| 186 | fmt.Fprintf(w, "graph G {\n") |
| 187 | fmt.Fprintf(w, " ranksep = 2\n") |
| 188 | fmt.Fprintf(w, " splines = polyline\n") |
| 189 | fmt.Fprintf(w, " rankdir = LR\n") |
| 190 | for _, machine := range s.gr.Machines { |
| 191 | portNames := []string{} |
| 192 | for _, port := range machine.Ports { |
| 193 | name := fmt.Sprintf("<%s> %s", port.Name, port.Name) |
| 194 | portNames = append(portNames, name) |
| 195 | } |
| 196 | ports := strings.Join(portNames, "|") |
| 197 | fmt.Fprintf(w, " %s [shape=record label=\"{ %s | { %s }}\"]\n", machine.Name, machine.Name, ports) |
| 198 | } |
| 199 | for _, sw := range s.gr.Switches { |
| 200 | portNames := []string{} |
| 201 | portsOrdered := []*graph.SwitchPort{} |
| 202 | for _, port := range sw.Ports { |
| 203 | portsOrdered = append(portsOrdered, port) |
| 204 | } |
| 205 | sort.Slice(portsOrdered, func(i, j int) bool { |
| 206 | return sortorder.NaturalLess(portsOrdered[i].Name, portsOrdered[j].Name) |
| 207 | }) |
| 208 | for _, port := range portsOrdered { |
| 209 | name := fmt.Sprintf("<%s> %s", port.Name, port.Name) |
| 210 | portNames = append(portNames, name) |
| 211 | } |
| 212 | ports := strings.Join(portNames, "|") |
| 213 | fmt.Fprintf(w, " %s [shape=record label=\"{{ %s } | %s}\"]\n", sw.Name, ports, sw.Name) |
| 214 | } |
| 215 | for _, machine := range s.gr.Machines { |
| 216 | for _, port := range machine.Ports { |
| 217 | if port.OtherEnd == nil { |
| 218 | continue |
| 219 | } |
| 220 | fmt.Fprintf(w, " %s:%q:e -- %s:%q:w\n", machine.Name, port.Name, port.OtherEnd.Switch.Name, port.OtherEnd.Name) |
| 221 | } |
| 222 | } |
| 223 | fmt.Fprintf(w, "}\n") |
| 224 | } |