-
Notifications
You must be signed in to change notification settings - Fork 3
/
file.go
37 lines (34 loc) · 1.01 KB
/
file.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
package inputs
import (
"github.com/hpcloud/tail"
"github.com/hexbotio/hex/models"
"github.com/hexbotio/hex/parse"
"log"
"os"
"strings"
)
type File struct {
}
// Read function
func (x File) Read(inputMsgs chan<- models.Message, service models.Service) {
defer Recovery(service)
if strings.ToLower(service.Config["Mode"]) == "read" || strings.ToLower(service.Config["Mode"]) == "r" {
hostname, _ := os.Hostname()
seek := tail.SeekInfo{Offset: 0, Whence: 2}
t, err := tail.TailFile(service.Config["File"], tail.Config{Follow: true, Location: &seek})
if err != nil {
log.Print(err)
}
for line := range t.Lines {
if service.Config["Filter"] != "" {
if parse.Match(service.Config["Filter"], line.Text) {
message := models.MakeMessage(service.Type, service.Name, hostname, service.Config["File"], line.Text)
inputMsgs <- message
}
} else {
message := models.MakeMessage(service.Type, service.Name, hostname, service.Config["File"], line.Text)
inputMsgs <- message
}
}
}
}