1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
kibana_loader.go 1.96 KB
一键复制 编辑 原始数据 按行查看 历史
Andrew Kroh 提交于 2017-07-12 19:24 . Code cleanup
package dashboards
import (
"bytes"
"fmt"
"io/ioutil"
"net/url"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/setup/kibana"
)
var importAPI = "/api/kibana/dashboards/import"
type KibanaLoader struct {
client *kibana.Client
config *Config
version string
msgOutputter MessageOutputter
}
func NewKibanaLoader(cfg *common.Config, dashboardsConfig *Config, msgOutputter MessageOutputter) (*KibanaLoader, error) {
client, err := kibana.NewKibanaClient(cfg)
if err != nil {
return nil, fmt.Errorf("Error creating Kibana client: %v", err)
}
loader := KibanaLoader{
client: client,
config: dashboardsConfig,
version: client.GetVersion(),
msgOutputter: msgOutputter,
}
loader.statusMsg("Initialize the Kibana %s loader", client.GetVersion())
return &loader, nil
}
func (loader KibanaLoader) ImportIndex(file string) error {
params := url.Values{}
params.Set("force", "true") //overwrite the existing dashboards
// read json file
content, err := ioutil.ReadFile(file)
if err != nil {
return fmt.Errorf("fail to read index-pattern: %v", err)
}
return loader.client.ImportJSON(importAPI, params, bytes.NewBuffer(content))
}
func (loader KibanaLoader) ImportDashboard(file string) error {
params := url.Values{}
params.Set("force", "true") //overwrite the existing dashboards
params.Add("exclude", "index-pattern") //don't import the index pattern from the dashboards
// read json file
content, err := ioutil.ReadFile(file)
if err != nil {
return fmt.Errorf("fail to read index-pattern: %v", err)
}
return loader.client.ImportJSON(importAPI, params, bytes.NewBuffer(content))
}
func (loader KibanaLoader) Close() error {
return loader.client.Close()
}
func (loader KibanaLoader) statusMsg(msg string, a ...interface{}) {
if loader.msgOutputter != nil {
loader.msgOutputter(msg, a...)
} else {
logp.Debug("dashboards", msg, a...)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v6.0.0-beta1

搜索帮助