-
Notifications
You must be signed in to change notification settings - Fork 159
/
run.go
280 lines (240 loc) · 6.02 KB
/
run.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package subcmd
import (
"context"
"fmt"
influxdb "github.com/influxdata/influxdb1-client"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/foundation/metrics"
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config"
"github.com/openziti/sdk-golang/ziti/edge"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"net"
"net/http"
"net/url"
"os"
"reflect"
"sync/atomic"
"time"
"unsafe"
)
var runCmd = &cobra.Command{
Use: "run <config>",
Short: "Runs ziti probe",
Long: "runs ziti probe: captures latency to edge routers and submits them to probe-service",
Args: cobra.MaximumNArgs(1),
Run: theProbe.run,
}
const (
DefaultDbName = "ziti"
DefaultDbType = "influxdb"
DefaultInterval = 5 * 60 // 5 minutes
ProbeService = "probe-service"
)
func init() {
root.AddCommand(runCmd)
}
var log = pfxlog.Logger()
type probeCfg struct {
dbName string
dbType string
dbUser string
dbPassword string
interval int
}
var defaultConfig = probeCfg{
dbName: DefaultDbName,
dbType: DefaultDbType,
interval: DefaultInterval,
}
type probe struct {
cfg *probeCfg
influxDb *influxdb.Client
ctx ziti.Context
latest atomic.Value
closer chan interface{}
}
var theProbe = &probe{
closer: make(chan interface{}),
}
func (p *probe) sendMetrics() {
for {
select {
case <-time.After(time.Duration(p.cfg.interval) * time.Second):
message := p.ctx.Metrics().Poll()
if message == nil {
continue
}
bp, err := metrics.AsBatch(message)
if err != nil {
logrus.Errorln(err)
return
}
bp.Database = p.cfg.dbName
_, err = p.influxDb.Write(*bp)
if err == nil {
logrus.Debug("write complete")
} else {
logrus.Errorf("error writing to influxdb: %v", err)
for {
logrus.Info("attempting to reconnect")
if service, ok := p.ctx.GetService(ProbeService); ok {
if err := p.connectToInfluxDb(service); err == nil {
logrus.Info("reconnected")
if _, err = p.influxDb.Write(*bp); err != nil {
logrus.Errorf("failed to write after reconnect")
} else {
break
}
} else {
logrus.Errorf("failed to reconnect: %v", err)
}
} else {
logrus.Errorf("could not find %s", ProbeService)
}
time.Sleep(30 * time.Second)
}
}
}
}
}
func (p *probe) run(_ *cobra.Command, args []string) {
var err error
if len(args) == 0 {
log.Infof("loading ziti identity from env.ZITI_SDK_CONFIG[%s]", os.Getenv("ZITI_SDK_CONFIG"))
p.ctx = ziti.NewContext()
} else {
if cfg, err := config.NewFromFile(args[0]); err != nil {
log.WithError(err).Fatalf("failed to load config from file[%s]", args[0])
} else {
cfg.ConfigTypes = append(cfg.ConfigTypes, "ziti-probe-config.v1")
p.ctx = ziti.NewContextWithConfig(cfg)
}
}
for {
if err = p.ctx.Authenticate(); err != nil {
sleepDuration := 5 * time.Second
log.Errorf("failed to authenticate, trying again in %.2f seconds: %v", sleepDuration.Seconds(), err)
time.Sleep(sleepDuration)
} else {
break
}
}
for {
if _, err = p.ctx.GetServices(); err != nil {
sleepDuration := 5 * time.Second
log.Errorf("failed to load available services, try again in %.2f seconds: %v", sleepDuration.Seconds(), err)
time.Sleep(sleepDuration)
} else {
break
}
}
var service *edge.Service
for {
var found bool
service, found = p.ctx.GetService(ProbeService)
if !found {
sleepDuration := 5 * time.Second
log.Errorf("required service was not found, try again in %.2f seconds", sleepDuration.Seconds())
time.Sleep(sleepDuration)
} else {
break
}
}
for {
err := p.connectToInfluxDb(service)
if err != nil {
sleepDuration := 5 * time.Second
log.Errorf("could not connect to influxDb, trying again in %.2f seconds: %v", sleepDuration.Seconds(), err)
time.Sleep(sleepDuration)
} else {
break
}
}
go p.sendMetrics()
select {
case <-p.closer:
}
p.ctx.Close()
}
func (p *probe) connectToInfluxDb(service *edge.Service) error {
p.cfg = getProbeConfig(service)
var err error
p.influxDb, err = p.createInfluxDbClient(p.cfg)
if err != nil {
return fmt.Errorf("could not create influxDb client: %v", err)
}
_, res, err := p.influxDb.Ping()
if err != nil {
return fmt.Errorf("failed to get influxDb server info: %v", err)
}
log.Info("connected to influx version = ", res)
return nil
}
func getProbeConfig(service *edge.Service) *probeCfg {
cfg, found := service.Configs["ziti-probe-config.v1"]
if !found {
return &defaultConfig
}
dbName := DefaultDbName
if val, found := cfg["dbName"]; found {
if str, ok := val.(string); ok {
dbName = str
}
}
dbType := DefaultDbType
if val, found := cfg["dbType"]; found {
if str, ok := val.(string); ok {
dbType = str
}
}
var dbUser string
if val, found := cfg["dbUser"]; found {
if str, ok := val.(string); ok {
dbUser = str
}
}
var dbPassword string
if val, found := cfg["dbPassword"]; found {
if str, ok := val.(string); ok {
dbPassword = str
}
}
interval := DefaultInterval
if val, found := cfg["interval"]; found {
if flt, ok := val.(float64); ok {
interval = int(flt)
}
}
return &probeCfg{
dbName: dbName,
dbType: dbType,
dbUser: dbUser,
dbPassword: dbPassword,
interval: interval,
}
}
func (p *probe) createInfluxDbClient(cfg *probeCfg) (*influxdb.Client, error) {
httpC := &http.Client{
Transport: &http.Transport{
DialContext: func(c context.Context, network, addr string) (net.Conn, error) {
log.Infof("connecting to %s:%s", network, addr)
return p.ctx.Dial("probe-service")
},
},
}
influxConfig := influxdb.NewConfig()
u, _ := url.Parse("http://" + ProbeService)
influxConfig.URL = *u
influxConfig.Username = cfg.dbUser
influxConfig.Password = cfg.dbPassword
clt, _ := influxdb.NewClient(influxConfig)
ic := reflect.ValueOf(clt)
ict := ic.Type()
hcf, _ := ict.Elem().FieldByName("httpClient")
hcp := unsafe.Pointer(uintptr(unsafe.Pointer(clt)) + hcf.Offset)
httpCppClient := (**http.Client)(hcp)
*httpCppClient = httpC
return clt, nil
}