Пример #1
0
// Get CPU information.
func Get() (CPU, error) {
	c := CPU{}

	m, err := parse.ExecRegexpMap("/usr/sbin/sysctl", []string{"-a"}, ":", "\\S+:\\s\\S+")
	if err != nil {
		return CPU{}, err
	}

	c.CoresPerSocket, err = parse.StrToInt(m, "machdep.cpu.core_count")
	if err != nil {
		return CPU{}, err
	}

	c.Physical, err = parse.StrToInt(m, "hw.physicalcpu_max")
	if err != nil {
		return CPU{}, err
	}

	c.Logical, err = parse.StrToInt(m, "hw.logicalcpu_max")
	if err != nil {
		return CPU{}, err
	}

	c.Sockets = c.Physical / c.CoresPerSocket
	c.ThreadsPerCore = c.Logical / c.Sockets / c.CoresPerSocket
	c.Model = m["machdep.cpu.brand_string"]
	c.Flags = strings.ToLower(m["machdep.cpu.features"])

	return c, nil
}
Пример #2
0
func Get() (System, error) {
	s := System{}

	m, err := parse.ExecRegexpMap("/usr/sbin/system_profiler", []string{"SPHardwareDataType"}, ":", "\\S+:\\s\\S+")
	if err != nil {
		return System{}, err
	}

	host, err := os.Hostname()
	if err != nil {
		return System{}, err
	}
	s.Hostname = host
	s.ShortHostname = strings.Split(host, ".")[0]

	s.PeekabooVersion = version.Version

	s.Manufacturer = "Apple Inc."
	s.Product = m["Model Name"]
	s.ProductVersion = m["Model Identifier"]
	s.SerialNumber = m["Serial Number (system)"]
	s.BootROMVersion = m["Boot ROM Version"]
	s.SMCVersion = m["SMC Version (system)"]

	return s, nil
}
Пример #3
0
func Get() (Memory, error) {
	mem := Memory{}

	m, err := parse.ExecRegexpMap("/usr/sbin/sysctl", []string{"-a"}, ":", "\\S+:\\s\\S+")
	if err != nil {
		return Memory{}, err
	}

	mem.TotalKB, err = parse.StrToInt(m, "hw.memsize")
	if err != nil {
		return Memory{}, err
	}
	mem.TotalKB = mem.TotalKB / 1024
	mem.TotalGB = mem.TotalKB / 1024 / 1024

	return mem, nil
}
Пример #4
0
func Get() (OpSys, error) {
	o := OpSys{}

	m, err := parse.ExecRegexpMap("/usr/bin/sw_vers", []string{}, ":", "\\S+:\\s\\S+")
	if err != nil {
		return OpSys{}, err
	}

	o.Kernel = runtime.GOOS
	o.Product = m["ProductName"]
	o.ProductVersion = m["ProductVersion"]

	o.KernelVersion, err = parse.Exec("uname", []string{"-r"})
	if err != nil {
		return OpSys{}, err
	}

	return o, nil
}
Пример #5
0
func Get() (OpSys, error) {
	op := OpSys{}

	o, err := parse.ExecRegexpMap("lsb_release", []string{"-a"}, ":", "\\S+:\\s\\S+")
	if err != nil {
		return OpSys{}, err
	}

	op.Kernel = runtime.GOOS
	op.Product = o["Distributor ID"]
	op.ProductVersion = o["Release"]

	op.KernelVersion, err = parse.Exec("uname", []string{"-r"})
	if err != nil {
		return OpSys{}, err
	}

	return op, nil
}
Пример #6
0
// Get network interfaces.
func Get() (Interfaces, error) {
	hasEthtool := false
	if err := parse.Exists("ethtool"); err == nil {
		hasEthtool = true
	}

	hasLldpctl := false
	if err := parse.Exists("lldpctl"); err == nil {
		hasLldpctl = true
	}

	hasSfctool := false
	if err := parse.Exists("sfctool"); err == nil {
		hasSfctool = true
	}

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

	i := Interfaces{}
	for _, rIntf := range rIntfs {
		// Skip loopback interfaces
		if rIntf.Flags&net.FlagLoopback != 0 {
			continue
		}

		addrs, err := rIntf.Addrs()
		if err != nil {
			return Interfaces{}, err
		}

		wIntf := Interface{
			Name:   rIntf.Name,
			HWAddr: rIntf.HardwareAddr.String(),
			MTU:    rIntf.MTU,
		}

		for _, addr := range addrs {
			wIntf.IPAddr = append(wIntf.IPAddr, addr.String())
		}

		if rIntf.Flags&net.FlagUp != 0 {
			wIntf.Flags = append(wIntf.Flags, "UP")
		}

		if rIntf.Flags&net.FlagBroadcast != 0 {
			wIntf.Flags = append(wIntf.Flags, "BROADCAST")
		}

		if rIntf.Flags&net.FlagPointToPoint != 0 {
			wIntf.Flags = append(wIntf.Flags, "POINTTOPOINT")
		}

		if rIntf.Flags&net.FlagMulticast != 0 {
			wIntf.Flags = append(wIntf.Flags, "MULTICAST")
		}

		if runtime.GOOS == "linux" && hasEthtool == true {
			m, err := parse.ExecRegexpMap("ethtool", []string{"-i", rIntf.Name}, ":", "\\S+:\\s\\S+")

			// Do nothing on error
			if err == nil {
				s1 := m["driver"]
				wIntf.Driver = &s1
				s2 := m["version"]
				wIntf.DriverVersion = &s2
				s3 := m["firmware-version"]
				wIntf.FirmwareVersion = &s3
				if strings.HasPrefix(m["bus-info"], "0000:") {
					s4 := m["bus-info"]
					wIntf.PCIBus = &s4
					s5 := fmt.Sprintf("/pci/%v", m["bus-info"])
					wIntf.PCIBusURL = &s5
				}
			}
		}

		if runtime.GOOS == "linux" && hasEthtool == true {
			m, err := parse.ExecRegexpMap("ethtool", []string{rIntf.Name}, ":", "\\S+:\\s\\S+")

			// Do nothing on error
			if err == nil {
				i, err := strconv.Atoi(strings.TrimRight(m["Speed"], "Mb/s"))
				if err == nil {
					wIntf.SpeedMbs = &i
				}

				if wIntf.SpeedMbs != nil {
					s := m["Duplex"]
					wIntf.Duplex = &s
				}

				switch m["Link detected"] {
				case "yes":
					b := true
					wIntf.LinkDetected = &b
				case "no":
					b := false
					wIntf.LinkDetected = &b
				}
			}
		}

		if runtime.GOOS == "linux" && hasEthtool == true {
			m, err := parse.ExecRegexpMap("ethtool", []string{"-P", rIntf.Name}, ":[ ]", "\\S+:\\s([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})")

			// Do nothing on error
			if err == nil {
				bia := m["Permanent address"]
				wIntf.PermanentHWAddr = &bia
			}
		}

		if runtime.GOOS == "linux" && hasEthtool && wIntf.Driver != nil && *wIntf.Driver != "sfc" {
			o, err := parse.Exec("ethtool", []string{"-m", rIntf.Name, "raw", "on"})

			// Do nothing on error, doesn't support getting Eeprom info
			if err == nil {
				s := hex.EncodeToString([]byte(o))

				var b []byte
				var eeprom string = ""

				switch s[:2] {
				case "03": // SFP+
					eeprom = s[0:]
				case "0d": // QSFP+
					eeprom = s[256:768]
				}

				length := len(eeprom)
				if length >= 512 {
					ep := eeprom
					wIntf.TransceiverEeprom = &ep

					// Serial Number
					b, err = hex.DecodeString(eeprom[136:168])
					if err == nil {
						sn := strings.Trim(string(b), " ")
						wIntf.TransceiverSN = &sn
					}

					// Vendor Name
					b, err = hex.DecodeString(eeprom[40:72])
					if err == nil {
						vn := strings.Trim(string(b), " ")
						wIntf.TransceiverVN = &vn
					}

					// Product Name
					b, err = hex.DecodeString(eeprom[80:112])
					if err == nil {
						pn := strings.Trim(string(b), " ")
						wIntf.TransceiverPN = &pn
					}

					// Arista specific QSFP-4xSFP sub-assembly
					if *wIntf.TransceiverVN == "Arista Networks" && strings.HasPrefix(*wIntf.TransceiverPN, "CAB-Q-S-") {
						b, err = hex.DecodeString(eeprom[240:242])
						if err == nil {
							sa := int(b[0])
							wIntf.TransceiverSA = &sa
						}
					}
				}
			}
		}

		if runtime.GOOS == "linux" && hasSfctool && wIntf.Driver != nil && *wIntf.Driver == "sfc" {
			o, err := parse.Exec("sfctool", []string{"-m", rIntf.Name, "raw", "on"})

			// Do nothing on error, doesn't support getting Eeprom info
			if err == nil {
				s := hex.EncodeToString([]byte(o))

				var b []byte
				var eeprom string = ""

				switch s[:2] {
				case "03": // SFP+
					eeprom = s[0:512]
				case "0d": // QSFP+
					eeprom = s[256:768]
				}

				length := len(eeprom)
				if length == 512 {
					ep := eeprom
					wIntf.TransceiverEeprom = &ep

					// Serial Number
					b, err = hex.DecodeString(eeprom[136:168])
					if err == nil {
						sn := strings.Trim(string(b), " ")
						wIntf.TransceiverSN = &sn
					}

					// Vendor Name
					b, err = hex.DecodeString(eeprom[40:72])
					if err == nil {
						vn := strings.Trim(string(b), " ")
						wIntf.TransceiverVN = &vn
					}

					// Product Name
					b, err = hex.DecodeString(eeprom[80:112])
					if err == nil {
						pn := strings.Trim(string(b), " ")
						wIntf.TransceiverPN = &pn
					}

					// Arista specific QSFP-4xSFP sub-assembly
					if *wIntf.TransceiverVN == "Arista Networks" && strings.HasPrefix(*wIntf.TransceiverPN, "CAB-Q-S-") {
						b, err = hex.DecodeString(eeprom[240:242])
						if err == nil {
							sa := int(b[0])
							wIntf.TransceiverSA = &sa
						}
					}
				}
			}
		}

		if runtime.GOOS == "linux" && hasLldpctl == true {
			o, err := parse.Exec("lldpctl", []string{rIntf.Name})

			// Do nothing on error
			if err == nil {
				for _, line := range strings.Split(o, "\n") {
					arr := strings.SplitN(line, ":", 2)
					if len(arr) < 2 {
						continue
					}

					key := strings.TrimSpace(arr[0])
					val := strings.TrimSpace(arr[1])

					switch key {
					case "ChassisID":
						wIntf.SwChassisID = &val
					case "SysName":
						wIntf.SwName = &val
					case "SysDescr":
						wIntf.SwDescr = &val
					case "PortID":
						wIntf.SwPortID = &val
					case "PortDescr":
						wIntf.SwPortDescr = &val
					case "VLAN":
						wIntf.SwVLAN = &val
					}
				}

			}
		}

		i = append(i, wIntf)
	}

	return i, nil
}