Ejemplo n.º 1
0
func Get() (Docker, error) {
	d := Docker{}

	if err := parse.Exists("docker"); err == nil {
		d.Installed = true
	} else {
		d.Installed = false
		d.Running = false
		return d, nil
	}

	o, err := parse.Exec("docker", []string{"--version"})
	if err != nil {
		return Docker{}, err
	}

	v := strings.Fields(o)

	d.Version = strings.TrimRight(v[2], ",")
	d.Build = v[4]

	_, err2 := parse.Exec("docker", []string{"ps"})
	if err2 != nil {
		d.Running = false
	} else {
		d.Running = true
	}

	return d, nil
}
Ejemplo n.º 2
0
func Get() (IPMI, error) {
	i := IPMI{}

	// ipmitool
	if err := parse.Exists("ipmitool"); err == nil {
		i.IpmitoolInstalled = true

		o, err := parse.Exec("ipmitool", []string{"-V"})
		if err != nil {
			return IPMI{}, err
		}
		arr := strings.Split(o, " ")
		i.IpmitoolVersion = arr[2]
	} else {
		i.IpmitoolInstalled = false
		return i, nil
	}

	o, err := parse.Exec("ipmitool", []string{"lan", "print", "1"})
	if err == nil {
		for _, line := range strings.Split(o, "\n") {
			a := strings.SplitN(line, ":", 2)
			if len(a) < 2 {
				continue
			}

			k := strings.TrimSpace(a[0])
			v := strings.TrimSpace(a[1])

			switch k {
			case "IP Address Source":
				i.IPSource = &v
			case "IP Address":
				i.IPAddr = &v
			case "Subnet Mask":
				i.Netmask = &v
			case "MAC Address":
				i.HWAddr = &v
			case "Default Gateway IP":
				i.Gateway = &v
			}
		}
	}

	if i.IPAddr != nil {
		names, err := net.LookupAddr(*i.IPAddr)
		if err == nil {
			for i, v := range names {
				names[i] = strings.TrimRight(v, ".")
			}

			i.DNSNames = &names
		}
	}

	return i, nil
}
Ejemplo n.º 3
0
func Get() (Network, error) {
	n := Network{}

	// ethtool
	n.EthtoolInstalled = false
	if err := parse.Exists("ethtool"); err == nil {
		n.EthtoolInstalled = true

		o, err := parse.Exec("ethtool", []string{"--version"})
		if err != nil {
			return Network{}, err
		}
		arr := strings.Split(o, " ")
		n.EthtoolVersion = arr[2]
	}

	// lldpctl
	n.LldpctlInstalled = false
	if err := parse.Exists("lldpctl"); err == nil {
		n.LldpctlInstalled = true

		o, err := parse.Exec("lldpctl", []string{"-v"})
		if err != nil {
			return Network{}, err
		}
		n.LldpctlVersion = o
	}

	// onload
	n.OnloadInstalled = false
	if err := parse.Exists("onload"); err == nil {
		n.OnloadInstalled = true

		o, err := parse.Exec("onload", []string{"--version"})
		if err != nil {
			return Network{}, err
		}

		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 "OpenOnload":
				n.OnloadVersion = val
			}
		}
	}

	// sfctool
	n.SfctoolInstalled = false
	if err := parse.Exists("sfctool"); err == nil {
		n.SfctoolInstalled = true

		o, err := parse.Exec("sfctool", []string{"--version"})
		if err != nil {
			return Network{}, err
		}
		arr := strings.Split(o, " ")
		n.SfctoolVersion = arr[2]
	}

	// sfckey
	n.SfkeyInstalled = false
	if err := parse.Exists("sfkey"); err == nil {
		n.SfkeyInstalled = true

		o, err := parse.Exec("sfkey", []string{"--version"})
		if err != nil {
			return Network{}, err
		}

		for _, line := range strings.Split(o, "\n") {
			arr := strings.Split(line, ":")
			if len(arr) < 2 {
				continue
			}

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

			switch key {
			case "sfkey firmware update utility":
				n.SfkeyVersion = strings.TrimLeft(val, "v")
			}
		}
	}

	return n, nil
}
Ejemplo n.º 4
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
}