func main() {
	args := os.Args

	if 1 == len(args) {
		log.Fatal("usage: nolo-ganglia path-to-plugin")
	}

	name := args[1]

	out, err := exec.Command(name).Output()
	if err != nil {
		log.Fatal(err)
	}

	basename := filepath.Base(name)
	plugin := nolo.Parse(basename, string(out))

	gIP := net.IPv4(127, 0, 0, 1)
	gangliaPort := 1234
	host := "127.0.0.1"
	spoofHost := "127.0.0.1:spoof"

	gm := gmetric.Gmetric{
		Host:  host,
		Spoof: spoofHost,
	}
	gm.AddServer(gmetric.Server{gIP, gangliaPort})

	for _, metric := range plugin.Metrics {
		m_name := metric.Identifier
		m_value := metric.Value
		m_units := ""
		m_type := uint32(gmetric.VALUE_UNSIGNED_INT)
		m_slope := uint32(gmetric.SLOPE_BOTH)
		m_grp := plugin.Identifier
		m_interval := uint32(300)

		go gm.SendMetric(
			m_name,
			m_value,
			m_type,
			m_units,
			m_slope,
			m_interval,
			m_interval,
			m_grp)
	}
}
Example #2
0
func main() {
	log.Printf("Initializing test")
	gIP := net.IPv4(127, 0, 0, 1)
	gangliaPort := 1234

	gm := gmetric.Gmetric{
		Host:  "127.0.0.1",
		Spoof: "127.0.0.1:spoof",
	}
	gm.AddServer(gmetric.Server{gIP, gangliaPort})

	for i := 0; i < 10; i++ {
		log.Printf("Sending packet for some_metric_%d", i)
		gm.SendMetric(fmt.Sprintf("some_metric_%d", i), "8675309", gmetric.VALUE_UNSIGNED_INT, "units", gmetric.SLOPE_BOTH, 300, 600, "GROUP")
	}
}
Example #3
0
func NewGmetric() (*gmetric.Gmetric, error) {
	b, err := ioutil.ReadFile(GmondConfig)
	if err != nil {
		return nil, err
	}
	stanzas := gmondChannelRe.FindAllStringSubmatch(string(b), -1)
	if len(stanzas) == 0 {
		return nil, fmt.Errorf("No udp_send_channel stanzas found in %s", GmondConfig)
	}

	servers := make([]gmetric.Server, 0)
	for _, stanza := range stanzas {
		var host, port string
		for _, match := range gmondHostPortRe.FindAllStringSubmatch(stanza[1], 2) {
			if match[1] == "host" {
				host = match[2]
			} else if match[1] == "port" {
				port = match[2]
			}
		}
		if host == "" || port == "" {
			return nil, fmt.Errorf("Missing host or port from %s stanza %q", GmondConfig, stanza[0])
		}
		portNum, err := strconv.Atoi(port)
		if err != nil {
			return nil, err
		}
		ips, err := net.LookupIP(host)
		if err != nil {
			return nil, err
		}
		for _, ip := range ips {
			vlog.VLogf("Reporting to Ganglia server at %s:%d", ip, portNum)
			servers = append(servers, gmetric.Server{ip, portNum})
		}
	}

	// see http://sourceforge.net/apps/trac/ganglia/wiki/gmetric_spoofing
	hostname, _ := os.Hostname()
	spoofName := fmt.Sprintf("%s:%s", hostname, hostname)

	gm := gmetric.Gmetric{Spoof: spoofName}
	for _, server := range servers {
		gm.AddServer(server)
	}
	return &gm, nil
}
Example #4
0
func submit() {
	var clientGraphite net.Conn
	if *graphiteAddress != "" {
		var err error
		clientGraphite, err = net.Dial(TCP, *graphiteAddress)
		if clientGraphite != nil {
			// Run this when we're all done, only if clientGraphite was opened.
			defer clientGraphite.Close()
		}
		if err != nil {
			log.Printf(err.Error())
		}
	}
	var useGanglia bool
	var gm gmetric.Gmetric
	gmSubmit := func(name string, value uint32) {
		if useGanglia {
			if *debug {
				log.Println("Ganglia send metric %s value %d\n", name, value)
			}
			m_value := fmt.Sprint(value)
			m_units := "count"
			m_type := uint32(gmetric.VALUE_UNSIGNED_INT)
			m_slope := uint32(gmetric.SLOPE_BOTH)
			m_grp := "statsd"
			m_ival := uint32(*flushInterval * int64(2))

			go gm.SendMetric(name, m_value, m_type, m_units, m_slope, m_ival, m_ival, m_grp)
		}
	}
	gmSubmitFloat := func(name string, value float64) {
		if useGanglia {
			if *debug {
				log.Println("Ganglia send float metric %s value %f\n", name, value)
			}
			m_value := fmt.Sprint(value)
			m_units := "count"
			m_type := uint32(gmetric.VALUE_DOUBLE)
			m_slope := uint32(gmetric.SLOPE_BOTH)
			m_grp := "statsd"
			m_ival := uint32(*flushInterval * int64(2))

			go gm.SendMetric(name, m_value, m_type, m_units, m_slope, m_ival, m_ival, m_grp)
		}
	}
	if *gangliaAddress != "" {
		gm = gmetric.Gmetric{
			Host:  *gangliaSpoofHost,
			Spoof: *gangliaSpoofHost,
		}
		gm.SetVerbose(false)

		if strings.Contains(*gangliaAddress, ",") {
			segs := strings.Split(*gangliaAddress, ",")
			for i := 0; i < len(segs); i++ {
				gIP, err := net.ResolveIPAddr("ip4", segs[i])
				if err != nil {
					panic(err.Error())
				}
				gm.AddServer(gmetric.GmetricServer{gIP.IP, *gangliaPort})
			}
		} else {
			gIP, err := net.ResolveIPAddr("ip4", *gangliaAddress)
			if err != nil {
				panic(err.Error())
			}
			gm.AddServer(gmetric.GmetricServer{gIP.IP, *gangliaPort})
		}
		useGanglia = true
	} else {
		useGanglia = false
	}
	numStats := 0
	now := time.Now()
	buffer := bytes.NewBufferString("")
	for s, c := range counters {
		value := float64(c) / float64((float64(*flushInterval)*float64(time.Second))/float64(1e3))
		fmt.Fprintf(buffer, "stats.%s %d %d\n", s, value, now)
		gmSubmitFloat(fmt.Sprintf("stats_%s", s), value)
		fmt.Fprintf(buffer, "stats_counts.%s %d %d\n", s, c, now)
		gmSubmit(fmt.Sprintf("stats_counts_%s", s), uint32(c))
		counters[s] = 0
		numStats++
	}
	for i, g := range gauges {
		value := int64(g)
		fmt.Fprintf(buffer, "stats.%s %d %d\n", i, value, now)
		gmSubmit(fmt.Sprintf("stats_%s", i), uint32(value))
		numStats++
	}
	for u, t := range timers {
		if len(t) > 0 {
			sort.Float64s(t)
			min := float64(t[0])
			max := float64(t[len(t)-1])
			mean := float64(min)
			maxAtThreshold := float64(max)
			count := len(t)
			if len(t) > 1 {
				var thresholdIndex int
				thresholdIndex = ((100 - *percentThreshold) / 100) * count
				numInThreshold := count - thresholdIndex
				values := t[0:numInThreshold]

				sum := float64(0)
				for i := 0; i < numInThreshold; i++ {
					sum += values[i]
				}
				mean = float64(sum) / float64(numInThreshold)
			}
			var z []float64
			timers[u] = z

			fmt.Fprintf(buffer, "stats.timers.%s.mean %f %d\n", u, mean, now)
			gmSubmitFloat(fmt.Sprintf("stats_timers_%s_mean", u), mean)
			fmt.Fprintf(buffer, "stats.timers.%s.upper %f %d\n", u, max, now)
			gmSubmitFloat(fmt.Sprintf("stats_timers_%s_upper", u), max)
			fmt.Fprintf(buffer, "stats.timers.%s.upper_%d %f %d\n", u,
				*percentThreshold, maxAtThreshold, now)
			gmSubmitFloat(fmt.Sprintf("stats_timers_%s_upper_%d", u, *percentThreshold), maxAtThreshold)
			fmt.Fprintf(buffer, "stats.timers.%s.lower %f %d\n", u, min, now)
			gmSubmitFloat(fmt.Sprintf("stats_timers_%s_lower", u), min)
			fmt.Fprintf(buffer, "stats.timers.%s.count %d %d\n", u, count, now)
			gmSubmit(fmt.Sprintf("stats_timers_%s_count", u), uint32(count))
		} else {
			// Need to still submit timers as zero
			fmt.Fprintf(buffer, "stats.timers.%s.mean %f %d\n", u, 0, now)
			gmSubmitFloat(fmt.Sprintf("stats_timers_%s_mean", u), 0)
			fmt.Fprintf(buffer, "stats.timers.%s.upper %f %d\n", u, 0, now)
			gmSubmitFloat(fmt.Sprintf("stats_timers_%s_upper", u), 0)
			fmt.Fprintf(buffer, "stats.timers.%s.upper_%d %f %d\n", u,
				*percentThreshold, 0, now)
			gmSubmitFloat(fmt.Sprintf("stats_timers_%s_upper_%d", u, *percentThreshold), 0)
			fmt.Fprintf(buffer, "stats.timers.%s.lower %f %d\n", u, 0, now)
			gmSubmitFloat(fmt.Sprintf("stats_timers_%s_lower", u), 0)
			fmt.Fprintf(buffer, "stats.timers.%s.count %d %d\n", u, 0, now)
			gmSubmit(fmt.Sprintf("stats_timers_%s_count", u), uint32(0))
		}
		numStats++
	}
	fmt.Fprintf(buffer, "statsd.numStats %d %d\n", numStats, now)
	gmSubmit("statsd_numStats", uint32(numStats))
	if clientGraphite != nil {
		if *debug {
			log.Println("Send to graphite: [[[%s]]]\n", string(buffer.Bytes()))
		}
		clientGraphite.Write(buffer.Bytes())
	}
}