forked from cloudfoundry-community/firehose-to-syslog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
206 lines (180 loc) · 8.54 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"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/firehose-to-syslog/uaatokenrefresher"
"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()
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("true").Envar("ENABLE_STATS_SERVER").Bool()
boltDatabasePath = kingpin.Flag("boltdb-path", "Bolt Database path ").Default("my.db").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").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()
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. 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()
ingestionKey = kingpin.Flag("ingestion-key", "use for auth").Default("").Envar("INGESTION_KEY").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()
os.Setenv("INGESTION_KEY", "[logdna@48950 key=\"" + *ingestionKey + "\" logtype=\"cf_json\"] ")
//Setup Logging
loggingClient := logging.NewLogging(*syslogServer, *syslogProtocol, *logFormatterType, *certPath, *debug)
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 cachingClient caching.Caching
if caching.IsNeeded(*wantedEvents) {
config := &caching.CachingBoltConfig{
Path: *boltDatabasePath,
IgnoreMissingApps: *ignoreMissingApps,
CacheInvalidateTTL: *tickerTime,
RequestBySec: *requestLimit,
}
cachingClient, err = caching.NewCachingBolt(cfClient, config)
if err != nil {
logging.LogError("Failed to create boltdb cache", err)
return ExitCodeError
}
} else {
cachingClient = caching.NewCachingEmpty()
}
if err := cachingClient.Open(); err != nil {
logging.LogError("Error open cache: ", err)
return ExitCodeError
}
defer cachingClient.Close()
//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
events := eventRouting.NewEventRouting(cachingClient, loggingClient, statistic)
err = events.SetupEventRouting(*wantedEvents)
if err != nil {
logging.LogError("Error setting up event routing: ", err)
return ExitCodeError
}
//Set extrafields if needed
events.SetExtraFields(*extraFields)
uaaRefresher, err := uaatokenrefresher.NewUAATokenRefresher(
cfClient.Endpoint.AuthEndpoint,
*clientID,
*clientSecret,
*skipSSLValidation,
)
if err != nil {
logging.LogError(fmt.Sprint("Failed connecting to Get token from UAA..", err), "")
}
firehoseConfig := &firehoseclient.FirehoseConfig{
TrafficControllerURL: cfClient.Endpoint.DopplerEndpoint,
InsecureSSLSkipVerify: *skipSSLValidation,
IdleTimeoutSeconds: *keepAlive,
FirehoseSubscriptionID: *subscriptionId,
MinRetryDelay: *minRetryDelay,
MaxRetryDelay: *maxRetryDelay,
MaxRetryCount: *maxRetryCount,
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
}
firehoseClient := firehoseclient.NewFirehoseNozzle(uaaRefresher, events, firehoseConfig, statistic)
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...\n")
firehoseClient.StopReading()
cctx, tcancel := context.WithTimeout(context.TODO(), 30*time.Second)
tcancel()
firehoseClient.Draining(cctx)
cleanupDone <- true
}
}()
<-cleanupDone
return ExitCodeOk
}