Add LowCollector & rename Supervisor to Monitoring
[src/xds/xds-agent.git] / lib / agent / apiv1-monitoring.go
diff --git a/lib/agent/apiv1-monitoring.go b/lib/agent/apiv1-monitoring.go
new file mode 100644 (file)
index 0000000..cf651ce
--- /dev/null
@@ -0,0 +1,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
+}