commit 1364a13a8d9950078b37c7d2f5ef4f032a30529b Author: Jörg Thalheim Date: Fri Oct 4 00:19:10 2013 +0200 first commit diff --git a/assets/app.css b/assets/app.css new file mode 100644 index 0000000..5cb313e --- /dev/null +++ b/assets/app.css @@ -0,0 +1,3 @@ +.loading { + cursor: progress; +} diff --git a/assets/app.js b/assets/app.js new file mode 100644 index 0000000..af6f9f9 --- /dev/null +++ b/assets/app.js @@ -0,0 +1,23 @@ +$(function() { + $("#nodeTable tr[data-node]").each(function(){ + var $this = $(this); + var node = $this.data("node"); + $.getJSON("/status/"+node, function (data) { + $this.find("span.loading").hide(); + var label; + if (data["Status"] === "UP") { + label = "label-success"; + $this.addClass("success"); + $this.find("td.ping").text(data["Ping"] + "ms"); + } else if (data["Status"] === "SLOW") { + label = "label-warning"; + $this.addClass("warning"); + $this.find("td.ping").text(data["Ping"] + "ms"); + } else { + label = "label-danger"; + $this.addClass("danger"); + } + $this.find("span.status").addClass(label).text(data["Status"]); + }) + }); +}) diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..ef80076 --- /dev/null +++ b/html/index.html @@ -0,0 +1,37 @@ + + + + {{.Title}} + + + + + + + + + +
+ +
{{.Title}}
+ + + + + + + {{range $node := .Nodes}} + + + + + + {{end}} +
HostStatusPing
{{$node}} + Loading... + + + - +
+
+ diff --git a/nodes.json b/nodes.json new file mode 100644 index 0000000..f435e77 --- /dev/null +++ b/nodes.json @@ -0,0 +1 @@ +["matchbox.vpn", "turingmachine.vpn", "higgsboson.vpn", "devkid-router.vpn", "devkid-desktop.vpn", "devkid-server.vpn", "devkid-nas.vpn"] diff --git a/server.go b/server.go new file mode 100644 index 0000000..45c36ea --- /dev/null +++ b/server.go @@ -0,0 +1,111 @@ +package main + +import ( + "fmt" + "net/http" + "html/template" + "os/exec" + "os" + "regexp" + "strconv" + "bytes" + "encoding/json" +) + +var indexTemplate = template.Must(template.ParseFiles("html/index.html")) +var nodeList = []string{"higgsboson.vpn","turingmachine.vpn"} + +func stringInSlice(a string, list []string) bool { + for _, b := range list { + if b == a { + return true + } + } + return false +} + +type IndexPage struct { + Title string + Nodes []string +} + +type Status int + +const ( + On Status = iota + Off + Slow +) + +func indexHandler(w http.ResponseWriter, r *http.Request) { + indexTemplate.Execute(w, &IndexPage{Title: "VPN Node Status", Nodes: nodeList}) +} + +func ping(node string) (int64) { + cmd := exec.Command("fping", "-e", node) + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err == nil { + match := regexp.MustCompile("\\d+").Find(out.Bytes()) + if (match != nil) { + time, _ := strconv.ParseInt(string(match), 10, 0); + return time; + } + return -1 + } else { + return -1 + } +} + +type NodeStatus struct { + Domain string + Status string + Ping int64 +} + +const lenPath = len("/status/") +func statusHandler(w http.ResponseWriter, r *http.Request) { + node := r.URL.Path[lenPath:]; + if ! stringInSlice(node, nodeList) { + w.WriteHeader(http.StatusNotFound) + } else { + var state string + elapsed := ping(node); + if elapsed == -1 { + state = "DOWN" + } else if elapsed > 200 { + state = "SLOW" + } else { + state = "UP" + } + status := &NodeStatus{Domain: node, Status: state, Ping: elapsed} + s, err := json.Marshal(status) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + fmt.Printf("error while serializing json %s", err) + } else { + w.Write(s) + } + } +} + +func main() { + config, err := os.Open("./nodes.json") + if err != nil { + fmt.Printf("Cannot open nodes.json: '%s'", err) + os.Exit(1); + } + jsonParser := json.NewDecoder(config) + + if err = jsonParser.Decode(&nodeList); err != nil { + fmt.Printf("parsing config file: '%s'", err.Error()) + os.Exit(1); + } + + http.HandleFunc("/status/", statusHandler) + http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./assets/")))) + http.HandleFunc("/", indexHandler) + http.ListenAndServe(":8080", nil) + fmt.Printf("Running on 8080") +}