forked from krujos/usagereport-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusagereport.go
204 lines (181 loc) · 4.12 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
name string
buildpack string
buildpack_detected string
}
//GetMetadata returns metatada
func (cmd *UsageReportCmd) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "snapshot",
Version: plugin.VersionType{
Major: 1,
Minor: 0,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "snapshot",
HelpText: "Report AI and memory usage for orgs and spaces",
UsageDetails: plugin.Usage{
Usage: "cf snapshot",
},
},
},
}
}
//UsageReportCommand doer
func (cmd *UsageReportCmd) UsageReportCommand(args []string) {
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)
}
reader := bufio.NewReader(os.Stdin)
fmt.Print("Windows or Linux (w or l):")
env, _ := reader.ReadString('\n')
out_dir := " "
if env.str == "l" or env.str == "L" {
out_dir = "/home/"
} else if env.str ="w" or env.str == "W" {
out_dir = "C:\Users\Public\Desktop"
} else {
out_dir = "Error"
}
totalApps := 0
totalInstances := 0
fmt.Printf("App,Space,Org,Memory Usage,Total Instances, Status, Buildpack, Buildpack Detected\n")
for _, org := range orgs {
for _, space := range org.spaces {
consumed := 0
instances := 0
runningApps := 0
stoppedApps := 0
runningInstances := 0
var appStatus string
for _, app := range space.apps {
if app.running {
consumed += int(app.instances * app.ram)
runningApps++
runningInstances += app.instances
appStatus = "Running"
} else {
stoppedApps++
appStatus = "Stopped"
}
instances += int(app.instances)
fmt.Printf("%s,%s,%s,%d MB,%d,%s, %s, %s \n",
app.name, space.name, org.name, consumed, app.instances, appStatus, app.buildpack, app.buildpack_detected )
}
totalInstances += instances
totalApps += len(space.apps)
totalInstances += instances
totalApps += len(space.apps)
}
}
}
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,
name: a.Name,
buildpack: a.Buildpack,
buildpack_detected: a.BuildpackDetected,
})
}
return apps, nil
}
//Run runs the plugin
func (cmd *UsageReportCmd) Run(cli plugin.CliConnection, args []string) {
if args[0] == "snapshot" {
cmd.apiHelper = &apihelper.APIHelper{}
cmd.cli = cli
cmd.UsageReportCommand(args)
}
}
func main() {
plugin.Start(new(UsageReportCmd))
}