-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
230 lines (204 loc) · 5.16 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/spf13/viper"
"github.com/vjeantet/grok"
)
type Meta struct {
Pattern, Key, Good, Tags string
Valid map[string]string
}
// Translate controls how the map values are converted to a Point
type Translate struct {
Pattern string // Pattern to apply when parsing data
Key string // Lookup the value of Key, if found use it as point name, otherwise use Key itself
TSLayout string // Timestamp Layout for parsing
TSField string // Field to be parsed for timestamp
TFields string // Space delimited list of tag fields to use
VFields string // Space delimited list of fiedlds to use for values
}
var (
debug bool
listen bool
file string
directory = "patterns"
tcp = syslogPort
udp = syslogPort
ip = "0.0.0.0"
filters []Meta
batchSize = 64
queueSize = 8192
period = 60
ErrNoMatch = fmt.Errorf("No match")
)
const (
syslogPort = 514
)
func init() {
flag.BoolVar(&debug, "d", debug, "debug")
flag.BoolVar(&listen, "l", listen, "listen for syslog messages")
flag.StringVar(&file, "f", file, "file to process")
flag.StringVar(&ip, "i", ip, "ip to bind to")
flag.IntVar(&tcp, "t", syslogPort, "tcp port")
flag.IntVar(&udp, "u", syslogPort, "udp port")
flag.StringVar(&directory, "p", directory, "Patterns dir")
viper.AddConfigPath(".")
viper.SetConfigName("config")
viper.SetConfigType("json")
viper.SetDefault("Influxdb.Batch.RetentionPolicy", "default")
}
func unquoted(s string) string {
if len(s) > 0 && s[0] == '"' {
return s[1 : len(s)-1]
}
return s
}
// pretty print for debugging
func pp(x interface{}) string {
b, _ := json.MarshalIndent(x, " ", " ")
return fmt.Sprintln(string(b))
}
// numerical returns the parsed data type in its native form
func numerical(s string) interface{} {
if f, err := strconv.ParseFloat(s, 64); err == nil {
return f
}
if i, err := strconv.ParseInt(s, 0, 64); err == nil {
return i
}
return s
}
// AddPoint will convert the values of m into an influxdb Point and queue it to send
func AddPoint(m, tags map[string]string, t Translate, send Sender) (err error) {
found, ok := m[t.Key]
if !ok {
found = t.Key
}
for _, tag := range strings.Fields(t.TFields) {
tags[tag] = m[tag]
}
f := make(map[string]interface{})
for _, val := range strings.Fields(t.VFields) {
f[val] = numerical(m[val])
}
var ts time.Time
if len(t.TSLayout) > 0 && len(t.TSField) > 0 {
if ts, err = time.Parse(t.TSLayout, m[t.TSField]); err != nil {
return err
}
} else {
ts = time.Now()
}
return send(found, tags, f, ts)
}
// refine will check to see if it is selected, then apply regex and capture
func refine(g *grok.Grok, data map[string]string, trans *Translate, valid map[string]string, ptrn, key, good, tags string) error {
if valid != nil {
for k, v := range valid {
if data[k] != v {
return ErrNoMatch
}
}
}
if len(ptrn) > 0 {
matched, err := g.Parse(ptrn, data[key])
if err != nil {
return err
}
// TODO: this can overwrite existing values. Bug or Feature?
for k, v := range matched {
data[k] = unquoted(v)
}
}
trans.VFields += " " + good
trans.TFields += " " + tags
return nil
}
// Parse will apply a filter and submit results to influxdb as a data point
func Parse(trans Translate, tags map[string]string, reader *bufio.Reader, s Sender) error {
g, _ := grok.New()
if len(directory) > 0 {
if err := g.AddPatternsFromPath(directory); err != nil {
return err
}
}
process := func(m map[string]string) error {
if debug {
for k, v := range m {
fmt.Println("Key:", k, "Value:", v)
}
}
var err error
t := trans
for _, meta := range filters {
if err = refine(g, m, &t, meta.Valid, meta.Pattern, meta.Key, meta.Good, meta.Tags); err == nil {
break
}
}
if err != nil {
return err
}
return AddPoint(m, tags, t, s)
}
return g.ParseStream(reader, trans.Pattern, process)
}
func parseFile(file string, trans Translate, tags map[string]string, sender Sender) error {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
reader := bufio.NewReader(f)
return Parse(trans, tags, reader, sender)
}
func main() {
flag.Parse()
if err := viper.ReadInConfig(); err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
mapping := Translate{
viper.GetString("Pattern"),
viper.GetString("Key"),
viper.GetString("TSLayout"),
viper.GetString("TSField"),
viper.GetString("TFields"),
viper.GetString("VFields"),
}
tags := viper.GetStringMapString("Tags")
if err := viper.UnmarshalKey("Filters", &filters); err != nil {
panic(err)
}
var influx InfluxConfig
if err := viper.UnmarshalKey("Influxdb", &influx); err != nil {
fmt.Printf("influx config error:", err)
}
sender, err := influx.NewSender(batchSize, queueSize, period)
if err != nil {
panic(err)
}
if listen {
reader, err := Listen(ip, tcp, udp)
if err != nil {
panic(err)
}
for err == nil {
err = Parse(mapping, tags, reader, sender)
}
if err != nil {
panic(err)
}
return
}
if len(file) > 0 {
if err := parseFile(file, mapping, tags, sender); err != nil {
panic(err)
}
}
}