This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
/
cli_formatter.go
108 lines (91 loc) · 2.06 KB
/
cli_formatter.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
// Copyright (c) 2012 VMware, Inc.
package gonit
import (
"fmt"
"io"
"text/tabwriter"
"time"
)
// format data returned from the API
type CliFormatter interface {
Print(w io.Writer)
}
func (p *ProcessStatus) Print(w io.Writer) {
writeTable(w, func(tw io.Writer) {
p.write(tw)
})
}
func (g *ProcessGroupStatus) Print(w io.Writer) {
writeTable(w, func(tw io.Writer) {
for _, p := range g.Group {
p.write(tw)
}
})
}
func (s *Summary) Print(w io.Writer) {
writeTable(w, func(tw io.Writer) {
for _, p := range s.Processes {
p.write(tw)
}
})
}
func writeTable(w io.Writer, f func(io.Writer)) {
tw := new(tabwriter.Writer)
tw.Init(w, 0, 8, 8, ' ', 0)
f(tw)
tw.Flush()
}
func (p *ProcessSummary) monitorString() string {
monitor := []struct {
mode int
label string
}{
{MONITOR_WAITING, "waiting"},
{MONITOR_INIT, "initializing"},
{MONITOR_YES, "monitored"},
{MONITOR_NOT, "not monitored"},
}
for _, state := range monitor {
if (p.ControlState.Monitor & state.mode) == state.mode {
return state.label
}
}
panic("not reached")
}
func (p *ProcessSummary) runningString() string {
if p.Running {
return "running"
}
return "not running"
}
func (p *ProcessStatus) uptime() string {
if p.Time.StartTime == 0 {
return "-"
}
start := time.Unix(int64(p.Time.StartTime)/1000, 0)
return time.Since(start).String()
}
func (p *ProcessSummary) write(tw io.Writer) {
fmt.Fprintf(tw, "Process '%s'\t%s\n", p.Name, p.runningString())
}
func (p *ProcessStatus) write(tw io.Writer) {
fmt.Fprintf(tw, "Process '%s'\t\n", p.Summary.Name)
status := []struct {
label string
data interface{}
}{
{"status", p.Summary.runningString()},
{"monitoring status", p.Summary.monitorString()},
{"starts", p.Summary.ControlState.Starts},
{"pid", p.Pid},
{"parent pid", p.State.Ppid},
{"uptime", p.uptime()},
{"memory kilobytes", p.Mem.Resident / 1024},
{"cpu", p.Time.FormatTotal()}, // TODO %cpu
// TODO "data collected"
}
for _, entry := range status {
fmt.Fprintf(tw, " %s\t%v\n", entry.label, entry.data)
}
fmt.Fprintf(tw, "\t\n")
}