【问题标题】:How To run Azure Log Analytics query api, Using python?如何使用 python 运行 Azure Log Analytics 查询 api?
【发布时间】:2020-09-10 08:42:31
【问题描述】:

我一直在尝试使用 python 运行日志分析查询,它在以下查询中运行

os.system("curl -X POST \'https://api.loganalytics.io/v1/workspaces/0000000000000000000000-0000-c05f0ccc198d/query\' -d \'{\"query\": \"ContainerLog\",\"timespan\": \"PT12H\"}\' -H \'Authorization:Bearer"+auth+" \' -H \'Content-Type: application/json\'")

但它不适用于以下查询

os.system("curl -X POST \'https://api.loganalytics.io/v1/workspaces/0000000000000000000000-c05f0ccc198d/query\' -d \'{\"query\": \"search in (ContainerLog) \"error\" | where TimeGenerated > datetime(\"2020-09-09\") | project LogEntry\"}\' -H \'Authorization:Bearer "+auth+"\' -H \'Content-Type: application/json\'")

错误:

{"error":{"message":"请求有一些无效的属性","code":"BadArgumentError","correlationId":"3cf07489-2a00-4ef6-9c00-15bd7d1be648","details": [{"code":"InvalidJsonBody","message":"JSON 中第 38 位的意外标记 e","target":null}],"innererror":{"code":"QueryValidationError","message": "解析查询失败"}}}

【问题讨论】:

    标签: json python-3.x azure azure-devops azure-api-management


    【解决方案1】:

    \"error\"\"2020-09-09\" 应该使用单引号而不是双引号。

    例如,使用\'error\' 代替\"error\";使用datetime(\'2020-09-09\') 而不是datetime(\"2020-09-09\")

    查询如下所示:

    os.system("curl -X POST \'https://api.loganalytics.io/v1/workspaces/0000000000000000000000-c05f0ccc198d/query\' -d \'{\"query\": \"search in (ContainerLog) \'error\' | where TimeGenerated > datetime(\'2020-09-09\') | project LogEntry\"}\' -H \'Authorization:Bearer "+auth+"\' -H \'Content-Type: application/json\'")
    

    【讨论】:

    • {"error":{"message":"请求有一些无效属性","code":"BadArgumentError","correlationId":"086c0ed6-3058-4dc1-95b6-9c22a2db1044" ,"innererror":{"code":"SemanticError","message":"发生语义错误。","innererror":{"code":"SEM0100","message":"'where' 运算符:失败解析名为'error'的标量表达式"}}}}
    • @pappu.jerin,请尝试在page中使用此查询search in (ContainerLog) 'error' | where TimeGenerated > datetime('2020-09-09') | project LogEntry,看看是否仍然出现错误。
    • 请记住在“Workspace ID”和“API Key”字段中填写您的真实值。
    【解决方案2】:
       use the below code 
    
        import requests
        url = "https://api.loganalytics.io/v1/workspaces/{workspace_id}/query"
        
        payload = "{\"query\": \"search in (ContainerLog) 'error' | top 50 by TimeGenerated asc   | project LogEntry\"}"
        headers = {
          'Authorization': 'Bearer access token',
          'Content-Type': 'application/json'
        }
        
        response = requests.request("POST", url, headers=headers, data = payload)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 2020-11-09
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多