-
Notifications
You must be signed in to change notification settings - Fork 6
/
gatherer.go
142 lines (120 loc) · 4.69 KB
/
gatherer.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
package gather
import "strconv"
//go:generate counterfeiter . DatabaseClient
type DatabaseClient interface {
ShowGlobalStatus() (map[string]string, error)
ShowGlobalVariables() (map[string]string, error)
ShowSlaveStatus() (map[string]string, error)
HeartbeatStatus() (map[string]string, error)
ServicePlansDiskAllocated() (map[string]string, error)
IsAvailable() bool
IsFollower() (bool, error)
}
//go:generate counterfeiter . Stater
type Stater interface {
Stats(path string) (bytesFree, bytesTotal, inodesFree, inodesTotal uint64, err error)
}
//go:generate counterfeiter . CpuStater
type CpuStater interface {
GetPercentage() (int, error)
}
type Gatherer struct {
client DatabaseClient
stater Stater
cpuStater CpuStater
previousQueries int
}
func NewGatherer(client DatabaseClient, stater Stater, cpuStater CpuStater) *Gatherer {
return &Gatherer{
client: client,
stater: stater,
cpuStater: cpuStater,
previousQueries: -1,
}
}
func (g Gatherer) BrokerStats() (map[string]string, error) {
return g.client.ServicePlansDiskAllocated()
}
func (g Gatherer) CPUStats() (map[string]string, error) {
percentage, err := g.cpuStater.GetPercentage()
if err != nil {
return nil, err
}
return map[string]string{"cpu_utilization_percent": strconv.Itoa(percentage)}, err
}
func (g Gatherer) DiskStats() (map[string]string, error) {
bytesFreePersistent, bytesTotalPersistent, inodesFreePersistent, inodesTotalPersistent, err := g.stater.Stats("/var/vcap/store")
if err != nil {
return nil, err
}
bytesFreeEphemeral, bytesTotalEphemeral, inodesFreeEphemeral, inodesTotalEphemeral, err := g.stater.Stats("/var/vcap/data")
if err != nil {
return nil, err
}
persistentDiskUsedBytes := bytesTotalPersistent - bytesFreePersistent
ephemeralDiskUsedBytes := bytesTotalEphemeral - bytesFreeEphemeral
persistentInodesUsed := inodesTotalPersistent - inodesFreePersistent
ephemeralInodesUsed := inodesTotalEphemeral - inodesFreeEphemeral
return map[string]string{
"persistent_disk_used": strconv.FormatUint(persistentDiskUsedBytes/1024, 10),
"persistent_disk_free": strconv.FormatUint(bytesFreePersistent/1024, 10),
"persistent_disk_used_percent": strconv.FormatUint(g.calculateWholePercent(persistentDiskUsedBytes, bytesTotalPersistent), 10),
"persistent_disk_inodes_used": strconv.FormatUint(persistentInodesUsed, 10),
"persistent_disk_inodes_free": strconv.FormatUint(inodesFreePersistent, 10),
"persistent_disk_inodes_used_percent": strconv.FormatUint(g.calculateWholePercent(persistentInodesUsed, inodesTotalPersistent), 10),
"ephemeral_disk_used": strconv.FormatUint(ephemeralDiskUsedBytes/1024, 10),
"ephemeral_disk_free": strconv.FormatUint(bytesFreeEphemeral/1024, 10),
"ephemeral_disk_used_percent": strconv.FormatUint(g.calculateWholePercent(ephemeralDiskUsedBytes, bytesTotalEphemeral), 10),
"ephemeral_disk_inodes_used": strconv.FormatUint(ephemeralInodesUsed, 10),
"ephemeral_disk_inodes_free": strconv.FormatUint(inodesFreeEphemeral, 10),
"ephemeral_disk_inodes_used_percent": strconv.FormatUint(g.calculateWholePercent(ephemeralInodesUsed, inodesTotalEphemeral), 10),
}, nil
}
func (Gatherer) calculateWholePercent(numerator, denominator uint64) uint64 {
numeratorFloat := float64(numerator)
denominatorFloat := float64(denominator)
return uint64((numeratorFloat / denominatorFloat) * 100)
}
func (g Gatherer) IsDatabaseAvailable() bool {
return g.client.IsAvailable()
}
func (g Gatherer) IsDatabaseFollower() (bool, error) {
return g.client.IsFollower()
}
func (g *Gatherer) DatabaseMetadata() (globalStatus map[string]string, globalVariables map[string]string, err error) {
globalStatus, err = g.client.ShowGlobalStatus()
if err != nil {
return nil, nil, err
}
globalVariables, err = g.client.ShowGlobalVariables()
if err != nil {
return nil, nil, err
}
currentQueries := -1
if currentQueriesString, ok := globalStatus["queries"]; ok {
var err error
if currentQueries, err = strconv.Atoi(currentQueriesString); err != nil {
globalStatus["queries_delta"] = "0"
}
} else {
globalStatus["queries_delta"] = "0"
}
if g.previousQueries != -1 {
if currentQueries-g.previousQueries >= 0 {
globalStatus["queries_delta"] = strconv.Itoa(currentQueries - g.previousQueries)
}
}
g.previousQueries = currentQueries
return
}
func (g Gatherer) FollowerMetadata() (slaveStatus map[string]string, heartbeatStatus map[string]string, err error) {
slaveStatus, err = g.client.ShowSlaveStatus()
if err != nil {
return nil, nil, err
}
heartbeatStatus, err = g.client.HeartbeatStatus()
if err != nil {
return nil, nil, err
}
return
}