aboutsummaryrefslogtreecommitdiffstats
path: root/lib/agent/apiv1-monitoring.go
blob: cf651ce72a502b96aa1ebc1c6d29c405092c23fe (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
/*
 * Copyright (C) 2017-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 (
	"net/http"

	"gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/aglafb"
	common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
	"github.com/gin-gonic/gin"
)

//************************* AGL XDS Monitoring *************************

// getMonitoringTopo : return current AGL daemons topology using monitoring
func (s *APIService) getMonitoringTopo(c *gin.Context) {

	xdspvr, err := s._initXdsMonitoring()
	if err != nil {
		common.APIError(c, err.Error())
		return
	}

	var res aglafb.AfbReply
	if err = xdspvr.GetTopo(&res); err != nil {
		common.APIError(c, err.Error())
		return
	}

	if res.Request.Status != "success" {
		common.APIError(c, res.Request.Info)
		return
	}

	c.JSON(http.StatusOK, res.Response)
}

// startMonitoring : resquest to monitoring to start tracing
func (s *APIService) startMonitoring(c *gin.Context) {

	xdspvr, err := s._initXdsMonitoring()
	if err != nil {
		common.APIError(c, err.Error())
		return
	}

	var cfg XdsSuperVTraceConfig
	if c.BindJSON(&cfg) != nil {
		common.APIError(c, "Invalid config argument")
		return
	}
	s.Log.Debugf("Start Monitoring cfgArg %v", cfg)

	res := aglafb.NewAfbReply()
	if err = xdspvr.StartTrace(cfg, res); err != nil {
		common.APIError(c, err.Error())
		return
	}

	if !res.Success() {
		common.APIError(c, res.GetInfo())
		return
	}

	c.JSON(http.StatusOK, res.Response)
}

// stopMonitoring : resquest to monitoring to stop tracing
func (s *APIService) stopMonitoring(c *gin.Context) {

	xdspvr, err := s._initXdsMonitoring()
	if err != nil {
		common.APIError(c, err.Error())
		return
	}

	var res aglafb.AfbReply
	if err = xdspvr.StopTrace(&res); err != nil {
		common.APIError(c, err.Error())
		return
	}

	if res.Request.Status != "success" {
		common.APIError(c, res.Request.Info)
		return
	}

	c.JSON(http.StatusOK, res.Response)
}

// _initXdsMonitoring .
func (s *APIService) _initXdsMonitoring() (*XdsMonitoring, error) {

	if s.XdsMonitoring == nil {
		xs := NewXdsMonitoring(s.Context)
		if err := xs.Connect(); err != nil {
			return nil, err
		}
		s.XdsMonitoring = xs
	}
	return s.XdsMonitoring, nil
}

//*************************  AGL Low Collector *************************

// XdsLowCollectorConfig Configuration structure for ALC
type XdsLowCollectorConfig struct {
	Time int `json:"time"`
}

// StartLowCollector : resquest to Start low collector
func (s *APIService) StartLowCollector(c *gin.Context) {

	alc, err := s._initXdsLowCollector()
	if err != nil {
		common.APIError(c, err.Error())
		return
	}

	s.Log.Debugf("Init & config AGL Low Collector")

	if err = alc.Init(); err != nil {
		common.APIError(c, err.Error())
		return
	}

	// // Config is optional, if not set used define settings
	var cfg XdsLowCollectorConfig
	c.ShouldBindJSON(&cfg)

	s.Log.Debugf("Start Low Collector cfgArg %v", cfg)

	if err = alc.Start(cfg.Time); err != nil {
		common.APIError(c, err.Error())
		return
	}

	c.JSON(http.StatusOK, "done")
}

// StopLowCollector : resquest to Stop low collector
func (s *APIService) StopLowCollector(c *gin.Context) {

	alc, err := s._initXdsLowCollector()
	if err != nil {
		common.APIError(c, err.Error())
		return
	}

	s.Log.Debugf("Stop AGL Low Collector")

	if err = alc.Stop(); err != nil {
		common.APIError(c, err.Error())
	}

	// SEB TODO
	res := "done"
	c.JSON(http.StatusOK, res)
}

// ReadLowCollector : read one data
func (s *APIService) ReadLowCollector(c *gin.Context) {

	alc, err := s._initXdsLowCollector()
	if err != nil {
		common.APIError(c, err.Error())
		return
	}
	plugin := "cpu"
	s.Log.Debugf("Read data of %s plugin AGL Low Collector", plugin)

	var data interface{}
	if err = alc.Read(&data); err != nil {
		common.APIError(c, err.Error())
	}

	// SEB TODO
	res := "done"
	c.JSON(http.StatusOK, res)
}

// ResetLowCollector : Reset Low Collector
func (s *APIService) ResetLowCollector(c *gin.Context) {
	// SEB TODO
	common.APIError(c, "Not implemented yet")
}

// _initXdsLowCollector .
func (s *APIService) _initXdsLowCollector() (*XdsLowCollector, error) {

	if s.XdsLowCollector == nil {
		alc := NewXdsLowCollector(s.Context)
		if err := alc.Connect(); err != nil {
			return nil, err
		}
		s.XdsLowCollector = alc
	}
	return s.XdsLowCollector, nil
}