This repository has been archived by the owner on Sep 21, 2021. It is now read-only.
/
output.go
136 lines (126 loc) · 4.6 KB
/
output.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
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package data
import (
"fmt"
"log"
"os"
"regexp"
"strings"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/awslabs/amazon-ec2-instance-qualifier/pkg/cmdutil"
"github.com/awslabs/amazon-ec2-instance-qualifier/pkg/config"
"github.com/awslabs/amazon-ec2-instance-qualifier/pkg/resources"
)
const (
finalOutputTableHeader = "INSTANCE TYPE,STATUS,CPU_USAGE_ACTIVE,CPU_THRESHOLD,MEM_USED_PERCENT,MEM_THRESHOLD,ALL TESTS PASS?,TOTAL EXECUTION TIME (sec)"
notApplicable = "N/A"
instanceIdRegex = "i-[0-9a-z]{17}"
)
// OutputAsTable parses the final result json file and outputs in table format.
func OutputAsTable(sess *session.Session, outputStream *os.File, results []*cloudwatch.MetricDataResult) error {
svc := resources.New(sess)
testFixture := config.GetTestFixture()
finalResult, err := updateResults(results, testFixture)
if err != nil {
return err
}
log.Println("Updating local and remote results files after merging CloudWatch data")
localPath := resultsDir + "/" + testFixture.FinalResultFilename
remotePath := testFixture.BucketRootDir + "/" + testFixture.FinalResultFilename
if err := cmdutil.MarshalToFile(finalResult, localPath); err != nil {
log.Printf("There was an error saving updated results locally. final result: %v\n", finalResult)
}
if err := svc.UploadToBucket(testFixture.BucketName, localPath, remotePath); err != nil {
log.Println("There was an error uploading updated results to S3")
}
var tableData [][]string
for _, instanceResult := range finalResult {
row, err := parseInstanceResultToRow(instanceResult)
if err != nil {
return err
}
tableData = append(tableData, row)
}
instances, err := svc.GetInstancesInCfnStack()
if err != nil {
return err
}
for _, instance := range instances {
isFound := false
for _, instanceResult := range finalResult {
if instance.InstanceType == instanceResult.InstanceType {
isFound = true
break
}
}
if !isFound {
var row []string
row = append(row, instance.InstanceType, notApplicable, notApplicable, notApplicable, notApplicable, notApplicable, notApplicable, notApplicable)
tableData = append(tableData, row)
}
}
cmdutil.RenderTable(tableData, strings.Split(finalOutputTableHeader, ","), outputStream)
fmt.Fprintf(outputStream, "\nDetailed test results can be found in s3://%s/%s\n", testFixture.BucketName, testFixture.BucketRootDir)
return nil
}
// updateResults updates the FinalResult of a test fixture with corresponding CloudWatch data
// then saves updates results file locally and remotely
func updateResults(results []*cloudwatch.MetricDataResult, testFixture config.TestFixture) ([]resources.Instance, error) {
cwMetrics := make(map[string][]resources.Metric)
metricThresholds := map[string]int{
"cpu_usage_active": testFixture.CpuThreshold,
"mem_used_percent": testFixture.MemThreshold,
}
for _, metricData := range results {
if metricData.Values != nil {
splitLabel := strings.Split(*metricData.Label, " ")
var instanceId string
for i, tag := range splitLabel {
matched, err := regexp.MatchString(instanceIdRegex, tag)
if err != nil {
log.Println("Could not extract instanceId from MetricDataResult")
return nil, err
}
if matched {
instanceId = splitLabel[i]
break
}
}
if instanceId != "" {
metricName := splitLabel[len(splitLabel)-1] //name is always last in label
metricValue := *metricData.Values[0]
thresholdValue := metricThresholds[metricName]
metric := resources.Metric{
MetricUsed: metricName,
Value: metricValue,
Threshold: float64(thresholdValue),
Unit: "Percent", //UserConfig
}
cwMetrics[instanceId] = append(cwMetrics[instanceId], metric)
}
}
}
finalResult, err := finalResultToArray(testFixture.FinalResultFilename)
if err != nil {
return finalResult, err
}
for _, instanceResult := range finalResult {
oldRes := instanceResult.Results
for i := range oldRes {
oldRes[i].Metrics = cwMetrics[instanceResult.InstanceId]
}
}
return finalResult, nil
}