-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticsearch.go
194 lines (163 loc) · 3.94 KB
/
elasticsearch.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
package unifi
import (
"bytes"
"context"
"encoding/json"
"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
"log"
"sync"
"time"
)
var (
wg sync.WaitGroup
)
type Document struct {
Time time.Time `json:"@timestamp"`
System System `json:"system"`
ECS ECS `json:"ecs"`
Event Event `json:"event"`
Host Host `json:"host"`
Agent Agent `json:"agent"`
Service Service `json:"service"`
}
type Event struct {
Module string `json:"module"`
Dataset string `json:"dataset"`
}
type ECS struct {
Version string `json:"version"`
}
type Network struct {
Name string `json:"name"`
In NetworkCounters `json:"in"`
Out NetworkCounters `json:"out"`
}
type NetworkCounters struct {
Errors int64 `json:"errors"`
Dropped int64 `json:"dropped"`
Packets int64 `json:"packets"`
Bytes int64 `json:"bytes"`
}
type Host struct {
Name string `json:"name"`
Hostname string `json:"hostname"`
Architecture string `json:"architecture"`
OS OS `json:"os"`
ID string `json:"id"`
IP string `json:"ip"`
MAC string `json:"mac"`
Uptime int64 `json:"uptime"`
Type string `json:"type"`
}
type OS struct {
Platform string `json:"platform"`
Name string `json:"name"`
Family string `json:"family"`
Version string `json:"version"`
Kernel string `json:"kernel"`
Build string `json:"build"`
}
type CPU struct {
Steal Percentage `json:"steal"`
Total Percentage `json:"total"`
Cores int64 `json:"cores"`
System Percentage `json:"system"`
Nice Percentage `json:"nice"`
Irq Percentage `json:"irq"`
Idle Percentage `json:"idle"`
User Percentage `json:"user"`
IOWait Percentage `json:"iowait"`
Softirq Percentage `json:"softirq"`
}
type Percentage struct {
Percentage float64 `json:"pct"`
}
type Load struct {
Cores int64 `json:"cores"`
Normalized Intervals `json:"norm"`
Intervals
}
type Intervals struct {
OneMinute float64 `json:"1"`
FiveMinute float64 `json:"5"`
FifteenMinute float64 `json:"15"`
}
type System struct {
CPU *CPU `json:"cpu,omitempty"`
Load *Load `json:"load,omitempty"`
Memory *Memory `json:"memory,omitempty"`
Network *Network `json:"network,omitempty"`
}
type Memory struct {
Actual MemoryActual `json:"actual"`
Free int64 `json:"free"`
Swap MemorySwap `json:"swap"`
Total int64 `json:"total"`
Used MemoryUsed `json:"used"`
}
type MemoryUsed struct {
Bytes int64 `json:"bytes"`
Percentage float64 `json:"pct"`
}
type MemoryActual struct {
Free int64 `json:"free"`
Used MemoryUsed `json:"used"`
}
type MemorySwap struct {
Free int64 `json:"free"`
Total int64 `json:"total"`
Used MemoryUsed `json:"used"`
}
type Agent struct {
Hostname string `json:"hostname"`
Version string `json:"version"`
Type string `json:"type"`
}
type Service struct {
Type string `json:"type"`
}
func Publish(es *elasticsearch.Client, docs []Document) {
index := "metricbeat-7.6.2-2020.04.22-000001" // TODO: Create custom index and mappings
for _, doc := range docs {
wg.Add(1)
go func(doc Document) {
defer wg.Done()
data, err := json.Marshal(doc)
if err != nil {
log.Println("Cannot marshal doc to JSON", doc, err)
return
}
req := esapi.IndexRequest{
Index: index,
Body: bytes.NewReader(data),
Refresh: "true",
}
res, err := req.Do(context.Background(), es)
if err != nil {
log.Println("Index request failed", err)
return
}
defer res.Body.Close()
if res.IsError() {
log.Println(res.Status())
return
}
}(doc)
}
wg.Wait()
}
func NewClient(host string, user string, pass string) (*elasticsearch.Client, error) {
cfg := elasticsearch.Config{
Addresses: []string{
host,
},
Username: user,
Password: pass,
}
es, err := elasticsearch.NewClient(cfg)
if err != nil {
return nil, err
}
return es, nil
}