簡體   English   中英

Python (Jupyter) -> Vega -> Kibana?

[英]Python (Jupyter) -> Vega -> Kibana?

因此,我們希望在我們的 ELK 數據上利用 Python 的數據科學和可視化優勢,然后使用 Elastic API 發送生成的可視化結果以更新儀表板。

有很多資源討論這個問題,其中只有一個實際提供了一個工作示例,但是他們利用了現在已棄用的安全繞過,更多的是黑客攻擊。 具體來說,這是指將索引名稱設置為.kibana ,現在會導致錯誤:

AuthorizationException: AuthorizationException(403, 'security_exception', 'action [indices:data/write/bulk[s]] is unauthorized for user [elastic] with roles [superuser] on indices [.kibana_8.1.2_001,.kibana], this action is granted by the index privileges [create_doc,create,delete,index,write,all]')

我們認為這一定可以通過正常的 API 使用而無需禁用任何安全設置。 我們確實嘗試添加一個用戶,並添加所有可能的權限,但在我們的測試期間它無法執行此操作。

這是引用的示例以及啟發我們嘗試這個的總體項目

在此處輸入圖像描述

請注意,Vega現在是 Kibana 的默認功能而不是插件,因此此工作流程現在應該更加可行。

所以我們的代碼是這樣的:

import eland as ed
import datetime
import altair as alt
import eland as ed
import json
import numpy as np
import matplotlib.pyplot as plt
import vega_datasets
from elasticsearch import Elasticsearch

cloud_id = "secret"
http_auth = ("username", "password")
es = Elasticsearch(cloud_id=cloud_id, http_auth=http_auth)

data = vega_datasets.data
pd_df = data.cars()
chart = alt.Chart(pd_df).mark_point().encode(
    x='Miles_per_Gallon',
    y='Horsepower'
).interactive()

def saveVegaVis(client, index, visName, altairChart, resultSize=100, timeField=True):
    chart_json = json.loads(altairChart.to_json())
    visState = {
      "type": "vega",
      "aggs": [],
      "params": {
        "spec": json.dumps(chart_json, sort_keys=True, indent=4, separators=(',', ': ')),
      },
      "title": visName
    }
    visSavedObject={
        "visualization" : {
          "title" : visName,
          "visState" : json.dumps(visState, sort_keys=True, indent=4, separators=(',', ': ')),
          "uiStateJSON" : "{}",
          "description" : "",
          "version" : 1,
          "kibanaSavedObjectMeta" : {
            "searchSourceJSON" : json.dumps({
              "query": {
                "language": "kuery",
                "query": ""
              },
              "filter": []
            }),
          }
        },
        "type" : "visualization",
        "references" : [ ],
        "migrationVersion" : {
          "visualization" : "8.0.0"
        },
        "updated_at" : datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")
    }


    return client.index(index=index,id='visualization:'+visName,body=visSavedObject)

saveVegaVis(es, 'test_visuals', 'def-vega-cars-1', chart, resultSize=1000)

執行此代碼后,我們會收到一條成功消息:

ObjectApiResponse({'_index': 'test_visuals', '_id': 'visualization:def-vega-cars-1', '_version': 8, 'result': 'updated', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 7, '_primary_term': 1})

但在 ELK 中,生成的 object 並未被視為可視化,而是被視為普通索引條目。

我們希望它顯示如下: 在此處輸入圖像描述

相反,我們只能將其視為普通索引條目,如下所示:

在此處輸入圖像描述

在我們看來,可視化的所有特征都在那里。 為了驗證這一點,我們導出了一個 Vega 可視化來觀察數據結構(請原諒任何奇怪的地方,導出留下了很多我們試圖清理的轉義字符):

{
    "attributes": {
        "description": "",
        "kibanaSavedObjectMeta": {
            "searchSourceJSON": {
                "query ":{
                    "query":"",
                    "language":"kuery"
                },
                "filter":[]
            }
        },
        "title": "TEST_VISUAL_PLZ_WORK",
        "uiStateJSON": "{}",
        "version": 1,
        "visState": {
        "title":"TEST_VISUAL_PLZ_WORK",
        "type":"vega",
        "aggs":[],
        "params":{
            "spec":" {
            "$schema": "https://vega.github.io/schema/vega/v3.json", n "width": 300, "height": 100, "data": [{
                n "name": "vals",
                n "values": [n {
                        "category": 50,
                        "count": 30
                    }, {
                        "category": 100,
                        "count": 80
                    }, {
                        "category": 150,
                        "count": 10
                    }, {
                        "category": 200,
                        "count": 50
                    }
                ]
            }], "marks": [{
                "type": "rect",
                "from": {
                    "data": "vals"
                },
                "encode": {
                    "update": {
                        "x": {
                            "field": "category"
                        },
                        "width": {
                            "value": 30
                        },
                        "y": {
                            "field": "count"
                        },
                        "y2": {
                            "value": 0
                        }
                    }
                }
            }]

        }
        "}}"
    },
    "coreMigrationVersion": "8.1.2",
    "id": "6e130cc0-b694-11ec-8df1-41f60ea92d87",
    "migrationVersion": {
        "visualization": "8.0.0"
    },
    "references": [],
    "type": "visualization",
    "updated_at": "2022-04-07T17:04:32.085Z",
    "version": "WzYxOTQsMl0="
} {
    "excludedObjects": [],
    "excludedObjectsCount": 0,
    "exportedCount": 1,
    "missingRefCount": 0,
    "missingReferences": []
}

看起來我們的數據結構與此匹配。

那么我們是不是走錯了路? 有什么小錯誤嗎? 是否可以這樣做(不禁用安全協議/“破解”它)?

查看示例,他們似乎將其放入 a.kibana 索引中,我知道這是與 kibana 相關的特殊索引。 您可能希望將索引更改為該索引,然后看看會發生什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM