-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (152 loc) · 3.93 KB
/
main.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
package main
import (
"encoding/json"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/fictionbase/fictionbase"
"github.com/fictionbase/fictionbase/type/fbhttp"
"github.com/fictionbase/fictionbase/type/fbresource"
"go.uber.org/zap"
)
func init() {
fictionbase.SetViperConfig()
}
// Resources struct
type typeChecker struct {
TypeKey string `json:"type_key"`
StorageKey string `json:"storage_key"`
TimeKey string `json:"time_key"`
}
var (
sq *fictionbase.Sqs
cw *fictionbase.Cw
logger *zap.Logger
wg sync.WaitGroup
messages []*sqs.Message
err error
)
// @TODO more Elegant...
func main() {
sq = fictionbase.NewSqs()
cw = fictionbase.NewCw()
var typeChecker typeChecker
logger := fictionbase.GetLogger()
for {
messages, err = sq.GetFictionbaseMessage()
if err != nil {
logger.Error(err.Error())
return
}
// Get All SQS Data
if len(messages) == 0 {
return
}
for _, m := range messages {
wg.Add(1)
go func(m *sqs.Message) {
defer wg.Done()
err = json.Unmarshal([]byte(*m.Body), &typeChecker)
if err != nil {
logger.Error(err.Error())
return
}
if typeChecker.TypeKey == "fbresource.Resources" {
SetFbresource(m)
return
}
if typeChecker.TypeKey == "fbhttp.HTTP" {
SetFbHTTP(m)
return
}
}(m)
}
wg.Wait()
}
}
// SetFbresource Set For fbresource
func SetFbresource(message *sqs.Message) {
var sqsData fbresource.Resources
err := json.Unmarshal([]byte(*message.Body), &sqsData)
if err != nil {
logger.Error(err.Error())
return
}
// @TODO OtherResources
dimensionParam := &cloudwatch.Dimension{
Name: aws.String("Hostname"),
Value: aws.String(sqsData.Host.Hostname),
}
metricDataParam := &cloudwatch.MetricDatum{
Dimensions: []*cloudwatch.Dimension{dimensionParam},
MetricName: aws.String("DiskUsage"),
Timestamp: &sqsData.TimeKey,
Unit: aws.String("Percent"),
Value: aws.Float64(sqsData.Disk.UsedPercent),
StorageResolution: aws.Int64(1),
}
putMetricDataInput := &cloudwatch.PutMetricDataInput{
MetricData: []*cloudwatch.MetricDatum{metricDataParam},
Namespace: aws.String("EC2"),
}
err = cw.SendCloudWatch(putMetricDataInput)
if err != nil {
logger.Error(err.Error())
return
}
sq.DeleteFictionbaseMessage(message)
if err != nil {
logger.Error(err.Error())
return
}
}
// SetFbHTTP Set For fbhttp
func SetFbHTTP(message *sqs.Message) {
var sqsData fbhttp.HTTP
err := json.Unmarshal([]byte(*message.Body), &sqsData)
if err != nil {
logger.Error(err.Error())
return
}
dimensionParam := &cloudwatch.Dimension{
Name: aws.String("MonitorHTTP"),
Value: aws.String(sqsData.MonitorHTTP),
}
metricDataParam := &cloudwatch.MetricDatum{}
// statuscode Error
if sqsData.Status != 0 {
metricDataParam = &cloudwatch.MetricDatum{
Dimensions: []*cloudwatch.Dimension{dimensionParam},
MetricName: aws.String("Status"),
Timestamp: &sqsData.TimeKey,
Unit: aws.String("StatusCode"),
Value: aws.Float64(sqsData.Status),
StorageResolution: aws.Int64(1),
}
// ResponseTime Too long
} else if sqsData.ResponseTime != 0 {
metricDataParam = &cloudwatch.MetricDatum{
Dimensions: []*cloudwatch.Dimension{dimensionParam},
MetricName: aws.String("ResponseTime"),
Timestamp: &sqsData.TimeKey,
Unit: aws.String("ResponseTime"),
Value: aws.Float64(sqsData.ResponseTime),
StorageResolution: aws.Int64(1),
}
}
putMetricDataInput := &cloudwatch.PutMetricDataInput{
MetricData: []*cloudwatch.MetricDatum{metricDataParam},
Namespace: aws.String("EC2"),
}
err = cw.SendCloudWatch(putMetricDataInput)
if err != nil {
logger.Error(err.Error())
return
}
sq.DeleteFictionbaseMessage(message)
if err != nil {
logger.Error(err.Error())
return
}
}