-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzabbixmon_tui.go
140 lines (112 loc) · 3.45 KB
/
zabbixmon_tui.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
package main
import (
"log/slog"
"time"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/fabiang/go-zabbix"
"github.com/samber/lo"
)
type model struct {
zapi *zabbix.Session
prevItems []zabbixmonItem
items []zabbixmonItem
refresh int
table table.Model
}
type tickMsg time.Time
var baseStyle = lipgloss.NewStyle()
func (m model) Init() tea.Cmd {
return tea.Batch(tick(), tea.EnterAltScreen)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "s":
if err := openUrl("ssh://" + m.items[m.table.Cursor()].Host); err != nil {
slog.Warn(err.Error(), slog.String("scope", "opening url"))
}
case "o", "enter":
if err := openUrl(m.items[m.table.Cursor()].Url); err != nil {
slog.Warn(err.Error(), slog.String("scope", "opening url"))
}
case "r":
m.prevItems = append([]zabbixmonItem(nil), m.items...)
m.items = getItems(m.zapi, config.ItemTypes, config.MinSeverity, config.Grep)
cursor := m.table.Cursor()
m.table = updateTable(m.items)
m.table.SetCursor(cursor)
m.refresh = config.Refresh
notify(m.items, m.prevItems)
case "ctrl+c", "q":
return m, tea.Quit
}
case tickMsg:
m.refresh -= 1
if m.refresh <= 0 {
m.prevItems = append([]zabbixmonItem(nil), m.items...)
m.items = getItems(m.zapi, config.ItemTypes, config.MinSeverity, config.Grep)
cursor := m.table.Cursor()
m.table = updateTable(m.items)
m.table.SetCursor(cursor)
m.refresh = config.Refresh
notify(m.items, m.prevItems)
}
return m, tick()
}
m.table, cmd = m.table.Update(msg)
return m, cmd
}
func (m model) View() string {
return baseStyle.Render(m.table.View() + "\n")
}
func tick() tea.Cmd {
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}
// update items table
func updateTable(items []zabbixmonItem) table.Model {
var maxHostWidth, maxStatusWidth, maxDescWidth, maxTimeWidth int
rows := []table.Row{}
for _, item := range items {
maxHostWidth = lo.Ternary(len([]rune(item.Host)) > maxHostWidth, len([]rune(item.Host)), maxHostWidth)
maxStatusWidth = lo.Ternary(len([]rune(item.Status)) > maxStatusWidth, len([]rune(item.Status)), maxStatusWidth)
maxDescWidth = lo.Ternary(len([]rune(item.Description)) > maxDescWidth, len([]rune(item.Description)), maxDescWidth)
maxTimeWidth = lo.Ternary(len([]rune(item.Time)) > maxTimeWidth, len([]rune(item.Time)), maxTimeWidth)
rows = append(rows, []string{item.Host, item.Status, item.Description, item.Time, lo.Ternary(item.Ack, "✓", "✗")})
}
columns := []table.Column{
{Title: "Host", Width: maxHostWidth},
{Title: "Status", Width: maxStatusWidth},
{Title: "Description", Width: maxDescWidth},
{Title: "Time", Width: maxTimeWidth},
{Title: "Ack", Width: 3},
}
return table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(len(rows)),
)
}
func initModel() model {
// zabbix auth
zapi := getSession(config.Server, config.Username, config.Password, config.ServerInsecure)
// fetch items
items := getItems(zapi, config.ItemTypes, config.MinSeverity, config.Grep)
// build table
t := updateTable(items)
s := table.DefaultStyles()
t.SetStyles(s)
return model{
zapi: zapi,
prevItems: []zabbixmonItem{},
items: items,
refresh: config.Refresh,
table: t,
}
}