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 (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2015-09-23 03:56:31 +00:00
|
|
|
"net"
|
2015-09-18 21:56:27 +00:00
|
|
|
"os"
|
|
|
|
"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-23 15:12:56 +00:00
|
|
|
// NewNetworkFingerprint returns a new NetworkFingerprinter with the given
|
|
|
|
// logger
|
|
|
|
func NewNetworkFingerprinter(logger *log.Logger) Fingerprint {
|
|
|
|
f := &NetworkFingerprint{logger: logger}
|
2015-09-18 21:56:27 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
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-09-23 03:56:31 +00:00
|
|
|
// eth0 is the default device for Linux, and en0 is default for OS X
|
|
|
|
defaultDevice := "eth0"
|
|
|
|
if "darwin" == runtime.GOOS {
|
|
|
|
defaultDevice = "en0"
|
|
|
|
}
|
2015-09-23 04:51:56 +00:00
|
|
|
|
|
|
|
newNetwork.Device = defaultDevice
|
|
|
|
|
2015-09-23 03:56:31 +00:00
|
|
|
if ip := f.ifConfig(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-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 03:04:20 +00:00
|
|
|
if throughput := f.linkSpeed("eth0"); throughput > 0 {
|
2015-09-23 04:22:23 +00:00
|
|
|
newNetwork.MBits = throughput
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// LinkSpeed attempts to determine link speed, first by checking if any tools
|
|
|
|
// exist that can return the speed (ethtool for now). If no tools are found,
|
|
|
|
// fall back to /sys/class/net speed file, if it exists.
|
|
|
|
//
|
|
|
|
// The return value is in the format of "<int>MB/s"
|
|
|
|
//
|
|
|
|
// LinkSpeed returns an empty string if no tools or sys file are found
|
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
|
|
|
|
}
|
|
|
|
}
|
2015-09-23 19:09:55 +00:00
|
|
|
f.logger.Printf("[WARN] fingerprint.network: Ethtool not found, checking /sys/net speed file")
|
2015-09-18 21:56:27 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
// linkSpeedSys parses the information stored in the sys diretory for the
|
|
|
|
// default device. This method retuns an empty string if the file is not found
|
|
|
|
// or cannot be read
|
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)
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err != nil {
|
2015-09-23 19:09:55 +00:00
|
|
|
f.logger.Printf("[WARN] fingerprint.network: Error getting information about net speed")
|
2015-09-23 03:04:20 +00:00
|
|
|
return 0
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read contents of the device/speed file
|
|
|
|
content, err := ioutil.ReadFile(path)
|
|
|
|
if err == nil {
|
|
|
|
lines := strings.Split(string(content), "\n")
|
|
|
|
// convert to MB/s
|
|
|
|
mbs, err := strconv.Atoi(lines[0])
|
|
|
|
if err != nil {
|
2015-09-23 19:09:55 +00:00
|
|
|
f.logger.Println("[WARN] fingerprint.network: Enable to parse ethtool output")
|
2015-09-23 03:04:20 +00:00
|
|
|
return 0
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 03:04:20 +00:00
|
|
|
if mbs > 0 {
|
2015-09-23 04:51:56 +00:00
|
|
|
return mbs
|
2015-09-23 03:04:20 +00:00
|
|
|
}
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
2015-09-23 03:04:20 +00:00
|
|
|
return 0
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// linkSpeedEthtool uses the ethtool installed on the node to gather link speed
|
|
|
|
// information. It executes the command on the device specified and parses
|
|
|
|
// out the speed. The expected format is Mbps and converted to MB/s
|
|
|
|
// Returns an empty string there is an error in parsing or executing 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()
|
|
|
|
if err == nil {
|
|
|
|
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
|
2015-09-23 19:09:55 +00:00
|
|
|
f.logger.Println("[WARN] fingerprint.network: Ethtool output did not match regex")
|
2015-09-23 03:04:20 +00:00
|
|
|
return 0
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Split and trim the Mb/s unit from the string output
|
|
|
|
args := strings.Split(m, ": ")
|
|
|
|
raw := strings.TrimSuffix(args[1], "Mb/s")
|
|
|
|
|
|
|
|
// convert to MB/s
|
|
|
|
mbs, err := strconv.Atoi(raw)
|
|
|
|
if err != nil {
|
2015-09-23 19:09:55 +00:00
|
|
|
f.logger.Println("[WARN] fingerprint.network: Unable to parse ethtool output")
|
2015-09-23 03:04:20 +00:00
|
|
|
return 0
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 03:04:20 +00:00
|
|
|
if mbs > 0 {
|
2015-09-23 04:57:24 +00:00
|
|
|
return mbs
|
2015-09-23 03:04:20 +00:00
|
|
|
}
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
2015-09-23 19:09:55 +00:00
|
|
|
f.logger.Printf("[ERR] fingerprint.network: Error calling ethtool (%s): %s", path, err)
|
2015-09-23 03:04:20 +00:00
|
|
|
return 0
|
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")
|
|
|
|
if ifConfigPath != "" {
|
|
|
|
outBytes, err := exec.Command(ifConfigPath, device).Output()
|
|
|
|
if err == nil {
|
2015-09-23 03:56:31 +00:00
|
|
|
// 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
|
2015-09-18 21:56:27 +00:00
|
|
|
output := strings.TrimSpace(string(outBytes))
|
|
|
|
|
2015-09-23 03:56:31 +00:00
|
|
|
// 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 {
|
|
|
|
return ip
|
|
|
|
}
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
2015-09-23 19:09:55 +00:00
|
|
|
f.logger.Printf("[ERR] fingerprint.network: Error calling ifconfig (%s): %s", ifConfigPath, err)
|
2015-09-18 21:56:27 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:09:55 +00:00
|
|
|
f.logger.Println("[WARN] fingerprint.network: Ethtool not found")
|
2015-09-18 21:56:27 +00:00
|
|
|
return ""
|
|
|
|
}
|