kibana API

时间:Aug. 17, 2020 分类:

目录:

创建索引模式

参考官方文档Create saved objects API

调用url

api/saved_objects/index-pattern/nginx_blog.whysdomain.com

POST请求参数

    request_body = {
        "attributes":{
            "title": "ulp-*:nginx_blog.whysdomain.com-*",
            "timeFieldName": "@timestamp",
            "fields": "[]"
        }
    }

响应

{"type":"index-pattern","id":"nginx_blog.whysdomain.com","attributes":{"fields":"[]","timeFieldName":"@timestamp","title":"ulp-*:nginx_blog.whysdomain.com-*"},"references":[],"migrationVersion":{"index-pattern":"7.6.0"},"updated_at
":"2020-08-12T04:01:37.371Z","version":"WzgzNzcsMV0="}

kibana在请求的时候,会确认一下这个索引模式是否存在,参考get方式

curl -X GET api/saved_objects/_find?type=index-pattern&search_fields=title&per_page=10&search=ulp-*:nginx_blog.whysdomain.com-*

根据响应结果的total判断是否存在

{"page":1,"per_page":10,"total":0,"saved_objects":[]}

或者直接带着?overwrite=true去进行post请求

创建可视化

还是先获取可视化是否存在

/api/saved_objects/_find?fields=title&per_page=10&search=%22m_test%22&search_fields=title&type=visualization

调用url

api/saved_objects/visualization

POST请求数据参考

饼图示例

{
    "attributes":{
        "title":"[blog.whysdomain.com] 请求接口状态统计",
        "visState":"{\"type\":\"pie\",\"aggs\":[{\"id\":\"1\",\"enabled\":true,\"type\":\"count\",\"schema\":\"metric\",\"params\":{}},{\"id\":\"2\",\"enabled\":true,\"type\":\"terms\",\"schema\":\"segment\",\"params\":{\"field\":\"remote_addr.keyword\",\"orderBy\":\"1\",\"order\":\"desc\",\"size\":15,\"otherBucket\":false,\"otherBucketLabel\":\"其他\",\"missingBucket\":false,\"missingBucketLabel\":\"缺失\"}}],\"params\":{\"type\":\"pie\",\"addTooltip\":true,\"addLegend\":true,\"legendPosition\":\"right\",\"isDonut\":true,\"labels\":{\"show\":false,\"values\":true,\"last_level\":true,\"truncate\":100}},\"title\":\"我就测试一下,[标题]\"}",
        "uiStateJSON":"{}",
        "description":"我就测试一下,描述",
        "version":1,
        "kibanaSavedObjectMeta":{
            "searchSourceJSON":"{\"query\":{\"query\":\"\",\"language\":\"kuery\"},\"filter\":[],\"indexRefName\":\"kibanaSavedObjectMeta.searchSourceJSON.index\"}"
        }
    },
    "references":[
        {
            "name":"kibanaSavedObjectMeta.searchSourceJSON.index",
            "type":"index-pattern",
            # 索引模式ID
            "id":"b0d7f3d0-adfb-11e9-97f2-5b019591fc39"
        }
    ]
}

注意

  • visState,uiStateJSON和searchSourceJSON都是字符串格式的json数据
  • remote_addr.keyword是用于切分的字段
  • 展示的title以title字段为准,而不是visState中的title

响应结果

{"type":"visualization","id":"1b35f220-df1c-11ea-9b9b-f9adbcdf6121","attributes":{"title":"[blog.whysdomain.com] 请求接口状态统计","visState":"{\"type\":\"pie\",\"aggs\":[{\"id\":\"1\",\"enabled\":true,\"type\":\"count\",\"schema\":\"metric\",\"params\":{}},{\"id\":\"2\",\"enabled\":true,\"type\":\"terms\",\"schema\":\"segment\",\"params\":{\"field\":\"remote_addr.keyword\",\"orderBy\":\"1\",\"order\":\"desc\",\"size\":15,\"otherBucket\":false,\"otherBucketLabel\":\"其他\",\"missingBucket\":false,\"missingBucketLabel\":\"缺失\"}}],\"params\":{\"type\":\"pie\",\"addTooltip\":true,\"addLegend\":true,\"legendPosition\":\"right\",\"isDonut\":true,\"labels\":{\"show\":false,\"values\":true,\"last_level\":true,\"truncate\":100}},\"title\":\"我就测试一下,[标题]\"}","uiStateJSON":"{}","description":"我就测试一下,描述","version":1,"kibanaSavedObjectMeta":{"searchSourceJSON":"{\"query\":{\"query\":\"\",\"language\":\"kuery\"},\"filter\":[],\"indexRefName\":\"kibanaSavedObjectMeta.searchSourceJSON.index\"}"}},"references":[{"name":"kibanaSavedObjectMeta.searchSourceJSON.index","type":"index-pattern","id":"b0d7f3d0-adfb-11e9-97f2-5b019591fc39"}],"migrationVersion":{"visualization":"7.7.0"},"updated_at":"2020-08-15T17:23:59.682Z","version":"WzExNTE1LDFd"}

Timelion

{
    "attributes":{
        "title":"[blog.whysdomain.com] 流量天级同比图",
        "visState":"{\"type\":\"timelion\",\"aggs\":[],\"params\":{\"expression\":\".es(index=ulp-*:nginx_blog.whysdomain.com-*,timefield=@timestamp).label('Current').title(Traffic).color(#1E90FF), \\n.es(offset=-1d,index=ulp-*:nginx_blog.whysdomain.com-*,timefield=@timestamp).label('Yesterday').lines(fill=1,width=0.5).color(pink)\",\"interval\":\"1m\"},\"title\":\"[blog.whysdomain.com] 流量天级同比图\"}",
        "uiStateJSON":"{}",
        "description":"",
        "version":1,
        "kibanaSavedObjectMeta":{
            "searchSourceJSON":"{\"query\":{\"query\":\"\",\"language\":\"lucene\"},\"filter\":[]}"
        }
    },
    "references":[]
}

没有指定索引模式的

响应

{"type":"visualization","id":"ab3058a0-dfd2-11ea-9b9b-f9adbcdf6121","attributes":{"title":"[blog.whysdomain.com] 流量天级同比图","visState":"{\"type\":\"timelion\",\"aggs\":[],\"params\":{\"expression\":\".es(index=ulp-*:nginx_blog.whysdomain.com-*,timefield=@timestamp).label('Current').title(Traffic).color(#1E90FF), \\n.es(offset=-1d,index=ulp-*:nginx_blog.whysdomain.com-*,timefield=@timestamp).label('Yesterday').lines(fill=1,width=0.5).color(pink)\",\"interval\":\"1m\"},\"title\":\"[blog.whysdomain.com] 流量天级同比图\"}","uiStateJSON":"{}","description":"","version":1,"kibanaSavedObjectMeta":{"searchSourceJSON":"{\"query\":{\"query\":\"\",\"language\":\"lucene\"},\"filter\":[]}"}},"references":[],"migrationVersion":{"visualization":"7.7.0"},"updated_at":"2020-08-16T15:10:49.642Z","version":"WzExNjAyLDFd"}

其他未完待续

创建仪表盘

调用url

api/saved_objects/dashboard

POST请求数据参考

{
    "attributes":{
        "title":"[blog.whysdomain.com] 网关Dashboard",
        "hits":0,
        "description":"blog.whysdomain.com网关nginx仪表盘",
        "panelsJSON":"[{\"version\":\"7.7.0\",\"gridData\":{\"w\":24,\"h\":15,\"x\":0,\"y\":0,\"i\":\"602a8120-5aaa-4ea2-a39e-21f2b51b6927\"},\"panelIndex\":\"602a8120-5aaa-4ea2-a39e-21f2b51b6927\",\"embeddableConfig\":{},\"panelRefName\":\"panel_0\"},{\"version\":\"7.7.0\",\"gridData\":{\"w\":24,\"h\":15,\"x\":24,\"y\":0,\"i\":\"0a034d92-eb90-43c8-a227-38f5ea6d3ecd\"},\"panelIndex\":\"0a034d92-eb90-43c8-a227-38f5ea6d3ecd\",\"embeddableConfig\":{},\"panelRefName\":\"panel_1\"}]",
        "optionsJSON":"{\"useMargins\":true,\"hidePanelTitles\":false}",
        "version":1,
        # 这里false,如果在python里是False
        "timeRestore":false,
        "kibanaSavedObjectMeta":{
            "searchSourceJSON":"{\"query\":{\"query\":\"\",\"language\":\"kuery\"},\"filter\":[]}"
        }
    },  
    "references":[  
        {
            "name":"panel_0",
            "type":"visualization",
            "id":"439e0540-935a-11e9-9d17-3376c483360c"
        },
       {
            "name":"panel_1",
            "type":"visualization",
            "id":"68a6a710-935b-11e9-9d17-3376c483360c"
       }
    ]
}

这里gridData的数据

  • w 可视化的横向长度
  • h 可视化的竖向长度
  • x 可视化的左上顶点与整个仪表盘的左上顶点的横向距离
  • y 可视化的左上顶点与整个仪表盘的左上顶点的竖向距离
  • i 对应的索引

响应结果

{"type":"dashboard","id":"4a679b70-dfda-11ea-9b9b-f9adbcdf6121","attributes":{"title":"[blog.whysdomain.com] 网关Dashboard","hits":0,"description":"blog.whysdomain.com网关nginx仪表盘","panelsJSON":"[{\"version\":\"7.7.0\",\"gridData\":{\"w\":40,\"h\":40,\"x\":0,\"y\":40,\"i\":\"602a8120-5aaa-4ea2-a39e-21f2b51b6927\"},\"panelIndex\":\"602a8120-5aaa-4ea2-a39e-21f2b51b6927\",\"embeddableConfig\":{},\"panelRefName\":\"panel_0\"},{\"version\":\"7.7.0\",\"gridData\":{\"w\":42,\"h\":355,\"x\":0,\"y\":0,\"i\":\"0a034d92-eb90-43c8-a227-38f5ea6d3ecd\"},\"panelIndex\":\"0a034d92-eb90-43c8-a227-38f5ea6d3ecd\",\"embeddableConfig\":{},\"panelRefName\":\"panel_1\"}]","optionsJSON":"{\"useMargins\":true,\"hidePanelTitles\":false}","version":1,"timeRestore":false,"kibanaSavedObjectMeta":{"searchSourceJSON":"{\"query\":{\"query\":\"\",\"language\":\"kuery\"},\"filter\":[]}"}},"references":[{"name":"panel_0","type":"visualization","id":"439e0540-935a-11e9-9d17-3376c483360c"},{"name":"panel_1","type":"visualization","id":"68a6a710-935b-11e9-9d17-3376c483360c"}],"migrationVersion":{"dashboard":"7.3.0"},"updated_at":"2020-08-16T16:05:23.239Z","version":"WzExNjQyLDFd"}