You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
2.7 KiB
113 lines
2.7 KiB
8 years ago
|
/*
|
||
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
||
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
8 years ago
|
"io/ioutil"
|
||
8 years ago
|
|
||
|
"github.com/Sirupsen/logrus"
|
||
|
"gopkg.in/olivere/elastic.v3"
|
||
|
)
|
||
|
|
||
|
// elasticQueue is a elasticsearch event notification queue.
|
||
8 years ago
|
type elasticSearchNotify struct {
|
||
8 years ago
|
Enable bool `json:"enable"`
|
||
|
URL string `json:"url"`
|
||
|
Index string `json:"index"`
|
||
|
}
|
||
|
|
||
|
type elasticClient struct {
|
||
|
*elastic.Client
|
||
8 years ago
|
params elasticSearchNotify
|
||
8 years ago
|
}
|
||
|
|
||
|
// Connects to elastic search instance at URL.
|
||
8 years ago
|
func dialElastic(esNotify elasticSearchNotify) (*elastic.Client, error) {
|
||
|
if !esNotify.Enable {
|
||
|
return nil, errNotifyNotEnabled
|
||
8 years ago
|
}
|
||
8 years ago
|
client, err := elastic.NewClient(elastic.SetURL(esNotify.URL), elastic.SetSniff(false))
|
||
8 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return client, nil
|
||
|
}
|
||
|
|
||
8 years ago
|
func newElasticNotify(accountID string) (*logrus.Logger, error) {
|
||
|
esNotify := serverConfig.GetElasticSearchNotifyByID(accountID)
|
||
8 years ago
|
|
||
|
// Dial to elastic search.
|
||
8 years ago
|
client, err := dialElastic(esNotify)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
return nil, err
|
||
8 years ago
|
}
|
||
|
|
||
|
// Use the IndexExists service to check if a specified index exists.
|
||
8 years ago
|
exists, err := client.IndexExists(esNotify.Index).Do()
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
return nil, err
|
||
8 years ago
|
}
|
||
|
// Index does not exist, attempt to create it.
|
||
|
if !exists {
|
||
|
var createIndex *elastic.IndicesCreateResult
|
||
8 years ago
|
createIndex, err = client.CreateIndex(esNotify.Index).Do()
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
return nil, err
|
||
8 years ago
|
}
|
||
|
if !createIndex.Acknowledged {
|
||
8 years ago
|
return nil, errors.New("index not created")
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
elasticCl := elasticClient{
|
||
|
Client: client,
|
||
8 years ago
|
params: esNotify,
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
elasticSearchLog := logrus.New()
|
||
8 years ago
|
|
||
8 years ago
|
// Disable writing to console.
|
||
|
elasticSearchLog.Out = ioutil.Discard
|
||
8 years ago
|
|
||
8 years ago
|
// Add a elasticSearch hook.
|
||
|
elasticSearchLog.Hooks.Add(elasticCl)
|
||
8 years ago
|
|
||
8 years ago
|
// Set default JSON formatter.
|
||
|
elasticSearchLog.Formatter = new(logrus.JSONFormatter)
|
||
8 years ago
|
|
||
8 years ago
|
// Success, elastic search successfully initialized.
|
||
|
return elasticSearchLog, nil
|
||
8 years ago
|
}
|
||
|
|
||
|
// Fire is required to implement logrus hook
|
||
|
func (q elasticClient) Fire(entry *logrus.Entry) error {
|
||
|
_, err := q.Client.Index().Index(q.params.Index).
|
||
|
Type("event").
|
||
|
BodyJson(entry.Data).
|
||
|
Do()
|
||
|
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Required for logrus hook implementation
|
||
|
func (q elasticClient) Levels() []logrus.Level {
|
||
|
return []logrus.Level{
|
||
|
logrus.InfoLevel,
|
||
|
}
|
||
|
}
|