This repository has been archived by the owner on Dec 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elastic.go
127 lines (104 loc) · 2.6 KB
/
elastic.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
package main
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
vault "github.com/sosedoff/ansible-vault-go"
)
func GetPassword() string {
file, err := vault.DecryptFile("./conf/secrets.json", ".lab.")
if err != nil {
fmt.Printf("error: %v", err)
}
return file
}
func loadVars() {
file, err := os.Open("conf/vars.json")
if err != nil {
fmt.Printf("error: %v", err)
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&configuration)
if err != nil {
fmt.Printf("error: %v", err)
}
}
func loadSecrets() {
getSecrets := json.NewDecoder(strings.NewReader(GetPassword()))
err := getSecrets.Decode(&secrets)
if err != nil {
fmt.Printf("error: %v", err)
}
}
func loadESconfig() elasticsearch.Config {
transport := http.DefaultTransport
tlsClientConfig := &tls.Config{InsecureSkipVerify: true}
transport.(*http.Transport).TLSClientConfig = tlsClientConfig
cfg := elasticsearch.Config{
Addresses: []string{
configuration.EShost,
},
Username: secrets.USERelastic,
Password: secrets.PWDelastic,
}
return cfg
}
func toLogstash() {
}
func toElastic(doc elasticStruct) {
metric, _ := json.MarshalIndent(doc, "", " ")
fmt.Printf("%s\n", metric)
cfg := loadESconfig()
es, err := elasticsearch.NewClient(cfg)
if err != nil {
fmt.Printf("Error en la conexion contra elasticsearch: %s\n", err)
}
currentTime := time.Now()
fecha := currentTime.Format("2006.01.02")
index := "ibmmq.v1." + fecha
req := esapi.IndexRequest{
Index: index,
Body: strings.NewReader(string(metric)),
Refresh: "true",
}
res, err := req.Do(context.Background(), es)
if err != nil {
fmt.Printf("Error getting response: %s", err)
}
res.Body.Close()
}
type elasticStruct struct {
TimeStamp string `json:"timeStamp"`
Epoch int64 `json:"epoch"`
ObjectType string `json:"objectType"`
Tags map[string]string `json:"tags"`
Metric map[string]float64 `json:"metrics"`
}
func createJSON(j jsonReportStruct) {
var doc elasticStruct = elasticStruct{}
doc.TimeStamp = j.CollectionTime.TimeStamp
doc.Epoch = j.CollectionTime.Epoch
for _, value := range j.Points {
tags := make(map[string]string)
metrics := make(map[string]float64)
for tag, value := range value.Tags {
tags[tag] = value
}
for metric, value := range value.Metric {
metrics[metric] = value
}
doc.ObjectType = value.ObjectType
doc.Tags = tags
doc.Metric = metrics
if configuration.SendTO == "elastic" {
go toElastic(doc)
}
}
}