/
monitor.go
240 lines (202 loc) · 4.59 KB
/
monitor.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/nsf/termbox-go"
tchart "github.com/s-westphal/go-tchart"
ui "github.com/s-westphal/termui/v3"
)
const (
default_config string = "monitor_config.json"
histogramBinCount int = 50
bufferSizeScatter int = 100
bufferSizeLine int = 500
)
var (
config = flag.String(
"config",
default_config,
"path to config file",
)
)
type chartEntry struct {
Title string `json:"title"`
Frequency int `json:"frequency"`
Command string `json:"command"`
Delimiter string `json:"delimiter"`
PlotSpec string `json:"plotSpec"`
}
type rowEntry struct {
Entries []chartEntry `json:"charts"`
Height int `json:"height"`
}
func parseJSON(filename string) map[string][]rowEntry {
jsonFile, err := os.Open(filename)
if err != nil {
panic(err)
}
defer jsonFile.Close()
bytes, err := ioutil.ReadAll(jsonFile)
if err != nil {
panic(err)
}
var content = make(map[string][]rowEntry)
if err := json.Unmarshal(bytes, &content); err != nil {
panic(err)
}
return content
}
type CommandParser struct {
command string
delimiter string
}
func NewCommandParser(command string, delimiter string) *CommandParser {
return &CommandParser{
command: command,
delimiter: delimiter,
}
}
func (cp *CommandParser) runCommand() []float64 {
commandResult := cp.execute()
strSplit := strings.Split(commandResult, cp.delimiter)
result := make([]float64, 0, len(strSplit))
for _, res := range strSplit {
if len(res) == 0 {
// trailing delimiter or missing entry
continue
}
if x, err := strconv.ParseFloat(res, 64); err == nil {
result = append(result, x)
} else {
result = append(result, 0)
}
}
return result
}
func (cp *CommandParser) execute() string {
out, err := exec.Command("bash", "-c", cp.command).Output()
if err != nil {
log.Fatal(err)
}
return string(out)
}
type IntervalExecuter struct {
interval time.Duration
commandParser *CommandParser
storages []*tchart.Storage
}
func NewIntervalExecuter(timeDelta int, commandParser *CommandParser, storages []*tchart.Storage) *IntervalExecuter {
return &IntervalExecuter{
interval: time.Duration(timeDelta) * time.Second,
commandParser: commandParser,
storages: storages,
}
}
func (ie *IntervalExecuter) run() {
go func() {
tick := time.Tick(ie.interval)
for {
select {
case <-tick:
ie.addEvaluation()
}
}
}()
}
func (ie *IntervalExecuter) addEvaluation() {
now := time.Now()
label := fmt.Sprintf("%02d:%02d:%02d.%03d",
now.Hour(), now.Minute(), now.Second(), now.Nanosecond()/1e6)
var values []float64 = ie.commandParser.runCommand()
for i, value := range values {
if i >= len(ie.storages) {
err := fmt.Errorf("command returns more values than specified, skip")
fmt.Println(err.Error())
continue
}
ie.storages[i].Add(value, label)
}
}
func main() {
flag.Parse()
err := ui.Init()
if err != nil {
panic(err)
}
defer ui.Close()
app := tchart.NewApp()
conf := parseJSON(*config)
var dataStorages []*tchart.Storage
var numChartColumns int
var chartType string
for _, row := range conf["monitor"] {
widgets := make([]tchart.Widget, 0, len(row.Entries))
for _, chart := range row.Entries {
chartType = string(chart.PlotSpec[0])
numChartColumns = len(chart.PlotSpec)
dataStorages = make([]*tchart.Storage, numChartColumns)
var bufferSize int
switch chartType {
case "L":
bufferSize = bufferSizeLine
case "S":
bufferSize = bufferSizeScatter
case "P", "G", "B":
bufferSize = 1
}
for i := 0; i < numChartColumns; i++ {
dataStorages[i] = tchart.NewStorage(chart.Title, bufferSize, histogramBinCount)
}
widget, err := tchart.CreateWidget(chartType, chart.Title, dataStorages)
if err != nil {
panic(err)
}
widgets = append(widgets, widget)
intervalExecuter := NewIntervalExecuter(chart.Frequency, NewCommandParser(chart.Command, chart.Delimiter), dataStorages)
intervalExecuter.run()
}
app.AddWidgetRow(widgets, row.Height)
}
app.Render()
evt := make(chan termbox.Event)
pause := make(chan bool)
go func() {
for {
evt <- termbox.PollEvent()
}
}()
go func() {
tick := time.Tick(time.Duration(100) * time.Millisecond)
var p bool = false
for {
select {
case p = <-pause:
case <-tick:
if !p {
app.Update()
}
}
}
}()
for {
select {
case e := <-evt:
if e.Type == termbox.EventKey && e.Ch == 'q' {
return
}
if e.Type == termbox.EventResize {
pause <- true
app.Render()
pause <- false
}
}
}
}