簡體   English   中英

curl結果與Python請求結果之間的差異

[英]Discrepency between curl results and Python requests results

我正在查詢New Relic API,並嘗試從它們提供的指標中獲取CPU利用率。 當我運行以下curl命令(導出正確的代理設置后)時,我看到以下信息(其中包含所需的百分比值)-

 curl -X GET "https://api.newrelic.com/v2/applications/140456413/hosts/21044947/metrics/data.json" -H "X-Api-Key:myapikey" -i -d 'names[]=CPU/User+Time&values[]=percent&summarize=true&from=2018-07-15 16:22:30&to=2018-07-16 16:22:30'
HTTP/1.0 200 Connection Established
Proxy-agent: Apache
HTTP/1.1 200 OK
Server: openresty
Date: Tue, 17 Jul 2018 11:53:22 GMT
Content-Type: application/json
Content-Length: 290
Connection: keep-alive
Status: 200 OK
X-UA-Compatible: IE=Edge,chrome=1
ETag: "a35b451e27a49d0d5f4e16715429a17d"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: f4f8675f095aba80d5e089fbcbf1b172
X-Runtime: 0.168283
X-Rack-Cache: miss

{"metric_data":{"from":"2018-07-15T16:22:30+00:00","to":"2018-07-16T16:22:30+00:00","metrics_not_found":[],"metrics_found":["CPU/User Time"],"metrics":[{"name":"CPU/User Time","timeslices":[{"from":"2018-07-15T16:22:00+00:00","to":"2018-07-16T16:22:00+00:00","values":{"percent":9.52}}]}]}}

但是,當我嘗試在Python request模塊內部實現它時,我感興趣的“ Percent”值返回0。這是我調用它的代碼-

options = {"names[]": "CPU/User+Time", "values[]": "percent", "summarize": "true", "from": str(end_date), "to": str(start_date - datetime.timedelta(hours=6))}
path = "applications/140456413/hosts/" + server_id + "/metrics/data.json"
api_key = "myapikey"

headers = {'X-Api-Key': api_key}
url = "https://api.newrelic.com/v2/" + path
r = requests.get(url, headers=headers, data=options, proxies=myproxy.proxies)

這是我得到的(注意,百分比值現在為0)-

{u'metric_data': {u'metrics': [{u'timeslices': [{u'values': {u'percent': 0}, u'to': u'2018-07-17T01:35:00+00:00', u'from': u'2018-07-16T07:35:00+00:00'}], u'name': u'CPU/User+Time'}], u'to': u'2018-07-17T01:35:59+00:00', u'metrics_found': [u'CPU/User+Time'], u'from': u'2018-07-16T07:35:59+00:00', u'metrics_not_found': []}}

如何調整python請求以匹配與curl命令相同的輸出? 我們最初是使用=而不是鍵/值對在“ options”變量內傳遞選項,但請求模塊不會以這種格式處理它們。

這些是我引用的文檔頁面-

https://docs.newrelic.com/docs/apis/rest-api-v2/requirements/specifying-time-range-v2 https://docs.newrelic.com/docs/apis/rest-api-v2/application -examples-v2 / get-average-cpu-usage-host-app https://docs.newrelic.com/docs/apis/rest-api-v2/getting-started/introduction-new-relic-rest-api- V2#示例

謝謝。

稍微更改選項:

"values[]": "percent"

至:

"values": ["percent"]

暫無
暫無

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

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