blob: ec751530af37b2ed6d639f4756d16cc1b8bbd132 [file] [log] [blame]
Serge Bazanski8d7843c2018-10-04 10:37:36 +01001package main
2
Serge Bazanski46765082018-10-06 12:32:01 +01003import (
Serge Bazanskic7be4a12018-10-06 13:18:05 +01004 "context"
5 "fmt"
Serge Bazanski46765082018-10-06 12:32:01 +01006 "net/http"
Serge Bazanski69518ab2018-10-06 17:48:23 +01007 "sort"
Serge Bazanskide869df2018-10-06 13:55:49 +01008 "strings"
Serge Bazanski8d7843c2018-10-04 10:37:36 +01009
Serge Bazanskifa5d5562018-10-14 08:39:30 -070010 "code.hackerspace.pl/q3k/mirko"
Serge Bazanskic7be4a12018-10-06 13:18:05 +010011 "github.com/gobuffalo/packr"
Serge Bazanskic7be4a12018-10-06 13:18:05 +010012 "github.com/q3k/statusz"
Serge Bazanski69518ab2018-10-06 17:48:23 +010013 "vbom.ml/util/sortorder"
Serge Bazanskia758ef52018-10-06 17:54:25 +010014
15 "code.hackerspace.pl/q3k/topo/graph"
16 "code.hackerspace.pl/q3k/topo/state"
Serge Bazanski16e4ba22018-10-07 00:22:52 +010017
18 pb "code.hackerspace.pl/q3k/topo/proto/control"
Serge Bazanski46765082018-10-06 12:32:01 +010019)
20
Serge Bazanski46765082018-10-06 12:32:01 +010021type Service struct {
Serge Bazanskifa5d5562018-10-14 08:39:30 -070022 gr *graph.Graph
23 stm *state.StateManager
Serge Bazanski46765082018-10-06 12:32:01 +010024}
25
Serge Bazanskifa5d5562018-10-14 08:39:30 -070026func NewService(gr *graph.Graph, stm *state.StateManager) *Service {
Serge Bazanski46765082018-10-06 12:32:01 +010027 return &Service{
Serge Bazanskifa5d5562018-10-14 08:39:30 -070028 gr: gr,
29 stm: stm,
Serge Bazanski46765082018-10-06 12:32:01 +010030 }
31}
32
Serge Bazanskic7be4a12018-10-06 13:18:05 +010033const 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 Bazanski69518ab2018-10-06 17:48:23 +010044 document.getElementById("graph").appendChild(element);
Serge Bazanskic7be4a12018-10-06 13:18:05 +010045 });
46 }
47 };
48 xmlhttp.open('GET', '/debug/graphviz');
49 xmlhttp.send();
50
51 </script>
Serge Bazanski69518ab2018-10-06 17:48:23 +010052 <div id="graph" style="text-align: center;"></div>
Serge Bazanskic7be4a12018-10-06 13:18:05 +010053`
54
Serge Bazanski16e4ba22018-10-07 00:22:52 +010055const 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 Bazanskifa5d5562018-10-14 08:39:30 -0700107func (s *Service) Setup(m *mirko.Mirko) {
Serge Bazanskic7be4a12018-10-06 13:18:05 +0100108 assets := packr.NewBox("./assets")
Serge Bazanskifa5d5562018-10-14 08:39:30 -0700109 m.HTTPMux().Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(assets)))
110 m.HTTPMux().HandleFunc("/debug/graphviz", s.httpHandleGraphviz)
Serge Bazanski16e4ba22018-10-07 00:22:52 +0100111 statusz.AddStatusPart("Switch Ports", switchportsFragment, s.statusHandleSwitchports)
Serge Bazanskic7be4a12018-10-06 13:18:05 +0100112 statusz.AddStatusPart("Topology", topologyFragment, func(ctx context.Context) interface{} {
Serge Bazanski16e4ba22018-10-07 00:22:52 +0100113 return nil
Serge Bazanskic7be4a12018-10-06 13:18:05 +0100114 })
Serge Bazanski8d7843c2018-10-04 10:37:36 +0100115}
Serge Bazanski16e4ba22018-10-07 00:22:52 +0100116
117func (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 {
144 case pb.SwitchPort_LINKSTATE_DOWN:
145 state = "DOWN"
146 case pb.SwitchPort_LINKSTATE_UP:
147 state = "UP"
148 }
149 mode := "INVALID"
150 switch po.Proto.PortMode {
151 case pb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED:
152 mode = fmt.Sprintf("UNTAGGED (%d)", po.Proto.VlanNative)
153 case pb.SwitchPort_PORTMODE_SWITCHPORT_TAGGED:
154 mode = fmt.Sprintf("TAGGED (%v)", po.Proto.VlanTagged)
155 case pb.SwitchPort_PORTMODE_SWITCHPORT_GENERIC:
156 mode = "GENERIC"
157 case pb.SwitchPort_PORTMODE_ROUTED:
158 mode = "ROUTED"
159 case pb.SwitchPort_PORTMODE_MANGLED:
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
185func (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}