Ejemplo n.º 1
0
func newHTTPMonitorIPsJob(
	config *Config,
	addr string,
	tls *transport.TLSConfig,
	enc contentEncoder,
	body []byte,
	validator RespCheck,
) (monitors.Job, error) {
	typ := config.Name
	jobName := fmt.Sprintf("%v@%v", typ, addr)

	req, err := buildRequest(addr, config, enc)
	if err != nil {
		return nil, err
	}

	hostname, port, err := splitHostnamePort(req)
	if err != nil {
		return nil, err
	}

	pingFactory := createPingFactory(config, hostname, port, tls, req, body, validator)
	if ip := net.ParseIP(hostname); ip != nil {
		return monitors.MakeByIPJob(jobName, typ, ip, pingFactory)
	}
	return monitors.MakeByHostJob(jobName, typ, hostname, config.Mode, pingFactory)
}
Ejemplo n.º 2
0
func newTCPMonitorIPsJob(
	addr connURL,
	tls *transport.TLSConfig,
	config *Config,
) (monitors.Job, error) {
	typ := config.Name
	timeout := config.Timeout
	jobType := jobType(addr.Scheme)
	jobName := jobName(typ, jobType, addr.Host, addr.Ports)
	validator := makeValidateConn(config)

	dialerFactory, err := buildHostDialerChainFactory(addr.Scheme, tls, config)
	if err != nil {
		return nil, err
	}

	pingFactory := createPingFactory(dialerFactory, addr, timeout, validator)
	if ip := net.ParseIP(addr.Host); ip != nil {
		debugf("Make TCP by IP job: %v:%v", ip, addr.Ports)
		return monitors.MakeByIPJob(jobName, typ, ip, pingFactory)
	}

	debugf("Make TCP by Host job: %v:%v (mode=%#v)", addr.Host, addr.Ports, config.Mode)
	return monitors.MakeByHostJob(jobName, typ, addr.Host, config.Mode, pingFactory)
}
Ejemplo n.º 3
0
func create(
	info monitors.Info,
	cfg *common.Config,
) ([]monitors.Job, error) {
	config := DefaultConfig
	if err := cfg.Unpack(&config); err != nil {
		return nil, err
	}

	// TODO: check icmp is support by OS + check we've
	// got required credentials (implementation uses RAW socket, requires root +
	// not supported on all OSes)
	// TODO: replace icmp package base reader/sender using raw sockets with
	//       OS specific solution

	var jobs []monitors.Job
	addJob := func(t monitors.Job, err error) error {
		if err != nil {
			return err
		}
		jobs = append(jobs, t)
		return nil
	}

	ipVersion := config.Mode.Network()
	if len(config.Hosts) > 0 && ipVersion == "" {
		err := fmt.Errorf("pinging hosts requires ipv4 or ipv6 mode enabled")
		return nil, err
	}

	var loopErr error
	loopInit.Do(func() {
		debugf("initialize icmp handler")
		loop, loopErr = newICMPLoop()
	})
	if loopErr != nil {
		debugf("Failed to initialize ICMP loop %v", loopErr)
		return nil, loopErr
	}

	typ := config.Name
	network := config.Mode.Network()
	pingFactory := monitors.MakePingIPFactory(nil, createPingIPFactory(&config))

	for _, host := range config.Hosts {
		ip := net.ParseIP(host)
		if ip != nil {
			name := fmt.Sprintf("icmp-ip@%v", ip.String())
			err := addJob(monitors.MakeByIPJob(name, typ, ip, pingFactory))
			if err != nil {
				return nil, err
			}
			continue
		}

		name := fmt.Sprintf("%v-host-%v@%v", config.Name, network, host)
		err := addJob(monitors.MakeByHostJob(name, typ, host, config.Mode, pingFactory))
		if err != nil {
			return nil, err
		}
	}

	return jobs, nil
}