Utku Ozdemir f55f5df739
feat: move dashboard package & run it in tty2
Move dashboard package into a common location where both Talos and talosctl can use it.

Add support for overriding stdin, stdout, stderr and ctt in process runner.

Create a dashboard service which runs the dashboard on /dev/tty2.

Redirect kernel messages to tty1 and switch to tty2 after starting the dashboard on it.

Related to siderolabs/talos#6841, siderolabs/talos#4791.

Signed-off-by: Utku Ozdemir <utku.ozdemir@siderolabs.com>
2023-02-28 12:00:25 +01:00

74 lines
1.7 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package components
import (
"github.com/gizak/termui/v3/widgets"
"github.com/siderolabs/gen/slices"
"github.com/siderolabs/talos/internal/pkg/dashboard/data"
)
// BaseGraph represents the widget with some usage graph.
type BaseGraph struct {
widgets.Plot
}
// NewBaseGraph initializes BaseGraph.
func NewBaseGraph(title string, labels []string) *BaseGraph {
widget := &BaseGraph{
Plot: *widgets.NewPlot(),
}
widget.Border = false
widget.Title = title
widget.DataLabels = labels
widget.ShowAxes = false
// TODO: looks to be a bug as it requires at least 2 points
widget.Data = slices.Map(labels, func(label string) []float64 { return []float64{0, 0} })
return widget
}
// Update implements the DataWidget interface.
func (widget *BaseGraph) Update(node string, data *data.Data) {
nodeData := data.Nodes[node]
if nodeData == nil {
for i := range widget.Data {
widget.Data[i] = []float64{0, 0}
}
return
}
width := widget.Inner.Dx()
for i, name := range widget.DataLabels {
series := nodeData.Series[name]
if len(series) < width {
width = len(series)
}
widget.Data[i] = series[len(series)-width:]
}
}
// NewCPUGraph creates CPU usage graph.
func NewCPUGraph() *BaseGraph {
return NewBaseGraph("CPU USER/SYSTEM", []string{"user", "system"})
}
// NewMemGraph creates mem usage graph.
func NewMemGraph() *BaseGraph {
return NewBaseGraph("MEM USED", []string{"mem"})
}
// NewLoadAvgGraph creates loadavg graph.
func NewLoadAvgGraph() *BaseGraph {
return NewBaseGraph("LOAD AVG 60sec", []string{"loadavg"})
}