host_linux.go 7.8 KB
Newer Older
1
// +build linux
W
WAKAYAMA Shirou 已提交
2

3
package gopsutil
W
WAKAYAMA Shirou 已提交
4 5

import (
S
Shirou WAKAYAMA 已提交
6
	"bytes"
W
go fmt  
WAKAYAMA shirou 已提交
7
	"encoding/binary"
S
Shirou WAKAYAMA 已提交
8
	"io/ioutil"
W
WAKAYAMA Shirou 已提交
9
	"os"
10
	"os/exec"
11
	"regexp"
12
	"runtime"
S
go fmt  
Shirou WAKAYAMA 已提交
13
	"strings"
W
WAKAYAMA Shirou 已提交
14
	"syscall"
S
Shirou WAKAYAMA 已提交
15
	"unsafe"
16 17

	common "github.com/shirou/gopsutil/common"
W
WAKAYAMA Shirou 已提交
18 19
)

20 21 22 23 24 25 26
type LSB struct {
	ID          string
	Release     string
	Codename    string
	Description string
}

27
func HostInfo() (*HostInfoStat, error) {
W
WAKAYAMA Shirou 已提交
28
	hostname, err := os.Hostname()
S
Shirou WAKAYAMA 已提交
29
	if err != nil {
30
		return nil, err
S
Shirou WAKAYAMA 已提交
31 32
	}

33 34
	ret := &HostInfoStat{
		Hostname: hostname,
35
		OS:       runtime.GOOS,
36
	}
37

38
	platform, family, version, err := GetPlatformInformation()
S
go fmt  
Shirou WAKAYAMA 已提交
39
	if err == nil {
40 41 42 43
		ret.Platform = platform
		ret.PlatformFamily = family
		ret.PlatformVersion = version
	}
44 45 46 47 48
	system, role, err := GetVirtualization()
	if err == nil {
		ret.VirtualizationSystem = system
		ret.VirtualizationRole = role
	}
W
WAKAYAMA Shirou 已提交
49 50 51 52
	uptime, err := BootTime()
	if err == nil {
		ret.Uptime = uptime
	}
53

54 55 56
	return ret, nil
}

W
WAKAYAMA Shirou 已提交
57
func BootTime() (uint64, error) {
S
Shirou WAKAYAMA 已提交
58 59
	sysinfo := &syscall.Sysinfo_t{}
	if err := syscall.Sysinfo(sysinfo); err != nil {
60
		return 0, err
S
Shirou WAKAYAMA 已提交
61
	}
W
WAKAYAMA shirou 已提交
62
	return uint64(sysinfo.Uptime), nil
W
WAKAYAMA Shirou 已提交
63
}
S
Shirou WAKAYAMA 已提交
64 65 66 67 68 69

func Users() ([]UserStat, error) {
	utmpfile := "/var/run/utmp"

	file, err := os.Open(utmpfile)
	if err != nil {
S
Shirou WAKAYAMA 已提交
70
		return nil, err
S
Shirou WAKAYAMA 已提交
71 72 73 74
	}

	buf, err := ioutil.ReadAll(file)
	if err != nil {
S
Shirou WAKAYAMA 已提交
75
		return nil, err
S
Shirou WAKAYAMA 已提交
76 77 78 79 80 81
	}

	u := utmp{}
	entrySize := int(unsafe.Sizeof(u))
	count := len(buf) / entrySize

S
Shirou WAKAYAMA 已提交
82 83
	ret := make([]UserStat, 0, count)

S
Shirou WAKAYAMA 已提交
84 85 86
	for i := 0; i < count; i++ {
		b := buf[i*entrySize : i*entrySize+entrySize]

W
go fmt  
WAKAYAMA shirou 已提交
87 88 89
		var u utmp
		br := bytes.NewReader(b)
		err := binary.Read(br, binary.LittleEndian, &u)
S
Shirou WAKAYAMA 已提交
90 91 92 93
		if err != nil {
			continue
		}
		user := UserStat{
94 95 96
			User:     common.ByteToString(u.UtUser[:]),
			Terminal: common.ByteToString(u.UtLine[:]),
			Host:     common.ByteToString(u.UtHost[:]),
S
Shirou WAKAYAMA 已提交
97
			Started:  int(u.UtTv.TvSec),
S
Shirou WAKAYAMA 已提交
98 99 100 101 102 103 104
		}
		ret = append(ret, user)
	}

	return ret, nil

}
105 106 107 108

func getLSB() (*LSB, error) {
	ret := &LSB{}
	if pathExists("/etc/lsb-release") {
W
WAKAYAMA shirou 已提交
109
		contents, err := common.ReadLines("/etc/lsb-release")
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
		if err != nil {
			return ret, err // return empty
		}
		for _, line := range contents {
			field := strings.Split(line, "=")
			if len(field) < 2 {
				continue
			}
			switch field[0] {
			case "DISTRIB_ID":
				ret.ID = field[1]
			case "DISTRIB_RELEASE":
				ret.Release = field[1]
			case "DISTRIB_CODENAME":
				ret.Codename = field[1]
			case "DISTRIB_DESCRIPTION":
				ret.Description = field[1]
			}
		}
	} else if pathExists("/usr/bin/lsb_release") {
		out, err := exec.Command("/usr/bin/lsb_release").Output()
		if err != nil {
			return ret, err
		}
		for _, line := range strings.Split(string(out), "\n") {
			field := strings.Split(line, ":")
			if len(field) < 2 {
				continue
			}
			switch field[0] {
			case "Distributor ID":
				ret.ID = field[1]
			case "Release":
				ret.Release = field[1]
			case "Codename":
				ret.Codename = field[1]
			case "Description":
				ret.Description = field[1]
			}
		}

	}

	return ret, nil
}

L
Lukas Lueg 已提交
156
func GetPlatformInformation() (platform string, family string, version string, err error) {
157

W
WAKAYAMA Shirou 已提交
158
	lsb, err := getLSB()
159 160
	if err != nil {
		lsb = &LSB{}
W
WAKAYAMA Shirou 已提交
161
	}
162 163 164

	if pathExists("/etc/oracle-release") {
		platform = "oracle"
W
WAKAYAMA shirou 已提交
165
		contents, err := common.ReadLines("/etc/oracle-release")
166
		if err == nil {
167
			version = getRedhatishVersion(contents)
168 169 170
		}
	} else if pathExists("/etc/enterprise-release") {
		platform = "oracle"
W
WAKAYAMA shirou 已提交
171
		contents, err := common.ReadLines("/etc/enterprise-release")
172
		if err == nil {
173
			version = getRedhatishVersion(contents)
174 175
		}
	} else if pathExists("/etc/debian_version") {
S
go fmt  
Shirou WAKAYAMA 已提交
176 177 178 179 180 181 182 183 184 185 186 187
		if lsb.ID == "Ubuntu" {
			platform = "ubuntu"
			version = lsb.Release
		} else if lsb.ID == "LinuxMint" {
			platform = "linuxmint"
			version = lsb.Release
		} else {
			if pathExists("/usr/bin/raspi-config") {
				platform = "raspbian"
			} else {
				platform = "debian"
			}
W
WAKAYAMA shirou 已提交
188
			contents, err := common.ReadLines("/etc/debian_version")
S
go fmt  
Shirou WAKAYAMA 已提交
189 190 191
			if err == nil {
				version = contents[0]
			}
192
		}
S
go fmt  
Shirou WAKAYAMA 已提交
193
	} else if pathExists("/etc/redhat-release") {
W
WAKAYAMA shirou 已提交
194
		contents, err := common.ReadLines("/etc/redhat-release")
195
		if err == nil {
196 197
			version = getRedhatishVersion(contents)
			platform = getRedhatishPlatform(contents)
198
		}
S
go fmt  
Shirou WAKAYAMA 已提交
199
	} else if pathExists("/etc/system-release") {
W
WAKAYAMA shirou 已提交
200
		contents, err := common.ReadLines("/etc/system-release")
201
		if err == nil {
202 203
			version = getRedhatishVersion(contents)
			platform = getRedhatishPlatform(contents)
204
		}
S
go fmt  
Shirou WAKAYAMA 已提交
205
	} else if pathExists("/etc/gentoo-release") {
206
		platform = "gentoo"
W
WAKAYAMA shirou 已提交
207
		contents, err := common.ReadLines("/etc/gentoo-release")
208
		if err == nil {
209
			version = getRedhatishVersion(contents)
210 211 212
		}
		// TODO: suse detection
		// TODO: slackware detecion
S
go fmt  
Shirou WAKAYAMA 已提交
213
	} else if pathExists("/etc/arch-release") {
214 215
		platform = "arch"
		// TODO: exherbo detection
S
go fmt  
Shirou WAKAYAMA 已提交
216
	} else if lsb.ID == "RedHat" {
217 218
		platform = "redhat"
		version = lsb.Release
S
go fmt  
Shirou WAKAYAMA 已提交
219
	} else if lsb.ID == "Amazon" {
220
		platform = "amazon"
S
go fmt  
Shirou WAKAYAMA 已提交
221 222
		version = lsb.Release
	} else if lsb.ID == "ScientificSL" {
223
		platform = "scientific"
S
go fmt  
Shirou WAKAYAMA 已提交
224 225
		version = lsb.Release
	} else if lsb.ID == "XenServer" {
226
		platform = "xenserver"
S
go fmt  
Shirou WAKAYAMA 已提交
227 228
		version = lsb.Release
	} else if lsb.ID != "" {
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
		platform = strings.ToLower(lsb.ID)
		version = lsb.Release
	}

	switch platform {
	case "debian", "ubuntu", "linuxmint", "raspbian":
		family = "debian"
	case "fedora":
		family = "fedora"
	case "oracle", "centos", "redhat", "scientific", "enterpriseenterprise", "amazon", "xenserver", "cloudlinux", "ibm_powerkvm":
		family = "rhel"
	case "suse":
		family = "suse"
	case "gentoo":
		family = "gentoo"
	case "slackware":
		family = "slackware"
	case "arch":
		family = "arch"
	case "exherbo":
		family = "exherbo"
	}

	return platform, family, version, nil

}

256 257 258 259 260 261 262 263 264
func getRedhatishVersion(contents []string) string {
	c := strings.ToLower(strings.Join(contents, ""))

	if strings.Contains(c, "rawhide") {
		return "rawhide"
	}
	if matches := regexp.MustCompile(`release (\d[\d.]*)`).FindStringSubmatch(c); matches != nil {
		return matches[1]
	}
L
Lukas Lueg 已提交
265
	return ""
266 267
}

268 269 270 271 272 273 274 275 276
func getRedhatishPlatform(contents []string) string {
	c := strings.ToLower(strings.Join(contents, ""))

	if strings.Contains(c, "red hat") {
		return "redhat"
	}
	f := strings.Split(c, " ")

	return f[0]
277
}
278 279 280 281 282 283 284 285 286 287

func GetVirtualization() (string, string, error) {
	var system string
	var role string

	if pathExists("/proc/xen") {
		system = "xen"
		role = "guest" // assume guest

		if pathExists("/proc/xen/capabilities") {
W
WAKAYAMA shirou 已提交
288
			contents, err := common.ReadLines("/proc/xen/capabilities")
289 290 291 292 293 294 295 296
			if err == nil {
				if stringContains(contents, "control_d") {
					role = "host"
				}
			}
		}
	}
	if pathExists("/proc/modules") {
W
WAKAYAMA shirou 已提交
297
		contents, err := common.ReadLines("/proc/modules")
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
		if err == nil {
			if stringContains(contents, "kvm") {
				system = "kvm"
				role = "host"
			} else if stringContains(contents, "vboxdrv") {
				system = "vbox"
				role = "host"
			} else if stringContains(contents, "vboxguest") {
				system = "vbox"
				role = "guest"
			}
		}
	}

	if pathExists("/proc/cpuinfo") {
W
WAKAYAMA shirou 已提交
313
		contents, err := common.ReadLines("/proc/cpuinfo")
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
		if err == nil {
			if stringContains(contents, "QEMU Virtual CPU") ||
				stringContains(contents, "Common KVM processor") ||
				stringContains(contents, "Common 32-bit KVM processor") {
				system = "kvm"
				role = "guest"
			}
		}
	}

	if pathExists("/proc/bc/0") {
		system = "openvz"
		role = "host"
	} else if pathExists("/proc/vz") {
		system = "openvz"
		role = "guest"
	}

	// not use dmidecode because it requires root

	if pathExists("/proc/self/status") {
W
WAKAYAMA shirou 已提交
335
		contents, err := common.ReadLines("/proc/self/status")
336 337 338 339 340 341 342 343 344 345 346
		if err == nil {

			if stringContains(contents, "s_context:") ||
				stringContains(contents, "VxID:") {
				system = "linux-vserver"
			}
			// TODO: guest or host
		}
	}

	if pathExists("/proc/self/cgroup") {
W
WAKAYAMA shirou 已提交
347
		contents, err := common.ReadLines("/proc/self/cgroup")
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
		if err == nil {

			if stringContains(contents, "lxc") ||
				stringContains(contents, "docker") {
				system = "lxc"
				role = "guest"
			} else if pathExists("/usr/bin/lxc-version") { // TODO: which
				system = "lxc"
				role = "host"
			}
		}
	}

	return system, role, nil
}