-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod-system.go
executable file
·158 lines (133 loc) · 3.44 KB
/
mod-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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"context"
"errors"
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
"sync"
"github.com/parMaster/rpid/config"
"github.com/parMaster/rpid/storage"
"github.com/parMaster/rpid/storage/model"
)
type ShortFloat float64 // for JSON, to leave only 2 digits after the point
func (f ShortFloat) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%.2f", f)), nil
}
func (f ShortFloat) String() string {
return fmt.Sprintf("%.2f", f)
}
type Response struct {
TimeInState map[string]int
LoadAvg map[string][]ShortFloat
}
type SystemReporter struct {
data Response
dbg bool
mx sync.Mutex
store storage.Storer
}
func LoadSystemReporter(cfg config.System, store storage.Storer, dbg bool) (*SystemReporter, error) {
if !cfg.Enabled {
return nil, fmt.Errorf("SystemReporter is not enabled")
}
if store != nil {
log.Printf("[DEBUG] SystemReporter: using storage (%T)", store)
}
return &SystemReporter{
dbg: dbg,
store: store,
data: Response{
TimeInState: map[string]int{},
LoadAvg: map[string][]ShortFloat{},
},
}, nil
}
func (r *SystemReporter) Name() string {
return "system"
}
func (r *SystemReporter) Collect(ctx context.Context) (err error) {
r.mx.Lock()
defer r.mx.Unlock()
r.data.TimeInState, err = r.getCPUTimeInState(r.dbg)
if err != nil {
return errors.Join(err, errors.New("failed to get cpu time in state"))
}
la, err := r.getLoadAvg(r.dbg)
if err != nil {
return errors.Join(err, fmt.Errorf("failed to get load avg: %v", err))
} else {
r.data.LoadAvg["1m"] = append(r.data.LoadAvg["1m"], la["1m"])
r.data.LoadAvg["5m"] = append(r.data.LoadAvg["5m"], la["5m"])
r.data.LoadAvg["15m"] = append(r.data.LoadAvg["15m"], la["15m"])
if r.store != nil {
err := r.store.Write(ctx, model.Data{Module: r.Name(), Topic: "la5m", Value: la["5m"].String()})
if err != nil {
return errors.Join(err, fmt.Errorf("failed to write to storage: %v", err))
}
}
}
return err
}
func (r *SystemReporter) Report() (interface{}, error) {
r.mx.Lock()
defer r.mx.Unlock()
return r.data, nil
}
func (r *SystemReporter) getCPUTimeInState(dbg bool) (map[string]int, error) {
var (
out = map[string]int{}
data []byte
err error
)
if dbg {
data, err = os.ReadFile("data/cpu_time_in_state.txt")
} else { // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-thermal
data, err = os.ReadFile("/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state")
}
if err != nil {
return nil, err
}
for _, line := range strings.Split(string(data), "\n") {
if line == "" {
continue
}
parts := strings.Split(line, " ")
if len(parts) != 2 {
continue
}
parts[0] = parts[0][0 : len(parts[0])-3]
out[parts[0]], _ = strconv.Atoi(parts[1])
out[parts[0]] /= 100 // tens of milliseconds to seconds
}
return out, nil
}
// Load average for last 1, 5 and 15 minutes
func (r *SystemReporter) getLoadAvg(dbg bool) (map[string]ShortFloat, error) {
var (
out = map[string]ShortFloat{}
data []byte
err error
)
if dbg {
data, err = os.ReadFile("data/cpu_loadavg.txt")
} else {
data, err = os.ReadFile("/proc/loadavg")
}
if err != nil {
return nil, err
}
parts := strings.Split(string(data), " ")
if len(parts) != 5 {
return nil, fmt.Errorf("invalid loadavg data")
}
for i, v := range []string{"1m", "5m", "15m"} {
fv, _ := strconv.ParseFloat(parts[i], 32)
fv = math.Round(fv*100) / 100 // leave only 2 decimals
out[v] = ShortFloat(fv)
}
return out, nil
}