router7/cmd/diagd/diagd.go

146 lines
3.3 KiB
Go
Raw Normal View History

2018-06-28 13:39:48 +02:00
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2018-06-03 20:04:11 +02:00
// Binary diagd provides automated network diagnostics.
package main
import (
"encoding/json"
2018-06-03 20:04:11 +02:00
"flag"
"fmt"
"html"
"io"
"log"
"net"
2018-06-03 20:04:11 +02:00
"net/http"
"os"
"os/signal"
"strings"
2018-06-03 20:04:11 +02:00
"sync"
"syscall"
"github.com/gokrazy/gokrazy"
2018-06-03 20:04:11 +02:00
2018-07-09 08:54:04 +02:00
"github.com/rtr7/router7/internal/diag"
"github.com/rtr7/router7/internal/multilisten"
2020-09-14 22:09:46 +02:00
_ "net/http/pprof"
2018-06-03 20:04:11 +02:00
)
var httpListeners = multilisten.NewPool()
func updateListeners() error {
hosts, err := gokrazy.PrivateInterfaceAddrs()
if err != nil {
return err
}
httpListeners.ListenAndServe(hosts, func(host string) multilisten.Listener {
return &http.Server{Addr: net.JoinHostPort(host, "7733")}
})
return nil
}
func dump(indent int, w io.Writer, re *diag.EvalResult) {
2018-06-03 20:04:11 +02:00
symbol := "✔"
if re.Error {
symbol = "✘"
}
fmt.Fprintf(w, "<li>\n%s%s %s: %s<ul>",
strings.Repeat(" ", indent),
symbol,
html.EscapeString(re.Name),
html.EscapeString(re.Status))
2018-06-03 20:04:11 +02:00
for _, ch := range re.Children {
dump(indent+1, w, ch)
2018-06-03 20:04:11 +02:00
}
fmt.Fprintf(w, "</ul></li>")
}
func firstError(re *diag.EvalResult) string {
if re.Error {
return fmt.Sprintf("%s: %s", re.Name, re.Status)
}
for _, ch := range re.Children {
if msg := firstError(ch); msg != "" {
return msg
}
}
return ""
}
2018-06-03 20:04:11 +02:00
func logic() error {
var (
ifname = flag.String("interface",
"uplink0",
"interface name to query")
)
2018-06-03 20:04:11 +02:00
const (
ip6allrouters = "ff02::2" // no /etc/hosts on gokrazy
)
flag.Parse()
uplink := *ifname
2018-06-03 20:04:11 +02:00
m := diag.NewMonitor(diag.Link(uplink).
Then(diag.DHCPv4().
Then(diag.Ping4Gateway().
Then(diag.TCP4("www.google.ch:80")))).
2018-06-03 20:04:11 +02:00
Then(diag.DHCPv6().
Then(diag.TCP6("lan0", "www.google.ch:80"))).
2018-06-03 20:04:11 +02:00
Then(diag.RouterAdvertisments(uplink).
Then(diag.Ping6Gateway().
Then(diag.TCP6(uplink, "www.google.ch:80")))).
2018-06-03 20:04:11 +02:00
Then(diag.Ping6("", ip6allrouters+"%"+uplink)))
var mu sync.Mutex
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
re := m.Evaluate()
mu.Unlock()
fmt.Fprintf(w, `<!DOCTYPE html><style type="text/css">ul { list-style-type: none; }</style><ul>`)
dump(0, w, re)
2018-06-03 20:04:11 +02:00
})
http.HandleFunc("/health.json", func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
re := m.Evaluate()
mu.Unlock()
reply := struct {
FirstError string `json:"first_error"`
}{
FirstError: firstError(re),
}
b, err := json.Marshal(&reply)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(b)
})
2018-06-27 19:45:55 +02:00
if err := updateListeners(); err != nil {
return err
}
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGUSR1)
for range ch {
if err := updateListeners(); err != nil {
log.Printf("updateListeners: %v", err)
}
}
return nil
2018-06-03 20:04:11 +02:00
}
func main() {
if err := logic(); err != nil {
log.Fatal(err)
}
}