forked from krujos/usagereport-plugin
/
usagereport.go
182 lines (163 loc) · 3.87 KB
/
usagereport.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
package main
import (
"fmt"
"os"
"github.com/cloudfoundry/cli/plugin"
"github.com/krujos/usagereport-plugin/apihelper"
)
//UsageReportCmd the plugin
type UsageReportCmd struct {
apiHelper apihelper.CFAPIHelper
cli plugin.CliConnection
}
type org struct {
name string
memoryQuota int
memoryUsage int
spaces []space
}
type space struct {
apps []app
name string
}
type app struct {
ram int
instances int
running bool
}
//GetMetadata returns metatada
func (cmd *UsageReportCmd) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "usage-report",
Version: plugin.VersionType{
Major: 1,
Minor: 1,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "usage-report",
HelpText: "Report AI and memory usage for orgs and spaces",
UsageDetails: plugin.Usage{
Usage: "cf usage-report",
},
},
},
}
}
//UsageReportCommand doer
func (cmd *UsageReportCmd) UsageReportCommand(args []string) {
fmt.Println("Gathering usage information")
if nil == cmd.cli {
fmt.Println("ERROR: CLI Connection is nil!")
os.Exit(1)
}
orgs, err := cmd.getOrgs()
if nil != err {
fmt.Println(err)
os.Exit(1)
}
totalApps := 0
totalInstances := 0
for _, org := range orgs {
fmt.Printf("Org %s is consuming %d MB of %d MB.\n", org.name, org.memoryUsage, org.memoryQuota)
for _, space := range org.spaces {
consumed := 0
instances := 0
runningApps := 0
runningInstances := 0
for _, app := range space.apps {
if app.running {
consumed += int(app.instances * app.ram)
runningApps++
runningInstances += app.instances
}
instances += int(app.instances)
}
fmt.Printf("\tSpace %s is consuming %d MB memory (%d%%) of org quota.\n",
space.name, consumed, (100 * consumed / org.memoryQuota))
fmt.Printf("\t\t%d apps: %d running %d stopped\n", len(space.apps),
runningApps, len(space.apps)-runningApps)
fmt.Printf("\t\t%d instances: %d running, %d stopped\n", instances,
runningInstances, instances-runningInstances)
totalInstances += instances
totalApps += len(space.apps)
}
}
fmt.Printf("You are running %d apps in %d orgs, with a total of %d instances.\n",
totalApps, len(orgs), totalInstances)
}
func (cmd *UsageReportCmd) getOrgs() ([]org, error) {
rawOrgs, err := cmd.apiHelper.GetOrgs(cmd.cli)
if nil != err {
return nil, err
}
var orgs = []org{}
for _, o := range rawOrgs {
usage, err := cmd.apiHelper.GetOrgMemoryUsage(cmd.cli, o)
if nil != err {
return nil, err
}
quota, err := cmd.apiHelper.GetQuotaMemoryLimit(cmd.cli, o.QuotaURL)
if nil != err {
return nil, err
}
spaces, err := cmd.getSpaces(o.SpacesURL)
if nil != err {
return nil, err
}
orgs = append(orgs, org{
name: o.Name,
memoryQuota: int(quota),
memoryUsage: int(usage),
spaces: spaces,
})
}
return orgs, nil
}
func (cmd *UsageReportCmd) getSpaces(spaceURL string) ([]space, error) {
rawSpaces, err := cmd.apiHelper.GetOrgSpaces(cmd.cli, spaceURL)
if nil != err {
return nil, err
}
var spaces = []space{}
for _, s := range rawSpaces {
apps, err := cmd.getApps(s.AppsURL)
if nil != err {
return nil, err
}
spaces = append(spaces,
space{
apps: apps,
name: s.Name,
},
)
}
return spaces, nil
}
func (cmd *UsageReportCmd) getApps(appsURL string) ([]app, error) {
rawApps, err := cmd.apiHelper.GetSpaceApps(cmd.cli, appsURL)
if nil != err {
return nil, err
}
var apps = []app{}
for _, a := range rawApps {
apps = append(apps, app{
instances: int(a.Instances),
ram: int(a.RAM),
running: a.Running,
})
}
return apps, nil
}
//Run runs the plugin
func (cmd *UsageReportCmd) Run(cli plugin.CliConnection, args []string) {
if args[0] == "usage-report" {
cmd.apiHelper = &apihelper.APIHelper{}
cmd.cli = cli
cmd.UsageReportCommand(args)
}
}
func main() {
plugin.Start(new(UsageReportCmd))
}