/
port_allocation_error_count.go
200 lines (176 loc) · 6.9 KB
/
port_allocation_error_count.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
package NLB
import (
"encoding/json"
"fmt"
"log"
"time"
"github.com/Appkube-awsx/awsx-common/authenticate"
"github.com/Appkube-awsx/awsx-common/cmdb"
"github.com/Appkube-awsx/awsx-common/config"
"github.com/Appkube-awsx/awsx-common/awsclient"
"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 NlbPortErrorCountTime struct {
RawData []struct {
Timestamp time.Time
Value float64
} `json:"port_allocation_error_count_panel"`
}
var AwsxNlbPortAllocationErrorCountCmd = &cobra.Command{
Use: "port_allocation_error_count_panel",
Short: "get port allocation error count metrics data",
Long: `command to get target tls count 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 := GetPortAllocationErrorCountData(cmd, clientAuth, nil)
if err != nil {
log.Println("Error getting nlb target tls response data: ", err)
return
}
if responseType == "frame" {
fmt.Println(cloudwatchMetricResp)
} else {
fmt.Println(jsonResp)
}
}
},
}
func GetPortAllocationErrorCountData(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
if startTimeStr != "" {
parsedStartTime, err := time.Parse(time.RFC3339, startTimeStr)
if err != nil {
log.Printf("Error parsing start time: %v", err)
return "", nil, err
}
startTime = &parsedStartTime
} else {
defaultStartTime := time.Now().Add(-5 * time.Minute)
startTime = &defaultStartTime
}
if endTimeStr != "" {
parsedEndTime, err := time.Parse(time.RFC3339, endTimeStr)
if err != nil {
log.Printf("Error parsing end time: %v", err)
return "", nil, err
}
endTime = &parsedEndTime
} else {
defaultEndTime := time.Now()
endTime = &defaultEndTime
}
log.Printf("StartTime: %v, EndTime: %v", startTime, endTime)
cloudwatchMetricData := map[string]*cloudwatch.GetMetricDataOutput{}
// Fetch raw data
PortErrorCount, err := GetNlbPortAllocationErrorCountMetricValue(clientAuth, instanceId, elementType, startTime, endTime, "Sum", cloudWatchClient)
if err != nil {
log.Println("Error in getting NLB active connections data: ", err)
return "", nil, err
}
cloudwatchMetricData["PortErrorCount"] = PortErrorCount
result := ProcessPortAllocationResponseRawData(PortErrorCount)
jsonString, err := json.Marshal(result)
if err != nil {
log.Println("Error in marshalling json in string: ", err)
return "", nil, err
}
return string(jsonString), cloudwatchMetricData, nil
}
func GetNlbPortAllocationErrorCountMetricValue(clientAuth *model.Auth, instanceId string, elementType string, startTime, endTime *time.Time, statistic string, cloudWatchClient *cloudwatch.CloudWatch) (*cloudwatch.GetMetricDataOutput, error) {
input := &cloudwatch.GetMetricDataInput{
MetricDataQueries: []*cloudwatch.MetricDataQuery{
{
Id: aws.String("m1"),
MetricStat: &cloudwatch.MetricStat{
Metric: &cloudwatch.Metric{
Dimensions: []*cloudwatch.Dimension{
{
Name: aws.String("LoadBalancer"),
Value: aws.String("net/a0affec9643ca40c5a4e837eab2f07fb/f623f27b6210158f"),
},
},
Namespace: aws.String("AWS/NetworkELB"),
MetricName: aws.String("PortAllocationErrorCount"),
},
Period: aws.Int64(300),
Stat: aws.String(statistic),
},
},
},
StartTime: startTime,
EndTime: endTime,
}
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 ProcessPortAllocationResponseRawData(result *cloudwatch.GetMetricDataOutput) NlbPortErrorCountTime {
var rawData NlbPortErrorCountTime
rawData.RawData = make([]struct {
Timestamp time.Time
Value float64
}, len(result.MetricDataResults[0].Timestamps))
for i, timestamp := range result.MetricDataResults[0].Timestamps {
rawData.RawData[i].Timestamp = *timestamp
rawData.RawData[i].Value = *result.MetricDataResults[0].Values[i]
}
return rawData
}
func init() {
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("elementId", "", "element id")
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("elementType", "", "element type")
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("query", "", "query")
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("cmdbApiUrl", "", "cmdb api")
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("vaultUrl", "", "vault end point")
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("vaultToken", "", "vault token")
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("zone", "", "aws region")
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("accessKey", "", "aws access key")
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("secretKey", "", "aws secret key")
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("crossAccountRoleArn", "", "aws cross account role arn")
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("externalId", "", "aws external id")
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("cloudWatchQueries", "", "aws cloudwatch metric queries")
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("instanceId", "", "instance id")
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("startTime", "", "start time")
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("endTime", "", "end time")
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("responseType", "", "response type. json/frame")
AwsxNlbPortAllocationErrorCountCmd.PersistentFlags().String("ApiName", "", "api name")
}