Esempio n. 1
0
func sendMemUsage(interval time.Duration) {
	mem, err := linux.GetMemUsage()
	now := time.Now()
	if err != nil {
		log.Printf("[WARN] unable to get system memory usage: ", err.Error())
	} else {
		dispatch("system", now, mem)
	}

	conts, err := docker.ListContainers()
	if err != nil {
		log.Fatalln("[WARN] unable to get list of containers: ", err.Error())
	}
	for _, cont := range conts {
		mem, err = docker.GetMemUsage(cont)
		now = time.Now()
		if err != nil {
			log.Printf("[WARN] unable to get memory usage for %s: %s\n", cont, err.Error())
		} else {
			dispatch(cont, now, mem)
		}
	}

	for _, client := range clients {
		client.Flush()
	}
}
Esempio n. 2
0
func sendCPUUsage(interval time.Duration) {
	cpu, err := linux.GetCPUUsage()
	now := time.Now()
	if err != nil {
		log.Printf("[WARN] unable to get system cpu usage: ", err.Error())
	} else {
		ctup, ok := data["system"]
		if ok {
			avg := float64(cpu-ctup.cpu) * 1000000000 / float64(now.Sub(ctup.time))
			ctup.time = now
			ctup.cpu = cpu
			dispatch("system", now, avg)
		} else {
			data["system"] = &ctuple{time: now, cpu: cpu}
		}
	}

	conts, err := docker.ListContainers()
	if err != nil {
		log.Fatalln("[WARN] unable to get list of containers: ", err.Error())
	}
	for _, cont := range conts {
		cpu, err = docker.GetCPUUsage(cont)
		now = time.Now()
		if err != nil {
			log.Printf("[WARN] unable to get cpu usage for %s: %s\n", cont, err.Error())
			continue
		}

		ctup, ok := data[cont]
		if ok {
			avg := float64(cpu-ctup.cpu) * 1000000000 / float64(now.Sub(ctup.time))
			ctup.time = now
			ctup.cpu = cpu
			dispatch(cont, now, avg)
		} else {
			data[cont] = &ctuple{time: now, cpu: cpu}
		}
	}

	for _, client := range clients {
		client.Flush()
	}
}
Esempio n. 3
0
func sendNetUsage(interval time.Duration) {
	out, err := linux.Exec("ifconfig -s")
	now := time.Now()
	if err != nil {
		log.Printf("[WARN] unable to get system net usage: ", err.Error())
	} else {
		reader := csv.NewReader(strings.NewReader(out))
		reader.Comma = ' '
		reader.TrimLeadingSpace = true
		net_data, err := reader.ReadAll()
		if err != nil {
			log.Println("[WARN] unable to get system net usage: ", err.Error())
		} else {
			for index, row := range net_data {
				if index == 0 {
					continue
				}

				net, err := strconv.ParseInt(row[3], 10, 64)
				if err != nil {
					log.Println("[WARN] error in converting to integer: ", err.Error())
				} else {
					ntup, ok := rxdata[row[0]]
					if ok {
						avg := float64(net-ntup.net) * 1000000000 / float64(now.Sub(ntup.time))
						ntup.time = now
						ntup.net = net
						dispatch(row[0], now, avg, "rx")
					} else {
						rxdata[row[0]] = &ntuple{time: now, net: net}
					}
				}

				net, err = strconv.ParseInt(row[7], 10, 64)
				if err != nil {
					log.Println("[WARN] error in converting to integer: ", err.Error())
				} else {
					ntup, ok := txdata[row[0]]
					if ok {
						avg := float64(net-ntup.net) * 1000000000 / float64(now.Sub(ntup.time))
						ntup.time = now
						ntup.net = net
						dispatch(row[0], now, avg, "tx")
					} else {
						txdata[row[0]] = &ntuple{time: now, net: net}
					}
				}
			}
		}
	}

	conts, err := docker.ListContainers()
	if err != nil {
		log.Fatalln("[WARN] unable to get list of containers: ", err.Error())
	}
	for _, cont := range conts {
		net, err := docker.GetNetInUsage(cont)
		now = time.Now()
		if err != nil {
			log.Printf("[WARN] unable to get rx net usage for %s: %s\n", cont, err.Error())
		} else {
			ntup, ok := rxdata[cont]
			if ok {
				avg := float64(net-ntup.net) * 1000000000 / float64(now.Sub(ntup.time))
				ntup.time = now
				ntup.net = net
				dispatch(cont, now, avg, "rx")
			} else {
				rxdata[cont] = &ntuple{time: now, net: net}
			}
		}

		net, err = docker.GetNetOutUsage(cont)
		now = time.Now()
		if err != nil {
			log.Printf("[WARN] unable to get tx net usage for %s: %s\n", cont, err.Error())
		} else {
			ntup, ok := txdata[cont]
			if ok {
				avg := float64(net-ntup.net) * 1000000000 / float64(now.Sub(ntup.time))
				ntup.time = now
				ntup.net = net
				dispatch(cont, now, avg, "tx")
			} else {
				txdata[cont] = &ntuple{time: now, net: net}
			}
		}
	}

	for _, client := range clients {
		client.Flush()
	}
}