-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathi_system.go
115 lines (91 loc) · 2.42 KB
/
i_system.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"bufio"
"bytes"
"fmt"
"strings"
"os/exec"
"strconv"
"time"
"runtime"
"log"
)
type SystemStats struct{}
func (_ *SystemStats) Description() string {
return "Read metrics about system load & uptime"
}
func (_ *SystemStats) SampleConfig() string { return "" }
func (_ *SystemStats) Gather(acc Accumulator) error {
output, err := exec.Command("uptime").CombinedOutput()
if err != nil {
return fmt.Errorf("error getting System info: %s", err.Error())
}
stats := string(output)
log.Printf("D! Uptime Response: %s\n", stats)
rows := strings.Split(stats, "\n")
uptimeOutput := strings.Split(rows[0], ",")
load1, err := strconv.ParseFloat(strings.Trim(uptimeOutput[len(uptimeOutput)-3], "load average: "), 64)
load5, err := strconv.ParseFloat(strings.Trim(uptimeOutput[len(uptimeOutput)-2], " "), 64)
load15, err := strconv.ParseFloat(strings.Trim(uptimeOutput[len(uptimeOutput)-1], " "), 64)
users, err := strconv.ParseUint(strings.Trim(uptimeOutput[len(uptimeOutput)-4], " users"), 10, 64)
uptime, err := Uptime()
acc.AddGauge("system", map[string]interface{}{
"load1": load1,
"load5": load5,
"load15": load15,
"n_users": users,
"n_cpus": runtime.NumCPU(),
}, nil)
acc.AddCounter("system", map[string]interface{}{
"uptime": uptime,
}, nil)
acc.AddFields("system", map[string]interface{}{
"uptime_format": format_uptime(uptime),
}, nil)
return nil
}
func BootTime() (uint64, error) {
kstat, err := exec.LookPath("/usr/bin/kstat")
if err != nil {
return 0, err
}
out, err := exec.Command(kstat, "-p", "unix:0:system_misc:boot_time").CombinedOutput()
if err != nil {
return 0, err
}
output := string(out)
kstats := strings.Fields(output)
if len(kstats) != 2 {
return 0, fmt.Errorf("expected 2 kstat, found %d", len(kstats))
}
return strconv.ParseUint(kstats[1], 10, 64)
}
func Uptime() (uint64, error) {
bootTime, err := BootTime()
if err != nil {
return 0, err
}
return uptimeSince(bootTime), nil
}
func uptimeSince(since uint64) uint64 {
return uint64(time.Now().Unix()) - since
}
func format_uptime(uptime uint64) string {
buf := new(bytes.Buffer)
w := bufio.NewWriter(buf)
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()
}