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
|
package server
import (
"context"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"
"github.com/mimecast/dtail/internal/clients"
"github.com/mimecast/dtail/internal/config"
"github.com/mimecast/dtail/internal/io/logger"
"github.com/mimecast/dtail/internal/omode"
gossh "golang.org/x/crypto/ssh"
)
const authLength = 64
const authCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@$%^&*()_+[]"
type scheduler struct {
authPayload string
}
func newScheduler() *scheduler {
seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, authLength)
for i := range b {
b[i] = authCharset[seededRand.Intn(len(authCharset))]
}
return &scheduler{
authPayload: string(b),
}
}
func (s *scheduler) start(ctx context.Context) {
for {
select {
case <-time.After(time.Minute):
s.runJobs(ctx)
case <-ctx.Done():
return
}
}
}
func (s *scheduler) runJobs(ctx context.Context) {
for _, scheduled := range config.Server.Schedule {
if !scheduled.Enable {
logger.Debug(scheduled.Name, "Not running job as not enabled")
continue
}
hour, err := strconv.Atoi(time.Now().Format("15"))
if err != nil {
logger.Error(scheduled.Name, "Unable to create scheduled job", err)
continue
}
if hour < scheduled.TimeRange[0] || hour >= scheduled.TimeRange[1] {
logger.Debug(scheduled.Name, "Not running job out of time range")
continue
}
files := fillDates(scheduled.Files)
outfile := fillDates(scheduled.Outfile)
_, err = os.Stat(outfile)
if !os.IsNotExist(err) {
logger.Debug(scheduled.Name, "Not running job as outfile already exists", outfile)
continue
}
servers := scheduled.Servers
if servers == "" {
servers = config.Server.SSHBindAddress
}
args := clients.Args{
ConnectionsPerCPU: 10,
Discovery: scheduled.Discovery,
ServersStr: servers,
What: files,
Mode: omode.MapClient,
UserName: config.ScheduledUser,
}
args.SSHAuthMethods = append(args.SSHAuthMethods, gossh.Password(s.authPayload))
tmpOutfile := fmt.Sprintf("%s.tmp", outfile)
query := fmt.Sprintf("%s outfile %s", scheduled.Query, tmpOutfile)
client, err := clients.NewMaprClient(args, query)
if err != nil {
logger.Error(fmt.Sprintf("Unable to create scheduled job %s", scheduled.Name), err)
continue
}
logger.Info(fmt.Sprintf("Starting scheduled job %s", scheduled.Name))
status := client.Start(ctx)
logMessage := fmt.Sprintf("Job exited with status %d", status)
if err := os.Rename(tmpOutfile, outfile); err == nil {
logger.Info(scheduled.Name, fmt.Sprintf("Renamed %s to %s", tmpOutfile, outfile))
}
if status != 0 {
logger.Warn(logMessage)
continue
}
logger.Info(logMessage)
}
}
func fillDates(str string) string {
yyyesterday := time.Now().Add(3 * -24 * time.Hour).Format("20060102")
str = strings.ReplaceAll(str, "$yyyesterday", yyyesterday)
yyesterday := time.Now().Add(2 * -24 * time.Hour).Format("20060102")
str = strings.ReplaceAll(str, "$yyesterday", yyesterday)
yesterday := time.Now().Add(1 * -24 * time.Hour).Format("20060102")
str = strings.ReplaceAll(str, "$yesterday", yesterday)
today := time.Now().Format("20060102")
str = strings.ReplaceAll(str, "$today", today)
tomorrow := time.Now().Add(1 * 24 * time.Hour).Format("20060102")
return strings.ReplaceAll(str, "$tomorrow", tomorrow)
}
|