File: utils.go

package info (click to toggle)
tiup 1.16.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,384 kB
  • sloc: sh: 1,988; makefile: 138; sql: 16
file content (50 lines) | stat: -rw-r--r-- 987 bytes parent folder | download
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
package insight

import (
	"os"
	"strconv"
	"strings"
)

// Version information
var (
	// GitBranch is initialized during make
	GitBranch = "Not Provided"

	// GitCommit is initialized during make
	GitCommit = "Not Provided"

	// Proc dir path for Linux
	procPath = "/proc"
)

// GetProcPath is getting the proc path
func GetProcPath(paths ...string) string {
	switch len(paths) {
	case 0:
		return procPath
	default:
		all := make([]string, len(paths)+1)
		all[0] = procPath
		copy(all[1:], paths)
		return strings.Join(all, "/")
	}
}

// GetSysUptime gets the system uptime
func GetSysUptime() (float64, float64, error) {
	contents, err := os.ReadFile(GetProcPath("uptime"))
	if err != nil {
		return 0, 0, err
	}
	timerCounts := strings.Fields(string(contents))
	uptime, err := strconv.ParseFloat(timerCounts[0], 64)
	if err != nil {
		return 0, 0, err
	}
	idleTime, err := strconv.ParseFloat(timerCounts[1], 64)
	if err != nil {
		return 0, 0, err
	}
	return uptime, idleTime, err
}