diff --git a/config_example.toml b/config_example.toml index c23da7c..2f18e45 100644 --- a/config_example.toml +++ b/config_example.toml @@ -23,7 +23,7 @@ domains = ["city"] # name of interface on which this collector is running ifname = "br-ffhb" # ip address which is used for sending -# (optional - without definition used a address of ifname - prefered link local) +# (optional - without definition used a address of ifname - preferred link local) ip_address = "fd2f:5119:f2d::5" # disable sending multicast respondd request # (for receiving only respondd packages e.g. database respondd) diff --git a/docs/docs_configuration.md b/docs/docs_configuration.md index 06e3658..aedb41d 100644 --- a/docs/docs_configuration.md +++ b/docs/docs_configuration.md @@ -110,7 +110,7 @@ ifname = "br-ffhb" {% method %} ip address is the own address which is used for sending. If not set or set with empty string it will take an address of ifname. -(It preferes the link local address, so at babel mesh-network it should be configurated) +(It prefers the link local address, so at babel mesh-network it should be configurated) {% sample lang="toml" %} ```toml ip_address = "fe80::..." @@ -521,6 +521,21 @@ path = "/var/www/html/meshviewer/data/nodelist.json" ``` {% endmethod %} +## [[nodes.output.raw]] +{% method %} +This output takes the respondd response as sent by the node and includes it in a JSON document. +{% endmethod %} + + +### path +{% method %} +The path, where to store raw.json +{% sample lang="toml" %} +```toml +path = "/var/www/html/meshviewer/data/raw.json" +``` +{% endmethod %} + ## [database] diff --git a/output/all/main.go b/output/all/main.go index aa104ea..0934cdc 100644 --- a/output/all/main.go +++ b/output/all/main.go @@ -5,4 +5,5 @@ import ( _ "github.com/FreifunkBremen/yanic/output/meshviewer" _ "github.com/FreifunkBremen/yanic/output/meshviewer-ffrgb" _ "github.com/FreifunkBremen/yanic/output/nodelist" + _ "github.com/FreifunkBremen/yanic/output/raw" ) diff --git a/output/raw/output.go b/output/raw/output.go new file mode 100644 index 0000000..9c5fcbc --- /dev/null +++ b/output/raw/output.go @@ -0,0 +1,46 @@ +package raw + +import ( + "errors" + + "github.com/FreifunkBremen/yanic/output" + "github.com/FreifunkBremen/yanic/runtime" +) + +type Output struct { + output.Output + path string +} + +type Config map[string]interface{} + +func (c Config) Path() string { + if path, ok := c["path"]; ok { + return path.(string) + } + return "" +} + +func init() { + output.RegisterAdapter("raw", Register) +} + +func Register(configuration map[string]interface{}) (output.Output, error) { + var config Config + config = configuration + + if path := config.Path(); path != "" { + return &Output{ + path: path, + }, nil + } + return nil, errors.New("no path given") + +} + +func (o *Output) Save(nodes *runtime.Nodes) { + nodes.RLock() + defer nodes.RUnlock() + + runtime.SaveJSON(transform(nodes), o.path) +} diff --git a/output/raw/output_test.go b/output/raw/output_test.go new file mode 100644 index 0000000..925418d --- /dev/null +++ b/output/raw/output_test.go @@ -0,0 +1,28 @@ +package raw + +import ( + "os" + "testing" + + "github.com/FreifunkBremen/yanic/runtime" + "github.com/stretchr/testify/assert" +) + +func TestOutput(t *testing.T) { + assert := assert.New(t) + + out, err := Register(map[string]interface{}{}) + assert.Error(err) + assert.Nil(out) + + out, err = Register(map[string]interface{}{ + "path": "/tmp/raw.json", + }) + os.Remove("/tmp/raw.json") + assert.NoError(err) + assert.NotNil(out) + + out.Save(&runtime.Nodes{}) + _, err = os.Stat("/tmp/raw.json") + assert.NoError(err) +} diff --git a/output/raw/raw.go b/output/raw/raw.go new file mode 100644 index 0000000..15dad5e --- /dev/null +++ b/output/raw/raw.go @@ -0,0 +1,45 @@ +package raw + +import ( + "github.com/FreifunkBremen/yanic/data" + "github.com/FreifunkBremen/yanic/lib/jsontime" + "github.com/FreifunkBremen/yanic/runtime" +) + +// Node struct +type RawNode struct { + Firstseen jsontime.Time `json:"firstseen"` + Lastseen jsontime.Time `json:"lastseen"` + Online bool `json:"online"` + Statistics *data.Statistics `json:"statistics"` + Nodeinfo *data.Nodeinfo `json:"nodeinfo"` + Neighbours *data.Neighbours `json:"neighbours"` +} + +type NodeList struct { + Version string `json:"version"` + Timestamp jsontime.Time `json:"updated_at"` // Timestamp of the generation + List []*RawNode `json:"nodes"` +} + +func transform(nodes *runtime.Nodes) *NodeList { + nodelist := &NodeList{ + Version: "1.0.0", + Timestamp: jsontime.Now(), + } + + for _, nodeOrigin := range nodes.List { + if nodeOrigin != nil { + node := &RawNode{ + Firstseen: nodeOrigin.Firstseen, + Lastseen: nodeOrigin.Lastseen, + Online: nodeOrigin.Online, + Statistics: nodeOrigin.Statistics, + Nodeinfo: nodeOrigin.Nodeinfo, + Neighbours: nodeOrigin.Neighbours, + } + nodelist.List = append(nodelist.List, node) + } + } + return nodelist +} diff --git a/output/raw/raw_test.go b/output/raw/raw_test.go new file mode 100644 index 0000000..567ccad --- /dev/null +++ b/output/raw/raw_test.go @@ -0,0 +1,67 @@ +package raw + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/FreifunkBremen/yanic/data" + "github.com/FreifunkBremen/yanic/runtime" +) + +func TestTransform(t *testing.T) { + nodes := transform(createTestNodes()) + + assert := assert.New(t) + assert.Len(nodes.List, 3) +} + +func createTestNodes() *runtime.Nodes { + nodes := runtime.NewNodes(&runtime.NodesConfig{}) + + nodeData := &runtime.Node{ + Statistics: &data.Statistics{ + Clients: data.Clients{ + Total: 23, + }, + }, + Nodeinfo: &data.Nodeinfo{ + NodeID: "abcdef012345", + Hardware: data.Hardware{ + Model: "TP-Link 841", + }, + }, + } + nodeData.Nodeinfo.Software.Firmware.Release = "2016.1.6+entenhausen1" + nodes.AddNode(nodeData) + + nodes.AddNode(&runtime.Node{ + Statistics: &data.Statistics{ + Clients: data.Clients{ + Total: 2, + }, + }, + Nodeinfo: &data.Nodeinfo{ + NodeID: "112233445566", + Hardware: data.Hardware{ + Model: "TP-Link 841", + }, + Location: &data.Location{ + Latitude: 23, + Longitude: 2, + }, + }, + }) + + nodes.AddNode(&runtime.Node{ + Nodeinfo: &data.Nodeinfo{ + NodeID: "0xdeadbeef0x", + VPN: true, + Hardware: data.Hardware{ + Model: "Xeon Multi-Core", + }, + }, + }) + + return nodes +}