Пример #1
0
// Fetch fetches disk IO metrics from the OS.
func (m *MetricSet) Fetch() ([]common.MapStr, error) {
	stats, err := net.IOCounters(true)
	if err != nil {
		return nil, errors.Wrap(err, "network io counters")
	}

	var events []common.MapStr
	if m.interfaces == nil {
		// Include all stats.
		for _, counters := range stats {
			events = append(events, ioCountersToMapStr(counters))
		}
	} else {
		// Select stats by interface name.
		for _, counters := range stats {
			name := strings.ToLower(counters.Name)
			if _, include := m.interfaces[name]; include {
				events = append(events, ioCountersToMapStr(counters))
				continue
			}
		}
	}

	return events, nil
}
Пример #2
0
func GetNetStats() string {
	v, err := net.IOCounters(false)
	format := "bytes_sent=%d bytes_recv=%d packets_sent=%d packets_recv=%d err_in=%d err_out=%d drop_in=%d drop_out=%d"
	if err == nil {
		return fmt.Sprintf(format, v[0].BytesSent, v[0].BytesRecv, v[0].PacketsSent, v[0].PacketsRecv, v[0].Errin, v[0].Errout, v[0].Dropin, v[0].Dropout)
	} else {
		return fmt.Sprintf(format, 0, 0, 0, 0, 0, 0, 0, 0)
	}
}
Пример #3
0
// NetworkUsage - list
func NetworkUsage() (NetworkUsageList, error) {

	netio, _ := net.IOCounters(true)
	time.Sleep(1000 * time.Millisecond) // Sleep 1 second to get kb/s
	netioSecondRun, _ := net.IOCounters(true)
	ifaces, _ := net.Interfaces()
	var usage NetworkUsageList

	var validInterfaces []string
	for _, iface := range ifaces {
		if !stringInSlice("loopback", iface.Flags) {
			validInterfaces = append(validInterfaces, iface.Name)
		}

	}

	for _, io := range netio {
		if stringInSlice(io.Name, validInterfaces) {

			for _, lastio := range netioSecondRun {
				if lastio.Name == io.Name {
					Inbound := lastio.BytesRecv - io.BytesRecv
					InboundKB, _ := util.ConvertBytesTo(Inbound, "kb", 0)

					Outbound := lastio.BytesSent - io.BytesSent
					OutboundKB, _ := util.ConvertBytesTo(Outbound, "kb", 0)

					n := NetworkStruct{
						Name:     io.Name,
						Inbound:  InboundKB,
						Outbound: OutboundKB,
					}

					usage = append(usage, n)
				}
			}

		}

	}

	return usage, nil

}
Пример #4
0
func NetIfs(ifacePrefix []string) ([]*NetIf, error) {
	netifs := make([]*NetIf, 0)
	list, err := net.IOCounters(true)
	if err != nil {
		return nil, err
	}
	for _, v := range list {
		N := NetIf{
			v.Name, int64(v.BytesRecv), int64(v.PacketsRecv),
			int64(v.Errin), int64(v.Dropin), 0, 0, 0, 0, int64(v.BytesSent),
			int64(v.PacketsSent), int64(v.Errout), int64(v.Dropout), 0, 0, 0, 0,
			int64(v.BytesRecv + v.BytesSent), int64(v.PacketsRecv + v.PacketsSent),
			int64(v.Errin + v.Errout), int64(v.Dropin + v.Dropout), 0, 0, 0,
		}
		netifs = append(netifs, &N)
	}
	return netifs, nil
}
func netIOCounters(nss []core.Namespace) ([]plugin.MetricType, error) {
	// gather accumulated metrics for all interfaces
	netsAll, err := net.IOCounters(false)
	if err != nil {
		return nil, err
	}

	// gather metrics per nic
	netsNic, err := net.IOCounters(true)
	if err != nil {
		return nil, err
	}

	results := []plugin.MetricType{}

	for _, ns := range nss {
		// set requested metric name from last namespace element
		metricName := ns.Element(len(ns) - 1).Value
		// check if requested metric is dynamic (requesting metrics for all nics)
		if ns[3].Value == "*" {
			for _, net := range netsNic {
				// prepare namespace copy to update value
				// this will allow to keep namespace as dynamic (name != "")
				dyn := make([]core.NamespaceElement, len(ns))
				copy(dyn, ns)
				dyn[3].Value = net.Name
				// get requested metric value
				val, err := getNetIOCounterValue(&net, metricName)
				if err != nil {
					return nil, err
				}

				metric := plugin.MetricType{
					Namespace_: dyn,
					Data_:      val,
					Timestamp_: time.Now(),
					Unit_:      netIOCounterLabels[metricName].unit,
				}
				results = append(results, metric)
			}
		} else {
			stats := append(netsAll, netsNic...)
			// find stats for interface name or all nics
			stat := findNetIOStats(stats, ns[3].Value)
			if stat == nil {
				return nil, fmt.Errorf("Requested interface %s not found", ns[3].Value)
			}
			// get value for requested metric
			val, err := getNetIOCounterValue(stat, metricName)
			if err != nil {
				return nil, err
			}

			metric := plugin.MetricType{
				Namespace_: ns,
				Data_:      val,
				Timestamp_: time.Now(),
				Unit_:      netIOCounterLabels[metricName].unit,
			}
			results = append(results, metric)
		}
	}

	return results, nil
}
Пример #6
0
func main() {
	fmt.Println("Hello RGB-spear")

	ser, err := serial.OpenPort(&serial.Config{
		Name: flagPort,
		Baud: 9600,
	})
	if err != nil {
		panic(err)
	}
	defer ser.Close()
	defer ser.Flush()

	fmt.Println("Wating 10 seconds for booting...")
	time.Sleep(10 * time.Second)

	spr = new(Spear)

	// RED for cpu
	if flagCPU {
		fmt.Println("Enable CPU")
		go func() {
			for {
				c, err := cpu.Percent(100*time.Millisecond, false)
				if err != nil {
					panic(err)
				}

				t := byte((c[0] * 255) / 100)
				spr.Lock()
				spr.R = t
				// log.Println("R->", spr.R)
				spr.Unlock()
				time.Sleep(300 * time.Millisecond)
			}
		}()
	}

	// Green for memory
	if flagMEM {
		fmt.Println("Enable MEM")
		go func() {
			for {
				v, err := mem.VirtualMemory()
				if err != nil {
					panic(err)
				}
				t := byte((v.UsedPercent * 255) / 100)
				spr.Lock()
				spr.G = t
				// log.Println("G->", spr.R)
				spr.Unlock()
				time.Sleep(300 * time.Millisecond)
			}
		}()
	}

	// Blue for network
	if flagNET {
		fmt.Println("Enable Network")
		go func() {
			lastSent, lastRecv := uint64(0), uint64(0)

			for {
				c, err := net.IOCounters(false)
				if err != nil {
					panic(err)
				}
				currSent := c[0].BytesSent
				currRecv := c[0].BytesRecv

				sent := currSent - lastSent
				recv := currRecv - lastRecv

				t := byte(((sent + recv) * 255) / 1000000)

				spr.Lock()
				spr.B = t
				spr.Unlock()
				time.Sleep(1 * time.Second)

				lastSent = currSent
				lastRecv = currRecv
			}
		}()

		t := time.NewTicker(100 * time.Millisecond)
		for {
			select {
			case <-t.C:
				fmt.Fprint(ser, spr)
				// log.Println("spr:", spr)
				ser.Flush()
			}
		}
	}
}
Пример #7
0
func (s *systemPS) NetIO() ([]net.IOCountersStat, error) {
	return net.IOCounters(true)
}
Пример #8
0
func netMetrics(cycle int) []*types.TimeSeriesData {
	cnt, err := net.IOCounters(true)
	if err != nil {
		return nil
	}
	ts := time.Now().Unix()
	metrics := []*types.TimeSeriesData{}

	for _, v := range cnt {
		if NameNotAvalid(v.Name) {
			continue
		}
		metrics = append(metrics,
			&types.TimeSeriesData{
				Metric:    "net.bytessent",
				Value:     float64(v.BytesSent),
				Cycle:     cycle,
				Timestamp: ts,
				DataType:  "COUNTER",
				Tags:      map[string]string{"iface": v.Name},
			},
			&types.TimeSeriesData{
				Metric:    "net.bytesrecv",
				Value:     float64(v.BytesRecv),
				Cycle:     cycle,
				Timestamp: ts,
				DataType:  "COUNTER",
				Tags:      map[string]string{"iface": v.Name},
			},
			&types.TimeSeriesData{
				Metric:    "net.packetssent",
				Value:     float64(v.PacketsSent),
				Cycle:     cycle,
				Timestamp: ts,
				DataType:  "COUNTER",
				Tags:      map[string]string{"iface": v.Name},
			},
			&types.TimeSeriesData{
				Metric:    "net.packetsrecv",
				Value:     float64(v.PacketsRecv),
				Cycle:     cycle,
				Timestamp: ts,
				DataType:  "COUNTER",
				Tags:      map[string]string{"iface": v.Name},
			},
			&types.TimeSeriesData{
				Metric:    "net.errin",
				Value:     float64(v.Errin),
				Cycle:     cycle,
				Timestamp: ts,
				DataType:  "COUNTER",
				Tags:      map[string]string{"iface": v.Name},
			},
			&types.TimeSeriesData{
				Metric:    "net.errout",
				Value:     float64(v.Errout),
				Cycle:     cycle,
				Timestamp: ts,
				DataType:  "COUNTER",
				Tags:      map[string]string{"iface": v.Name},
			},
			&types.TimeSeriesData{
				Metric:    "net.dropin",
				Value:     float64(v.Dropin),
				Cycle:     cycle,
				Timestamp: ts,
				DataType:  "COUNTER",
				Tags:      map[string]string{"iface": v.Name},
			},
			&types.TimeSeriesData{
				Metric:    "net.dropout",
				Value:     float64(v.Dropout),
				Cycle:     cycle,
				Timestamp: ts,
				DataType:  "COUNTER",
				Tags:      map[string]string{"iface": v.Name},
			},
			&types.TimeSeriesData{
				Metric:    "net.fifoin",
				Value:     float64(v.Fifoin),
				Cycle:     cycle,
				Timestamp: ts,
				DataType:  "COUNTER",
				Tags:      map[string]string{"iface": v.Name},
			},
			&types.TimeSeriesData{
				Metric:    "net.fifoout",
				Value:     float64(v.Fifoout),
				Cycle:     cycle,
				Timestamp: ts,
				DataType:  "COUNTER",
				Tags:      map[string]string{"iface": v.Name},
			},
		)
	}

	return metrics
}