Beispiel #1
0
// Generate XXX
func (g *InterfaceGenerator) Generate() (interface{}, error) {
	var results []map[string]interface{}

	ifs, err := net.Interfaces()
	if err != nil {
		return nil, err
	}

	ai, err := windows.GetAdapterList()
	if err != nil {
		return nil, err
	}

	for _, ifi := range ifs {
		addr, err := ifi.Addrs()
		if err != nil {
			return nil, err
		}
		name := ifi.Name
		for ; ai != nil; ai = ai.Next {
			if ifi.Index == int(ai.Index) {
				name = windows.BytePtrToString(&ai.Description[0])
			}
		}

		results = append(results, map[string]interface{}{
			"name":       name,
			"ipAddress":  addr[0].String(),
			"macAddress": ifi.HardwareAddr.String(),
		})
	}

	return results, nil
}
Beispiel #2
0
// Generate XXX
func (g *InterfaceGenerator) Generate() ([]spec.NetInterface, error) {
	var results []spec.NetInterface

	ifs, err := net.Interfaces()
	if err != nil {
		return nil, err
	}

	ai, err := windows.GetAdapterList()
	if err != nil {
		return nil, err
	}

	for _, ifi := range ifs {
		if ifi.Flags&net.FlagLoopback != 0 {
			continue
		}

		// XXX occur mojibake when containing multi-byte strings
		name := ifi.Name
		for ; ai != nil; ai = ai.Next {
			if ifi.Index == int(ai.Index) {
				name = windows.BytePtrToString(&ai.Description[0])
			}
		}

		addrs, err := ifi.Addrs()
		if err != nil {
			return nil, err
		}
		ipv4Addresses := []string{}
		ipv6Addresses := []string{}
		for _, addr := range addrs {
			var ip net.IP
			switch v := addr.(type) {
			case *net.IPAddr:
				ip = v.IP
			}
			if ip == nil || ip.IsLoopback() {
				continue
			}
			if ipv4 := ip.To4(); ipv4 != nil {
				ipv4Addresses = append(ipv4Addresses, ipv4.String())
				continue
			}
			if ipv6 := ip.To16(); ipv6 != nil {
				ipv6Addresses = append(ipv6Addresses, ipv6.String())
			}
		}

		if len(ipv4Addresses) > 0 || len(ipv6Addresses) > 0 {
			results = append(results, spec.NetInterface{
				Name:          name,
				IPv4Addresses: ipv4Addresses,
				IPv6Addresses: ipv6Addresses,
				MacAddress:    ifi.HardwareAddr.String(),
			})
		}
	}
	return results, nil
}
Beispiel #3
0
// NewInterfaceGenerator XXX
func NewInterfaceGenerator(interval time.Duration) (*InterfaceGenerator, error) {
	g := &InterfaceGenerator{interval, 0, nil}

	var err error
	g.query, err = windows.CreateQuery()
	if err != nil {
		interfaceLogger.Criticalf(err.Error())
		return nil, err
	}

	ifs, err := net.Interfaces()
	if err != nil {
		interfaceLogger.Criticalf(err.Error())
		return nil, err
	}

	ai, err := windows.GetAdapterList()
	if err != nil {
		interfaceLogger.Criticalf(err.Error())
		return nil, err
	}

	for _, ifi := range ifs {
		for ; ai != nil; ai = ai.Next {
			if ifi.Index == int(ai.Index) {
				name := windows.BytePtrToString(&ai.Description[0])
				name = strings.Replace(name, "(", "[", -1)
				name = strings.Replace(name, ")", "]", -1)
				name = strings.Replace(name, "#", "_", -1)
				var counter *windows.CounterInfo

				counter, err = windows.CreateCounter(
					g.query,
					fmt.Sprintf(`interface.nic%d.rxBytes.delta`, ifi.Index),
					fmt.Sprintf(`\Network Interface(%s)\Bytes Received/sec`, name))
				if err != nil {
					interfaceLogger.Criticalf(err.Error())
					return nil, err
				}
				g.counters = append(g.counters, counter)
				counter, err = windows.CreateCounter(
					g.query,
					fmt.Sprintf(`interface.nic%d.txBytes.delta`, ifi.Index),
					fmt.Sprintf(`\Network Interface(%s)\Bytes Sent/sec`, name))
				if err != nil {
					interfaceLogger.Criticalf(err.Error())
					return nil, err
				}
				g.counters = append(g.counters, counter)
			}
		}
	}

	r, _, err := windows.PdhCollectQueryData.Call(uintptr(g.query))
	if r != 0 && err != nil {
		if r == windows.PDH_NO_DATA {
			interfaceLogger.Infof("this metric has not data. ")
			return nil, err
		}
		interfaceLogger.Criticalf(err.Error())
		return nil, err
	}

	return g, nil
}