-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathlow_service_usage.pt
341 lines (310 loc) · 11.3 KB
/
low_service_usage.pt
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
name "Low Service Usage"
rs_pt_ver 20180301
type "policy"
short_description "**Deprecated: This policy is no longer being updated. Please see [README](https://github.com/flexera-public/policy_templates/tree/master/cost/flexera/cco/low_service_usage/) for more details.** Analyze all service usage and determines recommend consolidation or deletion. See the [README](https://github.com/flexera-public/policy_templates/tree/master/cost/flexera/cco/low_service_usage/) and [docs.flexera.com/flexera/EN/Automation](https://docs.flexera.com/flexera/EN/Automation/AutomationGS.htm) to learn more."
long_description ""
severity "low"
category "Cost"
default_frequency "daily"
tenancy "single"
info(
version: "2.2.2",
provider: "Flexera",
service: "Cloud Cost Optimization",
policy_set: "Cloud Cost Optimization",
deprecated: "true",
hide_skip_approvals: "true"
)
###############################################################################
# Input Parameter
###############################################################################
parameter "param_threshold" do
label "Low Service Spend Threshold"
description "Estimated run-rate below which a service should be reviewed for wasted usage. Example: 100.0"
type "number"
default 100
min_value 1
max_value 1000000
end
parameter "param_email" do
label "Email addresses"
description "Email addresses of the recipients you wish to notify"
type "list"
end
parameter "param_billing_centers" do
label "Billing Center Name"
description "List of Billing Center Names to check"
type "list"
min_length 1
end
parameter "param_minimum_savings_threshold" do
label "Minimum Savings Threshold"
description "Specify the minimum monthly savings value required for a recommendation to be issued, on a per resource basis. Note: this setting applies to all recommendations. Example: 1.00"
type "number"
default 1
end
###############################################################################
# Authentication
###############################################################################
credentials "auth_flexera" do
schemes "oauth2"
label "flexera"
description "Select Flexera One OAuth2 credentials"
tags "provider=flexera"
end
###############################################################################
# Datasources
###############################################################################
datasource "ds_dimensions" do
request do
auth $auth_flexera
host rs_optima_host
path join(["/bill-analysis/orgs/",rs_org_id,"/costs/dimensions"])
header "Api-Version", "1.0"
header "User-Agent", "RS Policies"
end
end
datasource "ds_billing_centers" do
request do
auth $auth_flexera
host rs_optima_host
path join(["/analytics/orgs/",rs_org_id,"/billing_centers"])
header "Api-Version", "1.0"
header "User-Agent", "RS Policies"
query "view", "allocation_table"
end
result do
encoding "json"
collect jmes_path(response,"[*]") do
field "href", jmes_path(col_item,"href")
field "id", jmes_path(col_item,"id")
field "name", jmes_path(col_item,"name")
field "parent_id", jmes_path(col_item,"parent_id")
field "ancestor_ids", jmes_path(col_item,"ancestor_ids")
field "allocation_table", jmes_path(col_item,"allocation_table")
end
end
end
datasource "ds_cloud_vendor_accounts" do
request do
auth $auth_flexera
host rs_optima_host
path join(["/bill-analysis/orgs/",rs_org_id,"/cloud_vendor_accounts"])
header "Api-Version", "0.1"
header "User-Agent", "RS Policies"
query "cloud_vendor", "aws"
end
result do
encoding "json"
collect jmes_path(response,"[*]") do
field "id", jmes_path(col_item,"id")
field "name", jmes_path(col_item,"name")
field "vendor_name", jmes_path(col_item,"vendor_name")
end
end
end
datasource "ds_new_bc_costs" do
request do
run_script $js_new_costs_request, rs_org_id, $ds_billing_centers, $param_billing_centers, rs_optima_host
end
result do
encoding "json"
collect jmes_path(response,"rows[*]") do
field "vendor", jmes_path(col_item, "dimensions.vendor")
field "vendor_account_name", jmes_path(col_item,"dimensions.vendor_account_name")
field "cost_amortized_unblended_adj", jmes_path(col_item,"metrics.cost_nonamortized_blended_adj")
field "service", jmes_path(col_item, "dimensions.service")
field "region", jmes_path(col_item, "dimensions.region")
field "id", jmes_path(col_item,"dimensions.billing_center_id")
field "timestamp", jmes_path(col_item,"timestamp")
end
end
end
datasource "ds_format_costs" do
run_script $js_format_costs, $ds_new_bc_costs, $ds_cloud_vendor_accounts, $ds_billing_centers, $param_threshold, $param_minimum_savings_threshold
end
###############################################################################
# Scripts
###############################################################################
script "js_new_costs_request", type: "javascript" do
parameters "org_id","ds_billing_centers", "param_billing_centers", "rs_optima_host"
result "request"
code <<-EOS
function pad(n){return n<10 ? '0'+n : n}
var date = new Date();
var year = date.getUTCFullYear();
var month = (1 + date.getUTCMonth())
var yesterday = new Date(new Date().setDate(new Date().getDate()-1)).getDate()
if(yesterday==1){
yesterday++;
}
if(date.getDate()==1){
month=month-1;
if(month==1){
month=12;
year=year-1;
}
}
var end_at = year + "-" + pad(month) + "-" + pad(yesterday)
var start_at = year + "-" + pad(month) + '-01'
var billing_center_ids = []
if (param_billing_centers.length === 0){
var top_billing_centers = _.reject(ds_billing_centers, function(bc){ return bc.parent_id != null });
billing_center_ids = _.map(top_billing_centers, function(value, key){ return value.id });
} else {
// get array of billing center id's that match the names in param_billing_centers.
billing_center_names = _.map(param_billing_centers, function(name){ return name.toLowerCase(); });
billing_center_ids = _.compact(_.map(ds_billing_centers, function(value){ if(_.contains(billing_center_names, value.name.toLowerCase())){return value.id} }));
}
var request = {
auth: "auth_flexera",
verb: "POST",
host: rs_optima_host,
path: "/bill-analysis/orgs/" + org_id + "/costs/aggregated",
body_fields: {
"dimensions": ["billing_center_id", "vendor", "vendor_account_name", "service", "region"],
"granularity": "day",
"metrics": ['cost_nonamortized_blended_adj'],
"billing_center_ids": billing_center_ids,
"start_at": start_at,
"end_at": end_at
},
headers: {
"Api-Version": "1.0",
"User-Agent": "RS Policies",
}
}
EOS
end
script "js_format_costs", type: "javascript" do
parameters "new_bc_costs", "ds_cloud_vendor_accounts", "ds_billing_centers", "param_threshold", "param_minimum_savings_threshold"
result "formatted_data"
code <<-EOS
//https://www.w3resource.com/javascript-exercises/javascript-date-exercise-3.php
var getDaysInMonth = function(month,year) {
// Here January is 1 based
//Day 0 is the last day in the previous month
return new Date(year, month, 0).getDate();
// Here January is 0 based
// return new Date(year, month+1, 0).getDate();
};
var date = new Date()
var today = date.getDate()
var numdays = getDaysInMonth(date.getUTCMonth(),date.getUTCFullYear())
var yesterday = new Date(new Date().setDate(new Date().getDate()-1)).getDate()
var type = 'cost_amortized_unblended_adj';
var formatted_data = [];
var arr_results = [];
var bcs = [];
_.each(new_bc_costs, function(bcc){bcs.push(bcc.id)})
bcs = _.uniq(bcs) ;
_.each(bcs, function(bc_id){
var new_bc_cost_objs = _.reject(new_bc_costs, function(new_bc_cost){ return new_bc_cost.id != bc_id });
_.each(new_bc_cost_objs, function(new_bc_cost_obj){
var billing_center = _.reject(ds_billing_centers, function(bc){ return bc.id != new_bc_cost_obj.id })[0];
arr_results.push({
name: billing_center.name,
id: bc_id,
service: new_bc_cost_obj.service,
region: new_bc_cost_obj.region,
new_sum: new_bc_cost_obj.cost_amortized_unblended_adj,
vendor: new_bc_cost_obj.vendor,
vendor_account_name: new_bc_cost_obj.vendor_account_name,
timestamp: new_bc_cost_obj.timestamp
})
})
})
var groups = _.groupBy(arr_results, function(value){
return value.id + '#' + value.vendor + '#' + value.vendor_account_name + '#' + value.service + '#' + value.region
});
var unsorted_results = _.map(groups, function(group){
counter = 0
for (i = 0; i< group.length; i++){
item = group[i]
if (item.new_sum == 0){
counter=counter+1
} else {
break
}
}
var arr_new_sum = _.pluck(group, 'new_sum')
var summed = _.reduce(arr_new_sum, function(memo, num){ return memo + num; }, 0)
var monthcomplete = yesterday / (numdays-counter) ;
var runrate = summed / monthcomplete
return {
name: group[0].name,
id: group[0].id,
vendor: group[0].vendor,
vendor_account_name: group[0].vendor_account_name,
service: group[0].service,
region: group[0].region,
run_rate: runrate,
arr_new_sum: arr_new_sum
}
});
var checked_results = _.filter(unsorted_results, function(result){
if (parseFloat(result.run_rate) <= param_threshold && parseFloat(result.run_rate) >= param_minimum_savings_threshold) {
return result;
}
})
var formatted_data = _(checked_results).chain()
.sortBy(function(result){
return result.region;
}).sortBy(function(result) {
return result.vendor_account_name;
}).sortBy(function(result) {
return result.vendor;
}).value();
var total_sum = _.chain(formatted_data)
.pluck('run_rate')
.reduce(function(memo, num){ return memo + parseFloat(num); }, 0).value();
_.each(formatted_data, function(data){
data["total_sum"] = parseFloat(total_sum).toFixed(2)
data["run_rate"] = parseFloat(data.run_rate).toFixed(2)
return data
})
EOS
end
###############################################################################
# Escalations
###############################################################################
escalation "esc_budget_alert" do
automatic true
label "Send Email"
description "Send incident email"
email $param_email
end
###############################################################################
# Policy
###############################################################################
policy "policy_budget_alert" do
validate $ds_format_costs do
summary_template "{{ rs_org_name }} (Org ID: {{ rs_org_id }}): Low Service Usage Detected Below {{parameters.param_threshold}} - Total Savings: {{with(index data 0)}}{{.total_sum}}{{end}}"
escalate $esc_budget_alert
check eq(size(data), 0)
export do
resource_level true
field "name" do
label "Billing Center Name"
end
field "vendor" do
label "Vendor"
end
field "vendor_account_name" do
label "Account Name"
end
field "region" do
label "Region"
end
field "service" do
label "Service"
end
field "run_rate" do
label "Savings"
end
field "id" do
label "ID"
end
end
end
end