aboutsummaryrefslogtreecommitdiffstats
path: root/lib/agent/xds-low-collector.go
blob: fdf696db5d01ff1486391df330f8558ae4012c82 (plain)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * Copyright (C) 2019 "IoT.bzh"
 * Author Sebastien Douheret <sebastien@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package agent

import (
	"fmt"
	"io"
	"strings"
	"time"

	"gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/aglafb"
	common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
	uuid "github.com/satori/go.uuid"
)

// XdsLowCollector .
type XdsLowCollector struct {
	*Context
	ID                 string
	BaseURL            string
	ConnRetry          int
	Connected          bool
	Disabled           bool
	DefaultPlugins     []string
	DefaultCollectTime int
	// Private fields
	client      *common.HTTPClient
	logOut      io.Writer
	cbOnConnect OnConnectedXdsAlcCB
}

// OnConnectedXdsAlcCB connect callback
type OnConnectedXdsAlcCB func(svr *XdsLowCollector) error

// NewXdsLowCollector creates an instance of XdsLowCollector
func NewXdsLowCollector(ctx *Context) *XdsLowCollector {
	return &XdsLowCollector{
		Context:   ctx,
		ID:        "XdsAlc-" + uuid.NewV1().String(),
		BaseURL:   ctx.Config.FileConf.ProfileConf.XDSLowCollector.URL,
		ConnRetry: ctx.Config.FileConf.ProfileConf.XDSLowCollector.ConnRetry,
		Connected: false,
		Disabled:  false,
		logOut:    ctx.Log.Out,
		DefaultPlugins: []string{
			"cpu",
			"memory",
			// SEB "processes",
			//"cpufreq",
			//"thermal",
			//"systemd_journal",
			// SEB "systemd_file",
		},
		DefaultCollectTime: 5,
	}
}

// Connect Establish HTTP connection with XDS Low Collector Dameon
func (xs *XdsLowCollector) Connect() error {
	var err error
	var retry int

	xs.Disabled = false
	xs.Connected = false

	err = nil
	for retry = xs.ConnRetry; retry > 0; retry-- {
		if err = xs._CreateConnectHTTP(); err == nil {
			break
		}
		if retry == xs.ConnRetry {
			// Notify only on the first conn error
			// doing that avoid 2 notifs (conn false; conn true) on startup
			xs._NotifyState()
		}
		xs.Log.Infof("Establishing connection to XDS Low Collector daemon (retry %d/%d)", retry, xs.ConnRetry)
		time.Sleep(time.Second)
	}
	if retry == 0 {
		// FIXME: re-use _Reconnect to wait longer in background
		return fmt.Errorf("Connection to XDS Low Collector daemon failure")
	}
	if err != nil {
		return err
	}

	// Check HTTP connection and establish WS connection
	err = xs._Connect(false)

	return err
}

// ConnectOn Register a callback on events reception
func (xs *XdsLowCollector) ConnectOn(f OnConnectedXdsAlcCB) error {
	xs.cbOnConnect = f
	return nil
}

// GetVersion Send Get request to retrieve XDS Low Collector version
func (xs *XdsLowCollector) GetVersion(res interface{}) error {
	// FIXME add suffix URLSuffix in common HTTP client lib instead of _BuildURL
	return xs.client.Get(xs._BuildURL("/version"), &res)
}

// Init Initialize collector plugins
func (xs *XdsLowCollector) Init() error {
	var err error

	// Directly send config in order to init and config plugins

	type alcCfgPluginT struct {
		Plugin string `json:"plugin"`
		Config string `json:"config"`
	}

	cfg := []alcCfgPluginT{}

	for _, p := range xs.DefaultPlugins {
		cfg = append(cfg, alcCfgPluginT{
			Plugin: p,
			Config: "default",
		})
	}

	res := aglafb.NewAfbReply()
	xs.Log.Debugf("Low Collector /config %v", cfg)
	err = xs.client.Post(xs._BuildURL("/config"), cfg, res)

	if err == nil && !res.Success() {
		err = res.GetError()
	}

	return err
}

// Start data collection
func (xs *XdsLowCollector) Start(time int) error {
	var err error

	// TODO - SEB : support start one or multiple plugins

	if time == 0 {
		time = xs.DefaultCollectTime
	}

	type alcStartT struct {
		Plugin string `json:"plugin"`
		Time   int    `json:"time"`
	}

	// TODO SEB : allow to start only 1 plugin
	allInOne := true
	if allInOne {

		cfg := []alcStartT{}
		for _, p := range xs.DefaultPlugins {
			cfg = append(cfg, alcStartT{Plugin: p, Time: time})
		}

		res := aglafb.NewAfbReply()
		xs.Log.Debugf("Low Collector /start %v", cfg)
		err = xs.client.Post(xs._BuildURL("/start"), cfg, res)

		if err == nil && !res.Success() {
			err = res.GetError()
		}
	} else {
		for _, p := range xs.DefaultPlugins {
			cfg := alcStartT{Plugin: p, Time: time}

			res := aglafb.NewAfbReply()
			xs.Log.Debugf("Low Collector /start %v", cfg)
			err = xs.client.Post(xs._BuildURL("/start"), cfg, res)
			if err != nil {
				return err
			}
			if !res.Success() {
				return res.GetError()
			}
		}
	}

	return err
}

// Stop data collection
func (xs *XdsLowCollector) Stop() error {

	// TODO - SEB : support start one or multiple plugins

	type alcStopT struct {
		Plugin []string `json:"plugin"`
	}

	cfg := alcStopT{}
	for _, p := range xs.DefaultPlugins {
		cfg.Plugin = append(cfg.Plugin, p)
	}

	res := aglafb.NewAfbReply()
	xs.Log.Debugf("Low Collector /stop %v", cfg)
	err := xs.client.Post(xs._BuildURL("/stop"), cfg, res)

	if err == nil && !res.Success() {
		err = res.GetError()
	}

	return err
}

// Read a single data of a specific plugin
func (xs *XdsLowCollector) Read(data interface{}) error {
	return fmt.Errorf("No implemented")
}

/***
** Private functions
***/

// _BuildURL .
func (xs *XdsLowCollector) _BuildURL(url string) string {
	return url + "?token=HELLO&uuid=magic"
}

// Create HTTP client
func (xs *XdsLowCollector) _CreateConnectHTTP() error {
	var err error
	// FIXME SEB - Client key not in header but in cookie
	// temporary workaround: used _BuildURL to append uuid=magic in URL
	// map[Set-Cookie:[x-afb-uuid-5678=2b185cc3-276b-4097-91fa-d607eaf937e6; Path=/api; Max-Age=32000000; ...
	//port := strings.Split(xs.BaseURL, ":")[2]
	//"x-afb-uuid-" + port

	xs.client, err = common.HTTPNewClient(xs.BaseURL,
		common.HTTPClientConfig{
			//HeaderClientKeyName: "Xds-Sid",
			HeaderAPIKeyName: "token",
			Apikey:           "HELLO",
			URLPrefix:        "/api/alc",
			CsrfDisable:      true,
			LogOut:           xs.logOut,
			LogPrefix:        "XDSALC: ",
			LogLevel:         common.HTTPLogLevelWarning,
		})

	xs.client.SetLogLevel(xs.Log.Level.String())

	if err != nil {
		msg := ": " + err.Error()
		if strings.Contains(err.Error(), "connection refused") {
			msg = fmt.Sprintf("(url: %s)", xs.BaseURL)
		}
		return fmt.Errorf("ERROR: cannot connect to XDS Low Collector %s", msg)
	}
	if xs.client == nil {
		return fmt.Errorf("ERROR: cannot connect to XDS Low Collector (null client)")
	}

	return nil
}

// _Connect Established HTTP and WS connection
func (xs *XdsLowCollector) _Connect(reConn bool) error {
	var res interface{}
	if err := xs.client.Get(xs._BuildURL("/ping"), &res); err != nil {

		// SEB FIXME tempo Hack
		time.Sleep(time.Microsecond * 300)
		if err := xs.client.Get(xs._BuildURL("/ping"), &res); err != nil {
			// SEB Hack tempo
			// xs.Connected = false
			// if !reConn {
			// 	xs._NotifyState()
			// }
			// return err
		}
	}

	xs.Connected = true

	// Call OnConnect callback
	if xs.cbOnConnect != nil {
		xs.cbOnConnect(xs)
	}

	xs._NotifyState()
	return nil
}

// _NotifyState Send event to notify changes
func (xs *XdsLowCollector) _NotifyState() {

	/* TODO
	evSts := xaapiv1.ServerCfg{
		ID:         xs.ID,
		URL:        xs.BaseURL,
		APIURL:     xs.APIURL,
		PartialURL: xs.PartialURL,
		ConnRetry:  xs.ConnRetry,
		Connected:  xs.Connected,
	}
	if err := xs.events.Emit(xaapiv1.EVTServerConfig, evSts, ""); err != nil {
		xs.Log.Warningf("Cannot notify XdsServer state change: %v", err)
	}
	*/
}