Repository navigation
Expand file tree
/
Copy pathnode.go
More file actions
72 lines (62 loc) · 1.69 KB
/
Copy pathnode.go
File metadata and controls
72 lines (62 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright © 2016 Zlatko Čalušić
//
// Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.
package sysinfo
import (
"bufio"
"os"
"strings"
)
// Node information.
type Node struct {
Hostname string `json:"hostname,omitempty"`
MachineID string `json:"machineid,omitempty"`
Hypervisor string `json:"hypervisor,omitempty"`
Timezone string `json:"timezone,omitempty"`
}
func (si *SysInfo) getHostname() {
si.Node.Hostname = slurpFile("/proc/sys/kernel/hostname")
}
// Reading inventory must never repair or create host identity files.
func (si *SysInfo) getMachineID() {
si.Node.MachineID = slurpFile("/etc/machine-id")
if si.Node.MachineID == "" {
si.Node.MachineID = slurpFile("/var/lib/dbus/machine-id")
}
}
func (si *SysInfo) getTimezone() {
const zoneInfoPrefix = "/usr/share/zoneinfo/"
if fi, err := os.Lstat("/etc/localtime"); err == nil {
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
if tzfile, err := os.Readlink("/etc/localtime"); err == nil {
tzfile = strings.TrimPrefix(tzfile, "..")
if strings.HasPrefix(tzfile, zoneInfoPrefix) {
si.Node.Timezone = strings.TrimPrefix(tzfile, zoneInfoPrefix)
return
}
}
}
}
if timezone := slurpFile("/etc/timezone"); timezone != "" {
si.Node.Timezone = timezone
return
}
if f, err := os.Open("/etc/sysconfig/clock"); err == nil {
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
if sl := strings.Split(s.Text(), "="); len(sl) == 2 {
if sl[0] == "ZONE" {
si.Node.Timezone = strings.Trim(sl[1], `"`)
return
}
}
}
}
}
func (si *SysInfo) getNodeInfo() {
si.getHostname()
si.getMachineID()
si.getHypervisor()
si.getTimezone()
}