1. 程式人生 > >通過elasticsearch批量建立kibana的visualize圖表及dashboard

通過elasticsearch批量建立kibana的visualize圖表及dashboard

kibana中展示的visualize圖表及dashboard,是儲存在Elasticsearch的kibana-int索引的dashboard型別中的(以kibana3來說明)。

下面獲取一儲存的一個dashboard

GET http://localhost:9200/kibana-int/dashboard/skytone.optlog

{
    "_index": "kibana-int",
    "_type": "dashboard",
    "_id": "skytone.optlog",
    "_version": 11,
    "found": true,
    "_source"
: { "user": "guest", "group": "guest", "title": "skytone.optlog", "dashboard": "xxx" } }

其中,dashboard欄位中儲存著具體的kibana中展示的具體的內容,可以參照著kibana的介面設定,很容易明白其實資訊的含義的:

{
    "title": "skytone.optlog",
    "services": {
        "query": {
            "list": {
                "0
": { "query": "*", "alias": "", "color": "#7EB26D", "id": 0, "pin": false, "type": "lucene", "enable": true } }
, "ids": [ 0
]
}
, "filter": { "list": { "0": { "type": "time", "field": "@timestamp", "from": "now-24h", "to": "now", "mandate": "must", "active": true, "alias": "", "id": 0 }, "1": { "type": "querystring", "query": "_type=skytone_optlog", "mandate": "must", "active": true, "alias": "", "id": 1 } }, "ids": [ 0, 1 ] } }
, "rows": [ { "title": "Graph", "height": "350px", "editable": true, "collapse": true, "collapsable": true, "panels": [ { "span": 12, "editable": true, "group": [ "default" ], "type": "histogram", "mode": "count", "time_field": "@timestamp", "value_field": null, "auto_int": true, "resolution": 100, "interval": "10m", "fill": 3, "linewidth": 3, "timezone": "browser", "spyable": true, "zoomlinks": true, "bars": true, "stack": true, "points": false, "lines": false, "legend": true, "x-axis": true, "y-axis": true, "percentage": false, "interactive": true, "queries": { "mode": "all", "ids": [ 0 ] }, "title": "Events over time", "intervals": [ "auto", "1s", "1m", "5m", "10m", "30m", "1h", "3h", "12h", "1d", "1w", "1M", "1y" ], "options": true, "tooltip": { "value_type": "cumulative", "query_as_alias": true }, "scale": 1, "y_format": "none", "grid": { "max": null, "min": 0 }, "annotate": { "enable": false, "query": "*", "size": 20, "field": "_type", "sort": [ "_score", "desc" ] }, "pointradius": 5, "show_query": true, "legend_counts": true, "zerofill": true, "derivative": false } ], "notice": false }, { "title": "Events", "height": "350px", "editable": true, "collapse": false, "collapsable": true, "panels": [ { "title": "All events", "error": false, "span": 12, "editable": true, "group": [ "default" ], "type": "table", "size": 100, "pages": 5, "offset": 0, "sort": [ "optid", "desc" ], "style": { "font-size": "9pt" }, "overflow": "min-height", "fields": [ "optime", "optid", "opcode", "cardno", "callno", "result_code", "jsonreq", "jsonrep", "extras" ], "localTime": true, "timeField": "@timestamp", "highlight": [], "sortable": true, "header": true, "paging": true, "spyable": true, "queries": { "mode": "all", "ids": [ 0 ] }, "field_list": false, "status": "Stable", "trimFactor": 300, "normTimes": true, "all_fields": false } ], "notice": false } ], "editable": true, "failover": false, "index": { "interval": "day", "pattern": "[logstash-]YYYY.MM.DD", "default": "NO_TIME_FILTER_OR_INDEX_PATTERN_NOT_MATCHED", "warm_fields": true }, "style": "light", "panel_hints": true, "pulldowns": [ { "type": "query", "collapse": false, "notice": false, "query": "*", "pinned": true, "history": [ "*", "optid=9157" ], "remember": 10, "enable": true }, { "type": "filtering", "collapse": true, "notice": false, "enable": true } ], "nav": [ { "type": "timepicker", "collapse": false, "notice": false, "status": "Stable", "time_options": [ "5m", "15m", "1h", "6h", "12h", "24h", "2d", "7d", "30d" ], "refresh_intervals": [ "5s", "10s", "30s", "1m", "5m", "15m", "30m", "1h", "2h", "1d" ], "timefield": "@timestamp", "now": true, "filter_id": 0, "enable": true } ], "loader": { "save_gist": false, "save_elasticsearch": true, "save_local": true, "save_default": true, "save_temp": true, "save_temp_ttl_enable": true, "save_temp_ttl": "30d", "load_gist": true, "load_elasticsearch": true, "load_elasticsearch_size": 20, "load_local": true, "hide": false }, "refresh": "5m" }

因此,如果有大量的型別的dashboard需要新增的話,可以用工具或指令碼呼叫http rest api來向kibana-int/dashboard中新增上面格式的文件就可以了。

下面增加一個新的dashboard,skytone.optlog2

POST  http://localhost:9200/kibana-int/dashboard/skytone.optlog2
{
    "user": "guest",
    "group": "guest",
    "title": "skytone.optlog2",
    "dashboard": "XXXX"  //就是上面GET出來的內容,見於篇副省略了
}