forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
126 lines (108 loc) · 3.05 KB
/
status.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
package monit
import (
"encoding/xml"
"strconv"
)
type status struct {
XMLName xml.Name `xml:"monit"`
ID string `xml:"id,attr"`
Incarnation string `xml:"incarnation,attr"`
Version string `xml:"version,attr"`
Services servicesTag
ServiceGroups serviceGroupsTag
}
type servicesTag struct {
XMLName xml.Name `xml:"services"`
Services []serviceTag `xml:"service"`
}
type serviceTag struct {
XMLName xml.Name `xml:"service"`
Name string `xml:"name,attr"`
Pending int `xml:"pendingaction"`
Status int `xml:"status"`
StatusMessage string `xml:"status_message"`
Monitor int `xml:"monitor"`
Uptime int `xml:"uptime"`
Children int `xml:"children"`
Memory memoryTag `xml:"memory"`
CPU cpuTag `xml:"cpu"`
}
type memoryTag struct {
XMLName xml.Name `xml:"memory"`
Percent float64 `xml:"percent"`
PercentTotal float64 `xml:"percenttotal"`
Kilobyte int `xml:"kilobyte"`
KilobyteTotal int `xml:"kilobytetotal"`
}
type cpuTag struct {
XMLName xml.Name `xml:"cpu"`
Percent float64 `xml:"percent"`
PercentTotal float64 `xml:"percenttotal"`
}
type serviceGroupsTag struct {
XMLName xml.Name `xml:"servicegroups"`
ServiceGroups []serviceGroupTag `xml:"servicegroup"`
}
type serviceGroupTag struct {
XMLName xml.Name `xml:"servicegroup"`
Name string `xml:"name,attr"`
Services []string `xml:"service"`
}
func (s serviceTag) StatusString() string {
switch {
case s.Monitor == 0:
return StatusUnknown
case s.Monitor == 2:
return StatusStarting
case s.Status == 0:
return StatusRunning
default:
return StatusFailing
}
}
func (t serviceGroupsTag) Get(name string) (group serviceGroupTag, found bool) {
for _, g := range t.ServiceGroups {
if g.Name == name {
group = g
found = true
return
}
}
return
}
func (t serviceGroupTag) Contains(name string) bool {
for _, serviceName := range t.Services {
if serviceName == name {
return true
}
}
return false
}
func (status status) ServicesInGroup(name string) (services []Service) {
services = []Service{}
serviceGroupTag, found := status.ServiceGroups.Get(name)
if !found {
return
}
for _, serviceTag := range status.Services.Services {
if serviceGroupTag.Contains(serviceTag.Name) {
service := Service{
Name: serviceTag.Name,
Pending: serviceTag.Pending > 0,
Status: serviceTag.StatusString(),
Errored: serviceTag.Status > 0 && serviceTag.StatusMessage != "",
StatusMessage: serviceTag.StatusMessage,
Monitored: serviceTag.Monitor > 0,
Uptime: serviceTag.Uptime,
MemoryPercentTotal: serviceTag.Memory.PercentTotal,
MemoryKilobytesTotal: serviceTag.Memory.KilobyteTotal,
CPUPercentTotal: serviceTag.CPU.PercentTotal,
}
services = append(services, service)
}
}
return
}
func (status status) GetIncarnation() (int, error) {
return strconv.Atoi(status.Incarnation)
}