checkmgr/checkmgr.go
func New(cfg *Config) (*CheckManager, error) {

	if cfg == nil {
		return nil, errors.New("invalid Check Manager configuration (nil)")
	}

	cm := &CheckManager{enabled: true, initialized: false}

	// Setup logging for check manager
	cm.Debug = cfg.Debug
	cm.Log = cfg.Log
	if cm.Debug && cm.Log == nil {
		cm.Log = log.New(os.Stderr, "", log.LstdFlags)
	}
	if cm.Log == nil {
		cm.Log = log.New(ioutil.Discard, "", log.LstdFlags)
	}

	{
		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
	}

	if cfg.Check.SubmissionURL != "" {
		cm.checkSubmissionURL = api.URLType(cfg.Check.SubmissionURL)
	}

	// Blank API Token *disables* check management
	if cfg.API.TokenKey == "" {
		cm.enabled = false
	}

	if !cm.enabled && cm.checkSubmissionURL == "" {
		return nil, errors.New("invalid check manager configuration (no API token AND no submission url)")
	}

	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
	}

	// initialize check related data
	if cfg.Check.Type != "" {
		cm.checkType = CheckTypeType(cfg.Check.Type)
	} else {
		cm.checkType = defaultCheckType
	}

	idSetting := "0"
	if cfg.Check.ID != "" {
		idSetting = cfg.Check.ID
	}
	id, err := strconv.Atoi(idSetting)
	if err != nil {
		return nil, errors.Wrap(err, "converting check id")
	}
	cm.checkID = api.IDType(id)

	cm.checkInstanceID = CheckInstanceIDType(cfg.Check.InstanceID)
	cm.checkTarget = CheckTargetType(cfg.Check.TargetHost)
	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 {
		return nil, errors.Wrap(err, "parsing force metric activation")
	}
	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))
	}
	if cm.checkDisplayName == "" {
		cm.checkDisplayName = CheckDisplayNameType(cm.checkInstanceID)
	}
	if cm.checkTarget == "" {
		cm.checkTarget = CheckTargetType(cm.checkInstanceID)
	}

	if cfg.Check.SearchTag == "" {
		cm.checkSearchTag = []string{fmt.Sprintf("service:%s", an)}
	} else {
		cm.checkSearchTag = strings.Split(strings.Replace(cfg.Check.SearchTag, " ", "", -1), ",")
	}

	if cfg.Check.Tags != "" {
		cm.checkTags = strings.Split(strings.Replace(cfg.Check.Tags, " ", "", -1), ",")
	}

	cm.customConfigFields = make(map[string]string)
	if len(cfg.Check.CustomConfigFields) > 0 {
		for fld, val := range cfg.Check.CustomConfigFields {
			cm.customConfigFields[fld] = val
		}
	}

	dur := cfg.Check.MaxURLAge
	if dur == "" {
		dur = defaultTrapMaxURLAge
	}
	maxDur, err := time.ParseDuration(dur)
	if err != nil {
		return nil, errors.Wrap(err, "parsing max url age")
	}
	cm.trapMaxURLAge = maxDur

	// setup broker
	idSetting = "0"
	if cfg.Broker.ID != "" {
		idSetting = cfg.Broker.ID
	}
	id, err = strconv.Atoi(idSetting)
	if err != nil {
		return nil, errors.Wrap(err, "parsing broker id")
	}
	cm.brokerID = api.IDType(id)

	if cfg.Broker.SelectTag != "" {
		cm.brokerSelectTag = strings.Split(strings.Replace(cfg.Broker.SelectTag, " ", "", -1), ",")
	}

	dur = cfg.Broker.MaxResponseTime
	if dur == "" {
		dur = defaultBrokerMaxResponseTime
	}
	maxDur, err = time.ParseDuration(dur)
	if err != nil {
		return nil, errors.Wrap(err, "parsing broker max response time")
	}
	cm.brokerMaxResponseTime = maxDur

	// add user specified tls config for broker if provided
	cm.brokerTLS = cfg.Broker.TLSConfig

	// metrics
	cm.availableMetrics = make(map[string]bool)
	cm.metricTags = make(map[string][]string)

	return cm, nil
}
func New(cfg *Config) (*CheckManager, error) {

	if cfg == nil {
		return nil, errors.New("invalid Check Manager configuration (nil)")
	}

	cm := &CheckManager{enabled: true, initialized: false}

	// Setup logging for check manager
	cm.Debug = cfg.Debug
	cm.Log = cfg.Log
	if cm.Debug && cm.Log == nil {
		cm.Log = log.New(os.Stderr, "", log.LstdFlags)
	}
	if cm.Log == nil {
		cm.Log = log.New(ioutil.Discard, "", log.LstdFlags)
	}

	{
		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
	}

	if cfg.Check.SubmissionURL != "" {
		cm.checkSubmissionURL = api.URLType(cfg.Check.SubmissionURL)
	}

	// Blank API Token *disables* check management
	if cfg.API.TokenKey == "" {
		cm.enabled = false
	}

	if !cm.enabled && cm.checkSubmissionURL == "" {
		return nil, errors.New("invalid check manager configuration (no API token AND no submission url)")
	}

	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
	}

	// initialize check related data
	if cfg.Check.Type != "" {
		cm.checkType = CheckTypeType(cfg.Check.Type)
	} else {
		cm.checkType = defaultCheckType
	}

	idSetting := "0"
	if cfg.Check.ID != "" {
		idSetting = cfg.Check.ID
	}
	id, err := strconv.Atoi(idSetting)
	if err != nil {
		return nil, errors.Wrap(err, "converting check id")
	}
	cm.checkID = api.IDType(id)

	cm.checkInstanceID = CheckInstanceIDType(cfg.Check.InstanceID)
	cm.checkTarget = CheckTargetType(cfg.Check.TargetHost)
	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 {
		return nil, errors.Wrap(err, "parsing force metric activation")
	}
	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))
	}
	if cm.checkDisplayName == "" {
		cm.checkDisplayName = CheckDisplayNameType(cm.checkInstanceID)
	}
	if cm.checkTarget == "" {
		cm.checkTarget = CheckTargetType(cm.checkInstanceID)
	}

	if cfg.Check.SearchTag == "" {
		cm.checkSearchTag = []string{fmt.Sprintf("service:%s", an)}
	} else {
		cm.checkSearchTag = strings.Split(strings.Replace(cfg.Check.SearchTag, " ", "", -1), ",")
	}

	if cfg.Check.Tags != "" {
		cm.checkTags = strings.Split(strings.Replace(cfg.Check.Tags, " ", "", -1), ",")
	}

	cm.customConfigFields = make(map[string]string)
	if len(cfg.Check.CustomConfigFields) > 0 {
		for fld, val := range cfg.Check.CustomConfigFields {
			cm.customConfigFields[fld] = val
		}
	}

	dur := cfg.Check.MaxURLAge
	if dur == "" {
		dur = defaultTrapMaxURLAge
	}
	maxDur, err := time.ParseDuration(dur)
	if err != nil {
		return nil, errors.Wrap(err, "parsing max url age")
	}
	cm.trapMaxURLAge = maxDur

	// setup broker
	idSetting = "0"
	if cfg.Broker.ID != "" {
		idSetting = cfg.Broker.ID
	}
	id, err = strconv.Atoi(idSetting)
	if err != nil {
		return nil, errors.Wrap(err, "parsing broker id")
	}
	cm.brokerID = api.IDType(id)

	if cfg.Broker.SelectTag != "" {
		cm.brokerSelectTag = strings.Split(strings.Replace(cfg.Broker.SelectTag, " ", "", -1), ",")
	}

	dur = cfg.Broker.MaxResponseTime
	if dur == "" {
		dur = defaultBrokerMaxResponseTime
	}
	maxDur, err = time.ParseDuration(dur)
	if err != nil {
		return nil, errors.Wrap(err, "parsing broker max response time")
	}
	cm.brokerMaxResponseTime = maxDur

	// add user specified tls config for broker if provided
	cm.brokerTLS = cfg.Broker.TLSConfig

	// metrics
	cm.availableMetrics = make(map[string]bool)
	cm.metricTags = make(map[string][]string)

	return cm, nil
}
func New ( cfg * Config ) ( * CheckManager , error ) { if cfg == nil { return nil , errors . New ( " " ) } cm := & CheckManager { enabled : true , initialized : false } // Setup logging for check manager cm . Debug = cfg . Debug cm . Log = cfg . Log if cm . Debug && cm . Log == nil { cm . Log = log . New ( os . Stderr , " " , log . LstdFlags ) } if cm . Log == nil { cm . Log = log . New ( ioutil . Discard , " " , log . LstdFlags ) } { rx , err := regexp . Compile ( `^http\+unix://(?P<sockfile>.+)/write/(?P<id>.+)$` ) if err != nil { return nil , errors . Wrap ( err , " " ) } cm . sockRx = rx } if cfg . Check . SubmissionURL != " " { cm . checkSubmissionURL = api . URLType ( cfg . Check . SubmissionURL ) } // Blank API Token *disables* check management if cfg . API . TokenKey == " " { cm . enabled = false } if ! cm . enabled && cm . checkSubmissionURL == " " { return nil , errors . New ( " " ) } 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 , " " ) } cm . apih = apih } // initialize check related data if cfg . Check . Type != " " { cm . checkType = CheckTypeType ( cfg . Check . Type ) } else { cm . checkType = defaultCheckType } idSetting := " " if cfg . Check . ID != " " { idSetting = cfg . Check . ID } id , err := strconv . Atoi ( idSetting ) if err != nil { return nil , errors . Wrap ( err , " " ) } cm . checkID = api . IDType ( id ) cm . checkInstanceID = CheckInstanceIDType ( cfg . Check . InstanceID ) cm . checkTarget = CheckTargetType ( cfg . Check . TargetHost ) 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 { return nil , errors . Wrap ( err , " " ) } cm . forceMetricActivation = fm _ , an := path . Split ( os . Args [ 0 ] ) hn , err := os . Hostname ( ) if err != nil { hn = " " } if cm . checkInstanceID == " " { cm . checkInstanceID = CheckInstanceIDType ( fmt . Sprintf ( " " , hn , an ) ) } if cm . checkDisplayName == " " { cm . checkDisplayName = CheckDisplayNameType ( cm . checkInstanceID ) } if cm . checkTarget == " " { cm . checkTarget = CheckTargetType ( cm . checkInstanceID ) } if cfg . Check . SearchTag == " " { cm . checkSearchTag = [ ] string { fmt . Sprintf ( " " , an ) } } else { cm . checkSearchTag = strings . Split ( strings . Replace ( cfg . Check . SearchTag , " " , " " , - 1 ) , " " ) } if cfg . Check . Tags != " " { cm . checkTags = strings . Split ( strings . Replace ( cfg . Check . Tags , " " , " " , - 1 ) , " " ) } cm . customConfigFields = make ( map [ string ] string ) if len ( cfg . Check . CustomConfigFields ) > 0 { for fld , val := range cfg . Check . CustomConfigFields { cm . customConfigFields [ fld ] = val } } dur := cfg . Check . MaxURLAge if dur == " " { dur = defaultTrapMaxURLAge } maxDur , err := time . ParseDuration ( dur ) if err != nil { return nil , errors . Wrap ( err , " " ) } cm . trapMaxURLAge = maxDur // setup broker idSetting = " " if cfg . Broker . ID != " " { idSetting = cfg . Broker . ID } id , err = strconv . Atoi ( idSetting ) if err != nil { return nil , errors . Wrap ( err , " " ) } cm . brokerID = api . IDType ( id ) if cfg . Broker . SelectTag != " " { cm . brokerSelectTag = strings . Split ( strings . Replace ( cfg . Broker . SelectTag , " " , " " , - 1 ) , " " ) } dur = cfg . Broker . MaxResponseTime if dur == " " { dur = defaultBrokerMaxResponseTime } maxDur , err = time . ParseDuration ( dur ) if err != nil { return nil , errors . Wrap ( err , " " ) } cm . brokerMaxResponseTime = maxDur // add user specified tls config for broker if provided cm . brokerTLS = cfg . Broker . TLSConfig // metrics cm . availableMetrics = make ( map [ string ] bool ) cm . metricTags = make ( map [ string ] [ ] string ) return cm , nil }
// New returns a new check manager
New returns a new check manager