/
json_format.go
130 lines (113 loc) · 3.05 KB
/
json_format.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
package decoders
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/pquerna/ffjson/ffjson"
"github.com/stephane-martin/skewer/model"
)
func pJSON(m []byte) ([]*model.SyslogMessage, error) {
sourceMsg := model.RegularSyslog{}
err := ffjson.Unmarshal(m, &sourceMsg)
if err != nil {
return nil, UnmarshalJsonError(err)
}
return []*model.SyslogMessage{sourceMsg.Internal()}, nil
}
func pRsyslogJSON(m []byte) ([]*model.SyslogMessage, error) {
sourceMsg := model.JsonRsyslogMessage{}
err := ffjson.Unmarshal(m, &sourceMsg)
if err != nil {
return nil, UnmarshalJsonError(err)
}
pri, err := strconv.Atoi(sourceMsg.Priority)
if err != nil {
return nil, ErrInvalidPriority
}
n := time.Now()
generated := n
reported := n
if sourceMsg.TimeReported != "-" && len(sourceMsg.TimeReported) > 0 {
r, err := time.Parse(time.RFC3339Nano, sourceMsg.TimeReported)
if err != nil {
return nil, ErrInvalidTimestamp
}
reported = r
}
if sourceMsg.TimeGenerated != "-" && len(sourceMsg.TimeGenerated) > 0 {
g, err := time.Parse(time.RFC3339Nano, sourceMsg.TimeGenerated)
if err != nil {
return nil, ErrInvalidTimestamp
}
generated = g
}
hostname := ""
if sourceMsg.Hostname != "-" {
hostname = strings.TrimSpace(sourceMsg.Hostname)
}
appname := ""
if sourceMsg.Appname != "-" {
appname = strings.TrimSpace(sourceMsg.Appname)
}
procid := ""
if sourceMsg.Procid != "-" {
procid = strings.TrimSpace(sourceMsg.Procid)
}
msgid := ""
if sourceMsg.Msgid != "-" {
msgid = strings.TrimSpace(sourceMsg.Msgid)
}
structured := ""
if sourceMsg.Structured != "-" {
structured = strings.TrimSpace(sourceMsg.Structured)
}
msg := model.Factory()
msg.Priority = model.Priority(pri)
msg.Facility = model.Facility(pri / 8)
msg.Severity = model.Severity(pri % 8)
msg.Version = 1
msg.TimeReportedNum = reported.UnixNano()
msg.TimeGeneratedNum = generated.UnixNano()
msg.HostName = hostname
msg.AppName = appname
msg.ProcId = procid
msg.MsgId = msgid
msg.Structured = structured
msg.Message = strings.TrimSpace(sourceMsg.Message)
for k, v := range sourceMsg.Properties {
msg.SetProperty("rsyslog", strings.TrimSpace(k), strings.TrimSpace(fmt.Sprintf("%v", v)))
}
return []*model.SyslogMessage{msg}, nil
}
/*
{
"msg": " spamd: clean message (3.9\/5.0) for debian-spamd:110 in 2.2 seconds, 12388 bytes.",
"rawmsg": "<22>May 20 03:06:48 spamd[6948]: spamd: clean message (3.9\/5.0) for debian-spamd:110 in 2.2 seconds, 12388 bytes.",
"timereported": "2017-05-20T03:06:48.819757+02:00",
"hostname": "vmail_container",
"syslogtag": "spamd[6948]:",
"inputname": "imuxsock",
"fromhost": "vmail_container",
"fromhost-ip": "127.0.0.1",
"pri": "22",
"syslogfacility": "2",
"syslogseverity": "6",
"timegenerated": "2017-05-20T03:06:48.819757+02:00",
"programname": "spamd",
"protocol-version": "0",
"structured-data": "-",
"app-name": "spamd",
"procid": "6948",
"msgid": "-",
"uuid": null,
"$!":
{
"pid": 6948,
"uid": 110,
"gid": 116,
"appname": "spamd child",
"cmd": "spamd child"
}
}
*/