-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuptime.go
90 lines (70 loc) · 1.35 KB
/
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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"strconv"
"strings"
"syscall"
"time"
)
type (
Load struct {
L1, L5, L15 float64
}
Uptime struct {
Time float64
}
)
func (self *Load) Get() error {
line, err := ioutil.ReadFile("/proc/loadavg")
if err != nil {
return nil
}
f := strings.Fields(string(line))
self.L1, _ = strconv.ParseFloat(f[0], 64)
self.L5, _ = strconv.ParseFloat(f[1], 64)
self.L15, _ = strconv.ParseFloat(f[2], 64)
return nil
}
func (self *Uptime) Get() error {
sysinfo := syscall.Sysinfo_t{}
if err := syscall.Sysinfo(&sysinfo); err != nil {
return err
}
self.Time = float64(sysinfo.Uptime)
return nil
}
func (self *Uptime) Format() string {
buf := new(bytes.Buffer)
w := bufio.NewWriter(buf)
uptime := uint64(self.Time)
days := uptime / (60 * 60 * 24)
if days != 0 {
s := ""
if days > 1 {
s = "s"
}
fmt.Fprintf(w, "%d day%s, ", days, s)
}
minutes := uptime / 60
hours := minutes / 60
hours %= 24
minutes %= 60
fmt.Fprintf(w, "%2d:%02d", hours, minutes)
w.Flush()
return buf.String()
}
// TODO: online kullanıcı sayısını almak lazım.
func Users() int { return 0 }
func main() {
up := Uptime{}
up.Get()
load := Load{}
load.Get()
fmt.Printf(" %s up %s load average: %.2f, %.2f, %.2f\n",
time.Now().Format("15:04:05"),
up.Format(),
load.L1, load.L5, load.L15)
}