如何将 Sumologic API 中的数据集成到 Power BI - 用户未找到错误

How to integrate data from Sumologic API into Power BI - user-not-found error

目标:

我的目标是使用 Sumologic API 从 Sumologic 仪表板获取数据并将其显示在 Power BI 的仪表板中。我首先检查 Git Bash 中的调用,然后将 URL s 输入 Power BI Desktop 以可视化数据。

我从我的 Sumologic UI 的 URL 末尾获得了破折号 ID。

我已经使用 PBI 桌面 UI“基本”通过 API 成功访问了 https://api.sumologic.com/api/v1/collectors?limit=10 的数据 身份验证设置,通过将 username 的访问 ID 和 password.

的访问密钥替换

(As instructed by the Power BI Documentation on APIs see first few steps. It works, even though this states it is for Azure not SumoLogic)

问题:

但是当我在我的 Git Bash 中为 GET 仪表板执行 url 命令时,

curl -u "(AccessID):(AccessKey)" -X GET "https://api.sumologic.com/api/v2/dashboards/(dashboardID)"

我得到这个 json 响应:

{
    "id":"10NLC-3CNIS-5QN8J",
    "errors":[
        {"code":"user:not_found",
         "message":"Object Not Found."}
    ]
 }

我也无法将此调用添加到 Power BI 中,可能是因为同样的错误。我能够添加第一个 URL https://api.sumologic.com/api/v1/collectors?limit=10 而无需提供任何额外的用户信息。

这个错误似乎可以确认 ID 是正确的(因为我以前收到的是“不正确的 ID”错误,而不是之前的 json 响应),但我不确定。 如何解决此 未找到用户 错误?

TLDR

/api/v2/dashboards/:dashboardId 端点需要与您在查看仪表板时在用户界面中看到的相同(种类)的仪表板 ID

长答案

如果我有这个:

然后我可以:

curl -u "$USER:$PASSWORD" -X GET "https://api.sumologic.com/api/v2/dashboards/PzClm5SSgWJqFSAusVkhPE5yMRU7NIrGZj6X0M28wMSgsnvmAc98cdvLiDwD"

并收到有意义的答案:

{"title":"Dashboard Sep 27, 2021 06:53:55","description":"","folderId":"0000000000D87ECD","topologyLabelMap":{"data":{}},"domain":"",
...

免责声明

我目前受雇于 Sumo Logic

将 Sumologic 集成到 Power BI 中的最佳方法是使用 python 执行此 curl 的脚本以从 Sumologic 的 SearchJob API.

获取所需的信息

第 1 步:制作 Python 脚本 替换

  • URL 与您的 sourceName
  • 从和到时间
  • 用你的查询查询
#   IMPORTS
import requests
import json
import base64
import time
import pandas as pd
import pytz
# from async import asyncio
from datetime import datetime, timedelta, date

now = datetime.now()
current_time = now.strftime("%Y-%m-%dT%H:%M:%S")

#   VARIABLES
dt = []   # DATA TARGET - List of Data values (Index, Timestamp, Message/Query, Count)
queryList = []   # LIST of Queries
index = 0   # DATA TARGET COUNT - INDEX NumberqueryList = []
n = 1   # SET minutes - n = number of Minutes/Hours 5 min AGO TIME - the "from" value  for Query:
messageCount = 0

#   Subtract Desired time n  from datetime object containing current time
five_min_ago = now - timedelta(minutes=n)   # Change time unit here if desired
past_time = five_min_ago.strftime("%Y-%m-%dT%H:%M:%S")

print("CURRENT TIME: ", current_time, " PAST TIME: ", past_time)

def makeCall(queryString):   #   F U N C T I O N   
    #   CREDENTIALS: 
    global messageCount
    usrPass = "******"
    encoded_u = base64.b64encode(usrPass.encode()).decode()
    url = "https://api.sumologic.com/api/v1/search/jobs"
    
    fullQuery = '_sourceCategory=prod/../* _sourceName=/apps/apps/logs/../trace.log ' + queryString + " | count" # Form Query with Common Query Parts
    print(fullQuery)
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Basic '+encoded_u,
        'Cookie': '****'
        }
    payload = json.dumps({
        "query": fullQuery, 
        "from": past_time, #   Format: 2021-10-06T11:40:05 -Minute ago
        "to": current_time, 
        "timeZone": "EST",
        "byReceiptTime": True
    })  

    #   MAKE REQUEST AND GET RESPONSE - (CALL #1):  -------------------------------------------------------------------------------------------------------
    response = requests.request("POST", url, headers=headers, data=payload)
    print("RESPONSE: ", response.text)

    #   CONTENT EXTRACTION:
    jobId = json.loads(response.text).get("id") # Convert Response (text) to json
    queryUrl = url + "/" + jobId # Make URL Query

    #   SPECIFIC SEARCH JOB CALL - (CALL #2): ---------------------------------------------------------------------------------------------------------------
    response = requests.request("GET", queryUrl, headers=headers, data=payload)
    state = json.loads(response.text).get("state")
    print(state)

    quitVal = False
    while quitVal == False :
        time.sleep(5) 
        resp = requests.request("GET", queryUrl, headers=headers, data=payload)
        state = json.loads(resp.text).get("state")
        print("...")
        if state == "DONE GATHERING RESULTS" :
            quitVal = True
            print(state)
            messageCount = json.loads(resp.text).get("messageCount")
            print(messageCount)
            

    #   GET DATA CALL - (CALL #3): ---------------------------------------------------------------------------------------------------------------------------

    #   Form Result URL:
    resultUrl = queryUrl + "/messages?offset=0&limit=10000"  #records?offset=0&limit=1
    results = requests.request("GET", resultUrl, headers=headers, data=payload)

    messages = json.loads(results.text).get("messages") # Access Data

    return messages

#                     End of   F U N C T I O N   

# QUERIES By CATEGORY ----------------------------------------

#    Query string you are searching for within the log:
Query1 = "... :Exception"


queryList.append(Query1)


print(queryList)   #   Print to Check  

# ------------------------------------------------------------

#   EXECUTE Calls on Each Query - SYNC. = SLOW
for query in queryList:   
    messages = makeCall(query)
    #print(messages)
    dt.append((index, past_time,current_time, query, messageCount))
    
    index = index + 1 #  Increment the Index of DT for next Query

#   CHECK dt:
for each in dt:
    print()
    print(each)

#   ADD data to DataFrame Format for Power BI:
df = pd.DataFrame(dt, columns=('Index', 'FromTime','ToTime', 'Message', 'Messagecount'))    
print(df)
 

步骤 2:将 Python 添加到 Power BI .这只需要你去添加数据。然后是selectpython脚本。然后将您的脚本复制并粘贴到文本输入框中。应该会在您刷新 pbi 破折号时自动计算。