-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostinfo.go
119 lines (92 loc) · 2.38 KB
/
hostinfo.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
package main
/**
* Host information.
*/
import (
"fmt"
"strings"
"time"
linuxproc "github.com/c9s/goprocinfo/linux"
ui "github.com/gizak/termui"
)
////////////////////////////////////////////
// Utility: Time
////////////////////////////////////////////
func getTime() (time.Time, *linuxproc.Uptime) {
now := time.Now().UTC()
uptime, err := linuxproc.ReadUptime("/proc/uptime")
if err != nil {
uptime = nil
}
return now, uptime
}
////////////////////////////////////////////
// Widget: Host Information
////////////////////////////////////////////
type HostInfoWidget struct {
widget *ui.List
}
func NewHostInfoWidget() *HostInfoWidget {
// Create base element
e := ui.NewList()
e.Height = 5
e.Border = true
e.BorderFg = ui.ColorBlue | ui.AttrBold
// Create widget
w := &HostInfoWidget{
widget: e,
}
w.update()
w.resize()
return w
}
func (w *HostInfoWidget) getGridWidget() ui.GridBufferer {
return w.widget
}
func (w *HostInfoWidget) update() {
now, uptime := getTime()
krbText, krbAttr := getKerberosStatusString()
// Start building lines
w.widget.Items = []string{}
w.widget.PaddingLeft = 2
// Set time
w.widget.Items = append(w.widget.Items, fmt.Sprintf("[Time](fg-cyan)....... [%v](fg-magenta)", now.Local().Format("2006/01/02 15:04:05 MST")))
// Uptime
w.widget.Items = append(w.widget.Items, fmt.Sprintf("[Uptime](fg-cyan)..... [%v](fg-green)", uptime.GetTotalDuration()))
// Kerberos
w.widget.Items = append(w.widget.Items, fmt.Sprintf("[Kerberos](fg-cyan)... [%v](%v)", krbText, krbAttr))
}
func (w *HostInfoWidget) resize() {
// Do nothing
}
func getKerberosStatusString() (string, string) {
// Do we have a ticket?
_, exitCode, _ := execAndGetOutput("klist", nil, "-s")
hasTicket := exitCode == 0
// Get the time left
timeLeftOutput, _, err := execAndGetOutput("kleft", nil, "")
var hasTimeLeft = false
var timeLeft string
if err == nil {
timeLeftParts := strings.Split(timeLeftOutput, " ")
if len(timeLeftParts) > 1 {
hasTimeLeft = true
timeLeft = strings.TrimSpace(timeLeftParts[1])
}
}
// Piece it all together
krbText := "Kerberos Ticket"
krbAttrStr := ""
if hasTicket {
if hasTimeLeft {
krbText = fmt.Sprintf("OK (%v)", timeLeft)
} else {
krbText = fmt.Sprintf("OK")
}
krbAttrStr = "fg-green,fg-bold"
} else {
krbText = fmt.Sprintf("NO TICKET")
krbAttrStr = "fg-red,fg-bold"
}
return krbText, krbAttrStr
}