This repository has been archived by the owner on Mar 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
forward-journald.go
77 lines (65 loc) · 1.59 KB
/
forward-journald.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
// Main package for forward-journald
package main
import (
"bufio"
"flag"
"fmt"
"os"
"path"
"strings"
"github.com/coreos/go-systemd/journal"
)
var object_exe = flag.String("tag", "", "Add OBJECT_EXE=<tag> to journald entries")
var pri_info = flag.Bool("1", true, "forward stdin to journald as Priority Informational")
var pri_error = flag.Bool("2", false, "forward stdin to journald as Priority Error")
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %v:\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %v -tag TAG\n\n", path.Base(os.Args[0]))
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
var priority journal.Priority
var priority_name string
if *pri_error {
priority = journal.PriErr
priority_name = "Priority Error"
} else {
priority = journal.PriInfo
priority_name = "Priority Informational"
}
if journal.Enabled() {
journal.Send(
fmt.Sprintf("Forwarding stdin to journald using %v and tag %v", priority_name, *object_exe),
priority,
nil)
} else {
fmt.Fprintln(os.Stderr, "forward-journald: Unable to connect to journald")
os.Exit(1)
}
var fields map[string]string = nil
if len(*object_exe) > 0 {
fields = map[string]string{
"OBJECT_EXE": *object_exe,
}
}
reader := bufio.NewReader(os.Stdin)
var line string = ""
var err error = nil
for err == nil {
line, err = reader.ReadString('\n')
if journal.Enabled() {
if err == nil {
line = strings.TrimSpace(line)
journal.Send(line, priority, fields)
} else {
// log any partial lines
if len(line) > 0 {
journal.Send(line, priority, fields)
}
}
}
}
}