/
storage_utilization_panel.go
468 lines (410 loc) · 15.3 KB
/
storage_utilization_panel.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package EC2
import (
"encoding/json"
"fmt"
"log"
"strconv"
"time"
"github.com/Appkube-awsx/awsx-common/authenticate"
"github.com/Appkube-awsx/awsx-common/awsclient"
"github.com/Appkube-awsx/awsx-common/cmdb"
"github.com/Appkube-awsx/awsx-common/config"
"github.com/Appkube-awsx/awsx-common/model"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/spf13/cobra"
)
type StorageResult struct {
RootVolumeUtilization float64 `json:"RootVolumeUsage"`
EBS1VolumeUtilization float64 `json:"EBSVolume1Usage"`
EBS2VolumeUtilization float64 `json:"EBSVolume2Usage"`
}
var AwsxEc2StorageUtilizationCmd = &cobra.Command{
Use: "Storage_utilization_panel",
Short: "get storage utilization metrics data",
Long: `command to get storage utilization metrics data`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("running from child command")
var authFlag, clientAuth, err = authenticate.AuthenticateCommand(cmd)
if err != nil {
log.Printf("Error during authentication: %v\n", err)
err := cmd.Help()
if err != nil {
return
}
return
}
if authFlag {
responseType, _ := cmd.PersistentFlags().GetString("responseType")
jsonResp, cloudwatchMetricResp, err := GetStorageUtilizationPanel(cmd, clientAuth, nil)
if err != nil {
log.Println("Error getting cpu utilization: ", err)
return
}
if responseType == "frame" {
fmt.Println(cloudwatchMetricResp)
} else {
// default case. it prints json
fmt.Println(jsonResp)
}
}
},
}
func GetStorageUtilizationPanel(cmd *cobra.Command, clientAuth *model.Auth, cloudWatchClient *cloudwatch.CloudWatch) (string, map[string]*cloudwatch.GetMetricDataOutput, error) {
elementId, _ := cmd.PersistentFlags().GetString("elementId")
elementType, _ := cmd.PersistentFlags().GetString("elementType")
cmdbApiUrl, _ := cmd.PersistentFlags().GetString("cmdbApiUrl")
instanceId, _ := cmd.PersistentFlags().GetString("instanceId")
if elementId != "" {
log.Println("getting cloud-element data from cmdb")
apiUrl := cmdbApiUrl
if cmdbApiUrl == "" {
log.Println("using default cmdb url")
apiUrl = config.CmdbUrl
}
log.Println("cmdb url: " + apiUrl)
cmdbData, err := cmdb.GetCloudElementData(apiUrl, elementId)
if err != nil {
return "", nil, err
}
instanceId = cmdbData.InstanceId
}
startTimeStr, _ := cmd.PersistentFlags().GetString("startTime")
endTimeStr, _ := cmd.PersistentFlags().GetString("endTime")
var startTime, endTime *time.Time
// Parse start time if provided
if startTimeStr != "" {
parsedStartTime, err := time.Parse(time.RFC3339, startTimeStr)
if err != nil {
log.Printf("Error parsing start time: %v", err)
err := cmd.Help()
if err != nil {
return "", nil, err
}
return "", nil, err
}
startTime = &parsedStartTime
} else {
defaultStartTime := time.Now().Add(-15 * time.Minute)
startTime = &defaultStartTime
}
if endTimeStr != "" {
parsedEndTime, err := time.Parse(time.RFC3339, endTimeStr)
if err != nil {
log.Printf("Error parsing end time: %v", err)
err := cmd.Help()
if err != nil {
return "", nil, err
}
return "", nil, err
}
endTime = &parsedEndTime
} else {
defaultEndTime := time.Now()
endTime = &defaultEndTime
}
cloudwatchMetricData := map[string]*cloudwatch.GetMetricDataOutput{}
// Get Root Volume Utilization
rootVolumeUsage, err := GetStorageUtilizationMetricData(clientAuth, instanceId, elementType, startTime, endTime, "Average", "disk_used_percent", cloudWatchClient)
if err != nil {
log.Println("Error in getting Root Volume Utilization: ", err)
return "", nil, err
}
cloudwatchMetricData["RootVolumeUtilization"] = rootVolumeUsage
// Get EBS1 Volume Utilization
ebs1VolumeUsage, err := GetStorageUtilizationMetricData(clientAuth, instanceId, elementType, startTime, endTime, "Average", "disk_used_percent", cloudWatchClient)
if err != nil {
log.Println("Error in getting EBS1 Volume Utilization: ", err)
return "", nil, err
}
cloudwatchMetricData["EBS1VolumeUtilization"] = ebs1VolumeUsage
// Get EBS2 Volume Utilization
ebs2VolumeUsage, err := GetStorageUtilizationMetricData(clientAuth, instanceId, elementType, startTime, endTime, "Average", "disk_used_percent", cloudWatchClient)
if err != nil {
log.Println("Error in getting EBS2 Volume Utilization: ", err)
return "", nil, err
}
cloudwatchMetricData["EBS2VolumeUtilization"] = ebs2VolumeUsage
// Calculate average of all three volumes
rootVolumeAvg := calculateAverage(rootVolumeUsage)
ebs1VolumeAvg := calculateAverage(ebs1VolumeUsage) / 2 // Divide by 2
ebs2VolumeAvg := calculateAverage(ebs2VolumeUsage) / 2 // Divide by 2
// Format average utilizations to have two decimal places
rootVolumeAvgStr := strconv.FormatFloat(rootVolumeAvg, 'f', 2, 64)
ebs1VolumeAvgStr := strconv.FormatFloat(ebs1VolumeAvg, 'f', 2, 64)
ebs2VolumeAvgStr := strconv.FormatFloat(ebs2VolumeAvg, 'f', 2, 64)
// Convert formatted strings back to float64
rootVolumeAvgFloat, err := strconv.ParseFloat(rootVolumeAvgStr, 64)
if err != nil {
log.Println("Error converting string to float64: ", err)
return "", nil, err
}
ebs1VolumeAvgFloat, err := strconv.ParseFloat(ebs1VolumeAvgStr, 64)
if err != nil {
log.Println("Error converting string to float64: ", err)
return "", nil, err
}
ebs2VolumeAvgFloat, err := strconv.ParseFloat(ebs2VolumeAvgStr, 64)
if err != nil {
log.Println("Error converting string to float64: ", err)
return "", nil, err
}
// Create JSON output
averageStorageResult := StorageResult{
RootVolumeUtilization: rootVolumeAvgFloat,
EBS1VolumeUtilization: ebs1VolumeAvgFloat,
EBS2VolumeUtilization: ebs2VolumeAvgFloat,
}
jsonString, err := json.Marshal(averageStorageResult)
if err != nil {
log.Println("Error in marshalling json in string: ", err)
return "", nil, err
}
return string(jsonString), cloudwatchMetricData, nil
}
func GetStorageUtilizationMetricData(clientAuth *model.Auth, instanceID, elementType string, startTime, endTime *time.Time, statistic, metricName string, cloudWatchClient *cloudwatch.CloudWatch) (*cloudwatch.GetMetricDataOutput, error) {
log.Printf("Getting metric data for instance %s in namespace %s from %v to %v", instanceID, elementType, startTime, endTime)
elmType := "CWAgent"
// if elementType == "EC2" {
// elmType = "AWS/" + elementType
// }
input := &cloudwatch.GetMetricDataInput{
EndTime: endTime,
StartTime: startTime,
MetricDataQueries: []*cloudwatch.MetricDataQuery{
{
Id: aws.String("m1"),
MetricStat: &cloudwatch.MetricStat{
Metric: &cloudwatch.Metric{
Dimensions: []*cloudwatch.Dimension{
{
Name: aws.String("InstanceId"),
Value: aws.String(instanceID),
},
},
MetricName: aws.String(metricName),
Namespace: aws.String(elmType),
},
Period: aws.Int64(300),
Stat: aws.String(statistic),
},
},
},
}
if cloudWatchClient == nil {
cloudWatchClient = awsclient.GetClient(*clientAuth, awsclient.CLOUDWATCH).(*cloudwatch.CloudWatch)
}
result, err := cloudWatchClient.GetMetricData(input)
if err != nil {
return nil, err
}
return result, nil
}
func calculateAverage(result *cloudwatch.GetMetricDataOutput) float64 {
sum := 0.0
if len(result.MetricDataResults) > 0 && len(result.MetricDataResults[0].Values) > 0 {
for _, value := range result.MetricDataResults[0].Values {
sum += *value
}
return sum / float64(len(result.MetricDataResults[0].Values))
}
return 0
}
func init() {
AwsxEc2StorageUtilizationCmd.PersistentFlags().String("elementId", "", "element id")
AwsxEc2StorageUtilizationCmd.PersistentFlags().String("elementType", "", "element type")
AwsxEc2StorageUtilizationCmd.PersistentFlags().String("query", "", "query")
AwsxEc2StorageUtilizationCmd.PersistentFlags().String("cmdbApiUrl", "", "cmdb api")
AwsxEc2StorageUtilizationCmd.PersistentFlags().String("vaultUrl", "", "vault end point")
AwsxEc2StorageUtilizationCmd.PersistentFlags().String("vaultToken", "", "vault token")
AwsxEc2StorageUtilizationCmd.PersistentFlags().String("zone", "", "aws region")
AwsxEc2StorageUtilizationCmd.PersistentFlags().String("accessKey", "", "aws access key")
AwsxEc2StorageUtilizationCmd.PersistentFlags().String("secretKey", "", "aws secret key")
AwsxEc2StorageUtilizationCmd.PersistentFlags().String("crossAccountRoleArn", "", "aws cross account role arn")
AwsxEc2StorageUtilizationCmd.PersistentFlags().String("externalId", "", "aws external id")
AwsxEc2StorageUtilizationCmd.PersistentFlags().String("cloudWatchQueries", "", "aws cloudwatch metric queries")
AwsxEc2StorageUtilizationCmd.PersistentFlags().String("instanceId", "", "instance id")
AwsxEc2StorageUtilizationCmd.PersistentFlags().String("startTime", "", "start time")
AwsxEc2StorageUtilizationCmd.PersistentFlags().String("endTime", "", "endcl time")
AwsxEc2StorageUtilizationCmd.PersistentFlags().String("responseType", "", "response type. json/frame")
}
// package EC2
// import (
// "encoding/json"
// "fmt"
// "log"
// "time"
// "github.com/Appkube-awsx/awsx-common/authenticate"
// "github.com/Appkube-awsx/awsx-common/awsclient"
// "github.com/Appkube-awsx/awsx-common/cmdb"
// "github.com/Appkube-awsx/awsx-common/config"
// "github.com/Appkube-awsx/awsx-common/model"
// "github.com/aws/aws-sdk-go/aws"
// "github.com/aws/aws-sdk-go/service/cloudwatch"
// "github.com/spf13/cobra"
// )
// type StorageResult struct {
// RootVolumeUtilization float64 `json:"RootVolumeUtilization"`
// EBS1VolumeUtilization float64 `json:"EBS1VolumeUtilization"`
// EBS2VolumeUtilization float64 `json:"EBS2VolumeUtilization"`
// }
// const (
// bytesToGigabytes = 1024 * 1024 * 1024
// )
// var StorageUtilizationCmd = &cobra.Command{
// Use: "storage_utilization_panel",
// Short: "get storage utilization metrics data",
// Long: `command to get storage utilization metrics data`,
// Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("running storage utilization panel")
// var authFlag, clientAuth, err = authenticate.AuthenticateCommand(cmd)
// if err != nil {
// log.Printf("Error during authentication: %v\n", err)
// err := cmd.Help()
// if err != nil {
// return
// }
// return
// }
// if authFlag {
// jsonResp, err := GetStorageUtilizationPanel(cmd, clientAuth, nil)
// if err != nil {
// log.Println("Error getting storage utilization: ", err)
// return
// }
// fmt.Println(jsonResp)
// }
// },
// }
// func GetStorageUtilizationPanel(cmd *cobra.Command, clientAuth *model.Auth, cloudWatchClient *cloudwatch.CloudWatch) (string, error) {
// // Initialize CloudWatch client if not provided
// if cloudWatchClient == nil {
// cloudWatchClient = awsclient.GetClient(*clientAuth, awsclient.CLOUDWATCH).(*cloudwatch.CloudWatch)
// }
// elementId, _ := cmd.PersistentFlags().GetString("elementId")
// // elementType, _ := cmd.PersistentFlags().GetString("elementType")
// cmdbApiUrl, _ := cmd.PersistentFlags().GetString("cmdbApiUrl")
// instanceId, _ := cmd.PersistentFlags().GetString("instanceId")
// if elementId != "" {
// log.Println("getting cloud-element data from cmdb")
// apiUrl := cmdbApiUrl
// if cmdbApiUrl == "" {
// log.Println("using default cmdb url")
// apiUrl = config.CmdbUrl
// }
// log.Println("cmdb url: " + apiUrl)
// cmdbData, err := cmdb.GetCloudElementData(apiUrl, elementId)
// if err != nil {
// return "", nil
// }
// instanceId = cmdbData.InstanceId
// }
// startTimeStr, _ := cmd.PersistentFlags().GetString("startTime")
// endTimeStr, _ := cmd.PersistentFlags().GetString("endTime")
// var startTime, endTime *time.Time
// // Parse start time if provided
// if startTimeStr != "" {
// parsedStartTime, err := time.Parse(time.RFC3339, startTimeStr)
// if err != nil {
// log.Printf("Error parsing start time: %v", err)
// err := cmd.Help()
// if err != nil {
// return "", err
// }
// return "", err
// }
// startTime = &parsedStartTime
// }
// // Parse end time if provided
// if endTimeStr != "" {
// parsedEndTime, err := time.Parse(time.RFC3339, endTimeStr)
// if err != nil {
// log.Printf("Error parsing end time: %v", err)
// err := cmd.Help()
// if err != nil {
// return "", err
// }
// return "", err
// }
// endTime = &parsedEndTime
// }
// // If start time is not provided, use last 15 minutes
// if startTime == nil {
// defaultStartTime := time.Now().Add(-15 * time.Minute)
// startTime = &defaultStartTime
// }
// // If end time is not provided, use current time
// if endTime == nil {
// defaultEndTime := time.Now()
// endTime = &defaultEndTime
// }
// // If start time is after end time, return null
// if startTime.After(*endTime) {
// log.Println("Start time is after end time")
// return "null", nil
// }
// // Get metrics for root volume utilization
// rootVolumeUtilization, err := GetStorageUtilizationMetricData(clientAuth, instanceId, "disk_used", startTime, endTime, cloudWatchClient)
// if err != nil {
// return "", err
// }
// // Get metrics for EBS volume 1 utilization
// ebs1VolumeUtilization, err := GetStorageUtilizationMetricData(clientAuth, instanceId, "VolumeBytesUsed", startTime, endTime, cloudWatchClient)
// if err != nil {
// return "", err
// }
// // Get metrics for EBS volume 2 utilization
// ebs2VolumeUtilization, err := GetStorageUtilizationMetricData(clientAuth, instanceId, "VolumeBytesUsed", startTime, endTime, cloudWatchClient)
// if err != nil {
// return "", err
// }
// // Calculate percentages for utilization
// rootVolumeUtilizationPercent := (rootVolumeUtilization / bytesToGigabytes) * 100
// ebs1VolumeUtilizationPercent := (ebs1VolumeUtilization / bytesToGigabytes) * 100
// ebs2VolumeUtilizationPercent := (ebs2VolumeUtilization / bytesToGigabytes) * 100
// // Create JSON output
// jsonOutput := StorageResult{
// RootVolumeUtilization: rootVolumeUtilizationPercent,
// EBS1VolumeUtilization: ebs1VolumeUtilizationPercent,
// EBS2VolumeUtilization: ebs2VolumeUtilizationPercent,
// }
// jsonString, err := json.Marshal(jsonOutput)
// if err != nil {
// log.Println("Error in marshalling json in string: ", err)
// return "", err
// }
// return string(jsonString), nil
// }
// func GetStorageUtilizationMetricData(clientAuth *model.Auth, instanceId string, metricName string, startTime, endTime *time.Time, cloudWatchClient *cloudwatch.CloudWatch) (float64, error) {
// // Define input parameters for the metric query
// input := &cloudwatch.GetMetricDataInput{
// StartTime: startTime,
// EndTime: endTime,
// MetricDataQueries: []*cloudwatch.MetricDataQuery{
// {
// Id: aws.String("m1"),
// MetricStat: &cloudwatch.MetricStat{
// Metric: &cloudwatch.Metric{
// MetricName: aws.String(metricName),
// Namespace: aws.String("CWAgent"), // Adjust namespace if necessary
// },
// Period: aws.Int64(300),
// Stat: aws.String("Average"),
// },
// },
// },
// }
// // Make the API call to CloudWatch to get the metric data
// result, err := cloudWatchClient.GetMetricData(input)
// if err != nil {
// return 0, err
// }
// // Extract the metric value
// if len(result.MetricDataResults) > 0 && len(result.MetricDataResults[0].Values) > 0 {
// return *result.MetricDataResults[0].Values[0], nil
// }
// return 0, fmt.Errorf("no metric data available for %s", metricName)
// }
// func init() {
// // Initialize flags or command options if needed
// }