2015-12-29 03:08:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-02-25 20:06:37 +00:00
|
|
|
"encoding/json"
|
2016-02-19 10:30:42 +00:00
|
|
|
"flag"
|
2015-12-29 03:08:03 +00:00
|
|
|
"log"
|
2016-02-25 21:06:15 +00:00
|
|
|
"net"
|
2016-02-19 10:13:30 +00:00
|
|
|
"net/http"
|
2016-02-25 20:06:37 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/monitormap/micro-daemon/models"
|
|
|
|
"github.com/monitormap/micro-daemon/responed"
|
|
|
|
"github.com/monitormap/micro-daemon/websocketserver"
|
2015-12-29 13:05:47 +00:00
|
|
|
)
|
2016-02-25 20:06:37 +00:00
|
|
|
|
2016-02-19 10:30:42 +00:00
|
|
|
var (
|
2016-02-25 20:42:58 +00:00
|
|
|
wsserverForNodes = websocketserver.NewServer("/nodes")
|
|
|
|
responedDaemon *responed.Daemon
|
|
|
|
nodes = models.NewNodes()
|
|
|
|
aliases = models.NewNodes()
|
|
|
|
outputNodesFile string
|
|
|
|
outputAliasesFile string
|
|
|
|
collectInterval time.Duration
|
|
|
|
saveInterval time.Duration
|
2016-02-25 21:06:15 +00:00
|
|
|
listenPort string
|
|
|
|
listenAddr string
|
2016-02-19 10:30:42 +00:00
|
|
|
)
|
2016-02-25 20:06:37 +00:00
|
|
|
|
|
|
|
func main() {
|
2016-02-19 10:30:42 +00:00
|
|
|
var collectSeconds, saveSeconds int
|
|
|
|
|
2016-02-25 20:42:58 +00:00
|
|
|
flag.StringVar(&outputNodesFile, "output", "webroot/nodes.json", "path nodes.json file")
|
|
|
|
flag.StringVar(&outputAliasesFile, "aliases", "webroot/aliases.json", "path aliases.json file")
|
2016-02-25 21:06:15 +00:00
|
|
|
flag.StringVar(&listenPort, "p", "8080", "path aliases.json file")
|
|
|
|
flag.StringVar(&listenAddr, "h", "", "path aliases.json file")
|
2016-02-25 20:44:50 +00:00
|
|
|
flag.IntVar(&saveSeconds, "saveInterval", 60, "interval for data saving")
|
2016-02-25 20:42:58 +00:00
|
|
|
flag.IntVar(&collectSeconds, "collectInterval", 15, "interval for data collections")
|
2016-02-19 10:30:42 +00:00
|
|
|
flag.Parse()
|
2015-12-29 13:05:47 +00:00
|
|
|
|
2016-02-19 10:30:42 +00:00
|
|
|
collectInterval = time.Second * time.Duration(collectSeconds)
|
|
|
|
saveInterval = time.Second * time.Duration(saveSeconds)
|
|
|
|
|
2016-02-25 20:06:37 +00:00
|
|
|
go wsserverForNodes.Listen()
|
2016-02-25 20:42:58 +00:00
|
|
|
go nodes.Saver(outputNodesFile, saveInterval)
|
|
|
|
go aliases.Saver(outputAliasesFile, saveInterval)
|
2016-02-25 20:06:37 +00:00
|
|
|
responedDaemon = responed.NewDaemon(func(coll *responed.Collector, res *responed.Response) {
|
|
|
|
var result map[string]interface{}
|
|
|
|
json.Unmarshal(res.Raw, &result)
|
2016-02-19 10:30:42 +00:00
|
|
|
|
2016-02-25 20:06:37 +00:00
|
|
|
nodeID, _ := result["node_id"].(string)
|
|
|
|
|
|
|
|
if nodeID == "" {
|
|
|
|
log.Println("unable to parse node_id")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
node := nodes.Get(nodeID)
|
2015-12-29 13:05:47 +00:00
|
|
|
|
2016-02-25 20:06:37 +00:00
|
|
|
// Set result
|
|
|
|
elem := reflect.ValueOf(node).Elem()
|
|
|
|
field := elem.FieldByName(strings.Title(coll.CollectType))
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(field, result) {
|
2016-02-25 20:38:40 +00:00
|
|
|
log.Printf("Start sendAll after recieve %s", coll.CollectType)
|
|
|
|
log.Printf("Field: %V", field)
|
|
|
|
log.Printf("Result: %V", result)
|
2016-02-25 20:06:37 +00:00
|
|
|
wsserverForNodes.SendAll(node)
|
2016-02-25 20:38:40 +00:00
|
|
|
log.Print("End")
|
2016-02-25 20:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
field.Set(reflect.ValueOf(result))
|
|
|
|
})
|
|
|
|
go responedDaemon.ListenAndSend(collectInterval)
|
|
|
|
|
|
|
|
http.Handle("/", http.FileServer(http.Dir("webroot")))
|
|
|
|
//TODO bad
|
2016-02-25 21:06:15 +00:00
|
|
|
log.Fatal(http.ListenAndServe(net.JoinHostPort(listenAddr, listenPort), nil))
|
2016-02-25 20:06:37 +00:00
|
|
|
|
|
|
|
// Wait for End
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
sig := <-sigs
|
|
|
|
log.Println("received", sig)
|
|
|
|
|
|
|
|
// Close everything at the end
|
|
|
|
wsserverForNodes.Close()
|
|
|
|
responedDaemon.Close()
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|