-
Notifications
You must be signed in to change notification settings - Fork 20
/
elkpump-basic.go
284 lines (248 loc) · 6.78 KB
/
elkpump-basic.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
// Licensed to You under the Apache License, Version 2.0.
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"math"
"net/http"
"os"
"strconv"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
"github.com/dell/iDRAC-Telemetry-Reference-Tools/internal/databus"
"github.com/dell/iDRAC-Telemetry-Reference-Tools/internal/messagebus/stomp"
)
var configStrings = map[string]string{
"mbhost": "activemq",
"mbport": "61613",
}
type DataValueElasticSearch struct {
ID string
Context string
Label string
Value string
System string
Timestamp string
ValueAggregatable float64
}
func handleGroups(groupsChan chan *databus.DataGroup,
es *elasticsearch.Client, indexName string) {
type bulkResponse struct {
Errors bool `json:"errors"`
Items []struct {
Index struct {
ID string `json:"_id"`
Result string `json:"result"`
Status int `json:"status"`
Error struct {
Type string `json:"type"`
Reason string `json:"reason"`
Cause struct {
Type string `json:"type"`
Reason string `json:"reason"`
} `json:"caused_by"`
} `json:"error"`
} `json:"index"`
} `json:"items"`
}
var (
buf bytes.Buffer
res *esapi.Response
err error
raw map[string]interface{}
blk *bulkResponse
numErrors int
numIndexed int
)
for {
group := <-groupsChan
// log.Print("group: ", group)
for _, value := range group.Values {
log.Print("value: ", value)
// Prepare the metadata payload
meta := []byte(fmt.Sprintf(`{ "index" : { "_id" : "%s-%s" } }%s`, value.ID, value.Timestamp, "\n"))
// Prepare the data payload: encode article to JSON
if len(value.Value) == 0 {
continue
}
intVal, intErr := strconv.ParseInt(value.Value, 10, 64)
floatVal, floatErr := strconv.ParseFloat(value.Value, 64)
esvalue := DataValueElasticSearch{value.ID, value.Context, value.Label, value.Value, value.System, value.Timestamp, 0}
switch {
case intErr == nil:
esvalue.ValueAggregatable = float64(intVal)
case floatErr == nil && !math.IsNaN(floatVal):
esvalue.ValueAggregatable = floatVal
}
data, err := json.Marshal(esvalue)
if err != nil {
log.Fatalf("Cannot encode article %s: %s", value.ID, err)
}
// Append newline to the data payload
data = append(data, "\n"...) // <-- Comment out to trigger failure for batch
// Append payloads to the buffer (ignoring write errors)
buf.Grow(len(meta) + len(data))
buf.Write(meta)
buf.Write(data)
}
res, err = es.Bulk(bytes.NewReader(buf.Bytes()), es.Bulk.WithIndex(indexName))
if err != nil {
log.Fatalf("Failure indexing batch : %s", err)
}
if res.IsError() {
if err := json.NewDecoder(res.Body).Decode(&raw); err != nil {
log.Fatalf("Failure to to parse response body: %s", err)
} else {
log.Printf(" Error: [%d] %s: %s",
res.StatusCode,
raw["error"].(map[string]interface{})["type"],
raw["error"].(map[string]interface{})["reason"],
)
}
// A successful response might still contain errors for particular documents...
//
} else {
if err := json.NewDecoder(res.Body).Decode(&blk); err != nil {
log.Fatalf("Failure to to parse response body: %s", err)
} else {
for _, d := range blk.Items {
//so for any HTTP status above 201 ...
if d.Index.Status > 201 {
//increment the error counter ...
numErrors++
//and print the response status and error information ...
log.Printf(" Error: [%d]: %s: %s: %s: %s",
d.Index.Status,
d.Index.Error.Type,
d.Index.Error.Reason,
d.Index.Error.Cause.Type,
d.Index.Error.Cause.Reason,
)
} else {
//otherwise increase the success counter.
numIndexed++
}
}
}
}
res.Body.Close()
buf.Reset()
}
}
func getEnvSettings() {
mbHost := os.Getenv("MESSAGEBUS_HOST")
if len(mbHost) > 0 {
configStrings["mbhost"] = mbHost
}
mbPort := os.Getenv("MESSAGEBUS_PORT")
if len(mbPort) > 0 {
configStrings["mbport"] = mbPort
}
}
func main() {
var (
res *esapi.Response
err error
)
/*
mapping := `{
"mappings": {
"properties" : {
"Context" : {"type" : "keyword"},
"ID" : {"type" : "keyword"},
"Label" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"System" : {"type" : "keyword"},
"Timestamp" : {"type" : "date"},
"Value" : {"type" : "text"}
}
}
}`*/
//Gather configuration from environment variables
getEnvSettings()
dbClient := new(databus.DataBusClient)
for {
stompPort, _ := strconv.Atoi(configStrings["mbport"])
mb, err := stomp.NewStompMessageBus(configStrings["mbhost"], stompPort)
if err != nil {
log.Printf("Could not connect to message bus: %s", err)
time.Sleep(5 * time.Second)
} else {
log.Printf("Connected to message bus" )
dbClient.Bus = mb
defer mb.Close()
break
}
}
groupsIn := make(chan *databus.DataGroup, 10)
dbClient.Subscribe("/elkstack")
dbClient.Get("/elkstack")
go dbClient.GetGroup(groupsIn, "/elkstack")
//Initialize elasticsearch client
time.Sleep(15 * time.Second)
retryBackoff := backoff.NewExponentialBackOff()
es, err := elasticsearch.NewClient(elasticsearch.Config{
RetryOnStatus: []int{502, 503, 504, 429},
// Configure the backoff function
RetryBackoff: func(i int) time.Duration {
if i == 1 {
retryBackoff.Reset()
}
return retryBackoff.NextBackOff()
},
// Retry up to 5 attempts
MaxRetries: 5,
})
if err != nil {
log.Fatalf("Error creating the client: %s", err)
}
indexName := "poweredge_telemetry_metrics"
isSuccess := false
// wait for elastic search server to come up
for i:= 0; i< 10; i++{
res, err = es.Indices.Get([]string{indexName})
if err == nil{
res.Body.Close()
isSuccess = true
break
}
time.Sleep(30 * time.Second)
}
if !isSuccess{
log.Fatalf("ELK SErver is not up after 300 seconds")
}
log.Printf("ELK Server is up")
log.Printf("GET successful %s: %v", indexName,res)
// Re-create the index
res, err = es.Indices.Delete([]string{indexName})
if res, err = es.Indices.Delete([]string{indexName}); err != nil {
log.Fatalf("Cannot delete index:%s %s", indexName, err)
}
res.Body.Close()
// res, err = es.Indices.Create(indexName,
// es.Indices.Create.WithBody(strings.NewReader(mapping)))
res, err = es.Indices.Create(indexName)
if err != nil {
log.Fatalf("Cannot create index: %s", err)
}
if res.IsError() {
log.Fatalf("Cannot create index: %s", res)
}
res.Body.Close()
go handleGroups(groupsIn, es, indexName)
err = http.ListenAndServe(":8080", nil)
if err != nil {
log.Printf("Failed to start webserver %v", err)
}
}