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
9 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"
|
||
9 years ago
|
"io/ioutil"
|
||
9 years ago
|
|
||
|
"github.com/Sirupsen/logrus"
|
||
|
"gopkg.in/olivere/elastic.v3"
|
||
|
)
|
||
|
|
||
|
// elasticQueue is a elasticsearch event notification queue.
|
||
9 years ago
|
type elasticSearchNotify struct {
|
||
9 years ago
|
Enable bool `json:"enable"`
|
||
|
URL string `json:"url"`
|
||
|
Index string `json:"index"`
|
||
|
}
|
||
|
|
||
|
type elasticClient struct {
|
||
|
*elastic.Client
|
||
9 years ago
|
params elasticSearchNotify
|
||
9 years ago
|
}
|
||
|
|
||
|
// Connects to elastic search instance at URL.
|
||
9 years ago
|
func dialElastic(esNotify elasticSearchNotify) (*elastic.Client, error) {
|
||
|
if !esNotify.Enable {
|
||
|
return nil, errNotifyNotEnabled
|
||
9 years ago
|
}
|
||
9 years ago
|
client, err := elastic.NewClient(elastic.SetURL(esNotify.URL), elastic.SetSniff(false))
|
||
9 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return client, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
func newElasticNotify(accountID string) (*logrus.Logger, error) {
|
||
|
esNotify := serverConfig.GetElasticSearchNotifyByID(accountID)
|
||
9 years ago
|
|
||
|
// Dial to elastic search.
|
||
9 years ago
|
client, err := dialElastic(esNotify)
|
||
9 years ago
|
if err != nil {
|
||
9 years ago
|
return nil, err
|
||
9 years ago
|
}
|
||
|
|
||
|
// Use the IndexExists service to check if a specified index exists.
|
||
9 years ago
|
exists, err := client.IndexExists(esNotify.Index).Do()
|
||
9 years ago
|
if err != nil {
|
||
9 years ago
|
return nil, err
|
||
9 years ago
|
}
|
||
|
// Index does not exist, attempt to create it.
|
||
|
if !exists {
|
||
|
var createIndex *elastic.IndicesCreateResult
|
||
9 years ago
|
createIndex, err = client.CreateIndex(esNotify.Index).Do()
|
||
9 years ago
|
if err != nil {
|
||
9 years ago
|
return nil, err
|
||
9 years ago
|
}
|
||
|
if !createIndex.Acknowledged {
|
||
9 years ago
|
return nil, errors.New("index not created")
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
elasticCl := elasticClient{
|
||
|
Client: client,
|
||
9 years ago
|
params: esNotify,
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
elasticSearchLog := logrus.New()
|
||
9 years ago
|
|
||
9 years ago
|
// Disable writing to console.
|
||
|
elasticSearchLog.Out = ioutil.Discard
|
||
9 years ago
|
|
||
9 years ago
|
// Add a elasticSearch hook.
|
||
|
elasticSearchLog.Hooks.Add(elasticCl)
|
||
9 years ago
|
|
||
9 years ago
|
// Set default JSON formatter.
|
||
|
elasticSearchLog.Formatter = new(logrus.JSONFormatter)
|
||
9 years ago
|
|
||
9 years ago
|
// Success, elastic search successfully initialized.
|
||
|
return elasticSearchLog, nil
|
||
9 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,
|
||
|
}
|
||
|
}
|