Add LowCollector & rename Supervisor to Monitoring
[src/xds/xds-agent.git] / lib / agent / apiv1-monitoring.go
1 /*
2  * Copyright (C) 2017-2019 "IoT.bzh"
3  * Author Sebastien Douheret <sebastien@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package agent
19
20 import (
21         "net/http"
22
23         "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/aglafb"
24         common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
25         "github.com/gin-gonic/gin"
26 )
27
28 //************************* AGL XDS Monitoring *************************
29
30 // getMonitoringTopo : return current AGL daemons topology using monitoring
31 func (s *APIService) getMonitoringTopo(c *gin.Context) {
32
33         xdspvr, err := s._initXdsMonitoring()
34         if err != nil {
35                 common.APIError(c, err.Error())
36                 return
37         }
38
39         var res aglafb.AfbReply
40         if err = xdspvr.GetTopo(&res); err != nil {
41                 common.APIError(c, err.Error())
42                 return
43         }
44
45         if res.Request.Status != "success" {
46                 common.APIError(c, res.Request.Info)
47                 return
48         }
49
50         c.JSON(http.StatusOK, res.Response)
51 }
52
53 // startMonitoring : resquest to monitoring to start tracing
54 func (s *APIService) startMonitoring(c *gin.Context) {
55
56         xdspvr, err := s._initXdsMonitoring()
57         if err != nil {
58                 common.APIError(c, err.Error())
59                 return
60         }
61
62         var cfg XdsSuperVTraceConfig
63         if c.BindJSON(&cfg) != nil {
64                 common.APIError(c, "Invalid config argument")
65                 return
66         }
67         s.Log.Debugf("Start Monitoring cfgArg %v", cfg)
68
69         res := aglafb.NewAfbReply()
70         if err = xdspvr.StartTrace(cfg, res); err != nil {
71                 common.APIError(c, err.Error())
72                 return
73         }
74
75         if !res.Success() {
76                 common.APIError(c, res.GetInfo())
77                 return
78         }
79
80         c.JSON(http.StatusOK, res.Response)
81 }
82
83 // stopMonitoring : resquest to monitoring to stop tracing
84 func (s *APIService) stopMonitoring(c *gin.Context) {
85
86         xdspvr, err := s._initXdsMonitoring()
87         if err != nil {
88                 common.APIError(c, err.Error())
89                 return
90         }
91
92         var res aglafb.AfbReply
93         if err = xdspvr.StopTrace(&res); err != nil {
94                 common.APIError(c, err.Error())
95                 return
96         }
97
98         if res.Request.Status != "success" {
99                 common.APIError(c, res.Request.Info)
100                 return
101         }
102
103         c.JSON(http.StatusOK, res.Response)
104 }
105
106 // _initXdsMonitoring .
107 func (s *APIService) _initXdsMonitoring() (*XdsMonitoring, error) {
108
109         if s.XdsMonitoring == nil {
110                 xs := NewXdsMonitoring(s.Context)
111                 if err := xs.Connect(); err != nil {
112                         return nil, err
113                 }
114                 s.XdsMonitoring = xs
115         }
116         return s.XdsMonitoring, nil
117 }
118
119 //*************************  AGL Low Collector *************************
120
121 // XdsLowCollectorConfig Configuration structure for ALC
122 type XdsLowCollectorConfig struct {
123         Time int `json:"time"`
124 }
125
126 // StartLowCollector : resquest to Start low collector
127 func (s *APIService) StartLowCollector(c *gin.Context) {
128
129         alc, err := s._initXdsLowCollector()
130         if err != nil {
131                 common.APIError(c, err.Error())
132                 return
133         }
134
135         s.Log.Debugf("Init & config AGL Low Collector")
136
137         if err = alc.Init(); err != nil {
138                 common.APIError(c, err.Error())
139                 return
140         }
141
142         // // Config is optional, if not set used define settings
143         var cfg XdsLowCollectorConfig
144         c.ShouldBindJSON(&cfg)
145
146         s.Log.Debugf("Start Low Collector cfgArg %v", cfg)
147
148         if err = alc.Start(cfg.Time); err != nil {
149                 common.APIError(c, err.Error())
150                 return
151         }
152
153         c.JSON(http.StatusOK, "done")
154 }
155
156 // StopLowCollector : resquest to Stop low collector
157 func (s *APIService) StopLowCollector(c *gin.Context) {
158
159         alc, err := s._initXdsLowCollector()
160         if err != nil {
161                 common.APIError(c, err.Error())
162                 return
163         }
164
165         s.Log.Debugf("Stop AGL Low Collector")
166
167         if err = alc.Stop(); err != nil {
168                 common.APIError(c, err.Error())
169         }
170
171         // SEB TODO
172         res := "done"
173         c.JSON(http.StatusOK, res)
174 }
175
176 // ReadLowCollector : read one data
177 func (s *APIService) ReadLowCollector(c *gin.Context) {
178
179         alc, err := s._initXdsLowCollector()
180         if err != nil {
181                 common.APIError(c, err.Error())
182                 return
183         }
184         plugin := "cpu"
185         s.Log.Debugf("Read data of %s plugin AGL Low Collector", plugin)
186
187         var data interface{}
188         if err = alc.Read(&data); err != nil {
189                 common.APIError(c, err.Error())
190         }
191
192         // SEB TODO
193         res := "done"
194         c.JSON(http.StatusOK, res)
195 }
196
197 // ResetLowCollector : Reset Low Collector
198 func (s *APIService) ResetLowCollector(c *gin.Context) {
199         // SEB TODO
200         common.APIError(c, "Not implemented yet")
201 }
202
203 // _initXdsLowCollector .
204 func (s *APIService) _initXdsLowCollector() (*XdsLowCollector, error) {
205
206         if s.XdsLowCollector == nil {
207                 alc := NewXdsLowCollector(s.Context)
208                 if err := alc.Connect(); err != nil {
209                         return nil, err
210                 }
211                 s.XdsLowCollector = alc
212         }
213         return s.XdsLowCollector, nil
214 }