-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
236 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,31 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/naoina/toml" | ||
|
||
"github.com/FreifunkBremen/yanic/database" | ||
"github.com/FreifunkBremen/yanic/respond" | ||
"github.com/FreifunkBremen/yanic/runtime" | ||
"github.com/FreifunkBremen/yanic/webserver" | ||
) | ||
|
||
// Config represents the whole configuration | ||
type Config struct { | ||
Respondd respond.Config | ||
Webserver webserver.Config | ||
Nodes runtime.NodesConfig | ||
Database database.Config | ||
} | ||
|
||
var ( | ||
configPath string | ||
collector *respond.Collector | ||
nodes *runtime.Nodes | ||
) | ||
|
||
func loadConfig() *Config { | ||
config, err := ReadConfigFile(configPath) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, "unable to load config file:", err) | ||
os.Exit(2) | ||
} | ||
return config | ||
} | ||
|
||
// ReadConfigFile reads a config model from path of a yml file | ||
func ReadConfigFile(path string) (config *Config, err error) { | ||
config = &Config{} | ||
|
||
func ReadConfigFile(path string, config interface{}) error { | ||
file, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
return err | ||
} | ||
|
||
err = toml.Unmarshal(file, config) | ||
if err != nil { | ||
return nil, err | ||
return err | ||
} | ||
|
||
return | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/bdlm/log" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/FreifunkBremen/yanic/respond" | ||
) | ||
|
||
// serveCmd represents the serve command | ||
var responddCMD = &cobra.Command{ | ||
Use: "respondd", | ||
Short: "Runs a respond daemon", | ||
Example: "yanic respondd --config /etc/respondd.toml", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
daemon := &respond.Daemon{} | ||
if err := ReadConfigFile(configPath, daemon); err != nil { | ||
log.Panicf("unable to load config file: %s", err) | ||
} | ||
|
||
go daemon.Start() | ||
|
||
log.Info("respondd daemon started") | ||
// Wait for INT/TERM | ||
sigs := make(chan os.Signal, 1) | ||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) | ||
sig := <-sigs | ||
log.Infof("received %s", sig) | ||
|
||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(responddCMD) | ||
responddCMD.Flags().StringVarP(&configPath, "config", "c", "config-respondd.toml", "Path to configuration file") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
config-respondd_example.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
multi_instance = false | ||
data_interval = "3m" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package respond | ||
|
||
import ( | ||
"time" | ||
"os" | ||
"io/ioutil" | ||
"strings" | ||
"net" | ||
|
||
"github.com/bdlm/log" | ||
|
||
"github.com/FreifunkBremen/yanic/data" | ||
"github.com/FreifunkBremen/yanic/lib/duration" | ||
) | ||
|
||
type Daemon struct { | ||
MultiInstance bool `toml:"multi_instance"` | ||
DataInterval duration.Duration `toml:"data_interval"` | ||
Data *data.ResponseData `toml:"data"` | ||
} | ||
|
||
func (d *Daemon) Start() { | ||
if d.Data == nil { | ||
d.Data = &data.ResponseData{} | ||
} | ||
|
||
d.updateData() | ||
go d.updateWorker() | ||
|
||
socket, err := net.ListenMulticastUDP("udp6", nil, &net.UDPAddr{ | ||
IP: net.ParseIP(multicastAddressDefault), | ||
Port: port, | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
socket.SetReadBuffer(maxDataGramSize) | ||
|
||
// Loop forever reading from the socket | ||
for { | ||
buf := make([]byte, maxDataGramSize) | ||
n, src, err := socket.ReadFromUDP(buf) | ||
if err != nil { | ||
log.Errorf("ReadFromUDP failed: %s", err) | ||
} | ||
raw := make([]byte, n) | ||
copy(raw, buf) | ||
log.WithFields(map[string]interface{}{ | ||
"bytes": n, | ||
"data": string(raw), | ||
"src": src.String(), | ||
}).Debug("recieve request") | ||
|
||
// TODO handle single request | ||
|
||
res, err := NewRespone(d.Data, src) | ||
if err != nil { | ||
log.Errorf("Decode failed: %s", err) | ||
}else{ | ||
n, err := socket.WriteToUDP(res.Raw, res.Address); | ||
if err != nil { | ||
log.Errorf("WriteToUDP failed: %s", err) | ||
} else { | ||
log.WithFields(map[string]interface{}{ | ||
"bytes": n, | ||
"dest": res.Address.String(), | ||
}).Debug("send respond") | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
|
||
func trim(s string) string { | ||
return strings.TrimSpace(strings.Trim(s, "\n")) | ||
} | ||
|
||
func (d *Daemon) updateWorker() { | ||
c := time.Tick(d.DataInterval.Duration) | ||
|
||
for range c { | ||
d.updateData() | ||
} | ||
} | ||
|
||
func (d *Daemon) updateData() { | ||
nodeID := "" | ||
// Nodeinfo | ||
if d.Data.Nodeinfo == nil { | ||
d.Data.Nodeinfo = &data.Nodeinfo{} | ||
} else { | ||
nodeID = d.Data.Nodeinfo.NodeID | ||
} | ||
if d.Data.Nodeinfo.Hostname == "" { | ||
d.Data.Nodeinfo.Hostname, _ = os.Hostname() | ||
} | ||
|
||
// Statistics | ||
if d.Data.Statistics == nil { | ||
d.Data.Statistics = &data.Statistics{} | ||
} else if nodeID == "" { | ||
nodeID = d.Data.Statistics.NodeID | ||
} | ||
|
||
|
||
// Neighbours | ||
if d.Data.Neighbours == nil { | ||
d.Data.Neighbours = &data.Neighbours{} | ||
} else if nodeID == "" { | ||
nodeID = d.Data.Neighbours.NodeID | ||
} | ||
|
||
|
||
|
||
if nodeID == "" { | ||
if v, err := ioutil.ReadFile("/etc/machine-id"); err == nil { | ||
nodeID = trim(string(v))[:12] | ||
} | ||
} | ||
d.Data.Nodeinfo.NodeID = nodeID | ||
d.Data.Statistics.NodeID = nodeID | ||
d.Data.Neighbours.NodeID = nodeID | ||
} |
Oops, something went wrong.