Kibana 自动化创建索引


Kibana基于api创建索引

  • 基于curl创建索引
    Kibana中还有空间的概念,如果需要基于空间创建的话调用以下接口
IP:5601/s/<空间名称>/api/saved_objects/index-pattern/my-index



Kibana正常场景下的索引创建

curl --user elastic:xxxxx -X POST IP:5601/api/saved_objects/index-pattern/${line} -H 'kbn-xsrf: true' -H 'Content-Type: application/json' -d '{ "attributes": { "title": "'"${line}-*"'" }}'



  • 删除索引
curl -X DELETE "IP:5601/api/saved_objects/index-pattern/twitter" -H 'kbn-xsrf: true'



python调用Api

# -*- coding: utf-8 -*-
import json
import requests
from requests.auth import HTTPBasicAuth
import sys

'''
lupiaoer spec名称  my-pattern 索引名称
url = 'http://127.0.0.1:5601/s/lupiaoer/api/saved_objects/index-pattern/my-pattern'
'''

kibana_info = {
	"dev":{"url":"127.0.0.1","user":"elastic","pass":"kibana"},
}


class KibanaApi:
    def __init__(self, systemenv, index ,spec='default'):
        '''
        :param systemenv: 环境
        :param index: 索引
        :param spec:  kibana空间
        '''
        self.index = index
        self.systemenv = systemenv
        self.spec = spec

    def create_index_pattern(self):
        headers = {'Content-Type': 'application/json', 'kbn-xsrf': 'true'}
        info = kibana_info.get(self.systemenv)
        if info == None:
            return {'code':'9999','msg':'%s环境不存在' % self.systemenv}

        if spec == 'default':
            url = 'http://{}:5601/api/saved_objects/index-pattern/{}'.format(info.get('url'),self.index)
        else:
            url = 'http://{}:5601/s/{}/api/saved_objects/index-pattern/{}'.format(info.get('url'),self.spec,self.index)
            
        data = {
            "attributes": {
                "title": "{}-*".format(self.index),
                "timeFieldName": 'time'
            }
        }
        user,password  = info.get('user'),info.get('pass')
        # print(user,password)
        ret = requests.post(url.format(my_pattern=self.index), auth=HTTPBasicAuth(user,password), data=json.dumps(data),
                            headers=headers)  # json.dumps(data)

        # print(ret.status_code)  # 409 already exists; 200 success
        # print(ret.json())
        if ret.status_code == 200:
            return {'code':'200','msg':'ok'}
        elif ret.status_code == 409:
            return {'code':'409','msg':'ok'}
        else:
            return ret.json()


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM