forked from leifwalsh/olbermann
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dstat.go
83 lines (76 loc) · 2.07 KB
/
dstat.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
package olbermann
import (
"bytes"
"fmt"
"log"
"math"
"os"
"strings"
"time"
)
// DstatStyler is a Styler that produces output similar to dstat.
type DstatStyler struct {
Period time.Duration // How often to print
LinesBetweenHeaders int // After how many lines to print header info
Logger *log.Logger // A logger to print to
}
// A Styler that produces good default output similar to dstat, to
// standard out, with timestamps, once per second, with headers every 24
// lines.
var BasicDstatStyler = DstatStyler{Period: time.Second, LinesBetweenHeaders: 24, Logger: log.New(os.Stdout, "", log.LstdFlags)}
func (s *DstatStyler) period() time.Duration {
return s.Period
}
func (s *DstatStyler) linesBetweenHeaders() int {
return s.LinesBetweenHeaders
}
func (s *DstatStyler) printHeader(mst *metricSetType) {
var buf bytes.Buffer
for i := range mst.metrics {
mt := mst.metrics[i]
if i > 0 {
buf.WriteString("- -")
}
numSubHeaders := len(mt.reports)
colWidth := 12*numSubHeaders + numSubHeaders - 1
buf.WriteString(strings.Repeat("-", int(math.Floor(float64(colWidth-len(mt.name)-2)/2))))
fmt.Fprintf(&buf, " %s ", strings.ToLower(mt.name))
buf.WriteString(strings.Repeat("-", int(math.Ceil(float64(colWidth-len(mt.name)-2)/2))))
}
s.Logger.Print(buf.String())
buf.Reset()
for i := range mst.metrics {
mt := mst.metrics[i]
if i > 0 {
buf.WriteString(" | ")
}
for j := range mt.reports {
rt := mt.reports[j]
if j > 0 {
buf.WriteString(" ")
}
buf.WriteString(strings.Repeat(" ", 12-len(rt.name())))
buf.WriteString(rt.name())
}
}
s.Logger.Print(buf.String())
}
func (s *DstatStyler) printValues(curTime time.Time, mst *metricSetType, msv *metricSetValue) {
var buf bytes.Buffer
for i := range mst.metrics {
mt := mst.metrics[i]
mv := msv.metrics[i]
if i > 0 {
buf.WriteString(" | ")
}
for j := range mt.reports {
rt := mt.reports[j]
rv := mv.reports[j]
if j > 0 {
buf.WriteString(" ")
}
fmt.Fprintf(&buf, "%12s", rt.string(rv.value))
}
}
s.Logger.Print(buf.String())
}