blob: 5ce8d6f07957a354aa5d34efe5f033dce2191f1f [file] [log] [blame]
Aleš Komárek4fdb8e92016-11-26 12:11:16 +01001-- Copyright 2016 Mirantis, Inc.
2--
3-- Licensed under the Apache License, Version 2.0 (the "License");
4-- you may not use this file except in compliance with the License.
5-- You may obtain a copy of the License at
6--
7-- http://www.apache.org/licenses/LICENSE-2.0
8--
9-- Unless required by applicable law or agreed to in writing, software
10-- distributed under the License is distributed on an "AS IS" BASIS,
11-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-- See the License for the specific language governing permissions and
13-- limitations under the License.
14require "string"
15local l = require 'lpeg'
16l.locale(l)
17
18local patt = require 'patterns'
19local utils = require "lma_utils"
20
21local msg = {
22 Timestamp = nil,
23 Type = 'log',
24 Hostname = nil,
25 Payload = nil,
26 Pid = nil,
27 Fields = nil,
28 Severity = nil,
29}
30
31-- glusterfs log messages are formatted like this
32--
33-- [2016-11-09 08:31:55.192673] I [socket.c:3480:socket_init] 0-glusterfs: SSL support is NOT enabled
34local sp = l.space
35local timestamp = l.P'[' * l.Cg(patt.Timestamp, "Timestamp") * l.P']'
36local severity = l.Cg(l.R("AZ") / string.upper, "SeverityLabel")
37local message = l.Cg(patt.Message, "Message")
38
39local grammar = l.Ct(timestamp * sp * severity * sp * message)
40
41
42function process_message ()
43 local logger, programname = string.match(read_message('Logger'), '^([^.]+)%.(.+)')
44 if not logger or not programname then
45 return -1, string.format("Failed to parse Logger field: %s", read_message('Logger'))
46 end
47
48 local log = read_message("Payload")
49 local m = grammar:match(log)
50 if not m then
51 return -1, string.format("Failed to parse: %s", string.sub(log, 1, 64))
52 end
53
54 msg.Logger = logger
55 msg.Timestamp = m.Timestamp
56 msg.Severity = utils.label_to_severity_map[m.SeverityLabel] or utils.label_to_severity_map.DEBUG
57 msg.Payload = m.Message
58
59 msg.Fields = {}
60 msg.Fields.severity_label = utils.severity_to_label_map[msg.Severity]
61 msg.Fields.programname = programname
62
63 utils.inject_tags(msg)
64 return utils.safe_inject_message(msg)
65end
66