Exemplo n.º 1
0
func (i *Dns) Run(transport plugins.Transport, request plugins.Request) plugins.Result {
	entries := []net.IP{}

	start := time.Now()

	var err error

	switch i.RecordType {
	case "":
		fallthrough
	case "A*":
		entries, err = GetIP(i.Target)
	case "A":
		entries, err = GetIPv4(i.Target)
	case "AAAA":
		entries, err = GetIPv6(i.Target)
	default:
		return plugins.NewResult(plugins.Failed, nil, "method '%s' not supported", i.RecordType)
	}

	if err != nil {
		return plugins.NewResult(plugins.Failed, plugins.NewMeasurementCollection("time", time.Now().Sub(start)), err.Error())
	}

	if len(entries) > 0 {
		return plugins.NewResult(plugins.Ok, plugins.NewMeasurementCollection("time", time.Now().Sub(start)), "%d addresses", len(entries))
	} else {
		return plugins.NewResult(plugins.Failed, plugins.NewMeasurementCollection("time", time.Now().Sub(start)), "no addresses")
	}
}
Exemplo n.º 2
0
func (i *IcmpPing) Run(transport plugins.Transport, request plugins.Request) plugins.Result {
	ra, err := net.ResolveIPAddr("ip4:icmp", i.Target)

	if err != nil {
		return plugins.NewResult(plugins.Failed, nil, err.Error())
	}

	i.id = rand.Intn(0xffff)
	i.seq = rand.Intn(0xffff)
	i.payload = []byte("alerto pinger")

	bytes, err := (&icmp.Message{
		Type: ipv4.ICMPTypeEcho, Code: 0,
		Body: &icmp.Echo{
			ID: i.id, Seq: i.seq,
			Data: i.payload,
		},
	}).Marshal(nil)

	replyChannel := make(chan IcmpReply)

	activeLock.Lock()
	_, found := active[uint16(i.id)]
	for found {
		// NOTE: This becomes subject to the birthday paradox at some point
		i.id = rand.Intn(0xffff)

		_, found = active[uint16(i.id)]
	}
	active[uint16(i.id)] = replyChannel
	activeLock.Unlock()

	if n, err := conn.WriteTo(bytes, ra); err != nil {
		return plugins.NewResult(plugins.Failed, nil, err.Error())
	} else if n != len(bytes) {
		return plugins.NewResult(plugins.Failed, nil, "sent %d bytes; wanted %d", n, len(bytes))
	}

	start := time.Now()
	c := time.After(request.Timeout)

	for {
		select {
		case <-c:
			return plugins.NewResult(plugins.Failed, plugins.NewMeasurementCollection("time", time.Now().Sub(start)), "timeout [%s]", i.Target)

		case reply := <-replyChannel:
			activeLock.Lock()
			delete(active, uint16(i.id))
			activeLock.Unlock()

			switch reply.Status {
			case Reply:
				return plugins.NewResult(plugins.Ok, plugins.NewMeasurementCollection("time", time.Now().Sub(start)), "reply from %s [%s]", reply.Source, i.Target)
			case Unreachable:
				return plugins.NewResult(plugins.Failed, plugins.NewMeasurementCollection("time", time.Now().Sub(start)), "unreachable from %s [%s]", reply.Source, i.Target)
			}
		}
	}
}
Exemplo n.º 3
0
func (h *Http) Run(transport plugins.Transport, request plugins.Request) plugins.Result {
	start := time.Now()

	tr := newTransport(transport.Dial)
	client := &http.Client{Transport: tr}
	resp, err := client.Get(h.Url)
	if err != nil {
		return plugins.NewResult(plugins.Failed, plugins.NewMeasurementCollection("time", time.Now().Sub(start)), err.Error())
	}
	defer resp.Body.Close()

	c := plugins.NewMeasurementCollection(
		"status", resp.StatusCode,
		"time", tr.Duration(),
		"connectDuration", tr.ConnectDuration(),
		"requestDuration", tr.RequestDuration(),
	)

	return plugins.NewResult(plugins.Ok, c, "returned %d", resp.StatusCode)
}
Exemplo n.º 4
0
func (l *Load) Run(transport plugins.Transport, request plugins.Request) plugins.Result {
	file, err := transport.ReadFile("/proc/loadavg")
	if err != nil {
		return plugins.NewResult(plugins.Failed, nil, err.Error())
	}

	var load1 float64
	var load5 float64
	var load15 float64
	var activeTasks int64
	var tasks int64

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		text := scanner.Text()

		data := strings.Fields(strings.Trim(text, " "))
		if len(data) != 5 {
			continue
		}

		load1, _ = strconv.ParseFloat(data[0], 64)
		load5, _ = strconv.ParseFloat(data[1], 64)
		load15, _ = strconv.ParseFloat(data[2], 64)

		sep := strings.Index(data[3], "/")
		if sep > 0 {
			activeTasks, _ = strconv.ParseInt(data[3][0:sep], 10, 64)
			tasks, _ = strconv.ParseInt(data[3][sep+1:], 10, 64)
		}
	}

	return plugins.NewResult(
		plugins.Ok,
		plugins.NewMeasurementCollection(
			"load1", load1,
			"load5", load5,
			"load15", load15,
			"activeTasks", activeTasks,
			"tasks", tasks),
		"%.02f %.02f %.02f %d/%d", load1, load5, load15, activeTasks, tasks)
}
Exemplo n.º 5
0
func (p *PidOf) Run(transport plugins.Transport, request plugins.Request) plugins.Result {
	stdout, _, err := transport.Exec("/bin/pidof", p.ProcessName)
	if err != nil {
		return plugins.NewResult(plugins.Failed, nil, err.Error())
	}

	content, err := ioutil.ReadAll(stdout)
	if err != nil {
		return plugins.NewResult(plugins.Failed, nil, err.Error())
	}

	fields := strings.Fields(string(content))

	return plugins.NewResult(
		plugins.Ok,
		plugins.NewMeasurementCollection(
			"count",
			len(fields)),
		"%s has PID(s) %s", p.ProcessName, strings.TrimSpace(string(content)))
}