checkmgr/broker.go
func (cm *CheckManager) isValidBroker(broker *api.Broker) bool {
	var brokerHost string
	var brokerPort string

	if broker.Type != "circonus" && broker.Type != "enterprise" {
		return false
	}

	valid := false

	for _, detail := range broker.Details {
		detail := detail

		// broker must be active
		if detail.Status != statusActive {
			if cm.Debug {
				cm.Log.Printf("[DEBUG] Broker '%s' is not active.\n", broker.Name)
			}
			continue
		}

		// broker must have module loaded for the check type to be used
		if !cm.brokerSupportsCheckType(cm.checkType, &detail) {
			if cm.Debug {
				cm.Log.Printf("[DEBUG] Broker '%s' does not support '%s' checks.\n", broker.Name, cm.checkType)
			}
			continue
		}

		if detail.ExternalPort != 0 {
			brokerPort = strconv.Itoa(int(detail.ExternalPort))
		} else {
			if detail.Port != nil && *detail.Port != 0 {
				brokerPort = strconv.Itoa(int(*detail.Port))
			} else {
				brokerPort = "43191"
			}
		}

		if detail.ExternalHost != nil && *detail.ExternalHost != "" {
			brokerHost = *detail.ExternalHost
		} else if detail.IP != nil && *detail.IP != "" {
			brokerHost = *detail.IP
		}

		if brokerHost == "" {
			cm.Log.Printf("[WARN] Broker '%s' instance %s has no IP or external host set", broker.Name, detail.CN)
			continue
		}

		if brokerHost == "trap.noit.circonus.net" && brokerPort != "443" {
			brokerPort = "443"
		}

		retries := 5
		for attempt := 1; attempt <= retries; attempt++ {
			// broker must be reachable and respond within designated time
			conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%s", brokerHost, brokerPort), cm.brokerMaxResponseTime)
			if err == nil {
				conn.Close()
				valid = true
				break
			}

			cm.Log.Printf("[WARN] Broker '%s' unable to connect, %v. Retrying in 2 seconds, attempt %d of %d.", broker.Name, err, attempt, retries)
			time.Sleep(2 * time.Second)
		}

		if valid {
			if cm.Debug {
				cm.Log.Printf("[DEBUG] Broker '%s' is valid\n", broker.Name)
			}
			break
		}
	}
	return valid
}
func (cm *CheckManager) isValidBroker(broker *api.Broker) bool {
	var brokerHost string
	var brokerPort string

	if broker.Type != "circonus" && broker.Type != "enterprise" {
		return false
	}

	valid := false

	for _, detail := range broker.Details {
		detail := detail

		// broker must be active
		if detail.Status != statusActive {
			if cm.Debug {
				cm.Log.Printf("[DEBUG] Broker '%s' is not active.\n", broker.Name)
			}
			continue
		}

		// broker must have module loaded for the check type to be used
		if !cm.brokerSupportsCheckType(cm.checkType, &detail) {
			if cm.Debug {
				cm.Log.Printf("[DEBUG] Broker '%s' does not support '%s' checks.\n", broker.Name, cm.checkType)
			}
			continue
		}

		if detail.ExternalPort != 0 {
			brokerPort = strconv.Itoa(int(detail.ExternalPort))
		} else {
			if detail.Port != nil && *detail.Port != 0 {
				brokerPort = strconv.Itoa(int(*detail.Port))
			} else {
				brokerPort = "43191"
			}
		}

		if detail.ExternalHost != nil && *detail.ExternalHost != "" {
			brokerHost = *detail.ExternalHost
		} else if detail.IP != nil && *detail.IP != "" {
			brokerHost = *detail.IP
		}

		if brokerHost == "" {
			cm.Log.Printf("[WARN] Broker '%s' instance %s has no IP or external host set", broker.Name, detail.CN)
			continue
		}

		if brokerHost == "trap.noit.circonus.net" && brokerPort != "443" {
			brokerPort = "443"
		}

		retries := 5
		for attempt := 1; attempt <= retries; attempt++ {
			// broker must be reachable and respond within designated time
			conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%s", brokerHost, brokerPort), cm.brokerMaxResponseTime)
			if err == nil {
				conn.Close()
				valid = true
				break
			}

			cm.Log.Printf("[WARN] Broker '%s' unable to connect, %v. Retrying in 2 seconds, attempt %d of %d.", broker.Name, err, attempt, retries)
			time.Sleep(2 * time.Second)
		}

		if valid {
			if cm.Debug {
				cm.Log.Printf("[DEBUG] Broker '%s' is valid\n", broker.Name)
			}
			break
		}
	}
	return valid
}
func ( cm * CheckManager ) isValidBroker ( broker * api . Broker ) bool { var brokerHost string var brokerPort string if broker . Type != " " && broker . Type != " " { return false } valid := false for _ , detail := range broker . Details { detail := detail // broker must be active if detail . Status != statusActive { if cm . Debug { cm . Log . Printf ( " \n " , broker . Name ) } continue } // broker must have module loaded for the check type to be used if ! cm . brokerSupportsCheckType ( cm . checkType , & detail ) { if cm . Debug { cm . Log . Printf ( " \n " , broker . Name , cm . checkType ) } continue } if detail . ExternalPort != 0 { brokerPort = strconv . Itoa ( int ( detail . ExternalPort ) ) } else { if detail . Port != nil && * detail . Port != 0 { brokerPort = strconv . Itoa ( int ( * detail . Port ) ) } else { brokerPort = " " } } if detail . ExternalHost != nil && * detail . ExternalHost != " " { brokerHost = * detail . ExternalHost } else if detail . IP != nil && * detail . IP != " " { brokerHost = * detail . IP } if brokerHost == " " { cm . Log . Printf ( " " , broker . Name , detail . CN ) continue } if brokerHost == " " && brokerPort != " " { brokerPort = " " } retries := 5 for attempt := 1 ; attempt <= retries ; attempt ++ { // broker must be reachable and respond within designated time conn , err := net . DialTimeout ( " " , fmt . Sprintf ( " " , brokerHost , brokerPort ) , cm . brokerMaxResponseTime ) if err == nil { conn . Close ( ) valid = true break } cm . Log . Printf ( " " , broker . Name , err , attempt , retries ) time . Sleep ( 2 * time . Second ) } if valid { if cm . Debug { cm . Log . Printf ( " \n " , broker . Name ) } break } } return valid }
// Is the broker valid (active, supports check type, and reachable)
Is the broker valid ( active supports check type and reachable )