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 | |
Sergiusz Bazanski | 838cf65 | 2019-07-16 23:31:25 +0200 | [diff] [blame] | 10 | "vbom.ml/util/sortorder" |
| 11 | |
Serge Bazanski | ab55cca | 2018-10-25 12:35:55 +0100 | [diff] [blame] | 12 | "code.hackerspace.pl/hscloud/go/mirko" |
Serge Bazanski | 8fab2be | 2018-10-25 23:37:37 +0200 | [diff] [blame] | 13 | "code.hackerspace.pl/hscloud/go/statusz" |
Sergiusz Bazanski | 838cf65 | 2019-07-16 23:31:25 +0200 | [diff] [blame] | 14 | |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 15 | dpb "code.hackerspace.pl/hscloud/dc/proto" |
Sergiusz Bazanski | 61594bb | 2019-07-21 15:20:51 +0200 | [diff] [blame] | 16 | "code.hackerspace.pl/hscloud/dc/topo/assets" |
| 17 | "code.hackerspace.pl/hscloud/dc/topo/graph" |
| 18 | "code.hackerspace.pl/hscloud/dc/topo/state" |
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) { |
Sergiusz Bazanski | 838cf65 | 2019-07-16 23:31:25 +0200 | [diff] [blame] | 108 | m.HTTPMux().Handle("/assets/", http.StripPrefix("/assets/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 109 | data, ok := assets.Data[r.RequestURI] |
| 110 | if !ok { |
| 111 | http.NotFound(w, r) |
| 112 | return |
| 113 | } |
| 114 | |
| 115 | if strings.HasSuffix(r.RequestURI, ".js") { |
| 116 | w.Header().Set("Content-Type", "text/javascript") |
| 117 | } |
| 118 | |
| 119 | w.Write(data) |
| 120 | }))) |
Serge Bazanski | fa5d556 | 2018-10-14 08:39:30 -0700 | [diff] [blame] | 121 | m.HTTPMux().HandleFunc("/debug/graphviz", s.httpHandleGraphviz) |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 122 | statusz.AddStatusPart("Switch Ports", switchportsFragment, s.statusHandleSwitchports) |
Serge Bazanski | c7be4a1 | 2018-10-06 13:18:05 +0100 | [diff] [blame] | 123 | statusz.AddStatusPart("Topology", topologyFragment, func(ctx context.Context) interface{} { |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 124 | return nil |
Serge Bazanski | c7be4a1 | 2018-10-06 13:18:05 +0100 | [diff] [blame] | 125 | }) |
Serge Bazanski | 8d7843c | 2018-10-04 10:37:36 +0100 | [diff] [blame] | 126 | } |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 127 | |
| 128 | func (s *Service) statusHandleSwitchports(ctx context.Context) interface{} { |
| 129 | managedPorts := make(map[string]bool) |
| 130 | s.gr.Mu.RLock() |
| 131 | for _, sw := range s.gr.Switches { |
| 132 | for _, port := range sw.Ports { |
| 133 | managedPorts[sw.Name+"|"+port.Name] = true |
| 134 | } |
| 135 | } |
| 136 | s.gr.Mu.RUnlock() |
| 137 | |
| 138 | s.stm.Mu.RLock() |
| 139 | defer s.stm.Mu.RUnlock() |
| 140 | |
| 141 | res := struct { |
| 142 | Ports []*struct { |
| 143 | Switch string |
| 144 | Name string |
| 145 | State string |
| 146 | Mode string |
| 147 | Managed bool |
| 148 | MTU string |
| 149 | } |
| 150 | }{} |
| 151 | for _, sw := range s.stm.Switches { |
| 152 | for _, po := range sw.Ports { |
| 153 | state := "INVALID" |
| 154 | switch po.Proto.LinkState { |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 155 | case dpb.SwitchPort_LINKSTATE_DOWN: |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 156 | state = "DOWN" |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 157 | case dpb.SwitchPort_LINKSTATE_UP: |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 158 | state = "UP" |
| 159 | } |
| 160 | mode := "INVALID" |
| 161 | switch po.Proto.PortMode { |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 162 | case dpb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED: |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 163 | mode = fmt.Sprintf("UNTAGGED (%d)", po.Proto.VlanNative) |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 164 | case dpb.SwitchPort_PORTMODE_SWITCHPORT_TAGGED: |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 165 | mode = fmt.Sprintf("TAGGED (%v)", po.Proto.VlanTagged) |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 166 | case dpb.SwitchPort_PORTMODE_SWITCHPORT_GENERIC: |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 167 | mode = "GENERIC" |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 168 | case dpb.SwitchPort_PORTMODE_ROUTED: |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 169 | mode = "ROUTED" |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 170 | case dpb.SwitchPort_PORTMODE_MANGLED: |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 171 | mode = "MANGLED" |
| 172 | } |
| 173 | |
| 174 | managed := managedPorts[sw.Name+"|"+po.Proto.Name] |
| 175 | res.Ports = append(res.Ports, &struct { |
| 176 | Switch string |
| 177 | Name string |
| 178 | State string |
| 179 | Mode string |
| 180 | Managed bool |
| 181 | MTU string |
| 182 | }{ |
| 183 | Switch: sw.Name, |
| 184 | Name: po.Proto.Name, |
| 185 | State: state, |
| 186 | Mode: mode, |
| 187 | Managed: managed, |
| 188 | MTU: fmt.Sprintf("%d", po.Proto.Mtu), |
| 189 | }) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return res |
| 194 | } |
| 195 | |
| 196 | func (s *Service) httpHandleGraphviz(w http.ResponseWriter, r *http.Request) { |
| 197 | fmt.Fprintf(w, "graph G {\n") |
| 198 | fmt.Fprintf(w, " ranksep = 2\n") |
| 199 | fmt.Fprintf(w, " splines = polyline\n") |
| 200 | fmt.Fprintf(w, " rankdir = LR\n") |
| 201 | for _, machine := range s.gr.Machines { |
| 202 | portNames := []string{} |
| 203 | for _, port := range machine.Ports { |
| 204 | name := fmt.Sprintf("<%s> %s", port.Name, port.Name) |
| 205 | portNames = append(portNames, name) |
| 206 | } |
| 207 | ports := strings.Join(portNames, "|") |
| 208 | fmt.Fprintf(w, " %s [shape=record label=\"{ %s | { %s }}\"]\n", machine.Name, machine.Name, ports) |
| 209 | } |
| 210 | for _, sw := range s.gr.Switches { |
| 211 | portNames := []string{} |
| 212 | portsOrdered := []*graph.SwitchPort{} |
| 213 | for _, port := range sw.Ports { |
| 214 | portsOrdered = append(portsOrdered, port) |
| 215 | } |
| 216 | sort.Slice(portsOrdered, func(i, j int) bool { |
| 217 | return sortorder.NaturalLess(portsOrdered[i].Name, portsOrdered[j].Name) |
| 218 | }) |
| 219 | for _, port := range portsOrdered { |
| 220 | name := fmt.Sprintf("<%s> %s", port.Name, port.Name) |
| 221 | portNames = append(portNames, name) |
| 222 | } |
| 223 | ports := strings.Join(portNames, "|") |
| 224 | fmt.Fprintf(w, " %s [shape=record label=\"{{ %s } | %s}\"]\n", sw.Name, ports, sw.Name) |
| 225 | } |
| 226 | for _, machine := range s.gr.Machines { |
| 227 | for _, port := range machine.Ports { |
| 228 | if port.OtherEnd == nil { |
| 229 | continue |
| 230 | } |
| 231 | fmt.Fprintf(w, " %s:%q:e -- %s:%q:w\n", machine.Name, port.Name, port.OtherEnd.Switch.Name, port.OtherEnd.Name) |
| 232 | } |
| 233 | } |
| 234 | fmt.Fprintf(w, "}\n") |
| 235 | } |