overflow_probes/probe/probe.go

114 lines
2.5 KiB
Go
Raw Normal View History

2017-09-18 09:21:58 +00:00
package probe
import (
"context"
2017-09-27 13:55:29 +00:00
"errors"
"fmt"
"net/http"
"path"
2017-09-21 11:04:30 +00:00
2017-09-27 13:55:29 +00:00
lfcc "git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_probes/central/api/module"
"git.loafle.net/overflow/overflow_probes/central/client"
2017-09-21 11:04:30 +00:00
"git.loafle.net/overflow/overflow_probes/commons"
2017-09-27 13:55:29 +00:00
"git.loafle.net/overflow/overflow_probes/config"
opuu "git.loafle.net/overflow/overflow_probes/util/url"
2017-09-18 09:21:58 +00:00
)
2017-09-27 13:55:29 +00:00
const (
probeEntryPoint = "/probe"
metricsEntryPoint = "/metrics"
)
2017-09-18 09:21:58 +00:00
type Probe interface {
2017-09-21 11:04:30 +00:00
commons.Handler
2017-09-18 09:21:58 +00:00
}
type probe struct {
2017-09-27 13:55:29 +00:00
configDir string
probeConfigPath string
probeConfig config.ProbeConfig
probeEntryURL string
metricsEntryURL string
probeClient client.Client
metricsClient client.Client
shutdownChan chan bool
}
func New(configDir string) (Probe, error) {
p := &probe{
configDir: configDir,
shutdownChan: make(chan bool),
}
var err error
if p.probeEntryURL, err = opuu.Join(config.Config.Central.URL, probeEntryPoint); nil != err {
return nil, err
}
if p.metricsEntryURL, err = opuu.Join(config.Config.Central.URL, metricsEntryPoint); nil != err {
return nil, err
}
p.probeConfigPath = path.Join(configDir, config.ProbeConfigFileName)
conf := lfcc.New()
if !lfcc.Exists(p.probeConfigPath) {
return nil, fmt.Errorf("Probe: Config file[%s] is not exist", p.probeConfigPath)
}
if err = conf.Load(&p.probeConfig, p.probeConfigPath); nil != err {
return nil, fmt.Errorf("Probe: Loading of Probe config file[%s] failed error[%v]", p.probeConfigPath, err)
}
return p, nil
2017-09-18 09:21:58 +00:00
}
2017-09-22 09:20:07 +00:00
func (p *probe) Serve() error {
2017-09-27 13:55:29 +00:00
var err error
2017-09-18 09:21:58 +00:00
2017-09-27 13:55:29 +00:00
if err = p.connectToCentral(); nil != err {
return err
}
ListenLoop:
for {
select {
case <-p.shutdownChan:
err = errors.New("Shutting down")
break ListenLoop
}
}
return err
2017-09-18 09:21:58 +00:00
}
func (p *probe) Shutdown(ctx context.Context) error {
2017-09-27 13:55:29 +00:00
p.shutdownChan <- true
return nil
}
func (p *probe) connectToCentral() error {
header := http.Header{}
header[module.ProbeHeader_ProbeKey] = []string{*p.probeConfig.ID}
var res *http.Response
var err error
p.probeClient = client.New()
if res, err = p.probeClient.Dial(p.probeEntryURL, header, 4096, 4096); nil != err {
return err
}
logging.Logger.Debug(fmt.Sprintf("Probe: Connect Probe HTTP Status[%s]", res.Status))
p.metricsClient = client.New()
if res, err = p.metricsClient.Dial(p.metricsEntryURL, header, 4096, 4096); nil != err {
return err
}
logging.Logger.Debug(fmt.Sprintf("Probe: Connect Metrics HTTP Status[%s]", res.Status))
2017-09-18 09:21:58 +00:00
return nil
}