Esempio n. 1
0
File: util.go Progetto: ruflin/beats
func MakePingAllIPPortFactory(
	fields common.MapStr,
	ports []uint16,
	f func(*net.IPAddr, uint16) (common.MapStr, error),
) func(*net.IPAddr) TaskRunner {
	if len(ports) == 1 {
		port := ports[0]
		fields := fields.Clone()
		fields["port"] = strconv.Itoa(int(port))
		return MakePingIPFactory(fields, func(ip *net.IPAddr) (common.MapStr, error) {
			return f(ip, port)
		})
	}

	return MakePingAllIPFactory(fields, func(ip *net.IPAddr) []func() (common.MapStr, error) {
		funcs := make([]func() (common.MapStr, error), len(ports))
		for i := range ports {
			port := ports[i]
			funcs[i] = func() (common.MapStr, error) {
				event, err := f(ip, port)
				if event == nil {
					event = common.MapStr{}
				}
				event["port"] = strconv.Itoa(int(port))
				return event, err
			}
		}
		return funcs
	})
}
Esempio n. 2
0
// Applies a sequence of filtering rules and returns the filtered event
func (filters *FilterList) Filter(event common.MapStr) common.MapStr {

	// Check if filters are set, just return event if not
	if len(filters.filters) == 0 {
		return event
	}

	// clone the event at first, before starting filtering
	filtered := event.Clone()
	var err error

	for _, filter := range filters.filters {
		filtered, err = filter.Filter(filtered)
		if err != nil {
			logp.Debug("filter", "fail to apply filtering rule %s: %s", filter, err)
		}
	}

	return filtered
}
Esempio n. 3
0
// Applies a sequence of processing rules and returns the filtered event
func (procs *Processors) Run(event common.MapStr) common.MapStr {

	// Check if processors are set, just return event if not
	if len(procs.list) == 0 {
		return event
	}

	// clone the event at first, before starting filtering
	filtered := event.Clone()
	var err error

	for _, p := range procs.list {
		filtered, err = p.Run(filtered)
		if err != nil {
			logp.Debug("filter", "fail to apply processor %s: %s", p, err)
		}
		if filtered == nil {
			// drop event
			return nil
		}
	}

	return filtered
}
Esempio n. 4
0
File: util.go Progetto: ruflin/beats
func MakeByHostJob(
	name, typ string,
	host string,
	settings IPSettings,
	pingFactory func(ip *net.IPAddr) TaskRunner,
) (Job, error) {
	network := settings.Network()
	if network == "" {
		return nil, errors.New("pinging hosts requires ipv4 or ipv6 mode enabled")
	}

	mode := settings.Mode
	if mode == PingAny {
		return MakeJob(name, typ, func() (common.MapStr, []TaskRunner, error) {
			event := common.MapStr{"host": host}

			dnsStart := time.Now()
			ip, err := net.ResolveIPAddr(network, host)
			if err != nil {
				return event, nil, err
			}

			dnsEnd := time.Now()
			dnsRTT := dnsEnd.Sub(dnsStart)
			event["resolve_rtt"] = look.RTT(dnsRTT)
			event["ip"] = ip.String()

			return WithFields(event, pingFactory(ip)).Run()
		}), nil
	}

	filter := makeIPFilter(network)
	return MakeJob(name, typ, func() (common.MapStr, []TaskRunner, error) {
		event := common.MapStr{"host": host}

		// TODO: check for better DNS IP lookup support:
		//         - The net.LookupIP drops ipv6 zone index
		//
		dnsStart := time.Now()
		ips, err := net.LookupIP(host)
		if err != nil {
			return event, nil, err
		}

		dnsEnd := time.Now()
		dnsRTT := dnsEnd.Sub(dnsStart)

		event["resolve_rtt"] = look.RTT(dnsRTT)
		if filter != nil {
			ips = filterIPs(ips, filter)
		}

		if len(ips) == 0 {
			err := fmt.Errorf("no %v address resolvable for host %v", network, host)
			return event, nil, err
		}

		// create ip ping tasks
		cont := make([]TaskRunner, len(ips))
		for i, ip := range ips {
			addr := &net.IPAddr{IP: ip}
			fields := event.Clone()
			fields["ip"] = ip.String()
			cont[i] = WithFields(fields, pingFactory(addr))
		}
		return nil, cont, nil
	}), nil
}