解析XML Filebeat> Logstash> Elasticsearch

Parsing XML Filebeat > Logstash > Elasticsearch

目标:将具有嵌套数据的XML文件解析到不同的Elasticsearch文档中。

我选择在这里使用logstash来帮助我,但是由于文件将位于不同的服务器上,因此我决定使用filebeat将这些文件提供给logstash。该安装程序听起来不错。

但是,我似乎无法获得文件信号来在一条消息中发送所有行,而是一行一行地获取它们:

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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
{
   "@timestamp" => 2017-10-15T20:30:11.825Z,
       "offset" => 44,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"<?xml version="1.0" encoding="iso-8859-1"?>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.825Z,
       "offset" => 108,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"<foo:statistics xsi:schemaLocation="http://www.foo.no foo.xsd"",
        "tags" =>
[
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.825Z,
       "offset" => 141,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"  xmlns:foo="http://www.foo.no"",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 198,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 231,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"  <foo:version>1.0</foo:version>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 258,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"  <foo:name>bar</foo:name>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 313,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"  <foo:start>2017-01-01T00:06:34.880+02:00</foo:start>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 366,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"  <foo:stop>2017-05-01T00:06:34.880+02:00</foo:stop>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 380,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"  <foo:place>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 409,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"    <foo:name>baz</foo:name>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 442,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"    <foo:id>1B445T4UV-W</foo:id>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 457,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"  </foo:place>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 471,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"  <foo:visit>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 526,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"    <foo:date>2017-04-17T04:06:34.880+02:00</foo:date>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 557,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"    <foo:status>2</foo:status>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 572,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"  </foo:visit>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 586,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"  <foo:visit>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 641,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"    <foo:date>2017-04-18T04:06:34.880+02:00</foo:date>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 672,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"    <foo:status>3</foo:status>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 687,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"  </foo:visit>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 701,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"  <foo:visit>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 756,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"    <foo:date>2017-04-19T04:06:34.880+02:00</foo:date>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 787,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"    <foo:status>1</foo:status>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}
[2017-10-15T20:30:42,853][WARN ][logstash.filters.split   ] Only String and Array types are splittable. field:visits is of type = NilClass
{
   "@timestamp" => 2017-10-15T20:30:11.826Z,
       "offset" => 802,
     "@version" =>"1",
         "beat" => {
           "name" =>"bb1ee9b80d2d",
       "hostname" =>"bb1ee9b80d2d",
        "version" =>"6.0.0-rc1"
    },
         "host" =>"bb1ee9b80d2d",
       "source" =>"/mnt/log/test4.xml",
      "message" =>"  </foo:visit>",
         "tags" => [
        [0]"beats_input_codec_plain_applied",
        [1]"_split_type_failure"
    ]
}

这是我的文件拍配置

1
2
3
4
5
6
7
8
9
10
filebeat.prospectors:
- type: log
  paths:
    - /mnt/log/*.xml
  multiline.pattern: '<?xml .*'
  multiline.negate: false
  multiline.match: after

output.logstash:
  hosts: ["logstash:5000"]

和我的XML文件:

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
<?xml version="1.0" encoding="iso-8859-1"?>
<foo:statistics xsi:schemaLocation="http://www.foo.no foo.xsd"
  xmlns:foo="http://www.foo.no"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <foo:version>1.0</foo:version>
  <foo:name>bar</foo:name>
  <foo:start>2017-01-01T00:06:34.880+02:00</foo:start>
  <foo:stop>2017-05-01T00:06:34.880+02:00</foo:stop>
  <foo:place>
    <foo:name>baz</foo:name>
    <foo:id>1B445T4UV-W</foo:id>
  </foo:place>
  <foo:visit>
    <foo:date>2017-04-17T04:06:34.880+02:00</foo:date>
    <foo:status>2</foo:status>
  </foo:visit>
  <foo:visit>
    <foo:date>2017-04-18T04:06:34.880+02:00</foo:date>
    <foo:status>3</foo:status>
  </foo:visit>
  <foo:visit>
    <foo:date>2017-04-19T04:06:34.880+02:00</foo:date>
    <foo:status>1</foo:status>
  </foo:visit>
</foo:statistics>

我希望将整个文件传递到logstash,这是该文件的配置。

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
input {
    beats {
        port => 5000
    }
}

filter {
    xml {
        namespaces => {
           "foo" =>"http://www.foo.no"
           "xsi" =>"http://www.w3.org/2001/XMLSchema-instance"
        }
        source =>"message"
        store_xml =>"false"

        xpath => ["/foo:statistics/foo:start/text()","start"]
        xpath => ["/foo:statistics/foo:stop/text()","stop"]
        xpath => ["/foo:statistics/foo:place/name/text()","place_name"]
        xpath => ["/foo:statistics/foo:place/id/text()","place_id"]
        xpath => ["/foo:statistics/foo:visit","visits"]
    }

    split {
        field =>"visits"
        remove_field =>"message"
    }

    xml {
        source =>"visits"
        store_xml =>"false"
        xpath => ["/foo:visit/foo:date/text()","date"]
        xpath => ["/foo:visit/foo:status/text()","status"]
        remove_field =>"visits"                
    }

    date {
        match => ["date","ISO8601"]
    }
}

output {
    stdout { codec => rubydebug }
    elasticsearch {
        hosts =>"elasticsearch:9200"
        index =>"maaling-%{+YYYY.MM.dd}"
    }
}

我们非常感谢您的帮助。

编辑:将模式更改为"


我将文件拍配置更改为:

1
2
3
4
5
6
7
8
9
10
11
filebeat.prospectors:
- type: log
  paths:
    - /mnt/log/*.ATKSTAT
  encoding: 'windows-1252'
  multiline.pattern: ' \\A.*'
  multiline.negate: true
  multiline.match: after

output.logstash:
  hosts: ["logstash:5000"]

这是麋鹿堆栈的v6.0.0