-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
222 lines (182 loc) · 4.5 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
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
// Fetch ElasticSearch cluster status and performance data and store it
// into Graphite.
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"log/syslog"
"net"
"net/http"
"time"
)
import _ "expvar"
// DefaultTimeout is connection timeout, in seconds
const DefaultTimeout = 5
var (
elasticHost = flag.String("eh", "", "IP, FQDN or hostname of your ElasticSearch host")
elasticPort = flag.Int("ep", 9200, "ElasticSearch hosts port")
graphiteHost = flag.String("gh", "", "IP, FQDN or hostname of your Carbon host")
graphitePort = flag.Int("gp", 2003, "Carbon hosts port")
graphiteDb = flag.String("gd", "elasticsearch.cluster", "Graphite database name")
pollPeriod = flag.Duration("poll", 20*time.Second, "Metrics poll interval")
)
var (
// elasticData is JSON response from ElasticSearch host
elasticData chan interface{}
// graphiteData is transformed data into valid Carbon metrics
graphiteData chan string
)
// Server describes how service should be configured
type Server struct {
// Shared syslog between methods for this receiver
Syslog *syslog.Writer
// Poll interval for fetching ES cluster data and storing it into Graphite
period time.Duration
// Stats URL of ES node
elasticURL string
// Carbon host
graphite string
// Graphite database name
graphiteDb string
// Connection timeout
timeout time.Duration
}
func init() {
flag.Parse()
}
func main() {
elasticData = make(chan interface{})
graphiteData = make(chan string)
s := NewServer()
s.run()
}
func NewServer() *Server {
s := &Server{
elasticURL: fmt.Sprintf("http://%s:%d/_cluster/health", *elasticHost, *elasticPort),
graphite: fmt.Sprintf("%s:%d", *graphiteHost, *graphitePort),
graphiteDb: *graphiteDb,
period: *pollPeriod,
timeout: DefaultTimeout * time.Second,
}
s.setupSyslog()
fmt.Printf("ElasticSearch cluster health URL: %s\n", s.elasticURL)
fmt.Printf("Poll period: %s\n", s.period)
return s
}
// Setups syslog
func (s *Server) setupSyslog() {
var err error
s.Syslog, err = syslog.New(syslog.LOG_ERR, "esmetrics")
defer s.Syslog.Close()
if err != nil {
log.Fatal("Could not establish connection to the system log daemon")
}
}
// Run our service
func (s *Server) run() {
for {
go s.fetchElasticData()
data := <-elasticData
if data != nil {
go s.createGraphiteData(data)
go s.sendToGraphite(<-graphiteData)
}
time.Sleep(s.period)
}
}
// Fetches data from ElasticSearch host and sends it
// to a channel. If there is a error nil will be sent to
// channel.
func (s *Server) fetchElasticData() {
var (
resp *http.Response
body []byte
jsonData interface{}
err error
)
resp, err = http.Get(s.elasticURL)
if err != nil {
s.Syslog.Err("Could not connect to ElasticSearch server")
elasticData <- nil
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
s.Syslog.Warning("HTTP response code was not 200: " + resp.Status)
elasticData <- nil
return
}
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
s.Syslog.Warning(err.Error())
elasticData <- nil
return
}
jsonData, err = decodeJSON(body)
if err != nil {
s.Syslog.Warning("Could not decode JSON data: " + err.Error())
elasticData <- nil
return
}
elasticData <- jsonData
}
// Creates valid Carbon metrics, in following format:
// "<graphite_database >.<metric> <value> <timestamp> \n"
func (s *Server) createGraphiteData(data interface{}) {
t := time.Now()
var tmp string
for k, v := range data.(map[string]interface{}) {
if k == "cluster_name" {
v = 0
} else if k == "timed_out" {
if v == true {
v = 1
} else {
v = 0
}
} else if k == "cluster_status" || k == "status" {
if v == "green" {
v = 0
}
if v == "yellow" {
v = 1
}
if v == "red" {
v = 2
}
}
tmp += fmt.Sprintf("%s.%s %v %d\n", s.graphiteDb, k, v, t.Unix())
}
graphiteData <- tmp
}
// Connects to Carbon host and sends data to it
func (s *Server) sendToGraphite(data string) {
var (
conn net.Conn
err error
)
conn, err = net.DialTimeout("tcp", s.graphite, s.timeout)
if err != nil {
s.Syslog.Warning("Could not connect to Carbon server")
return
}
defer conn.Close()
buf := bytes.NewBufferString(data)
_, err = conn.Write(buf.Bytes())
if err != nil {
s.Syslog.Warning("Could not send data to Carbon server: " + err.Error())
}
}
// Decodes JSON data
func decodeJSON(jsonBlob []byte) (interface{}, error) {
var r interface{}
err := json.Unmarshal(jsonBlob, &r)
if err != nil {
return nil, err
}
return r, nil
}