-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (105 loc) · 2.67 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
package main
import (
"bytes"
"context"
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/yaml.v2"
"os"
"os/exec"
"sync"
"time"
)
func main() {
logger, err := setupLogger(logfilePath)
if err != nil {
panic(err)
}
logger.Info("START")
defer logger.Info("END")
c := new(Config)
yamlFile, err := os.ReadFile(configPath)
if err != nil {
logger.Error("yaml.config.error", zap.Error(err))
return
}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
logger.Error("yaml.parse.error", zap.Error(err))
return
}
if len(c.Items) <= 0 {
logger.Error("config.not.provided")
return
}
var finalStatus []string
wg := sync.WaitGroup{}
// run all the sftp connections in parallel
for _, item := range c.Items {
wg.Add(1)
item := item
go func() {
defer wg.Done()
l := logger.With(zap.String("name", item.Name))
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*15)
defer cancelFunc()
_, _, err := cleanArgsAndExecute(ctx, item.Args)
if err != nil {
l.Error("error.parsing.command", zap.Error(err))
finalStatus = append(finalStatus, fmt.Sprintf(template, item.Name, 0))
return
}
finalStatus = append(finalStatus, fmt.Sprintf(template, item.Name, 1))
l.Info("complete")
return
}()
}
wg.Wait()
f, err := os.OpenFile(promFilePath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
logger.Error("error.opening.local.prom.file", zap.Error(err))
return
}
_, _ = f.WriteString("# TYPE sftp_outbound_status gauge")
for i := range finalStatus {
_, _ = f.WriteString("\n")
_, _ = f.WriteString(finalStatus[i])
}
_, _ = f.WriteString("\n")
_ = f.Sync()
_ = f.Close()
}
func setupLogger(filename string) (*zap.Logger, error) {
config := zap.NewProductionEncoderConfig()
config.EncodeTime = zapcore.ISO8601TimeEncoder
fileEncoder := zapcore.NewConsoleEncoder(config)
logFile, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
writer := zapcore.AddSync(logFile)
defaultLogLevel := zapcore.DebugLevel
core := zapcore.NewTee(
zapcore.NewCore(fileEncoder, writer, defaultLogLevel),
)
logger := zap.New(core, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel))
return logger, nil
}
func cleanArgsAndExecute(ctx context.Context, command string) (string, string, error) {
if ctx.Err() != nil {
return "", "", ctx.Err()
}
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd := exec.CommandContext(ctx, "bash", "-c", command)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Start(); err != nil {
return "", "", err
}
if err := cmd.Wait(); err != nil {
return "", "", err
}
return stdout.String(), stderr.String(), nil
}