2016-11-09 20:30:07 +00:00
|
|
|
// Copyright 2016 Circonus, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
// Package checkmgr provides a check management interface to circonus-gometrics
|
2016-07-19 23:40:41 +00:00
|
|
|
package checkmgr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
2019-06-19 12:50:48 +00:00
|
|
|
"regexp"
|
2016-07-19 23:40:41 +00:00
|
|
|
"strconv"
|
2016-11-09 20:30:07 +00:00
|
|
|
"strings"
|
2016-07-19 23:40:41 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/circonus-labs/circonus-gometrics/api"
|
2019-06-19 12:50:48 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/tv42/httpunix"
|
2016-07-19 23:40:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Check management offers:
|
|
|
|
//
|
|
|
|
// Create a check if one cannot be found matching specific criteria
|
|
|
|
// Manage metrics in the supplied check (enabling new metrics as they are submitted)
|
|
|
|
//
|
|
|
|
// To disable check management, leave Config.Api.Token.Key blank
|
|
|
|
//
|
|
|
|
// use cases:
|
|
|
|
// configure without api token - check management disabled
|
|
|
|
// - configuration parameters other than Check.SubmissionUrl, Debug and Log are ignored
|
|
|
|
// - note: SubmissionUrl is **required** in this case as there is no way to derive w/o api
|
|
|
|
// configure with api token - check management enabled
|
2019-06-19 12:50:48 +00:00
|
|
|
// - all other configuration parameters affect how the trap url is obtained
|
2016-07-19 23:40:41 +00:00
|
|
|
// 1. provided (Check.SubmissionUrl)
|
|
|
|
// 2. via check lookup (CheckConfig.Id)
|
|
|
|
// 3. via a search using CheckConfig.InstanceId + CheckConfig.SearchTag
|
|
|
|
// 4. a new check is created
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultCheckType = "httptrap"
|
|
|
|
defaultTrapMaxURLAge = "60s" // 60 seconds
|
|
|
|
defaultBrokerMaxResponseTime = "500ms" // 500 milliseconds
|
|
|
|
defaultForceMetricActivation = "false"
|
|
|
|
statusActive = "active"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CheckConfig options for check
|
|
|
|
type CheckConfig struct {
|
|
|
|
// a specific submission url
|
|
|
|
SubmissionURL string
|
|
|
|
// a specific check id (not check bundle id)
|
|
|
|
ID string
|
|
|
|
// unique instance id string
|
|
|
|
// used to search for a check to use
|
|
|
|
// used as check.target when creating a check
|
|
|
|
InstanceID string
|
2019-06-19 12:50:48 +00:00
|
|
|
// explicitly set check.target (default: instance id)
|
|
|
|
TargetHost string
|
|
|
|
// a custom display name for the check (as viewed in UI Checks)
|
|
|
|
// default: instance id
|
|
|
|
DisplayName string
|
2016-11-09 20:30:07 +00:00
|
|
|
// unique check searching tag (or tags)
|
2016-07-19 23:40:41 +00:00
|
|
|
// used to search for a check to use (combined with instanceid)
|
|
|
|
// used as a regular tag when creating a check
|
|
|
|
SearchTag string
|
|
|
|
// httptrap check secret (for creating a check)
|
|
|
|
Secret string
|
|
|
|
// additional tags to add to a check (when creating a check)
|
|
|
|
// these tags will not be added to an existing check
|
2016-11-09 20:30:07 +00:00
|
|
|
Tags string
|
2016-07-19 23:40:41 +00:00
|
|
|
// max amount of time to to hold on to a submission url
|
|
|
|
// when a given submission fails (due to retries) if the
|
|
|
|
// time the url was last updated is > than this, the trap
|
|
|
|
// url will be refreshed (e.g. if the broker is changed
|
|
|
|
// in the UI) **only relevant when check management is enabled**
|
|
|
|
// e.g. 5m, 30m, 1h, etc.
|
|
|
|
MaxURLAge string
|
|
|
|
// force metric activation - if a metric has been disabled via the UI
|
|
|
|
// the default behavior is to *not* re-activate the metric; this setting
|
|
|
|
// overrides the behavior and will re-activate the metric when it is
|
|
|
|
// encountered. "(true|false)", default "false"
|
|
|
|
ForceMetricActivation string
|
2019-06-19 12:50:48 +00:00
|
|
|
// Type of check to use (default: httptrap)
|
|
|
|
Type string
|
|
|
|
// Custom check config fields (default: none)
|
|
|
|
CustomConfigFields map[string]string
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BrokerConfig options for broker
|
|
|
|
type BrokerConfig struct {
|
|
|
|
// a specific broker id (numeric portion of cid)
|
|
|
|
ID string
|
2016-11-09 20:30:07 +00:00
|
|
|
// one or more tags used to select 1-n brokers from which to select
|
|
|
|
// when creating a new check (e.g. datacenter:abc or loc:dfw,dc:abc)
|
2016-07-19 23:40:41 +00:00
|
|
|
SelectTag string
|
|
|
|
// for a broker to be considered viable it must respond to a
|
|
|
|
// connection attempt within this amount of time e.g. 200ms, 2s, 1m
|
|
|
|
MaxResponseTime string
|
2019-06-19 12:50:48 +00:00
|
|
|
// TLS configuration to use when communicating within broker
|
|
|
|
TLSConfig *tls.Config
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config options
|
|
|
|
type Config struct {
|
|
|
|
Log *log.Logger
|
|
|
|
Debug bool
|
|
|
|
|
|
|
|
// Circonus API config
|
|
|
|
API api.Config
|
|
|
|
// Check specific configuration options
|
|
|
|
Check CheckConfig
|
|
|
|
// Broker specific configuration options
|
|
|
|
Broker BrokerConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckTypeType check type
|
|
|
|
type CheckTypeType string
|
|
|
|
|
|
|
|
// CheckInstanceIDType check instance id
|
|
|
|
type CheckInstanceIDType string
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
// CheckTargetType check target/host
|
|
|
|
type CheckTargetType string
|
|
|
|
|
2016-07-19 23:40:41 +00:00
|
|
|
// CheckSecretType check secret
|
|
|
|
type CheckSecretType string
|
|
|
|
|
|
|
|
// CheckTagsType check tags
|
2016-11-09 20:30:07 +00:00
|
|
|
type CheckTagsType string
|
2016-07-19 23:40:41 +00:00
|
|
|
|
|
|
|
// CheckDisplayNameType check display name
|
|
|
|
type CheckDisplayNameType string
|
|
|
|
|
|
|
|
// BrokerCNType broker common name
|
|
|
|
type BrokerCNType string
|
|
|
|
|
|
|
|
// CheckManager settings
|
|
|
|
type CheckManager struct {
|
|
|
|
enabled bool
|
|
|
|
Log *log.Logger
|
|
|
|
Debug bool
|
|
|
|
apih *api.API
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
initialized bool
|
|
|
|
initializedmu sync.RWMutex
|
|
|
|
|
2016-07-19 23:40:41 +00:00
|
|
|
// check
|
|
|
|
checkType CheckTypeType
|
|
|
|
checkID api.IDType
|
|
|
|
checkInstanceID CheckInstanceIDType
|
2019-06-19 12:50:48 +00:00
|
|
|
checkTarget CheckTargetType
|
2016-11-09 20:30:07 +00:00
|
|
|
checkSearchTag api.TagType
|
2016-07-19 23:40:41 +00:00
|
|
|
checkSecret CheckSecretType
|
2016-11-09 20:30:07 +00:00
|
|
|
checkTags api.TagType
|
2019-06-19 12:50:48 +00:00
|
|
|
customConfigFields map[string]string
|
2016-07-19 23:40:41 +00:00
|
|
|
checkSubmissionURL api.URLType
|
|
|
|
checkDisplayName CheckDisplayNameType
|
|
|
|
forceMetricActivation bool
|
2016-11-09 20:30:07 +00:00
|
|
|
forceCheckUpdate bool
|
|
|
|
|
|
|
|
// metric tags
|
|
|
|
metricTags map[string][]string
|
|
|
|
mtmu sync.Mutex
|
2016-07-19 23:40:41 +00:00
|
|
|
|
|
|
|
// broker
|
|
|
|
brokerID api.IDType
|
2016-11-09 20:30:07 +00:00
|
|
|
brokerSelectTag api.TagType
|
2016-07-19 23:40:41 +00:00
|
|
|
brokerMaxResponseTime time.Duration
|
2019-06-19 12:50:48 +00:00
|
|
|
brokerTLS *tls.Config
|
2016-07-19 23:40:41 +00:00
|
|
|
|
|
|
|
// state
|
2019-06-19 12:50:48 +00:00
|
|
|
checkBundle *api.CheckBundle
|
|
|
|
cbmu sync.Mutex
|
|
|
|
availableMetrics map[string]bool
|
|
|
|
availableMetricsmu sync.Mutex
|
|
|
|
trapURL api.URLType
|
|
|
|
trapCN BrokerCNType
|
|
|
|
trapLastUpdate time.Time
|
|
|
|
trapMaxURLAge time.Duration
|
|
|
|
trapmu sync.Mutex
|
|
|
|
certPool *x509.CertPool
|
|
|
|
sockRx *regexp.Regexp
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trap config
|
|
|
|
type Trap struct {
|
2019-06-19 12:50:48 +00:00
|
|
|
URL *url.URL
|
|
|
|
TLS *tls.Config
|
|
|
|
IsSocket bool
|
|
|
|
SockTransport *httpunix.Transport
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCheckManager returns a new check manager
|
|
|
|
func NewCheckManager(cfg *Config) (*CheckManager, error) {
|
2019-06-19 12:50:48 +00:00
|
|
|
return New(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new check manager
|
|
|
|
func New(cfg *Config) (*CheckManager, error) {
|
2016-07-19 23:40:41 +00:00
|
|
|
|
|
|
|
if cfg == nil {
|
2019-06-19 12:50:48 +00:00
|
|
|
return nil, errors.New("invalid Check Manager configuration (nil)")
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
cm := &CheckManager{enabled: true, initialized: false}
|
2016-07-19 23:40:41 +00:00
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
// Setup logging for check manager
|
2016-07-19 23:40:41 +00:00
|
|
|
cm.Debug = cfg.Debug
|
|
|
|
cm.Log = cfg.Log
|
2016-11-09 20:30:07 +00:00
|
|
|
if cm.Debug && cm.Log == nil {
|
|
|
|
cm.Log = log.New(os.Stderr, "", log.LstdFlags)
|
|
|
|
}
|
2016-07-19 23:40:41 +00:00
|
|
|
if cm.Log == nil {
|
2016-11-09 20:30:07 +00:00
|
|
|
cm.Log = log.New(ioutil.Discard, "", log.LstdFlags)
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
{
|
|
|
|
rx, err := regexp.Compile(`^http\+unix://(?P<sockfile>.+)/write/(?P<id>.+)$`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "compiling socket regex")
|
|
|
|
}
|
|
|
|
cm.sockRx = rx
|
|
|
|
}
|
|
|
|
|
2016-07-19 23:40:41 +00:00
|
|
|
if cfg.Check.SubmissionURL != "" {
|
|
|
|
cm.checkSubmissionURL = api.URLType(cfg.Check.SubmissionURL)
|
|
|
|
}
|
2019-06-19 12:50:48 +00:00
|
|
|
|
2016-07-19 23:40:41 +00:00
|
|
|
// Blank API Token *disables* check management
|
|
|
|
if cfg.API.TokenKey == "" {
|
2019-06-19 12:50:48 +00:00
|
|
|
cm.enabled = false
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
if !cm.enabled && cm.checkSubmissionURL == "" {
|
|
|
|
return nil, errors.New("invalid check manager configuration (no API token AND no submission url)")
|
|
|
|
}
|
2016-07-19 23:40:41 +00:00
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
if cm.enabled {
|
|
|
|
// initialize api handle
|
|
|
|
cfg.API.Debug = cm.Debug
|
|
|
|
cfg.API.Log = cm.Log
|
|
|
|
apih, err := api.New(&cfg.API)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "initializing api client")
|
|
|
|
}
|
|
|
|
cm.apih = apih
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialize check related data
|
2019-06-19 12:50:48 +00:00
|
|
|
if cfg.Check.Type != "" {
|
|
|
|
cm.checkType = CheckTypeType(cfg.Check.Type)
|
|
|
|
} else {
|
|
|
|
cm.checkType = defaultCheckType
|
|
|
|
}
|
2016-07-19 23:40:41 +00:00
|
|
|
|
|
|
|
idSetting := "0"
|
|
|
|
if cfg.Check.ID != "" {
|
|
|
|
idSetting = cfg.Check.ID
|
|
|
|
}
|
|
|
|
id, err := strconv.Atoi(idSetting)
|
|
|
|
if err != nil {
|
2019-06-19 12:50:48 +00:00
|
|
|
return nil, errors.Wrap(err, "converting check id")
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
cm.checkID = api.IDType(id)
|
|
|
|
|
|
|
|
cm.checkInstanceID = CheckInstanceIDType(cfg.Check.InstanceID)
|
2019-06-19 12:50:48 +00:00
|
|
|
cm.checkTarget = CheckTargetType(cfg.Check.TargetHost)
|
2016-07-19 23:40:41 +00:00
|
|
|
cm.checkDisplayName = CheckDisplayNameType(cfg.Check.DisplayName)
|
|
|
|
cm.checkSecret = CheckSecretType(cfg.Check.Secret)
|
|
|
|
|
|
|
|
fma := defaultForceMetricActivation
|
|
|
|
if cfg.Check.ForceMetricActivation != "" {
|
|
|
|
fma = cfg.Check.ForceMetricActivation
|
|
|
|
}
|
|
|
|
fm, err := strconv.ParseBool(fma)
|
|
|
|
if err != nil {
|
2019-06-19 12:50:48 +00:00
|
|
|
return nil, errors.Wrap(err, "parsing force metric activation")
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
cm.forceMetricActivation = fm
|
|
|
|
|
|
|
|
_, an := path.Split(os.Args[0])
|
|
|
|
hn, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
hn = "unknown"
|
|
|
|
}
|
|
|
|
if cm.checkInstanceID == "" {
|
|
|
|
cm.checkInstanceID = CheckInstanceIDType(fmt.Sprintf("%s:%s", hn, an))
|
|
|
|
}
|
2019-06-19 12:50:48 +00:00
|
|
|
if cm.checkDisplayName == "" {
|
|
|
|
cm.checkDisplayName = CheckDisplayNameType(cm.checkInstanceID)
|
|
|
|
}
|
|
|
|
if cm.checkTarget == "" {
|
|
|
|
cm.checkTarget = CheckTargetType(cm.checkInstanceID)
|
|
|
|
}
|
2016-11-09 20:30:07 +00:00
|
|
|
|
|
|
|
if cfg.Check.SearchTag == "" {
|
|
|
|
cm.checkSearchTag = []string{fmt.Sprintf("service:%s", an)}
|
|
|
|
} else {
|
|
|
|
cm.checkSearchTag = strings.Split(strings.Replace(cfg.Check.SearchTag, " ", "", -1), ",")
|
|
|
|
}
|
2016-07-19 23:40:41 +00:00
|
|
|
|
2016-11-09 20:30:07 +00:00
|
|
|
if cfg.Check.Tags != "" {
|
|
|
|
cm.checkTags = strings.Split(strings.Replace(cfg.Check.Tags, " ", "", -1), ",")
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
cm.customConfigFields = make(map[string]string)
|
|
|
|
if len(cfg.Check.CustomConfigFields) > 0 {
|
|
|
|
for fld, val := range cfg.Check.CustomConfigFields {
|
|
|
|
cm.customConfigFields[fld] = val
|
|
|
|
}
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dur := cfg.Check.MaxURLAge
|
|
|
|
if dur == "" {
|
|
|
|
dur = defaultTrapMaxURLAge
|
|
|
|
}
|
|
|
|
maxDur, err := time.ParseDuration(dur)
|
|
|
|
if err != nil {
|
2019-06-19 12:50:48 +00:00
|
|
|
return nil, errors.Wrap(err, "parsing max url age")
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
cm.trapMaxURLAge = maxDur
|
|
|
|
|
|
|
|
// setup broker
|
|
|
|
idSetting = "0"
|
|
|
|
if cfg.Broker.ID != "" {
|
|
|
|
idSetting = cfg.Broker.ID
|
|
|
|
}
|
|
|
|
id, err = strconv.Atoi(idSetting)
|
|
|
|
if err != nil {
|
2019-06-19 12:50:48 +00:00
|
|
|
return nil, errors.Wrap(err, "parsing broker id")
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
cm.brokerID = api.IDType(id)
|
|
|
|
|
2016-11-09 20:30:07 +00:00
|
|
|
if cfg.Broker.SelectTag != "" {
|
|
|
|
cm.brokerSelectTag = strings.Split(strings.Replace(cfg.Broker.SelectTag, " ", "", -1), ",")
|
|
|
|
}
|
2016-07-19 23:40:41 +00:00
|
|
|
|
|
|
|
dur = cfg.Broker.MaxResponseTime
|
|
|
|
if dur == "" {
|
|
|
|
dur = defaultBrokerMaxResponseTime
|
|
|
|
}
|
|
|
|
maxDur, err = time.ParseDuration(dur)
|
|
|
|
if err != nil {
|
2019-06-19 12:50:48 +00:00
|
|
|
return nil, errors.Wrap(err, "parsing broker max response time")
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
cm.brokerMaxResponseTime = maxDur
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
// add user specified tls config for broker if provided
|
|
|
|
cm.brokerTLS = cfg.Broker.TLSConfig
|
|
|
|
|
2016-07-19 23:40:41 +00:00
|
|
|
// metrics
|
|
|
|
cm.availableMetrics = make(map[string]bool)
|
2016-11-09 20:30:07 +00:00
|
|
|
cm.metricTags = make(map[string][]string)
|
2016-07-19 23:40:41 +00:00
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
return cm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize for sending metrics
|
|
|
|
func (cm *CheckManager) Initialize() {
|
|
|
|
|
|
|
|
// if not managing the check, quicker initialization
|
|
|
|
if !cm.enabled {
|
|
|
|
err := cm.initializeTrapURL()
|
|
|
|
if err == nil {
|
|
|
|
cm.initializedmu.Lock()
|
|
|
|
cm.initialized = true
|
|
|
|
cm.initializedmu.Unlock()
|
|
|
|
} else {
|
|
|
|
cm.Log.Printf("[WARN] error initializing trap %s", err.Error())
|
|
|
|
}
|
|
|
|
return
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
// background initialization when we have to reach out to the api
|
|
|
|
go func() {
|
|
|
|
cm.apih.EnableExponentialBackoff()
|
|
|
|
err := cm.initializeTrapURL()
|
|
|
|
if err == nil {
|
|
|
|
cm.initializedmu.Lock()
|
|
|
|
cm.initialized = true
|
|
|
|
cm.initializedmu.Unlock()
|
|
|
|
} else {
|
|
|
|
cm.Log.Printf("[WARN] error initializing trap %s", err.Error())
|
|
|
|
}
|
|
|
|
cm.apih.DisableExponentialBackoff()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsReady reflects if the check has been initialied and metrics can be sent to Circonus
|
|
|
|
func (cm *CheckManager) IsReady() bool {
|
|
|
|
cm.initializedmu.RLock()
|
|
|
|
defer cm.initializedmu.RUnlock()
|
|
|
|
return cm.initialized
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
// GetSubmissionURL returns submission url for circonus
|
|
|
|
func (cm *CheckManager) GetSubmissionURL() (*Trap, error) {
|
2016-07-19 23:40:41 +00:00
|
|
|
if cm.trapURL == "" {
|
2019-06-19 12:50:48 +00:00
|
|
|
return nil, errors.Errorf("get submission url - submission url unavailable")
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
trap := &Trap{}
|
|
|
|
|
|
|
|
u, err := url.Parse(string(cm.trapURL))
|
|
|
|
if err != nil {
|
2019-06-19 12:50:48 +00:00
|
|
|
return nil, errors.Wrap(err, "get submission url")
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
trap.URL = u
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
if u.Scheme == "http+unix" {
|
|
|
|
service := "circonus-agent"
|
|
|
|
sockPath := ""
|
|
|
|
metricID := ""
|
|
|
|
|
|
|
|
subNames := cm.sockRx.SubexpNames()
|
|
|
|
matches := cm.sockRx.FindAllStringSubmatch(string(cm.trapURL), -1)
|
|
|
|
for _, match := range matches {
|
|
|
|
for idx, val := range match {
|
|
|
|
switch subNames[idx] {
|
|
|
|
case "sockfile":
|
|
|
|
sockPath = val
|
|
|
|
case "id":
|
|
|
|
metricID = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sockPath == "" || metricID == "" {
|
|
|
|
return nil, errors.Errorf("get submission url - invalid socket url (%s)", cm.trapURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err = url.Parse(fmt.Sprintf("http+unix://%s/write/%s", service, metricID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "get submission url")
|
|
|
|
}
|
|
|
|
trap.URL = u
|
|
|
|
|
|
|
|
trap.SockTransport = &httpunix.Transport{
|
|
|
|
DialTimeout: 100 * time.Millisecond,
|
|
|
|
RequestTimeout: 1 * time.Second,
|
|
|
|
ResponseHeaderTimeout: 1 * time.Second,
|
|
|
|
}
|
|
|
|
trap.SockTransport.RegisterLocation(service, sockPath)
|
|
|
|
trap.IsSocket = true
|
|
|
|
}
|
|
|
|
|
2016-07-19 23:40:41 +00:00
|
|
|
if u.Scheme == "https" {
|
2019-06-19 12:50:48 +00:00
|
|
|
// preference user-supplied TLS configuration
|
|
|
|
if cm.brokerTLS != nil {
|
|
|
|
trap.TLS = cm.brokerTLS
|
|
|
|
return trap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// api.circonus.com uses a public CA signed certificate
|
|
|
|
// trap.noit.circonus.net uses Circonus CA private certificate
|
|
|
|
// enterprise brokers use private CA certificate
|
|
|
|
if trap.URL.Hostname() == "api.circonus.com" {
|
|
|
|
return trap, nil
|
|
|
|
}
|
|
|
|
|
2016-07-19 23:40:41 +00:00
|
|
|
if cm.certPool == nil {
|
2019-06-19 12:50:48 +00:00
|
|
|
if err := cm.loadCACert(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "get submission url")
|
|
|
|
}
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
t := &tls.Config{
|
|
|
|
RootCAs: cm.certPool,
|
|
|
|
}
|
|
|
|
if cm.trapCN != "" {
|
|
|
|
t.ServerName = string(cm.trapCN)
|
|
|
|
}
|
|
|
|
trap.TLS = t
|
|
|
|
}
|
|
|
|
|
|
|
|
return trap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetTrap URL, force request to the API for the submission URL and broker ca cert
|
|
|
|
func (cm *CheckManager) ResetTrap() error {
|
|
|
|
if cm.trapURL == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cm.trapURL = ""
|
2019-06-19 12:50:48 +00:00
|
|
|
cm.certPool = nil // force re-fetching CA cert (if custom TLS config not supplied)
|
|
|
|
return cm.initializeTrapURL()
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RefreshTrap check when the last time the URL was reset, reset if needed
|
2019-06-19 12:50:48 +00:00
|
|
|
func (cm *CheckManager) RefreshTrap() error {
|
2016-07-19 23:40:41 +00:00
|
|
|
if cm.trapURL == "" {
|
2019-06-19 12:50:48 +00:00
|
|
|
return nil
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if time.Since(cm.trapLastUpdate) >= cm.trapMaxURLAge {
|
2019-06-19 12:50:48 +00:00
|
|
|
return cm.ResetTrap()
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
2019-06-19 12:50:48 +00:00
|
|
|
|
|
|
|
return nil
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|