-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.go
230 lines (184 loc) · 4.38 KB
/
data.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"fmt"
"github.com/apcera/termtables"
"github.com/patwie/cluster-top/proc"
"os"
"os/user"
"sort"
"strconv"
"time"
)
func minimum(a int, b int) int {
if a < b {
return a
} else {
return b
}
}
type Cluster struct {
Nodes []Node `json:"nodes"`
}
type Process struct {
UID int
Username string
Usage float32
Info proc.PIDInfo
}
type Memory struct {
Total int64
Free int64
Available int64
Used int64
Usage float32
}
func FetchMemory(m *Memory) {
m.Total, m.Free, m.Available = proc.GetRAMMemoryInfo()
m.Used = m.Total - m.Free
m.Usage = float32(100 * float64(m.Used) / float64(m.Total))
}
type CpuInfo struct {
TotalTime int64
IoWait int64
}
type Cpu struct {
Cores int
Current CpuInfo
Previous CpuInfo
}
func (c *Cpu) Update() {
c.Previous.TotalTime = c.Current.TotalTime
c.Previous.IoWait = c.Current.IoWait
c.Current.TotalTime, c.Current.IoWait = proc.CpuInfo()
}
func (c *Cpu) RelativeIoWait() float64 {
totalPeriod := float64(c.Current.TotalTime - c.Previous.TotalTime)
waitPeriod := float64(c.Current.IoWait - c.Previous.IoWait)
// TODO check 10000 (should be 100?)
wa := float64(waitPeriod / totalPeriod * 10000)
if wa <= 0 {
wa = 0
}
return wa
}
type Node struct {
Name string
Processes []Process
Memory Memory
Time time.Time `json:"time"` // current timestamp from message
Cpu Cpu
}
func InitNode(n *Node) {
name, err := os.Hostname()
if err != nil {
panic(err)
}
n.Name = name
n.Cpu.Cores = proc.NumberCPUCores()
}
type ByUsage []Process
func (a ByUsage) Len() int { return len(a) }
func (a ByUsage) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByUsage) Less(i, j int) bool {
return a[i].Usage > a[j].Usage
}
type ByName []Node
func (a ByName) Len() int { return len(a) }
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByName) Less(i, j int) bool {
return a[i].Name < a[j].Name
}
func (c *Cluster) Sort() {
sort.Sort(ByName(c.Nodes))
}
func (c *Cluster) Print(show_time bool) {
table := termtables.CreateTable()
tableHeader := []interface{}{"Node", "RAM-Util", "wa", "PID", "User", "Command", "CPU-Util"}
if show_time {
tableHeader = append(tableHeader, "Last Seen")
}
table.AddHeaders(tableHeader...)
for n_id, n := range c.Nodes {
node_lastseen := n.Time.Format("Mon Jan 2 15:04:05 2006")
if len(n.Processes) == 0 {
memory := fmt.Sprintf("%vGiB / %vGiB (%3v%%)",
int(n.Memory.Used/1024/1024),
int(n.Memory.Total/1024/1024),
int(n.Memory.Usage),
)
tableRow := []interface{}{
n.Name,
memory,
"",
"",
"",
"",
"",
}
if show_time {
tableRow = append(tableRow, node_lastseen)
}
table.AddRow(tableRow...)
table.SetAlign(termtables.AlignRight, 2)
} else {
for p_id, p := range n.Processes {
name := ""
memory := ""
wa := ""
if p_id == 0 {
name = n.Name
memory = fmt.Sprintf("%vGiB / %vGiB (%3v%%)",
int(n.Memory.Used/1024/1024),
int(n.Memory.Total/1024/1024),
int(n.Memory.Usage),
)
wa = fmt.Sprintf("%0.1f", n.Cpu.RelativeIoWait())
}
tableRow := []interface{}{
name,
memory,
wa,
p.Info.PID,
p.Username,
p.Info.Command,
strconv.Itoa(int(p.Usage)) + "%",
}
if show_time {
if p_id == 0 {
tableRow = append(tableRow, node_lastseen)
} else {
tableRow = append(tableRow, "")
}
}
table.AddRow(tableRow...)
table.SetAlign(termtables.AlignRight, 2)
}
}
if n_id < len(c.Nodes)-1 {
table.AddSeparator()
}
}
fmt.Printf("\033[2J")
fmt.Println(time.Now().Format("Mon Jan 2 15:04:05 2006") + " (http://github.com/patwie/cluster-top)")
fmt.Println(table.Render())
}
func GetProcesses(procs map[int]*proc.Process, factor float32, max int) []Process {
var m_display []Process
for _, v := range procs {
if v.Dirty == false && v.Active == true {
if v.CurrentTime()-v.TimePrev > 0 {
usage := 1. / factor * float32(v.CurrentTime()-v.TimePrev)
copy_proc := Process{v.UID, "", usage, v.PIDInfo}
// fmt.Println(copy_proc)
m_display = append(m_display, copy_proc)
}
}
}
sort.Sort(ByUsage(m_display))
m_display = m_display[:minimum(max, len(m_display))]
for i := 0; i < len(m_display); i++ {
user, _ := user.LookupId(strconv.Itoa(m_display[i].UID))
m_display[i].Username = user.Username
}
return m_display
}