2015-09-18 21:56:27 +00:00
|
|
|
// +build linux darwin
|
2015-09-28 06:53:25 +00:00
|
|
|
|
2015-09-18 21:56:27 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
2015-10-01 15:31:47 +00:00
|
|
|
"errors"
|
2015-09-18 21:56:27 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2015-09-23 03:56:31 +00:00
|
|
|
"net"
|
2015-09-18 21:56:27 +00:00
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
2015-09-23 03:56:31 +00:00
|
|
|
"runtime"
|
2015-09-18 21:56:27 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2015-09-23 15:12:56 +00:00
|
|
|
// NetworkFingerprint is used to fingerprint the Network capabilities of a node
|
|
|
|
type NetworkFingerprint struct {
|
2015-09-18 21:56:27 +00:00
|
|
|
logger *log.Logger
|
|
|
|
}
|
|
|
|
|
2015-09-28 23:23:30 +00:00
|
|
|
// NewNetworkFingerprinter returns a new NetworkFingerprinter with the given
|
2015-09-23 15:12:56 +00:00
|
|
|
// logger
|
|
|
|
func NewNetworkFingerprinter(logger *log.Logger) Fingerprint {
|
|
|
|
f := &NetworkFingerprint{logger: logger}
|
2015-09-18 21:56:27 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2015-10-25 14:30:56 +00:00
|
|
|
func flagsSet(flags net.Flags, test net.Flags) bool {
|
|
|
|
return flags&test != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func flagsClear(flags net.Flags, test net.Flags) bool {
|
|
|
|
return flags&test == 0
|
|
|
|
}
|
|
|
|
|
2015-09-23 15:12:56 +00:00
|
|
|
func (f *NetworkFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
|
2015-09-23 04:22:23 +00:00
|
|
|
// newNetwork is populated and addded to the Nodes resources
|
|
|
|
newNetwork := &structs.NetworkResource{}
|
2015-10-25 14:30:56 +00:00
|
|
|
defaultDevice := ""
|
2015-09-23 04:22:23 +00:00
|
|
|
|
2015-10-25 14:30:56 +00:00
|
|
|
// Use user-defined network device, otherwise use first interface found in the system
|
2015-10-02 07:29:18 +00:00
|
|
|
if cfg.NetworkInterface != "" {
|
|
|
|
defaultDevice = cfg.NetworkInterface
|
2015-10-25 14:30:56 +00:00
|
|
|
} else {
|
|
|
|
intfs, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range intfs {
|
|
|
|
if flagsSet(i.Flags, net.FlagUp) && flagsClear(i.Flags, net.FlagLoopback|net.FlagPointToPoint) {
|
|
|
|
defaultDevice = i.Name
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-10-01 15:31:47 +00:00
|
|
|
}
|
2015-09-23 04:51:56 +00:00
|
|
|
|
2015-10-25 14:30:56 +00:00
|
|
|
if defaultDevice != "" {
|
|
|
|
newNetwork.Device = defaultDevice
|
|
|
|
} else {
|
|
|
|
return false, fmt.Errorf("Unable to find any network interface")
|
|
|
|
}
|
2015-09-23 04:51:56 +00:00
|
|
|
|
2015-10-01 15:31:47 +00:00
|
|
|
if ip := f.ipAddress(defaultDevice); ip != "" {
|
2015-09-18 21:56:27 +00:00
|
|
|
node.Attributes["network.ip-address"] = ip
|
2015-09-23 04:22:23 +00:00
|
|
|
newNetwork.IP = ip
|
2015-09-23 04:51:56 +00:00
|
|
|
newNetwork.CIDR = newNetwork.IP + "/32"
|
2015-10-03 00:32:11 +00:00
|
|
|
} else {
|
|
|
|
return false, fmt.Errorf("Unable to determine IP on network interface %v", defaultDevice)
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 23:23:30 +00:00
|
|
|
if throughput := f.linkSpeed(defaultDevice); throughput > 0 {
|
2015-09-23 04:22:23 +00:00
|
|
|
newNetwork.MBits = throughput
|
2015-10-03 00:32:11 +00:00
|
|
|
} else {
|
2015-10-03 00:23:58 +00:00
|
|
|
f.logger.Printf("[DEBUG] fingerprint.network: Unable to read link speed; setting to default %v", cfg.NetworkSpeed)
|
2015-10-03 00:32:11 +00:00
|
|
|
newNetwork.MBits = cfg.NetworkSpeed
|
2015-09-23 04:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if node.Resources == nil {
|
|
|
|
node.Resources = &structs.Resources{}
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 04:22:23 +00:00
|
|
|
node.Resources.Networks = append(node.Resources.Networks, newNetwork)
|
|
|
|
|
2015-09-18 21:56:27 +00:00
|
|
|
// return true, because we have a network connection
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 20:33:11 +00:00
|
|
|
// linkSpeed returns link speed in Mb/s, or 0 when unable to determine it.
|
2015-09-23 15:12:56 +00:00
|
|
|
func (f *NetworkFingerprint) linkSpeed(device string) int {
|
2015-09-18 21:56:27 +00:00
|
|
|
// Use LookPath to find the ethtool in the systems $PATH
|
|
|
|
// If it's not found or otherwise errors, LookPath returns and empty string
|
|
|
|
// and an error we can ignore for our purposes
|
|
|
|
ethtoolPath, _ := exec.LookPath("ethtool")
|
|
|
|
if ethtoolPath != "" {
|
2015-09-23 03:04:20 +00:00
|
|
|
if speed := f.linkSpeedEthtool(ethtoolPath, device); speed > 0 {
|
2015-09-18 21:56:27 +00:00
|
|
|
return speed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back on checking a system file for link speed.
|
2015-09-22 20:59:00 +00:00
|
|
|
return f.linkSpeedSys(device)
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 20:33:11 +00:00
|
|
|
// linkSpeedSys parses link speed in Mb/s from /sys.
|
2015-09-23 15:12:56 +00:00
|
|
|
func (f *NetworkFingerprint) linkSpeedSys(device string) int {
|
2015-09-18 21:56:27 +00:00
|
|
|
path := fmt.Sprintf("/sys/class/net/%s/speed", device)
|
|
|
|
|
|
|
|
// Read contents of the device/speed file
|
|
|
|
content, err := ioutil.ReadFile(path)
|
2015-09-28 23:23:30 +00:00
|
|
|
if err != nil {
|
|
|
|
f.logger.Printf("[WARN] fingerprint.network: Unable to read link speed from %s", path)
|
|
|
|
return 0
|
|
|
|
}
|
2015-09-18 21:56:27 +00:00
|
|
|
|
2015-09-28 23:23:30 +00:00
|
|
|
lines := strings.Split(string(content), "\n")
|
|
|
|
mbs, err := strconv.Atoi(lines[0])
|
|
|
|
if err != nil || mbs <= 0 {
|
|
|
|
f.logger.Printf("[WARN] fingerprint.network: Unable to parse link speed from %s", path)
|
|
|
|
return 0
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
2015-09-28 23:23:30 +00:00
|
|
|
|
|
|
|
return mbs
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 20:33:11 +00:00
|
|
|
// linkSpeedEthtool determines link speed in Mb/s with 'ethtool'.
|
2015-09-23 15:12:56 +00:00
|
|
|
func (f *NetworkFingerprint) linkSpeedEthtool(path, device string) int {
|
2015-09-18 21:56:27 +00:00
|
|
|
outBytes, err := exec.Command(path, device).Output()
|
2015-09-28 23:23:30 +00:00
|
|
|
if err != nil {
|
|
|
|
f.logger.Printf("[WARN] fingerprint.network: Error calling ethtool (%s %s): %v", path, device, err)
|
|
|
|
return 0
|
|
|
|
}
|
2015-09-18 21:56:27 +00:00
|
|
|
|
2015-09-28 23:23:30 +00:00
|
|
|
output := strings.TrimSpace(string(outBytes))
|
|
|
|
re := regexp.MustCompile("Speed: [0-9]+[a-zA-Z]+/s")
|
|
|
|
m := re.FindString(output)
|
|
|
|
if m == "" {
|
|
|
|
// no matches found, output may be in a different format
|
|
|
|
f.logger.Printf("[WARN] fingerprint.network: Unable to parse Speed in output of '%s %s'", path, device)
|
|
|
|
return 0
|
|
|
|
}
|
2015-09-18 21:56:27 +00:00
|
|
|
|
2015-09-28 23:23:30 +00:00
|
|
|
// Split and trim the Mb/s unit from the string output
|
|
|
|
args := strings.Split(m, ": ")
|
|
|
|
raw := strings.TrimSuffix(args[1], "Mb/s")
|
2015-09-18 21:56:27 +00:00
|
|
|
|
2015-09-28 23:23:30 +00:00
|
|
|
// convert to Mb/s
|
|
|
|
mbs, err := strconv.Atoi(raw)
|
|
|
|
if err != nil || mbs <= 0 {
|
|
|
|
f.logger.Printf("[WARN] fingerprint.network: Unable to parse Mb/s in output of '%s %s'", path, device)
|
|
|
|
return 0
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
2015-09-28 23:23:30 +00:00
|
|
|
|
|
|
|
return mbs
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 15:31:47 +00:00
|
|
|
// ipAddress returns the first IPv4 address on the configured default interface
|
|
|
|
// Tries Golang native functions and falls back onto ifconfig
|
|
|
|
func (f *NetworkFingerprint) ipAddress(device string) string {
|
|
|
|
if ip, err := f.nativeIpAddress(device); err == nil {
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.ifConfig(device)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *NetworkFingerprint) nativeIpAddress(device string) (string, error) {
|
|
|
|
// Find IP address on configured interface
|
|
|
|
var ip string
|
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.New("could not retrieve interface list")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: should we handle IPv6 here? How do we determine precedence?
|
|
|
|
for _, i := range ifaces {
|
2015-10-01 19:16:39 +00:00
|
|
|
if i.Name != device {
|
|
|
|
continue
|
|
|
|
}
|
2015-10-01 15:33:40 +00:00
|
|
|
|
2015-10-01 19:16:39 +00:00
|
|
|
addrs, err := i.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.New("could not retrieve interface IP addresses")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, a := range addrs {
|
|
|
|
switch v := a.(type) {
|
|
|
|
case *net.IPNet:
|
|
|
|
if v.IP.To4() != nil {
|
|
|
|
ip = v.IP.String()
|
|
|
|
}
|
|
|
|
case *net.IPAddr:
|
|
|
|
if v.IP.To4() != nil {
|
|
|
|
ip = v.IP.String()
|
2015-10-01 15:31:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if net.ParseIP(ip) == nil {
|
|
|
|
return "", errors.New(fmt.Sprintf("could not parse IP address `%s`", ip))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip, nil
|
|
|
|
}
|
|
|
|
|
2015-09-18 21:56:27 +00:00
|
|
|
// ifConfig returns the IP Address for this node according to ifConfig, for the
|
|
|
|
// specified device.
|
2015-09-23 15:12:56 +00:00
|
|
|
func (f *NetworkFingerprint) ifConfig(device string) string {
|
2015-09-18 21:56:27 +00:00
|
|
|
ifConfigPath, _ := exec.LookPath("ifconfig")
|
2015-09-28 23:23:30 +00:00
|
|
|
if ifConfigPath == "" {
|
2015-09-29 20:06:37 +00:00
|
|
|
f.logger.Println("[WARN] fingerprint.network: ifconfig not found")
|
2015-09-28 23:23:30 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
outBytes, err := exec.Command(ifConfigPath, device).Output()
|
|
|
|
if err != nil {
|
|
|
|
f.logger.Printf("[WARN] fingerprint.network: Error calling ifconfig (%s %s): %v", ifConfigPath, device, err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse out the IP address returned from ifconfig for this device
|
|
|
|
// Tested on Ubuntu, the matching part of ifconfig output for eth0 is like
|
|
|
|
// so:
|
|
|
|
// inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
|
|
|
|
// For OS X and en0, we have:
|
|
|
|
// inet 192.168.0.7 netmask 0xffffff00 broadcast 192.168.0.255
|
|
|
|
output := strings.TrimSpace(string(outBytes))
|
|
|
|
|
|
|
|
// re is a regular expression, which can vary based on the OS
|
|
|
|
var re *regexp.Regexp
|
|
|
|
|
|
|
|
if "darwin" == runtime.GOOS {
|
|
|
|
re = regexp.MustCompile("inet [0-9].+")
|
|
|
|
} else {
|
|
|
|
re = regexp.MustCompile("inet addr:[0-9].+")
|
|
|
|
}
|
|
|
|
args := strings.Split(re.FindString(output), " ")
|
|
|
|
|
|
|
|
var ip string
|
|
|
|
if len(args) > 1 {
|
|
|
|
ip = strings.TrimPrefix(args[1], "addr:")
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate what we've sliced out is a valid IP
|
|
|
|
if net.ParseIP(ip) == nil {
|
|
|
|
f.logger.Printf("[WARN] fingerprint.network: Unable to parse IP in output of '%s %s'", ifConfigPath, device)
|
2015-09-18 21:56:27 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-09-28 23:23:30 +00:00
|
|
|
return ip
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|