gpt4 book ai didi

python - Azure 日志分析 : How to send custom python pandas DataFrames into LAW

转载 作者:行者123 更新时间:2023-12-03 06:06:29 27 4
gpt4 key购买 nike

我有一个 Python 脚本,我想每天运行它来生成 Pandas 数据帧。

我希望每天将此数据框添加到我的日志分析工作区。

我有一个 Windows 服务器,可以用来运行我的 Python 脚本。

我需要做什么才能使这项工作成功?有没有办法从 DataFrames 推送到 Syslog 服务器?

最佳答案

Azure Log Analytics: How to send custom python pandas DataFrames into LAW

我已经在我的环境中重现了,以下是我的预期结果:

我从 Microsoft-Document 获取了以下代码并进行了一些修改:

import json
import requests
import datetime
import hashlib
import hmac
import base64

logtype2="RithwikLogs1"
def build_signature(customer_id, shared_key, date, content_length, method, content_type, resource):
x_headers = 'x-ms-date:' + date
string_to_hash = method + "\n" + str(content_length) + "\n" + content_type + "\n" + x_headers + "\n" + resource
bytes_to_hash = bytes(string_to_hash, encoding="utf-8")
decoded_key = base64.b64decode(shared_key)
encoded_hash = base64.b64encode(hmac.new(decoded_key, bytes_to_hash, digestmod=hashlib.sha256).digest()).decode()
authorization = "SharedKey {}:{}".format(customer_id,encoded_hash)
return authorization

def post_data1(customer_id, shared_key, body, log_type):
method = 'POST'
content_type = 'application/json'
resource = '/api/logs'
rfc1123date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
content_length = len(body)
signature = build_signature(customer_id, shared_key, rfc1123date, content_length, method, content_type, resource)
uri = 'https://' + customer_id + '.ods.opinsights.azure.com' + resource + '?api-version=2016-04-01'

headers = {
'content-type': content_type,
'Authorization': signature,
'Log-Type': log_type,
'x-ms-date': rfc1123date
}

response = requests.post(uri,data=body, headers=headers)
if (response.status_code >= 200 and response.status_code <= 299):
print('Rithwik Data Frame is sent to Log Analytics Worksapce ')
else:
print("Response code: {}".format(response.status_code))
rithwik_data1 = pd.DataFrame({
'Name': ['Rithwik', 'Bojja', 'Chotu'],
'Age': [23, 23, 20]
})

post_data1('310603f7', 'a3m2jEErIT6HONEdrAhgIGBrT9L78AK0wk0H8HJKkEdTva4nmw==',rithwik_data1.to_json(orient="records")
, logtype2)

输出:

enter image description here

enter image description here

enter image description here

运行Python代码后,Log Analytics中需要一些时间才能生成日志(等待大约5-10分钟)。

注意:

在函数调用中:

post_data1('yourworkspaceid', 'Primarykey(sharedkey)',body(json of df), logtype2)

您可以通过以下方式获取上述值:

enter image description here

关于python - Azure 日志分析 : How to send custom python pandas DataFrames into LAW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77263051/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com