コード例 #1
0
// Handles incoming requests.
func handleRequest(conn net.Conn, c *client.Client) {
	defer conn.Close()
	// Make a buffer to hold incoming data.
	buf := make([]byte, 102400)
	// Read the incoming connection into the buffer.
	numbytes, err := conn.Read(buf)
	if err != nil {
		fmt.Println("Error reading:", err.Error())
		return
	}

	var evt Event
	err = json.Unmarshal(buf[:numbytes], &evt)
	if err != nil {
		fmt.Printf("Error unmarshalling event: %s for input %s\n", err.Error(), string(buf[:numbytes]))
		return
	}

	outputlines := strings.Split(strings.TrimSpace(evt.Check.Output), "\n")

	seriesdata := make(map[string][][]interface{})

	for _, l := range outputlines {
		line := strings.TrimSpace(l)
		pieces := strings.Split(line, " ")
		if len(pieces) != 3 {
			continue
		}
		keys := strings.SplitN(pieces[0], ".", 2)
		if len(keys) != 2 {
			continue
		}
		keyraw := keys[1]
		key := strings.Replace(keyraw, ".", "_", -1)

		val, verr := strconv.ParseFloat(pieces[1], 64)
		if verr != nil {
			fmt.Printf("Error parsing value (%s): %s\n", pieces[1], verr.Error())
			continue
		}

		time, terr := strconv.ParseInt(pieces[2], 10, 64)
		if terr != nil {
			fmt.Printf("Error parsing time (%s): %s\n", pieces[2], terr.Error())
			continue
		}

		seriesdata[key] = append(seriesdata[key], []interface{}{time, evt.Client.Name, evt.Client.Address, val})
	}

	serieses := make([]*client.Series, 0)
	for key, points := range seriesdata {
		series := &client.Series{
			Name:    key,
			Columns: []string{"time", "host", "ip", "value"},
			Points:  points,
		}
		serieses = append(serieses, series)
	}

	if err := c.WriteSeriesWithTimePrecision(serieses, client.Second); err != nil {
		fmt.Printf("Error sending data to influx: %s, data: %+v\n", err.Error(), serieses)
	}

}