summaryrefslogtreecommitdiff
path: root/internal/server/continuous.go
blob: cf89cdddc83ab42f597f4a8977965e5a87ed1e11 (plain)
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
package server

import (
	"context"
	"fmt"
	"os"
	"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"
)

type continuous struct {
}

func newContinuous() *continuous {
	return &continuous{}
}

func (s *continuous) start(ctx context.Context) {
	// First run after just 10s!
	time.Sleep(time.Second * 10)
	s.runJobs(ctx)

	for {
		select {
		case <-time.After(time.Minute):
			s.runJobs(ctx)
		case <-ctx.Done():
			return
		}
	}
}

func (s *continuous) runJobs(ctx context.Context) {
	for _, job := range config.Server.Schedule {
		if !job.Enable {
			logger.Debug(job.Name, "Not running job as not enabled")
			continue
		}

		files := fillDates(job.Files)
		outfile := fillDates(job.Outfile)

		servers := strings.Join(job.Servers, ",")
		if servers == "" {
			servers = config.Server.SSHBindAddress
		}

		args := clients.Args{
			ConnectionsPerCPU: 10,
			Discovery:         job.Discovery,
			ServersStr:        servers,
			What:              files,
			Mode:              omode.MapClient,
			UserName:          config.BackgroundUser,
		}

		args.SSHAuthMethods = append(args.SSHAuthMethods, gossh.Password(job.Name))

		tmpOutfile := fmt.Sprintf("%s.tmp", outfile)
		query := fmt.Sprintf("%s outfile %s", job.Query, tmpOutfile)

		client, err := clients.NewMaprClient(args, query, clients.NonCumulativeMode)
		if err != nil {
			logger.Error(fmt.Sprintf("Unable to create job job %s", job.Name), err)
			continue
		}

		jobCtx, cancel := context.WithCancel(ctx)
		defer cancel()

		logger.Info(fmt.Sprintf("Starting job job %s", job.Name))
		status := client.Start(jobCtx)
		logMessage := fmt.Sprintf("Job exited with status %d", status)

		if err := os.Rename(tmpOutfile, outfile); err == nil {
			logger.Info(job.Name, fmt.Sprintf("Renamed %s to %s", tmpOutfile, outfile))
		}

		if status != 0 {
			logger.Warn(logMessage)
			continue
		}
		logger.Info(logMessage)
	}
}