Exemple #1
0
// CPUInfo gathers information related to installed CPUs
func (c *Collector) CPUInfo() (*CPU, error) {
	if _, err := os.Stat(c.hwinfoFile); err != nil {
		if err = c.Hwinfo2Json(); err != nil {
			return nil, utils.FormatError(err)
		}
	}

	out, err := mxj.NewMapsFromJsonFile(c.hwinfoFile)
	if err != nil {
		return nil, utils.FormatError(err)
	}

	cpu := new(CPU)
	cpu.Config = make(map[string]interface{})
	cpu.Cap = make(map[string]interface{})
	for _, m := range out {
		if val, err := m.ValueForPathString("id"); err == nil {
			if val == "cpu" || val == "cpu:0" {
				if val, err := m.ValueForPathString("product"); err == nil {
					cpu.Type = val
				}
				if val, err := m.ValueForPath("capacity"); err == nil {
					cpu.Capacity = val.(float64)
				}
				if val, err := m.ValueForPath("clock"); err == nil {
					cpu.Clock = val.(float64)
				}

				conf, err := m.ValueForPath("configuration")
				if err == nil {
					for k, v := range conf.(map[string]interface{}) {
						cpu.Config[k] = v
					}
				}
				caps, err := m.ValueForPath("capabilities")
				if err == nil {
					for k, v := range caps.(map[string]interface{}) {
						cpu.Cap[k] = v
					}
				}
			}
		}
	}
	return cpu, nil
}
Exemple #2
0
// NICInfo gathers information related to installed NICs
func (c *Collector) NICInfo() (NICList, error) {
	if _, err := os.Stat(c.hwinfoFile); err != nil {
		if err = c.Hwinfo2Json(); err != nil {
			return nil, utils.FormatError(err)
		}
	}
	out, err := mxj.NewMapsFromJsonFile(c.hwinfoFile)
	if err != nil {
		return nil, utils.FormatError(err)
	}

	list := NewNICList()
	for _, m := range out {
		if val, err := m.ValueForPathString("id"); err == nil && strings.HasPrefix(val, "network") {
			if desc, err := m.ValueForPathString("description"); err == nil &&
				(desc == "Ethernet interface" || desc == "Ethernet controller" ||
					desc == "Network controller" || desc == "interface") {
				var name string
				name, err := m.ValueForPathString("logicalname")
				if err != nil {
					name = "N/A"
				} else {
					if name == "ovs-system" {
						continue
					}
				}

				nic := new(NIC)
				nic.Name = name
				nic.PCIAddr = "N/A"
				nic.NUMANode = NoNUMA

				conf, err := m.ValueForPath("configuration")
				if err != nil {
					nic.Driver = "N/A"
				} else {
					if val, ok := conf.(map[string]interface{})["driver"]; ok {
						nic.Driver = val.(string)
					}
				}
				switch nic.Driver {
				case "tun", "veth", "macvlan":
					continue
				case "openvswitch":
					nic.Desc = "Open vSwitch interface"
					nic.Type = NicTypeOVS
				default:
					if prod, err := m.ValueForPathString("product"); err == nil {
						if vendor, err := m.ValueForPathString("vendor"); err == nil {
							if _, err := c.Run(fmt.Sprintf("[[ -d /sys/class/net/%s/master || -d /sys/class/net/%s/brport ]]", nic.Name, nic.Name)); err == nil {
								// the interface is part of a bridge
								continue
							}
							if businfo, err := m.ValueForPathString("businfo"); err == nil {
								nic.PCIAddr = strings.Split(businfo, "@")[1]
							}

							nic.Model = prod
							nic.Vendor = vendor
							numa, err := c.numa4Nic(nic.PCIAddr)
							if err != nil {
								return nil, utils.FormatError(err)
							}
							nic.NUMANode = numa
							nic.Desc = vendor + " " + prod
							nic.Type = NicTypePhys

							// find Virtual Functions for appropriate physical NIC
							vfs, err := c.virtualFunctions(nic)
							if err == nil {
								list.Add(vfs[0:]...)
							}
						}
					}
				}
				list.Add(nic)
			}
		}
	}

	// lshw cannot treat linux bridges so let's do it manually
	res, err := c.Run(`out="";for n in /sys/class/net/*;do [ -d $n/bridge ] && out="$out ${n##/sys/class/net/}";done;echo $out`)
	if err != nil {
		return nil, utils.FormatError(err)
	}
	if res != "" {
		for _, n := range strings.Split(res, " ") {
			br := &NIC{
				Name:     n,
				Driver:   "bridge",
				Desc:     "Bridge interface",
				NUMANode: NoNUMA,
				PCIAddr:  "N/A",
				Type:     NicTypeBridge,
			}
			list.Add(br)
		}
	}
	list.SortByPCI()
	return list, nil
}