-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
110 lines (90 loc) · 2.47 KB
/
monitor.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
package ecstaskmonitoring
import (
"fmt"
"strconv"
"sync"
"time"
)
// Monitor ... Monitor the cluster
func (c *Cluster) Monitor(exitErrCh chan error) {
tasks, err := c.NewTasks()
if err != nil {
exitErrCh <- err
return
}
if tasks == nil {
return
}
c.Tasks = tasks
// concurrency
var wg sync.WaitGroup
wg.Add(2)
go MonitorTaskThreshold(*c, &wg)
go MonitorTaskParallel(*c, &wg)
wg.Wait()
return
}
// MonitorTaskThreshold ... Monitor the number of tasks in the cluster
func MonitorTaskThreshold(c Cluster, wg *sync.WaitGroup) {
defer wg.Done()
CurrentTaskThresholdFailureCount++
if len(c.Tasks) > c.TaskThreshold {
if (CurrentTaskThresholdFailureCount - c.FailureCount) == 0 {
log.sugar.Info("detect %s cluster in %s task threshold: %d", c.Name, c.AwsProfile, len(c.Tasks))
a := c.NewSlackAttachmentMessage(strconv.Itoa(len(c.Tasks)))
a.PostSlackMessage(c.IncomingWebhook)
CurrentTaskThresholdFailureCount = 0
}
}
return
}
// MonitorTaskParallel ... Monitor if task is running in parallel
func MonitorTaskParallel(c Cluster, wg *sync.WaitGroup) {
defer wg.Done()
for _, v := range c.Tasks {
// if setting count 0, ignore check
if v.Times == 0 {
continue
}
if len(v.EcsDescribeTasks) > v.Times {
IncreaseTimesParallels(v.Name)
log.sugar.Infof("detect parallel: %s", v.Name)
for _, v := range v.EcsDescribeTasks {
log.sugar.Infof("createdAt: %d, stoppedAt: %d, taskArn: %s", *v.CreatedAt, *v.StoppedAt, *v.TaskArn)
}
}
}
// Notify every 60 minutes
if time.Now().Sub(ParallelNotifyTime).Minutes() >= float64(ParallelNotifyInterval) {
notify := ParallelsToParallelNotify(c)
if notify.Message != "" {
a := notify.NewSlackAttachmentMessage("")
a.PostSlackMessage(c.IncomingWebhook)
}
// after notify, clear time and Parallels data
ParallelNotifyTime = time.Now()
Parallels = []*Parallel{}
}
}
// IncreaseTimesParallels ... Increase in number of times Parallels variable.
func IncreaseTimesParallels(taskName string) {
for _, v := range Parallels {
if v.Name == taskName {
v.Times++
return
}
}
Parallels = append(Parallels, &Parallel{taskName, 1})
}
// ParallelsToParallelNotify ... Parallels struct to ParallelNotify
func ParallelsToParallelNotify(c Cluster) *ParallelNotify {
result := &ParallelNotify{
ClusterName: c.Name,
AwsProfile: c.AwsProfile,
AwsRegion: c.AwsRegion,
}
for _, v := range Parallels {
result.Message += fmt.Sprintf("%s: %d times\n", v.Name, v.Times)
}
return result
}