/
mnr.go
138 lines (119 loc) · 3.91 KB
/
mnr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* skogul, M&R port collector sender
*
* Copyright (c) 2019 Telenor Norge AS
* Author(s):
* - Kristian Lyngstøl <kly@kly.no>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
package sender
import (
"bytes"
"fmt"
"net"
"github.com/telenornms/skogul"
)
var mnrLog = skogul.Logger("sender", "mnr")
/*
MnR sender writes to M&R port collector.
The output format is:
<timestamp>\t<groupname>\t<variable>\t<value>(\t<property>=<value>)*
Example:
1199145600 group myDevice.Variable1 100 device=myDevice name=MyVariable1
Two special metadata fields can be provided: "group" will set the M&R storage group,
and "prefix" will be used to prefix all individual data variables.
E.g:
{
"template": {
"timestamp": "2019-03-15T11:08:02+01:00",
"metadata": {
"server": "somewhere.example.com"
}
},
"metrics": [
{
"metadata": {
"prefix": "myDevice.",
"key": "value",
"paramkey": "paramvalue"
},
"data": {
"astring": "text",
"float": 1.11,
"integer": 5
}
}
]
}
Will result in:
1552644482 group myDevice.astring text key=value paramkey=paramvalue server=somewhere.example.com
1552644482 group myDevice.float 1.11 key=value paramkey=paramvalue server=somewhere.example.com
1552644482 group myDevice.integer 5 key=value paramkey=paramvalue server=somewhere.example.com
The default group is set to that of MnR DefaultGroup. If this is unset, the
default group is "group". Meaning:
- If metadata provides "group" key, this is used
- Otherwise, if DefaultGroup is set in MnR sender, this is used
- Otherwise, "group" is used.
*/
type MnR struct {
Address string `doc:"Address to send data to" example:"192.168.1.99:1234"`
DefaultGroup string `doc:"Default group to use if the metadatafield group is missing."`
}
/*
Send to MnR.
Implementation details: We need to write each value as its own variable to
MnR, so we start by constructing two buffers for what comes before and after
the key\tvalue, then iterate over m.Data.
Also, we open a new TCP connection for each call to Send() at the moment,
which is really suboptimal for large quantities of data, but ok for
occasional data dumps. If large metric containers are received, the cost will
be negligible. But this should, of course, be fixed in the future.
*/
func (mnr *MnR) Send(c *skogul.Container) error {
d, err := net.Dial("tcp", mnr.Address)
if err != nil {
return fmt.Errorf("unable to connect to MnR at %s: %w", mnr.Address, err)
}
for _, m := range c.Metrics {
var bufferpre bytes.Buffer
var bufferpost bytes.Buffer
fmt.Fprintf(&bufferpre, "%d\t", m.Time.Unix())
if m.Metadata["group"] == nil {
if mnr.DefaultGroup == "" {
fmt.Fprintf(&bufferpre, "group\t")
} else {
fmt.Fprintf(&bufferpre, "%s\t", mnr.DefaultGroup)
}
} else {
fmt.Fprintf(&bufferpre, "%s\t", m.Metadata["group"])
}
pre := ""
if m.Metadata["prefix"] != nil {
pre = m.Metadata["prefix"].(string)
}
for key, value := range m.Metadata {
if key != "prefix" && key != "group" {
fmt.Fprintf(&bufferpost, "\t%s=%v", key, value)
}
}
for key, value := range m.Data {
fmt.Fprintf(d, "%s%s%s\t%v\tname=%s%s\n", bufferpre.String(), pre, key, value, key, bufferpost.String())
}
}
d.Close()
return nil
}