overflow_probes/probe/probe.go

85 lines
2.0 KiB
Go
Raw Normal View History

2017-09-18 09:21:58 +00:00
package probe
import (
2017-09-27 13:55:29 +00:00
"fmt"
"net/http"
2017-09-21 11:04:30 +00:00
2017-09-27 13:55:29 +00:00
"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-29 12:30:27 +00:00
"git.loafle.net/overflow/overflow_probes/collector"
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 (
2017-09-29 12:30:27 +00:00
probeEntryPoint = "/probe"
fileEntryPoint = "/file"
2017-09-27 13:55:29 +00:00
)
2017-09-18 09:21:58 +00:00
type probe struct {
2017-09-29 12:30:27 +00:00
*commons.Handlers
probeEntryURL string
fileEntryURL string
metricEntryURL string
2017-09-27 13:55:29 +00:00
2017-09-29 12:30:27 +00:00
probeClient client.Client
fileClient client.Client
metricClient client.Client
2017-09-27 13:55:29 +00:00
2017-09-29 12:30:27 +00:00
collector collector.CollectorHandler
2017-09-27 13:55:29 +00:00
}
2017-09-29 12:30:27 +00:00
func New() (ProbeHandler, error) {
p := &probe{}
p.Handlers = commons.NewHandlers()
2017-09-27 13:55:29 +00:00
var err error
2017-09-29 12:30:27 +00:00
if p.probeEntryURL, err = opuu.Join(config.CFG.Central.URL, probeEntryPoint); nil != err {
2017-09-27 13:55:29 +00:00
return nil, err
}
2017-09-29 12:30:27 +00:00
if p.fileEntryURL, err = opuu.Join(config.CFG.Central.URL, fileEntryPoint); nil != err {
2017-09-27 13:55:29 +00:00
return nil, err
}
2017-09-29 12:30:27 +00:00
if c.metricEntryURL, err = opuu.Join(config.CFG.Central.URL, metricEntryPoint); nil != err {
return nil, err
2017-09-27 13:55:29 +00:00
}
2017-09-29 12:30:27 +00:00
p.probeClient = client.New()
p.fileClient = client.New()
c.metricClient = client.New()
2017-09-27 13:55:29 +00:00
2017-09-29 12:30:27 +00:00
if p.collector, err = collector.New(); nil != err {
return nil, err
}
2017-09-18 09:21:58 +00:00
2017-09-29 12:30:27 +00:00
return p, nil
2017-09-27 13:55:29 +00:00
}
2017-09-29 12:30:27 +00:00
func (p *probe) connectToCentralProbe() error {
2017-09-27 13:55:29 +00:00
var err error
2017-09-29 12:30:27 +00:00
var res *http.Response
if res, err = p.ConnectToCentralAsProbe(p.probeClient, p.probeEntryURL); nil != err {
2017-09-27 13:55:29 +00:00
return err
}
2017-09-29 12:30:27 +00:00
encryptionKey := res.Header.Get(module.ProbeHeader_Probe_EncryptionKey)
config.EncryptionKey = &encryptionKey
2017-09-28 06:08:43 +00:00
p.probeClient.OnNotify(p.onNotify)
2017-09-27 13:55:29 +00:00
2017-09-29 12:30:27 +00:00
if _, err = p.ConnectToCentralAsProbe(p.metricClient, p.metricEntryURL); nil != err {
2017-09-27 13:55:29 +00:00
return err
}
2017-09-18 09:21:58 +00:00
return nil
}
2017-09-29 12:30:27 +00:00
func (p *probe) sendNotifyToCentral(method string, params ...string) {
if err := p.probeClient.Notify(method, params); nil != err {
logging.Logger.Error(fmt.Sprintf("Probe notify error: [%v]", err))
}
}