Skip to content

Commit

Permalink
Merge pull request kubernetes#560 from simon3z/master
Browse files Browse the repository at this point in the history
Add system uuid and machine id information
  • Loading branch information
vmarmol committed Mar 4, 2015
2 parents 7b3fdb8 + 02a3e46 commit 7839dd8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions info/v1/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ type MachineInfo struct {
// The amount of memory (in bytes) in this machine
MemoryCapacity int64 `json:"memory_capacity"`

// The machine id
MachineID string `json:"machine_id"`

// The system uuid
SystemUUID string `json:"system_uuid"`

// Filesystems on this machine.
Filesystems []FsInfo `json:"filesystems"`

Expand Down
20 changes: 20 additions & 0 deletions manager/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package manager

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"regexp"
Expand All @@ -24,6 +25,7 @@ import (
"syscall"

dclient "github.com/fsouza/go-dockerclient"
"github.com/golang/glog"
"github.com/google/cadvisor/container/docker"
"github.com/google/cadvisor/fs"
info "github.com/google/cadvisor/info/v1"
Expand All @@ -39,6 +41,8 @@ var nodeRegExp = regexp.MustCompile("physical id\\t*: +([0-9]+)")
var CpuClockSpeedMHz = regexp.MustCompile("cpu MHz\\t*: +([0-9]+.[0-9]+)")
var memoryCapacityRegexp = regexp.MustCompile("MemTotal: *([0-9]+) kB")

var machineIdFilePath = flag.String("machine_id_file", "/etc/machine-id", "File containing the machine-id")

func getClockSpeed(procInfo []byte) (uint64, error) {
// First look through sys to find a max supported cpu frequency.
const maxFreqFile = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"
Expand Down Expand Up @@ -205,6 +209,15 @@ func getTopology(sysFs sysfs.SysFs, cpuinfo string) ([]info.Node, int, error) {
return nodes, numCores, nil
}

func getMachineID() string {
id, err := ioutil.ReadFile(*machineIdFilePath)
if err != nil {
glog.Error("Couldn't collect machine-id: ", err)
return ""
}
return strings.TrimSpace(string(id))
}

func getMachineInfo(sysFs sysfs.SysFs) (*info.MachineInfo, error) {
cpuinfo, err := ioutil.ReadFile("/proc/cpuinfo")
clockSpeed, err := getClockSpeed(cpuinfo)
Expand Down Expand Up @@ -247,13 +260,20 @@ func getMachineInfo(sysFs sysfs.SysFs) (*info.MachineInfo, error) {
return nil, err
}

system_uuid, err := sysinfo.GetSystemUUID(sysFs)
if err != nil {
return nil, err
}

machineInfo := &info.MachineInfo{
NumCores: numCores,
CpuFrequency: clockSpeed,
MemoryCapacity: memoryCapacity,
DiskMap: diskMap,
NetworkDevices: netDevices,
Topology: topology,
MachineID: getMachineID(),
SystemUUID: system_uuid,
}

for _, fs := range filesystems {
Expand Down
4 changes: 4 additions & 0 deletions utils/sysfs/fakesysfs/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,7 @@ func (self *FakeSysFs) SetCacheInfo(cache sysfs.CacheInfo) {
func (self *FakeSysFs) SetEntryName(name string) {
self.info.EntryName = name
}

func (self *FakeSysFs) GetSystemUUID() (string, error) {
return "1F862619-BA9F-4526-8F85-ECEAF0C97430", nil
}
11 changes: 11 additions & 0 deletions utils/sysfs/sysfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
blockDir = "/sys/block"
cacheDir = "/sys/devices/system/cpu/cpu"
netDir = "/sys/class/net"
dmiDir = "/sys/class/dmi"
)

type CacheInfo struct {
Expand Down Expand Up @@ -61,6 +62,8 @@ type SysFs interface {
GetCaches(id int) ([]os.FileInfo, error)
// Get information for a cache accessible from the given cpu.
GetCacheInfo(cpu int, cache string) (CacheInfo, error)

GetSystemUUID() (string, error)
}

type realSysFs struct{}
Expand Down Expand Up @@ -228,3 +231,11 @@ func (self *realSysFs) GetCacheInfo(id int, name string) (CacheInfo, error) {
Cpus: cpuCount,
}, nil
}

func (self *realSysFs) GetSystemUUID() (string, error) {
id, err := ioutil.ReadFile(path.Join(dmiDir, "id", "product_uuid"))
if err != nil {
return "", err
}
return strings.TrimSpace(string(id)), nil
}
4 changes: 4 additions & 0 deletions utils/sysinfo/sysinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,7 @@ func getNetworkStats(name string, sysFs sysfs.SysFs) (info.NetworkStats, error)
}
return stats, nil
}

func GetSystemUUID(sysFs sysfs.SysFs) (string, error) {
return sysFs.GetSystemUUID()
}

0 comments on commit 7839dd8

Please sign in to comment.