-
Notifications
You must be signed in to change notification settings - Fork 0
/
journalcheck.go
183 lines (165 loc) · 3.65 KB
/
journalcheck.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"mime/quotedprintable"
"os"
"os/exec"
"time"
"github.com/jorgenschaefer/journalcheck/config"
"github.com/jorgenschaefer/journalcheck/journal"
"github.com/jorgenschaefer/journalcheck/matcher"
)
func main() {
var unfilteredEntries = make(chan *journal.Entry)
var filteredEntries = make(chan *journal.Entry)
var finalCursor = make(chan string)
go generate(unfilteredEntries, finalCursor)
go filter(unfilteredEntries, filteredEntries)
recipient, ok := config.RecipientAddress()
if ok {
sendmail(filteredEntries, recipient)
} else {
writeentries(filteredEntries)
}
cursor, ok := <-finalCursor
if ok {
writeCursor(cursor)
}
}
func generate(unfilteredEntries chan *journal.Entry, finalCursor chan string) {
var lastEntry *journal.Entry
j, err := journal.New()
if err != nil {
log.Fatal(err)
}
cursorfile, ok := config.CursorFile()
if ok {
cursor, err := ioutil.ReadFile(cursorfile)
if err != nil {
j.SeekLast(1)
entry, err := j.Next()
if err != nil {
log.Fatal(err)
}
close(unfilteredEntries)
finalCursor <- entry.Cursor
close(finalCursor)
return
} else {
j.SeekCursor(string(cursor))
}
} else {
j.SeekLast(config.NumEntries())
}
for {
entry, err := j.Next()
if err != nil {
log.Fatal(err)
}
if entry == nil {
close(unfilteredEntries)
if lastEntry != nil {
finalCursor <- lastEntry.Cursor
}
close(finalCursor)
return
}
lastEntry = entry
unfilteredEntries <- entry
}
}
func filter(unfilteredEntries, filteredEntries chan *journal.Entry) {
filterfile, ok := config.FilterFile()
if !ok {
for entry := range unfilteredEntries {
filteredEntries <- entry
}
} else {
m, err := matcher.New(filterfile)
if err != nil {
log.Fatal(err)
}
for entry := range unfilteredEntries {
matches := m.Matches(entry.MatchString())
if !matches {
filteredEntries <- entry
}
}
}
close(filteredEntries)
}
func sendmail(filteredEntries chan *journal.Entry, recipient string) {
var b bytes.Buffer
var doSend bool = false
hostname, err := os.Hostname()
if err != nil {
hostname = "localhost"
}
mail := quotedprintable.NewWriter(&b)
_, err = fmt.Fprintf(mail, `From: logcheck@%s
To: %s
Subject: Journalcheck at %s
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
MIME: 1.0
This email is sent by journalcheck. If you no longer wish to receive
such mail, you can either deinstall the journalcheck package or modify
the configuration.
`, hostname, recipient, time.Now().Format(time.RFC822))
if err != nil {
log.Fatal(err)
}
for entry := range filteredEntries {
doSend = true
fmt.Fprintln(mail, format(entry))
}
if !doSend {
return
}
cmd := exec.Command("/usr/sbin/sendmail", recipient)
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
if _, err := b.WriteTo(stdin); err != nil {
log.Fatal(err)
}
if err := stdin.Close(); err != nil {
log.Fatal(err)
}
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}
}
func writeentries(filteredEntries chan *journal.Entry) {
for entry := range filteredEntries {
fmt.Println(format(entry))
}
}
func writeCursor(cursor string) {
cursorfile, ok := config.CursorFile()
if ok {
err := ioutil.WriteFile(cursorfile, ([]byte)(cursor), 0600)
if err != nil {
log.Fatal(err)
}
}
}
func format(entry *journal.Entry) string {
switch config.OutputFormat() {
case "short":
return entry.ShortString()
case "verbose":
return entry.VerboseString()
case "match":
return entry.MatchString()
default:
panic("Can't happen, should be checked in config")
}
}