-
Notifications
You must be signed in to change notification settings - Fork 4
/
app-monitor.go
90 lines (79 loc) · 1.84 KB
/
app-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
package main
import (
"fmt"
"log"
"sync"
"time"
"github.com/ashwanthkumar/golang-utils/maps"
marathon "github.com/gambol99/go-marathon"
)
// Task - Task related information
type Task struct {
App string
Labels map[string]string
TaskID string
Hostname string
}
// LogEnabledLabel - Label to check if logging should be enabled or not
const LogEnabledLabel = "logs.enabled"
type AppMonitor struct {
Client marathon.Marathon
RunWaitGroup sync.WaitGroup
CheckInterval time.Duration
stopChannel chan bool
TasksChannel chan Task // TaskInfo without CWD will be sent to this channel
}
func (a *AppMonitor) Start() {
fmt.Println("Starting App Checker...")
a.RunWaitGroup.Add(1)
a.stopChannel = make(chan bool)
a.TasksChannel = make(chan Task)
go a.run()
fmt.Println("App Checker Started.")
fmt.Printf("App Checker - Checking the status of all the apps every %v\n", a.CheckInterval)
}
func (a *AppMonitor) Stop() {
fmt.Println("Stopping App Checker...")
close(a.stopChannel)
a.RunWaitGroup.Done()
}
func (a *AppMonitor) run() {
running := true
for running {
select {
case <-time.After(a.CheckInterval):
err := a.monitorApps()
if err != nil {
log.Fatalf("Unexpected error - %v\n", err)
}
case <-a.stopChannel:
running = false
}
time.Sleep(1 * time.Second)
}
}
func (a *AppMonitor) monitorApps() error {
apps, err := a.Client.Applications(nil)
if err != nil {
return err
}
for _, app := range apps.Apps {
isLogEnabled := maps.GetBoolean(*app.Labels, LogEnabledLabel, false)
if isLogEnabled {
app, err := a.Client.Application(app.ID)
if err != nil {
return err
}
for _, task := range app.Tasks {
taskInfo := Task{
App: app.ID,
Labels: *app.Labels,
TaskID: task.ID,
Hostname: task.Host,
}
a.TasksChannel <- taskInfo
}
}
}
return nil
}