-
Notifications
You must be signed in to change notification settings - Fork 58
/
cli.go
216 lines (189 loc) · 9.15 KB
/
cli.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"context"
"fmt"
"github.com/cloudfoundry-community/firehose-to-syslog/authclient"
"github.com/cloudfoundry-incubator/uaago"
"log"
"os"
"os/signal"
"strings"
"time"
"github.com/cloudfoundry-community/firehose-to-syslog/caching"
"github.com/cloudfoundry-community/firehose-to-syslog/eventRouting"
"github.com/cloudfoundry-community/firehose-to-syslog/firehoseclient"
"github.com/cloudfoundry-community/firehose-to-syslog/logging"
"github.com/cloudfoundry-community/firehose-to-syslog/stats"
"github.com/cloudfoundry-community/go-cfclient"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
debug = kingpin.Flag("debug", "Enable debug mode. This disables forwarding to syslog").Default("false").Envar("DEBUG").Bool()
apiEndpoint = kingpin.Flag("api-endpoint", "Api endpoint address. For bosh-lite installation of CF: https://api.10.244.0.34.xip.io").Envar("API_ENDPOINT").Required().String()
dopplerEndpoint = kingpin.Flag("doppler-endpoint", "Overwrite default doppler endpoint return by /v2/info").Envar("DOPPLER_ENDPOINT").String()
syslogServer = kingpin.Flag("syslog-server", "Syslog server.").Envar("SYSLOG_ENDPOINT").String()
syslogProtocol = kingpin.Flag("syslog-protocol", "Syslog protocol (tcp/udp/tcp+tls).").Default("tcp").Envar("SYSLOG_PROTOCOL").String()
skipSSLSyslog = kingpin.Flag("skip-ssl-validation-syslog", "Skip Ssl validation for syslog").Default("false").Envar("SKIP_SSL_VALIDATION_SYSLOG").Bool()
subscriptionId = kingpin.Flag("subscription-id", "Id for the subscription.").Default("firehose").Envar("FIREHOSE_SUBSCRIPTION_ID").String()
clientID = kingpin.Flag("client-id", "Client ID.").Envar("FIREHOSE_CLIENT_ID").Required().String()
clientSecret = kingpin.Flag("client-secret", "Client secret.").Envar("FIREHOSE_CLIENT_SECRET").Required().String()
skipSSLValidation = kingpin.Flag("skip-ssl-validation", "Please don't").Default("false").Envar("SKIP_SSL_VALIDATION").Bool()
keepAlive = kingpin.Flag("fh-keep-alive", "Keep Alive duration for the firehose consumer").Default("25s").Envar("FH_KEEP_ALIVE").Duration()
minRetryDelay = kingpin.Flag("min-retry-delay", "Doppler Cloud Foundry Doppler min. retry delay duration").Default("500ms").Envar("MIN_RETRY_DELAY").Duration()
maxRetryDelay = kingpin.Flag("max-retry-delay", "Doppler Cloud Foundry Doppler max. retry delay duration").Default("1m").Envar("MAX_RETRY_DELAY").Duration()
maxRetryCount = kingpin.Flag("max-retry-count", "Doppler Cloud Foundry Doppler max. retry Count duration").Default("1000").Envar("MAX_RETRY_COUNT").Int()
bufferSize = kingpin.Flag("logs-buffer-size", "Number of envelope to be buffered").Default("10000").Envar("LOGS_BUFFER_SIZE").Int()
wantedEvents = kingpin.Flag("events", fmt.Sprintf("Comma separated list of events you would like. Valid options are %s", eventRouting.GetListAuthorizedEventEvents())).Default("LogMessage").Envar("EVENTS").String()
statServer = kingpin.Flag("enable-stats-server", "Will enable stats server on 8080").Default("false").Envar("ENABLE_STATS_SERVER").Bool()
boltDatabasePath = kingpin.Flag("boltdb-path", "Bolt Database path ").Envar("BOLTDB_PATH").String()
tickerTime = kingpin.Flag("cc-pull-time", "CloudController Polling time in sec").Default("60s").Envar("CF_PULL_TIME").Duration()
requestLimit = kingpin.Flag("cc-rps", "CloudController Polling request by second (IGNORED)").Default("50").Envar("CF_RPS").Int()
extraFields = kingpin.Flag("extra-fields", "Extra fields you want to annotate your events with, example: '--extra-fields=env:dev,something:other ").Default("").Envar("EXTRA_FIELDS").String()
orgs = kingpin.Flag("orgs", "Forwarded on the app logs from theses organisations' example: --orgs=org1,org2").Default("").Envar("ORGS").String()
modeProf = kingpin.Flag("mode-prof", "Enable profiling mode, one of [cpu, mem, block]").Default("").Envar("MODE_PROF").String()
pathProf = kingpin.Flag("path-prof", "Set the Path to write profiling file").Default("").Envar("PATH_PROF").String()
logFormatterType = kingpin.Flag("log-formatter-type", "Log formatter type to use. Valid options are text, json, json-cee. If none provided, defaults to json.").Envar("LOG_FORMATTER_TYPE").String()
certPath = kingpin.Flag("cert-pem-syslog", "Certificate Pem file").Envar("CERT_PEM").Default("").String()
ignoreMissingApps = kingpin.Flag("ignore-missing-apps", "Enable throttling on cache lookup for missing apps").Envar("IGNORE_MISSING_APPS").Default("false").Bool()
stripAppSuffixes = kingpin.Flag("strip-app-name-suffixes", "Suffixes that should be stripped from application names, comma separated").Envar("STRIP_APP_NAME_SUFFIXES").Default("").String()
)
const (
ExitCodeOk = 0
ExitCodeError = 1 + iota
)
var (
version = "0.0.0"
)
// CLI is the command line object
type CLI struct {
}
// Run invokes the CLI with the given arguments.
func (cli *CLI) Run(args []string) int {
kingpin.Version(version)
kingpin.Parse()
//Setup Logging
loggingClient := logging.NewLogging(*syslogServer, *syslogProtocol, *logFormatterType, *certPath, *debug, *skipSSLSyslog)
logging.LogStd(fmt.Sprintf("Starting firehose-to-syslog %s ", version), true)
//
// if *modeProf != "" {
// switch *modeProf {
// case "cpu":
// defer profile.Start(profile.CPUProfile, profile.ProfilePath(*pathProf)).Stop()
// case "mem":
// defer profile.Start(profile.MemProfile, profile.ProfilePath(*pathProf)).Stop()
// case "block":
// defer profile.Start(profile.BlockProfile, profile.ProfilePath(*pathProf)).Stop()
// default:
// // do nothing
// }
// }
c := cfclient.Config{
ApiAddress: *apiEndpoint,
ClientID: *clientID,
ClientSecret: *clientSecret,
SkipSslValidation: *skipSSLValidation,
UserAgent: "firehose-to-syslog/" + version,
}
cfClient, err := cfclient.NewClient(&c)
if err != nil {
logging.LogError("New Client: ", err)
return ExitCodeError
}
if len(*dopplerEndpoint) > 0 {
cfClient.Endpoint.DopplerEndpoint = *dopplerEndpoint
}
fmt.Println(cfClient.Endpoint.DopplerEndpoint)
logging.LogStd(fmt.Sprintf("Using %s as doppler endpoint", cfClient.Endpoint.DopplerEndpoint), true)
//Creating Caching
var cacheStore caching.CacheStore
switch {
case boltDatabasePath != nil && *boltDatabasePath != "":
cacheStore = &caching.BoltCacheStore{
Path: *boltDatabasePath,
}
default:
cacheStore = &caching.MemoryCacheStore{}
}
if err := cacheStore.Open(); err != nil {
logging.LogError("Error open cache: ", err)
return ExitCodeError
}
defer cacheStore.Close()
cachingClient := caching.NewCacheLazyFill(
&caching.CFClientAdapter{
CF: cfClient,
},
cacheStore,
&caching.CacheLazyFillConfig{
IgnoreMissingApps: *ignoreMissingApps,
CacheInvalidateTTL: *tickerTime,
StripAppSuffixes: strings.Split(*stripAppSuffixes, ","),
})
if caching.IsNeeded(*wantedEvents) {
// Bootstrap cache
logging.LogStd("Pre-filling cache...", true)
err = cachingClient.FillCache()
if err != nil {
logging.LogError("Error pre-filling cache: ", err)
return ExitCodeError
}
logging.LogStd("Cache filled.", true)
}
//Adding Stats
statistic := stats.NewStats()
go statistic.PerSec()
////Starting Http Server for Stats
if *statServer {
Server := &stats.Server{
Logger: log.New(os.Stdout, "", 1),
Stats: statistic,
}
go Server.Start()
}
//Creating Events
eventFilters := []eventRouting.EventFilter{eventRouting.HasIgnoreField, eventRouting.NotInCertainOrgs(*orgs)}
events := eventRouting.NewEventRouting(cachingClient, loggingClient, statistic, eventFilters)
err = events.SetupEventRouting(*wantedEvents)
if err != nil {
logging.LogError("Error setting up event routing: ", err)
return ExitCodeError
}
//Set extrafields if needed
events.SetExtraFields(*extraFields)
firehoseConfig := &firehoseclient.FirehoseConfig{
RLPAddr: strings.Replace(cfClient.Config.ApiAddress, "api", "log-stream", 1),
InsecureSSLSkipVerify: *skipSSLValidation,
FirehoseSubscriptionID: *subscriptionId,
BufferSize: *bufferSize,
}
if loggingClient.Connect() || *debug {
logging.LogStd("Connected to Syslog Server! Connecting to Firehose...", true)
} else {
logging.LogError("Failed connecting to the Syslog Server...Please check settings and try again!", "")
return ExitCodeError
}
uaa, err := uaago.NewClient(cfClient.Endpoint.AuthEndpoint)
if err != nil {
logging.LogError(fmt.Sprint("Failed connecting to Get token from UAA..", err), "")
}
ac := authclient.NewHttp(uaa, *clientID, *clientSecret, *skipSSLValidation)
firehoseClient := firehoseclient.NewFirehoseNozzle(events, firehoseConfig, statistic, ac)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
firehoseClient.Start(ctx)
signalChan := make(chan os.Signal, 1)
cleanupDone := make(chan bool)
signal.Notify(signalChan, os.Interrupt, os.Kill)
go func() {
for range signalChan {
fmt.Println("\nSignal Received, Stop reading and starting Draining...")
firehoseClient.StopReading()
cctx, tcancel := context.WithTimeout(context.TODO(), 30*time.Second)
tcancel()
firehoseClient.Draining(cctx)
cleanupDone <- true
}
}()
<-cleanupDone
return ExitCodeOk
}