/
httptuimon.go
153 lines (138 loc) · 3.12 KB
/
httptuimon.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"time"
"github.com/gizak/termui"
"github.com/jvgutierrez/httptuimon/monitor"
)
type configFile struct {
Entries []configEntry `json:"monitors"`
}
type configEntry struct {
URL string
}
type UIMonitor struct {
index uint32
monitor monitor.Monitor
}
const (
COLOR_OK = termui.ColorGreen
COLOR_KO = termui.ColorRed
)
func readConfig(fn string) []UIMonitor {
file, err := ioutil.ReadFile(fn)
if err != nil {
log.Fatalf("Unable to read config file: %v\n", err)
}
var cf configFile
err = json.Unmarshal(file, &cf)
if err != nil {
log.Fatalf("Unable to read config: %v\n", err)
}
ret := make([]UIMonitor, len(cf.Entries))
for i, entry := range cf.Entries {
ret[i].index = uint32(i)
ret[i].monitor = monitor.NewHTTPMonitor(entry.URL)
}
return ret
}
func main() {
configFile := flag.String("config", "config.json", "Configuration file")
flag.Parse()
monitors := readConfig(*configFile)
updates := make(chan monitor.CheckUpdate)
logbuf := new(bytes.Buffer)
log.SetOutput(logbuf)
err := termui.Init()
if err != nil {
log.Fatalf("Unable to init termui: %v\n", err)
}
defer termui.Close()
list := termui.NewList()
var urls []string
for _, m := range monitors {
e := fmt.Sprintf("[%v] %v", m.index, m.monitor.Source())
urls = append(urls, e)
if list.Width < int(float64(len(e))*float64(1.5)) {
list.Width = int(float64(len(e)) * float64(1.5))
}
}
list.Items = urls
list.ItemFgColor = termui.ColorYellow
list.BorderLabel = "URLs"
list.Height = 8
list.Y = 0
list.X = 0
sp := termui.NewSparklines()
sp.BorderLabel = "Response times"
sp.Y = list.Height
sp.X = 0
sp.Height = list.Height
for i, _ := range urls {
spark := termui.Sparkline{}
spark.Height = 1
spark.Title = fmt.Sprintf("URL %v", i)
spark.LineColor = termui.ColorCyan
spark.TitleColor = termui.ColorYellow
sp.Add(spark)
}
logPar := termui.NewPar(logbuf.String())
logPar.Height = 20
termui.Body.AddRows(
termui.NewRow(
termui.NewCol(6, 0, list),
termui.NewCol(6, 0, sp)),
termui.NewRow(
termui.NewCol(12, 0, logPar)))
termui.Body.Align()
for _, m := range monitors {
go m.monitor.Check(updates, m.index)
}
loop:
for {
select {
case u := <-updates:
if u.Healthy && u.Err == nil {
sp.Lines[u.Id].LineColor = COLOR_OK
} else {
sp.Lines[u.Id].LineColor = COLOR_KO
}
case <-time.After(2 * time.Second):
break loop
}
}
termui.Render(termui.Body)
termui.Handle("/sys/kbd/q", func(termui.Event) {
termui.StopLoop()
})
termui.Handle("/timer/1s", func(e termui.Event) {
t := e.Data.(termui.EvtTimer)
if t.Count%5 == 0 {
for _, m := range monitors {
go m.monitor.Check(updates, m.index)
}
loop:
for {
select {
case u := <-updates:
sp.Lines[u.Id].Data = append(sp.Lines[u.Id].Data, int(u.Duration))
if u.Healthy && u.Err == nil {
sp.Lines[u.Id].LineColor = COLOR_OK
} else {
sp.Lines[u.Id].LineColor = COLOR_KO
}
case <-time.After(2 * time.Second):
break loop
}
}
logPar.Text = logbuf.String()
termui.Render(termui.Body)
}
})
termui.Loop()
}