Exemple #1
0
func main() {
	esTraffic := eventsource.New()
	esSystemData := eventsource.New()

	tm := monitor.NewMonitor(&traffic.Monitor{}, 500*time.Millisecond)
	sm := monitor.NewMonitor(&system.Monitor{}, 10*time.Second)

	go tm.Start()
	go sm.Start()

	go trafficServer(esTraffic, tm)
	go systemDataServer(esSystemData, sm)

	srv := &http.Server{
		ReadTimeout:  2 * time.Second,
		WriteTimeout: 2 * time.Second,
		Addr:         "192.168.1.1:8000",
	}

	http.HandleFunc("/", indexHandler)
	http.HandleFunc("/nat.json", natJsonHandler)
	http.HandleFunc("/memory_usage.json", memoryUsageJsonHandler)
	http.HandleFunc("/uuid", uuidHandler)
	http.HandleFunc("/traffic_capture", trafficCaptureHandler)
	http.HandleFunc("/stop_capture", trafficCaptureStopHandler)

	http.Handle("/live/traffic_data/", esTraffic)
	http.Handle("/live/system_data/", esSystemData)

	http.HandleFunc("/resolve_ip/", resolveIPHandler)

	err := srv.ListenAndServe()
	if err != nil {
		panic("ListenANdServe: " + err.Error())
	}
}
func main() {
	tm := monitor.NewMonitor(&traffic.Monitor{}, 500*time.Millisecond)
	ch := make(chan interface{})
	tm.RegisterChannel(ch)
	go tm.Start()

	hosts := make(map[string]string)
	for {
		lines := &lineColorizer{}
		stats := (<-ch).([]traffic.ProgressiveStat)

		os.Stdout.WriteString("\033[2J\033[;H")

		tabWriter := &tabwriter.Writer{}
		tabWriter.Init(lines, 0, 0, 2, ' ', 0)
		// The \b are a messy hack to remove the spaces that the
		// tabwriter inserts because it counts ansi colors as actual
		// characters
		tabWriter.Write([]byte("Host\t         Down\t           Up\t│ Total down\t\b\b\bTotal up\n"))
		lines.colors = append(lines.colors, 255)
		sort.Sort(statSlice(stats))

		for index, stat := range stats {
			var color_index int
			if stat.BPSIn == 0 && stat.BPSOut == 0 {
				color_index = 0
			} else if float64(stat.BPSIn)/maxIn > float64(stat.BPSOut)/maxOut {
				// IN is the culprit
				color_index = int(stat.BPSIn/(maxIn/uint64(len(colors))-1) + 1)
			} else {
				// OUT is the culprit
				color_index = int(stat.BPSOut/(maxOut/uint64(len(colors))-1) + 1)
			}

			if color_index >= len(colors) {
				color_index = len(colors) - 1
			}

			if index == len(stats)-1 {
				lines.colors = append(lines.colors, 255)
				tabWriter.Write([]byte("──────────────────────────────────────────────────────────────┼───────────────────────\n"))
			}

			lines.colors = append(lines.colors, colors[color_index])
			host, ok := hosts[stat.Host]
			if !ok {
				if stat.Host == "total" {
					host = "total"
				} else {
					host = lookup.Resolve(net.ParseIP(stat.Host), false)
					hosts[stat.Host] = host
				}
			}

			tabWriter.Write([]byte(fmt.Sprintf("%-30.30s\t%11s/s\t%11s/s\t\033[39m│ %10s\t%10s\n",
				host,
				formatByteCount(stat.BPSIn),
				formatByteCount(stat.BPSOut),
				formatByteCount(stat.In),
				formatByteCount(stat.Out))))
		}

		// fmt.Print(buf.String())
		tabWriter.Flush()
		os.Stdout.WriteString(lines.String())
	}
}