Add boot time module
This commit is contained in:
parent
9f252300aa
commit
adeb1d8614
2 changed files with 56 additions and 0 deletions
41
core/boottime.go
Normal file
41
core/boottime.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
/// Get system boot time
|
||||
/// should fail on not-linux
|
||||
|
||||
package core
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Time = time.Time
|
||||
|
||||
func GetBootTime() (Time, error) {
|
||||
file, err := os.Open("/proc/stat")
|
||||
if err != nil {
|
||||
return Time{}, err
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
s_btime := "btime "
|
||||
if strings.HasPrefix(line, s_btime) {
|
||||
btime, err := strconv.ParseInt(line[len(s_btime):], 10, 64)
|
||||
if err != nil {
|
||||
return Time{}, err
|
||||
}
|
||||
return time.Unix(btime, 0), nil
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return Time{}, err
|
||||
}
|
||||
|
||||
return Time{}, fmt.Errorf("btime (boot time) not found in /proc/stat")
|
||||
}
|
15
proto/uptime/main.go
Normal file
15
proto/uptime/main.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"git.exozy.me/exozyme/status/core"
|
||||
)
|
||||
|
||||
func main() {
|
||||
btime, err := core.GetBootTime()
|
||||
if err!=nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(btime)
|
||||
}
|
Loading…
Reference in a new issue