-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslackd.go
More file actions
102 lines (95 loc) · 2.86 KB
/
slackd.go
File metadata and controls
102 lines (95 loc) · 2.86 KB
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
package main
import (
"flag"
"fmt"
"os"
"regexp"
"strconv"
"github.com/hpcloud/tail"
"github.com/nlopes/slack"
"github.com/vharitonsky/iniflags"
)
var (
token string
channel string
file string
includes string
excludes string
reopen bool
TOKEN = flag.String("TOKEN", "", "Your Slack token.")
CHANNEL = flag.String("CHANNEL", "", "The ID of the Slack channel to post to. EG: C0XXXXXXXXX")
FILE = flag.String("FILE", "", "The file path to watch for changes.")
INCLUDES = flag.String("LINE_INCLUDES", "", "Post line if this regexp DOES match.")
EXCLUDES = flag.String("LINE_EXCLUDES", "", "Post line if this regexp DOES NOT match.")
REOPEN = flag.Bool("REOPEN", false, "Reopen the file if it disappears. Useful with logrotation.")
)
func main() {
iniflags.Parse()
token = *TOKEN
if token == "" && os.Getenv("TOKEN") != "" {
token = os.Getenv("TOKEN")
}
channel = *CHANNEL
if channel == "" && os.Getenv("CHANNEL") != "" {
channel = os.Getenv("CHANNEL")
}
file = *FILE
if file == "" && os.Getenv("FILE") != "" {
file = os.Getenv("FILE")
}
includes = *INCLUDES
if includes == "" && os.Getenv("LINE_INCLUDES") != "" {
includes = os.Getenv("LINE_INCLUDES")
}
excludes = *EXCLUDES
if excludes == "" && os.Getenv("LINE_EXCLUDES") != "" {
excludes = os.Getenv("LINE_EXCLUDES")
}
reopen = *REOPEN
if reopen == false && os.Getenv("REOPEN") != "" {
var err error
reopen, err = strconv.ParseBool(os.Getenv("REOPEN"))
if err != nil {
fmt.Println("ERROR: Parsing the REOPEN boolean.")
fmt.Println(err)
os.Exit(2)
}
}
slackAPI := slack.New(token)
var include, exclude *regexp.Regexp
var err error
if includes != "" {
include, err = regexp.Compile(includes)
if err != nil {
fmt.Println("ERROR: Failed to compile `LINE_INCLUDES` regex.")
fmt.Println(err)
opt := slack.MsgOptionText(fmt.Sprintf("==> slackd failed to compile `LINE_INCLUDES` regex."), false)
slackAPI.PostMessage(channel, opt)
os.Exit(2)
}
}
if excludes != "" {
exclude, err = regexp.Compile(excludes)
if err != nil {
fmt.Println("ERROR: Failed to compile `LINE_EXCLUDES` regex.")
fmt.Println(err)
opt := slack.MsgOptionText(fmt.Sprintf("==> slackd failed to compile `LINE_EXCLUDES` regex."), false)
slackAPI.PostMessage(channel, opt)
os.Exit(2)
}
}
log, err := tail.TailFile(file, tail.Config{Follow: true, ReOpen: reopen, Poll: true})
if err != nil {
fmt.Println("ERROR: Could not tail the specified log.")
fmt.Println(err)
opt := slack.MsgOptionText(fmt.Sprintf("==> slackd could not tail the specified log."), false)
slackAPI.PostMessage(channel, opt)
os.Exit(2)
}
for line := range log.Lines {
if (include != nil && include.MatchString(line.Text)) || (exclude != nil && !exclude.MatchString(line.Text)) {
opt := slack.MsgOptionText(fmt.Sprintf("```%s```", line.Text), false)
slackAPI.PostMessage(channel, opt)
}
}
}