forked from jbelke/gotelemetry_agent
/
agent.go
130 lines (97 loc) · 2.8 KB
/
agent.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
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"github.com/telemetryapp/gotelemetry"
"github.com/telemetryapp/gotelemetry_agent/agent"
"github.com/telemetryapp/gotelemetry_agent/agent/aggregations"
"github.com/telemetryapp/gotelemetry_agent/agent/config"
"github.com/telemetryapp/gotelemetry_agent/agent/graphite"
"github.com/telemetryapp/gotelemetry_agent/agent/job"
"github.com/telemetryapp/gotelemetry_agent/agent/oauth"
"io/ioutil"
"log"
"os"
"sync"
_ "github.com/telemetryapp/gotelemetry_agent/plugin"
)
var VERSION = "3.0.0"
var SOURCE_DATE = "2016-02-09T17:53:32-08:00"
var configFile *config.ConfigFile
var errorChannel chan error
var completionChannel chan bool
func handleErrors(errorChannel chan error, wg *sync.WaitGroup) {
defer wg.Done()
for {
select {
case err, ok := <-errorChannel:
if !ok {
return
}
if e, ok := err.(*gotelemetry.Error); ok {
logLevel := e.GetLogLevel()
if logLevel >= config.CLIConfig.LogLevel {
prefix := "Error"
switch logLevel {
case gotelemetry.LogLevelLog:
prefix = "Info "
case gotelemetry.LogLevelDebug:
prefix = "Debug"
}
log.Printf("%s: %s", prefix, err)
}
continue
}
log.Printf("Error: %s", err.Error())
}
}
}
func main() {
var err error
config.Init(VERSION, SOURCE_DATE)
configFile, err = config.NewConfigFile()
if err != nil {
log.Fatalf("Initialization error: %s", err)
}
errorChannel = make(chan error, 0)
completionChannel = make(chan bool, 1)
wg := &sync.WaitGroup{}
wg.Add(1)
go handleErrors(errorChannel, wg)
go run()
for {
select {
case <-completionChannel:
goto Done
}
}
Done:
for len(errorChannel) > 0 {
}
close(errorChannel)
wg.Wait()
log.Println("No more jobs to run; exiting.")
}
func run() {
if err := aggregations.Init(configFile.DataConfig().Listen, configFile.DataConfig().DataLocation, configFile.DataConfig().TTL, errorChannel); err != nil {
log.Fatalf("Initialization error: %s", err)
}
if err := graphite.Init(configFile, errorChannel); err != nil {
log.Fatalf("Initialization error: %s", err)
}
oauth.Init(configFile.OAuthConfig())
if config.CLIConfig.IsPiping {
payload, err := ioutil.ReadAll(os.Stdin)
if err != nil {
errorChannel <- err
}
agent.ProcessPipeRequest(configFile, errorChannel, completionChannel, payload)
} else if config.CLIConfig.IsNotifying {
agent.ProcessNotificationRequest(configFile, errorChannel, completionChannel, config.CLIConfig.NotificationChannel, config.CLIConfig.NotificationFlow, config.CLIConfig.Notification)
} else if config.CLIConfig.OAuthCommand != config.OAuthCommands.None {
oauth.RunCommand(config.CLIConfig, errorChannel, completionChannel)
} else {
_, err := job.NewJobManager(configFile, errorChannel, completionChannel)
if err != nil {
log.Fatalf("Initialization error: %s", err)
}
}
}