diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/agent/agent.go | 15 | ||||
-rw-r--r-- | lib/agent/apiv1-monitoring.go | 214 | ||||
-rw-r--r-- | lib/agent/apiv1-supervisor.go | 114 | ||||
-rw-r--r-- | lib/agent/apiv1.go | 12 | ||||
-rw-r--r-- | lib/agent/xds-low-collector.go | 321 | ||||
-rw-r--r-- | lib/agent/xdsmonitoring.go (renamed from lib/agent/xdssupervior.go) | 69 | ||||
-rw-r--r-- | lib/aglafb/afb.go | 53 | ||||
-rw-r--r-- | lib/xdsconfig/config.go | 8 | ||||
-rw-r--r-- | lib/xdsconfig/configfile.go | 12 |
9 files changed, 647 insertions, 171 deletions
diff --git a/lib/agent/agent.go b/lib/agent/agent.go index 506976d..e90c1a0 100644 --- a/lib/agent/agent.go +++ b/lib/agent/agent.go @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017-2018 "IoT.bzh" + * Copyright (C) 2017-2019 "IoT.bzh" * Author Sebastien Douheret <sebastien@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -47,12 +47,13 @@ type Context struct { SThgCmd *exec.Cmd SThgInotCmd *exec.Cmd - webServer *WebServer - xdsServers map[string]*XdsServer - XdsSupervisor *XdsSupervisor - sessions *Sessions - events *Events - projects *Projects + webServer *WebServer + xdsServers map[string]*XdsServer + XdsMonitoring *XdsMonitoring + XdsLowCollector *XdsLowCollector + sessions *Sessions + events *Events + projects *Projects Exit chan os.Signal } diff --git a/lib/agent/apiv1-monitoring.go b/lib/agent/apiv1-monitoring.go new file mode 100644 index 0000000..cf651ce --- /dev/null +++ b/lib/agent/apiv1-monitoring.go @@ -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 +} diff --git a/lib/agent/apiv1-supervisor.go b/lib/agent/apiv1-supervisor.go deleted file mode 100644 index 20b9549..0000000 --- a/lib/agent/apiv1-supervisor.go +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (C) 2017-2018 "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" - - common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git" - "github.com/gin-gonic/gin" -) - -// getSupervisorTopo : return current AGL daemons topology using supervisor -func (s *APIService) getSupervisorTopo(c *gin.Context) { - - xdspvr, err := s._initXdsSupervisor() - if err != nil { - common.APIError(c, err.Error()) - return - } - - var res XdsSuperVReply - 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) -} - -// startSupervisor : resquest to supervisor to start tracing -func (s *APIService) startSupervisor(c *gin.Context) { - - xdspvr, err := s._initXdsSupervisor() - 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 Supervisor cfgArg %v\n", cfg) - - var res XdsSuperVReply - if err = xdspvr.StartTrace(cfg, &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) -} - -// stopSupervisor : resquest to supervisor to stop tracing -func (s *APIService) stopSupervisor(c *gin.Context) { - - xdspvr, err := s._initXdsSupervisor() - if err != nil { - common.APIError(c, err.Error()) - return - } - - var res XdsSuperVReply - 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) -} - -// _initXdsSupervisor . -func (s *APIService) _initXdsSupervisor() (*XdsSupervisor, error) { - - if s.XdsSupervisor == nil { - xs := NewXdsSupervisor(s.Context) - if err := xs.Connect(); err != nil { - return nil, err - } - s.XdsSupervisor = xs - } - return s.XdsSupervisor, nil -} diff --git a/lib/agent/apiv1.go b/lib/agent/apiv1.go index 4637bc4..ef9704c 100644 --- a/lib/agent/apiv1.go +++ b/lib/agent/apiv1.go @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017-2018 "IoT.bzh" + * Copyright (C) 2017-2019 "IoT.bzh" * Author Sebastien Douheret <sebastien@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -67,9 +67,13 @@ func NewAPIV1(ctx *Context) *APIService { s.apiRouter.POST("/events/register", s.eventsRegister) s.apiRouter.POST("/events/unregister", s.eventsUnRegister) - s.apiRouter.GET("/supervisor/topo", s.getSupervisorTopo) - s.apiRouter.POST("/supervisor/trace/start", s.startSupervisor) - s.apiRouter.POST("/supervisor/trace/stop", s.stopSupervisor) + s.apiRouter.GET("/monitoring/topo", s.getMonitoringTopo) + s.apiRouter.POST("/monitoring/trace/start", s.startMonitoring) + s.apiRouter.POST("/monitoring/trace/stop", s.stopMonitoring) + s.apiRouter.POST("/monitoring/alc/start", s.StartLowCollector) + s.apiRouter.POST("/monitoring/alc/stop", s.StopLowCollector) + s.apiRouter.GET("/monitoring/alc/read", s.ReadLowCollector) + s.apiRouter.POST("/monitoring/alc/reset", s.ResetLowCollector) return s } diff --git a/lib/agent/xds-low-collector.go b/lib/agent/xds-low-collector.go new file mode 100644 index 0000000..fdf696d --- /dev/null +++ b/lib/agent/xds-low-collector.go @@ -0,0 +1,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) + } + */ +} diff --git a/lib/agent/xdssupervior.go b/lib/agent/xdsmonitoring.go index 48b3f90..42e5324 100644 --- a/lib/agent/xdssupervior.go +++ b/lib/agent/xdsmonitoring.go @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017-2018 "IoT.bzh" + * Copyright (C) 2017-2019 "IoT.bzh" * Author Sebastien Douheret <sebastien@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -27,8 +27,8 @@ import ( uuid "github.com/satori/go.uuid" ) -// XdsSupervisor . -type XdsSupervisor struct { +// XdsMonitoring . +type XdsMonitoring struct { *Context ID string BaseURL string @@ -42,19 +42,6 @@ type XdsSupervisor struct { cbOnConnect OnConnectedXdsSupervCB } -// XdsSuperVRequest Resquest field of a reply -type XdsSuperVRequest struct { - Status string `json:"status"` - Info string `json:"info"` -} - -// XdsSuperVReply Reply structure of XDS Supervision Daemon -type XdsSuperVReply struct { - JType string `json:"jtype"` - Request XdsSuperVRequest `json:"request"` - Response interface{} `json:"response"` -} - // XdsSuperVTraceConfig type XdsSuperVTraceConfig struct { Pid int `json:"pid"` @@ -63,15 +50,15 @@ type XdsSuperVTraceConfig struct { } // OnConnectedXdsSupervCB connect callback -type OnConnectedXdsSupervCB func(svr *XdsSupervisor) error +type OnConnectedXdsSupervCB func(svr *XdsMonitoring) error -// NewXdsSupervisor creates an instance of XdsSupervisor -func NewXdsSupervisor(ctx *Context) *XdsSupervisor { - return &XdsSupervisor{ +// NewXdsMonitoring creates an instance of XdsMonitoring +func NewXdsMonitoring(ctx *Context) *XdsMonitoring { + return &XdsMonitoring{ Context: ctx, - ID: "XdsSupervisor-" + uuid.NewV1().String(), - BaseURL: ctx.Config.FileConf.ProfileConf.XDSBinder.URL, - ConnRetry: ctx.Config.FileConf.ProfileConf.XDSBinder.ConnRetry, + ID: "XdsMonitoring-" + uuid.NewV1().String(), + BaseURL: ctx.Config.FileConf.ProfileConf.XDSMonitoring.URL, + ConnRetry: ctx.Config.FileConf.ProfileConf.XDSMonitoring.ConnRetry, Connected: false, Disabled: false, @@ -79,8 +66,8 @@ func NewXdsSupervisor(ctx *Context) *XdsSupervisor { } } -// Connect Establish HTTP connection with XDS Supervisor Dameon -func (xs *XdsSupervisor) Connect() error { +// Connect Establish HTTP connection with XDS Monitoring Dameon +func (xs *XdsMonitoring) Connect() error { var err error var retry int @@ -97,12 +84,12 @@ func (xs *XdsSupervisor) Connect() error { // doing that avoid 2 notifs (conn false; conn true) on startup xs._NotifyState() } - xs.Log.Infof("Establishing connection to XDS Supervisor daemon (retry %d/%d)", retry, xs.ConnRetry) + xs.Log.Infof("Establishing connection to XDS Monitoring 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 Supervisor daemon failure") + return fmt.Errorf("Connection to XDS Monitoring daemon failure") } if err != nil { return err @@ -115,29 +102,29 @@ func (xs *XdsSupervisor) Connect() error { } // ConnectOn Register a callback on events reception -func (xs *XdsSupervisor) ConnectOn(f OnConnectedXdsSupervCB) error { +func (xs *XdsMonitoring) ConnectOn(f OnConnectedXdsSupervCB) error { xs.cbOnConnect = f return nil } -// GetVersion Send Get request to retrieve XDS Supervision version -func (xs *XdsSupervisor) GetVersion(res interface{}) error { +// GetVersion Send Get request to retrieve XDS Monitoring version +func (xs *XdsMonitoring) GetVersion(res interface{}) error { // FIXME add suffix URLSuffix in common HTTP client lib instead of _BuildURL return xs.client.Get(xs._BuildURL("/version"), &res) } // GetTopo Send Get request to retrieve Services/Daemons topology -func (xs *XdsSupervisor) GetTopo(res interface{}) error { +func (xs *XdsMonitoring) GetTopo(res interface{}) error { return xs.client.Get(xs._BuildURL("/list"), &res) } -// StartTrace Send Supervisor config and start tracing -func (xs *XdsSupervisor) StartTrace(cfg XdsSuperVTraceConfig, res interface{}) error { +// StartTrace Send Monitoring config and start tracing +func (xs *XdsMonitoring) StartTrace(cfg XdsSuperVTraceConfig, res interface{}) error { return xs.client.Post(xs._BuildURL("/trace/start"), cfg, &res) } -// StopTrace Send Supervisor stop tracing -func (xs *XdsSupervisor) StopTrace(res interface{}) error { +// StopTrace Send Monitoring stop tracing +func (xs *XdsMonitoring) StopTrace(res interface{}) error { var cfg interface{} return xs.client.Post(xs._BuildURL("/trace/stop"), cfg, res) } @@ -147,12 +134,12 @@ func (xs *XdsSupervisor) StopTrace(res interface{}) error { ***/ // _BuildURL . -func (xs *XdsSupervisor) _BuildURL(url string) string { +func (xs *XdsMonitoring) _BuildURL(url string) string { return url + "?token=HELLO&uuid=magic" } // Create HTTP client -func (xs *XdsSupervisor) _CreateConnectHTTP() error { +func (xs *XdsMonitoring) _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 @@ -179,17 +166,17 @@ func (xs *XdsSupervisor) _CreateConnectHTTP() error { if strings.Contains(err.Error(), "connection refused") { msg = fmt.Sprintf("(url: %s)", xs.BaseURL) } - return fmt.Errorf("ERROR: cannot connect to XDS Supervisor %s", msg) + return fmt.Errorf("ERROR: cannot connect to XDS Monitoring %s", msg) } if xs.client == nil { - return fmt.Errorf("ERROR: cannot connect to XDS Supervisor (null client)") + return fmt.Errorf("ERROR: cannot connect to XDS Monitoring (null client)") } return nil } // _Connect Established HTTP and WS connection -func (xs *XdsSupervisor) _Connect(reConn bool) error { +func (xs *XdsMonitoring) _Connect(reConn bool) error { var res interface{} if err := xs.client.Get(xs._BuildURL("/ping"), &res); err != nil { @@ -212,7 +199,7 @@ func (xs *XdsSupervisor) _Connect(reConn bool) error { } // _NotifyState Send event to notify changes -func (xs *XdsSupervisor) _NotifyState() { +func (xs *XdsMonitoring) _NotifyState() { /* TODO evSts := xaapiv1.ServerCfg{ diff --git a/lib/aglafb/afb.go b/lib/aglafb/afb.go new file mode 100644 index 0000000..85f6686 --- /dev/null +++ b/lib/aglafb/afb.go @@ -0,0 +1,53 @@ +/* + * 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 aglafb + +import "fmt" + +// AfbRequest Resquest field of a reply +type AfbRequest struct { + Status string `json:"status"` + Info string `json:"info"` +} + +// AfbReply Reply structure of XDS Monitoring Daemon +type AfbReply struct { + JType string `json:"jtype"` + Request AfbRequest `json:"request"` + Response interface{} `json:"response"` +} + +func NewAfbReply() *AfbReply { + return &AfbReply{} +} + +func (r *AfbReply) Success() bool { + return r.Request.Status == "success" +} + +func (r *AfbReply) Failure() bool { + return !r.Success() +} + +func (r *AfbReply) GetError() error { + return fmt.Errorf(r.Request.Info) +} + +func (r *AfbReply) GetInfo() string { + return r.Request.Info +} diff --git a/lib/xdsconfig/config.go b/lib/xdsconfig/config.go index 93c8f4b..6a5e750 100644 --- a/lib/xdsconfig/config.go +++ b/lib/xdsconfig/config.go @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017-2018 "IoT.bzh" + * Copyright (C) 2017-2019 "IoT.bzh" * Author Sebastien Douheret <sebastien@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -92,10 +92,14 @@ func Init(ctx *cli.Context, log *logrus.Logger) (*Config, error) { Home: defaultSTHomeDir, }, ProfileConf: ProfileConfT{ - XDSBinder: XDSBinderConf{ + XDSMonitoring: XDSMonitoringConf{ URL: "http://localhost:8810", ConnRetry: 10, }, + XDSLowCollector: XDSLowCollectorConf{ + URL: "http://localhost:8820", + ConnRetry: 10, + }, }, }, Log: log, diff --git a/lib/xdsconfig/configfile.go b/lib/xdsconfig/configfile.go index 7ddb010..85e26bc 100644 --- a/lib/xdsconfig/configfile.go +++ b/lib/xdsconfig/configfile.go @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017-2018 "IoT.bzh" + * Copyright (C) 2017-2019 "IoT.bzh" * Author Sebastien Douheret <sebastien@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -43,13 +43,19 @@ type XDSServerConf struct { APIPartialURL string `json:"-"` } -type XDSBinderConf struct { +type XDSMonitoringConf struct { + URL string `json:"url"` + ConnRetry int `json:"connRetry"` +} + +type XDSLowCollectorConf struct { URL string `json:"url"` ConnRetry int `json:"connRetry"` } type ProfileConfT struct { - XDSBinder XDSBinderConf `json:"xdsBinder"` + XDSMonitoring XDSMonitoringConf `json:"xdsMonitoring"` + XDSLowCollector XDSLowCollectorConf `json:"xdsLowCollector"` } type FileConfig struct { |