forked from likexian/host-stat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uptime.go
44 lines (33 loc) · 917 Bytes
/
uptime.go
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
/*
* Go module for collecting host stat
* https://www.likexian.com/
*
* Copyright 2014-2015, Li Kexian
* Released under the Apache License, Version 2.0
*
*/
package host_stat
import (
"strings"
"strconv"
)
type UptimeStat struct {
Uptime float64 `json:"uptime"`
IdleTime float64 `json:"idle_time"`
IdleRate float64 `json:"idle_rate"`
}
func GetUptimeStat() (stat UptimeStat, err error) {
text, err := ReadFirstLine("/proc/uptime")
if err != nil {
return
}
lines := strings.Split(text, "\n")
fields := strings.Fields(lines[0])
stat = UptimeStat{}
stat.Uptime, _ = strconv.ParseFloat(fields[0], strconv.IntSize)
stat.IdleTime, _ = strconv.ParseFloat(fields[1], strconv.IntSize)
stat.IdleRate = Round(stat.IdleTime * 100 / stat.Uptime, 2)
stat.Uptime = Round(stat.Uptime, 2)
stat.IdleTime = Round(stat.IdleTime, 2)
return
}