This repository has been archived by the owner on Mar 28, 2020. It is now read-only.
/
utils.go
197 lines (163 loc) · 4.15 KB
/
utils.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
package status
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"strconv"
"strings"
"time"
. "github.com/logrusorgru/aurora"
proc "github.com/shirou/gopsutil/process"
yaml "gopkg.in/yaml.v2"
)
const (
bLoad string = "█"
wLoad string = "░"
)
type pumaStateFile struct {
Pid int `yaml:"pid"`
ControlURL string `yaml:"control_url"`
ControlAuthToken string `yaml:"control_auth_token"`
}
// readPumaStats get and unmarshal JSON using puma unix socket
func readPumaStats(paths []string) ([]pumaStatus, error) {
var pspaths []pumaStatus
for _, path := range paths {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var psf pumaStateFile
if err := yaml.Unmarshal(data, &psf); err != nil {
return nil, err
}
httpc := http.Client{
Transport: &http.Transport{
Dial: func(_, _ string) (net.Conn, error) {
return net.Dial("unix", strings.TrimPrefix(psf.ControlURL, "unix://"))
},
},
}
r, err := httpc.Get(fmt.Sprintf("http://unix/stats?token=%s", psf.ControlAuthToken))
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
ps := pumaStatus{
MainPid: psf.Pid,
}
if err := json.Unmarshal(body, &ps); err != nil {
return nil, err
}
pspaths = append(pspaths, ps)
}
//body, _ := exec.Command("cat", "/go/src/github.com/dimelo/puma-helper/output.txt").Output()
//fmt.Println(pspath)
return pspaths, nil
}
func getTotalUptimeFromPID(pid int32) (int64, error) {
p, err := proc.NewProcess(pid)
if err != nil {
return 0, err
}
t, err := p.CreateTime()
if err != nil {
return 0, err
}
return t, nil
}
func getCPUPercentFromPID(pid int32) (float64, error) {
p, err := proc.NewProcess(pid)
if err != nil {
return 0.0, err
}
cpu, err := p.CPUPercent()
if err != nil {
return 0.0, err
}
return cpu, nil
}
func getCPUTimesFromPID(pid int32) (int, error) {
p, err := proc.NewProcess(pid)
if err != nil {
return 0.0, err
}
t, err := p.Times()
if err != nil {
return 0.0, err
}
return strconv.Atoi(fmt.Sprintf("%.0f", t.Total()-(t.Idle+t.Iowait)))
}
func getMemoryFromPID(pid int32) (float64, error) {
p, err := proc.NewProcess(pid)
if err != nil {
return 0.0, err
}
mem, err := p.MemoryInfoEx()
if err != nil {
return 0.0, err
}
return float64(mem.RSS+mem.Shared) / float64(1024*1024), nil
}
func timeElapsed(nT string) string {
tx, err := time.Parse(time.RFC3339, nT)
if err != nil {
return "unrecognized time format"
}
elapsed := time.Since(tx).String()
if strings.Contains(elapsed, "ms") {
return "~0s"
}
return fmt.Sprintf("%ss", strings.Split(elapsed, ".")[0])
}
func timeElapsedFromSeconds(t int) string {
return timeElapsed(time.Now().Add(time.Duration(-int64(t)) * time.Second).Format(time.RFC3339))
}
func colorState(fvalue, warnstate, criticalstate float64, strvalue string) string {
if fvalue > criticalstate {
return Red(strvalue).String()
} else if fvalue > warnstate {
return Brown(strvalue).String()
}
return Green(strvalue).String()
}
func asciiThreadLoad(load int, capacity int) string {
formatted := fmt.Sprintf("%d[%s%s]%d", load, strings.Repeat(bLoad, load), strings.Repeat(wLoad, capacity-load), capacity)
total := (float64(load) / float64(capacity)) * 100
pwarn := CfgFile.Applications[currentApp].ThreadWarn
if pwarn == 0 {
pwarn = 50
}
pcritical := CfgFile.Applications[currentApp].ThreadCritical
if pcritical == 0 {
pcritical = 80
}
return colorState(total, float64(pwarn), float64(pcritical), formatted)
}
func colorCPU(cpu float64) string {
cwarn := CfgFile.Applications[currentApp].CPUWarn
if cwarn == 0 {
cwarn = 50
}
ccritical := CfgFile.Applications[currentApp].CPUCritical
if ccritical == 0 {
ccritical = 80
}
return colorState(cpu, float64(cwarn), float64(ccritical), fmt.Sprintf("%.1f", cpu))
}
func colorMemory(memory float64) string {
mwarn := CfgFile.Applications[currentApp].MemoryWarn
if mwarn == 0 {
mwarn = 500
}
mcritical := CfgFile.Applications[currentApp].MemoryCritical
if mcritical == 0 {
mcritical = 1000
}
return colorState(memory, float64(mwarn), float64(mcritical), fmt.Sprintf("%.1f", memory))
}