-
Notifications
You must be signed in to change notification settings - Fork 4
/
parse.go
91 lines (77 loc) · 1.64 KB
/
parse.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
package main
import (
"bytes"
"fmt"
"strings"
"time"
)
const seperator = ' '
var (
errTime = fmt.Errorf("Unexpected rsyslog time format")
errLevel = fmt.Errorf("Unexpected rsyslog level format")
errLength = fmt.Errorf("Unexpected rsyslog message length")
)
func parseLine(raw []byte, promOnly bool) (*LogLine, error) {
var err error
ll := &LogLine{
Raw: raw,
}
if len(ll.Raw) < 46 {
return nil, errLength
}
if !promOnly {
ll.Timestamp, err = time.Parse((time.RFC3339), string(ll.Raw[:32]))
if err != nil {
return nil, errTime
}
}
if ll.Severity, err = getSeverity(ll.Raw[33]); err != nil {
return nil, err
}
var curPos, endPos = 35, 35
endPos = bytes.IndexRune(ll.Raw[curPos:], seperator)
if endPos == -1 {
}
endPos += curPos
ll.Hostname = string(ll.Raw[curPos:endPos])
curPos = endPos + 1
endPos = bytes.IndexRune(ll.Raw[curPos:], seperator)
if endPos == -1 {
return nil, fmt.Errorf("Unexpected rsyslog template format in %s", raw)
}
endPos += curPos
ll.Program = string(ll.Raw[curPos:endPos])
curPos = endPos + 1
ll.MsgPos = curPos
if !ll.Valid() {
return nil, fmt.Errorf("Invalid rsyslog template format in %s", raw)
}
if !promOnly {
ll.Msg = string(ll.Raw[ll.MsgPos:])
ll.Msg = strings.ToValidUTF8(ll.Msg, "")
}
return ll, nil
}
func getSeverity(in byte) (out string, err error) {
switch in {
case 48: // 0
out = "emergency"
case 49: // 1
out = "alert"
case 50: // 2
out = "critical"
case 51: // 3
out = "error"
case 52: // 4
out = "warning"
case 53: // 5
out = "notice"
case 54: // 6
out = "info"
case 55: // 7
out = "debug"
default:
return "", errLevel
}
return out, nil
}