-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
193 lines (160 loc) · 4.57 KB
/
main.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
184
185
186
187
188
189
190
191
192
193
package main
import (
"fmt"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
"time"
"github.com/Luzifer/rconfig"
"github.com/elastic/beats/libbeat/common/dtfmt"
"github.com/olivere/elastic"
"github.com/robfig/cron"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
elogrus "gopkg.in/sohlich/elogrus.v3"
"gopkg.in/yaml.v2"
)
var (
cfg = struct {
ConfigFile string `flag:"config" default:"config.yaml" description:"Cron definition file"`
Hostname string `flag:"hostname" description:"Overwrite system hostname"`
PingTimout time.Duration `flag:"ping-timeout" default:"1s" description:"Timeout for success / failure pings"`
}{}
version = "dev"
)
type cronConfig struct {
Elasticsearch struct {
Auth []string `yaml:"auth"`
Index string `yaml:"index"`
Servers []string `yaml:"servers"`
} `yaml:"elasticsearch"`
Jobs []cronJob `yaml:"jobs"`
}
type cronJob struct {
Name string `yaml:"name"`
Schedule string `yaml:"schedule"`
Command string `yaml:"cmd"`
Arguments []string `yaml:"args"`
PingSuccess string `yaml:"ping_success"`
PingFailure string `yaml:"ping_failure"`
}
func init() {
rconfig.ParseAndValidate(&cfg)
if cfg.Hostname == "" {
hostname, _ := os.Hostname()
cfg.Hostname = hostname
}
}
func readConfig() (*cronConfig, error) {
fp, err := os.Open(cfg.ConfigFile)
if err != nil {
return nil, err
}
defer fp.Close()
cc := &cronConfig{}
cc.Elasticsearch.Index = "elastic_cron-%{+YYYY.MM.dd}"
return cc, yaml.NewDecoder(fp).Decode(cc)
}
func main() {
cc, err := readConfig()
if err != nil {
log.Fatalf("Unable to read config file: %s", err)
}
c := cron.New()
for i := range cc.Jobs {
job := cc.Jobs[i]
if err := c.AddFunc(job.Schedule, getJobExecutor(job)); err != nil {
log.Fatalf("Unable to add job '%s': %s", job.Name, err)
}
}
opts := []elastic.ClientOptionFunc{
elastic.SetURL(cc.Elasticsearch.Servers...),
}
if cc.Elasticsearch.Auth != nil && len(cc.Elasticsearch.Auth) == 2 && cc.Elasticsearch.Auth[0] != "" {
opts = append(opts, elastic.SetBasicAuth(cc.Elasticsearch.Auth[0], cc.Elasticsearch.Auth[1]))
}
esClient, err := elastic.NewSimpleClient(opts...)
if err != nil {
log.WithError(err).Fatal("Unable to create elasticsearch client")
}
hook, err := elogrus.NewElasticHookWithFunc(esClient, cfg.Hostname, log.InfoLevel, getIndexNameFunc(cc))
if err != nil {
log.WithError(err).Fatal("Unable to create elasticsearch log hook")
}
log.AddHook(hook)
c.Run()
}
func getIndexNameFunc(cc *cronConfig) func() string {
if !strings.Contains(cc.Elasticsearch.Index, `%{+`) {
// Simple string without date expansion
return func() string { return cc.Elasticsearch.Index }
}
return func() string {
rex := regexp.MustCompile(`%{\+([^}]+)}`)
return rex.ReplaceAllStringFunc(cc.Elasticsearch.Index, func(f string) string {
f = strings.TrimSuffix(strings.TrimPrefix(f, `%{+`), `}`)
d, _ := dtfmt.Format(time.Now(), f)
return d
})
}
}
func getJobExecutor(job cronJob) func() {
return func() {
logger := log.WithFields(log.Fields{
"job": job.Name,
})
stdout := logger.WriterLevel(log.InfoLevel)
defer stdout.Close()
stderr := logger.WriterLevel(log.ErrorLevel)
defer stderr.Close()
fmt.Fprintln(stdout, "[SYS] Starting job")
cmd := exec.Command(job.Command, job.Arguments...)
cmd.Stdout = stdout
cmd.Stderr = stderr
err := cmd.Run()
switch err.(type) {
case nil:
logger.Info("[SYS] Command execution successful")
go func(url string) {
if err := doPing(url); err != nil {
logger.WithError(err).Errorf("[SYS] Ping to URL %q caused an error", url)
}
}(job.PingSuccess)
case *exec.ExitError:
logger.Info("[SYS] Command exited with unexpected exit code != 0")
go func(url string) {
if err := doPing(url); err != nil {
logger.WithError(err).Errorf("[SYS] Ping to URL %q caused an error", url)
}
}(job.PingFailure)
default:
logger.WithError(err).Error("[SYS] Execution caused error")
go func(url string) {
if err := doPing(url); err != nil {
logger.WithError(err).Errorf("[SYS] Ping to URL %q caused an error", url)
}
}(job.PingFailure)
}
}
}
func doPing(url string) error {
if url == "" {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), cfg.PingTimout)
defer cancel()
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return err
}
if resp.StatusCode > 299 {
return fmt.Errorf("Expected HTTP2xx status, got HTTP%d", resp.StatusCode)
}
return nil
}