forked from arouene/postfix_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogsource_systemd.go
143 lines (118 loc) · 3.62 KB
/
logsource_systemd.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
// +build !nosystemd,linux
package main
import (
"context"
"fmt"
"log"
"time"
"bytes"
"github.com/alecthomas/kingpin"
"github.com/coreos/go-systemd/v22/sdjournal"
)
// timeNow is a test fake injection point.
var timeNow = time.Now
// A SystemdLogSource reads log records from the given Systemd
// journal.
type SystemdLogSource struct {
journal *sdjournal.JournalReader
path string
}
// NewSystemdLogSource returns a log source for reading Systemd
// journal entries. `unit` and `slice` provide filtering if non-empty
// (with `slice` taking precedence).
func NewSystemdLogSource(path, unit, slice string) (*SystemdLogSource, error) {
var matches []sdjournal.Match
if slice != "" {
matches = append(matches, sdjournal.Match{
Field: sdjournal.SD_JOURNAL_FIELD_SYSTEMD_SLICE,
Value: slice,
})
} else if unit != "" {
matches = append(matches, sdjournal.Match{
Field: sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT,
Value: unit,
})
}
config := sdjournal.JournalReaderConfig{
NumFromTail: 1,
Matches: matches,
Path: path,
Formatter: fullMessageFormatter,
}
j, err := sdjournal.NewJournalReader(config)
if err != nil {
return nil, err
}
return &SystemdLogSource{journal: j, path: path}, nil
}
// fullMessageFormatter is a formatter for journalreader
// It returns a string representing the current journal entry
// includes the entry timestamp, hostname, syslog_identifier, pid
// and MESSAGE field.
func fullMessageFormatter(entry *sdjournal.JournalEntry) (string, error) {
msg, ok := entry.Fields["MESSAGE"]
if !ok {
return "", fmt.Errorf("no MESSAGE field present in journal entry")
}
host, ok := entry.Fields["_HOSTNAME"]
if !ok {
return "", fmt.Errorf("no HOSTNAME field present in journal entry")
}
id, ok := entry.Fields["SYSLOG_IDENTIFIER"]
if !ok {
return "", fmt.Errorf("no SYSLOG_IDENTIFIER field present in journal entry")
}
pid, ok := entry.Fields["_PID"]
if !ok {
return "", fmt.Errorf("no PID field present in journal entry")
}
usec := entry.RealtimeTimestamp
timestamp := time.Unix(0, int64(usec)*int64(time.Microsecond))
return fmt.Sprintf("%s %s %s[%s]: %s", timestamp, host, id, pid, msg), nil
}
func (s *SystemdLogSource) Close() error {
return s.journal.Close()
}
func (s *SystemdLogSource) Path() string {
return s.path
}
func (s *SystemdLogSource) Read(ctx context.Context) (string, error) {
const BUFFER_LEN = 255
var err error
var msg bytes.Buffer
var b = make([]byte, BUFFER_LEN)
for {
n, err := s.journal.Read(b)
if err != nil || n == 0 {
break
}
msg.Write(b)
// A whole entry has been read
if n < BUFFER_LEN || b[n-1] == '\n' {
break
}
}
return msg.String(), err
}
// A systemdLogSourceFactory is a factory that can create
// SystemdLogSources from command line flags.
type systemdLogSourceFactory struct {
enable bool
unit, slice, path string
}
func (f *systemdLogSourceFactory) Init(app *kingpin.Application) {
app.Flag("systemd.enable", "Read from the systemd journal instead of log").Default("false").BoolVar(&f.enable)
app.Flag("systemd.unit", "Name of the Postfix systemd unit.").Default("postfix.service").StringVar(&f.unit)
app.Flag("systemd.slice", "Name of the Postfix systemd slice. Overrides the systemd unit.").Default("").StringVar(&f.slice)
app.Flag("systemd.journal_path", "Path to the systemd journal").Default("").StringVar(&f.path)
}
func (f *systemdLogSourceFactory) New(ctx context.Context) (LogSourceCloser, error) {
if !f.enable {
return nil, nil
}
log.Println("Reading log events from systemd")
return NewSystemdLogSource(f.path, f.unit, f.slice)
}
func init() {
RegisterLogSourceFactory(&systemdLogSourceFactory{})
}