從a中提取指標Monitoring使用監控 API 實例
您可以從IBM Cloud Monitoring實例透過使用Monitoring API。
透過使用獲取指標cURL
您可以使用以下內容 cURL命令 獲取指標:
curl -X POST <SYSDIG_REST_API_ENDPOINT>/data -H "Authorization: $AUTH_TOKEN" -H "IBMInstanceID: $GUID" -H "SysdigTeamID: $TEAM_ID" -H "content-type: application/json" -d DATA
其中
-
<SYSDIG_REST_API_ENDPOINT>指示 REST API 呼叫的目標端點。 有關更多信息,請參閱 監控 REST API 端點。 例如,us-south 中可用的實例的公共端點如下:https://us-south.monitoring.cloud.ibm.com/api -
您可以使用以下方法傳遞多個標頭
-H。Authorization和IBMInstanceID是身份驗證所需的標頭。SysdigTeamID是可選的。 當您指定此標頭時,您將請求限制為指定團隊可用的資料和資源。為了得到一個
AUTH_TOKEN和GUID看,IAM 令牌的標頭。 -
你可以傳遞文件
metrics.json透過使用提取指標-d,例如,-d @metrics.json。
以下範例顯示了一個模板 metrics.json 文件:
{
"start": "Number: seconds",
"end": "Number: seconds",
"last": "Number: last available seconds",
"metrics": [
{
"id": "<>",
"aggregations": {}
}
],
"sampling": "Number: <10|60|600|3600|86400>",
"dataSourceType": "<host|container>",
"filter": "<>",
"paging": {
"from": "Number",
"to": "Number"
}
}
指標字典
若要查看 Sysdig 預先定義的指標,請參閱 指標字典。
查看由下列項目定義的預定義指標IBM Cloud支援 Sysdig 的服務,請參閱 雲端服務。
資料聚合
若要了解資料聚合,請參閱 資料聚合。
範例:擷取平台指標
此範例展示如何從中提取平台指標Cloud Foundry在美國南方過去 24 小時。
curl -X POST https://us-south.monitoring.cloud.ibm.com/api/data -H "Authorization: Bearer $AUTH_TOKEN" -H "IBMInstanceID: $GUID" -H "content-type: application/json" -d @metrics.json
下面的例子是 metrics.json 文件顯示如何設定檔以按實例 ID、應用程式容器期限和應用程式正在使用的位元組數提取資料:
{
"metrics": [
{
"id": "ibm_resource"
},
{
"id": "ibm_cloudfoundry_app_container_age",
"aggregations": {"group": "max", "time": "max"}
},
{
"id": "ibm_cloudfoundry_app_memory_bytes_used",
"aggregations": {"group": "avg", "time": "avg"}
}
],
"filter": "ibm_location = \"us-south\"",
"sampling": 86400,
"last": 86400
}
提取資料的結果傳回以下資訊:
{
"data": [
{
"d": [
"73a9d202-7e97-45b2-a107-3b1528856be3",
316660942465643,
18809015.575
],
"t": 1587772800
}
],
"end": 1587772800,
"start": 1587686400
}
範例:提取CPU數據
此範例示範如何透過主機提取具有開始、結束和採樣限制的 CPU 資料。
下面的例子是 metrics.json 檔案顯示如何設定檔以提取 CPU 資料:
{
"start":"1555404790",
"end":"1555404850",
"sampling": 10,
"metrics": [
{
"id": "host.hostName"
},
{
"id": "cpu.used.percent",
"aggregations": {"time": "avg"}
}
],
"dataSourceType": "host"
}
提取資料的結果傳回以下資訊:
{
"data": [
{
"d": [
"j-rhel71.fyre.ibm.com",
3.621
],
"t": 1555404800
},
{
"d": [
"j-rhel71.fyre.ibm.com",
3.597
],
"t": 1555404810
},
{
"d": [
"j-rhel71.fyre.ibm.com",
3.993
],
"t": 1555404820
},
{
"d": [
"j-rhel71.fyre.ibm.com",
3.145
],
"t": 1555404830
},
{
"d": [
"j-rhel71.fyre.ibm.com",
4.09
],
"t": 1555404840
},
{
"d": [
"j-rhel71.fyre.ibm.com",
5.057
],
"t": 1555404850
}
],
"end": 1555404850,
"start": 1555404790
}
樣本:cURL擷取最新指標的樣本
若要取得指標的最新值,請僅指定指標名稱。 傳回不超過 5 分鐘前產生的最新值。
例如,要取得最新值 host_cpu_used_percent:
curl <SYSDIG_REST_API_ENDPOINT>/prometheus/api/v1/query?query=sysdig_host_cpu_used_percent -H "Authorization: $AUTH_TOKEN" -H "IBMInstanceID: $GUID" -H "SysdigTeamID: $TEAM_ID" -H "content-type: application/json"
其中
-
<SYSDIG_REST_API_ENDPOINT>指示 REST API 呼叫的目標端點。 有關更多信息,請參閱 監控 REST API 端點。 例如,us-south 中可用的實例的公共端點如下:https://us-south.monitoring.cloud.ibm.com/api -
您可以使用以下方法傳遞多個標頭
-H。Authorization和IBMInstanceID是身份驗證所需的標頭。SysdigTeamID是可選的。 當您指定此標頭時,您將請求限制為指定團隊可用的資料和資源。為了得到一個
AUTH_TOKEN和GUID看,IAM 令牌的標頭。
所有儀表板均支援完整PromQL API。 如需更多關於 PromQL, 的資訊,請參閱 Prometheus 文件。
樣本:cURL為團隊提取 CPU 資料的範例
此範例示範如何提取團隊上下文中可用的 CPU 資料。
curl -X POST https://us-south.monitoring.cloud.ibm.com/api/data -H "Authorization: Bearer $AUTH_TOKEN" -H "IBMInstanceID: $GUID" -H "SysdigTeamID: 30785" -H "content-type: application/json" -d @metrics.json
範例:從中提取指標數據Monitoring到Event Streams
您可以使用以下程式碼範例從您的資料中提取指標Monitoring實例到Event Streams。
- 代替
<region>與您的地區Monitoring實例,例如us-east。 - 代替
<apikey>與IBM CloudIAM API 金鑰令牌。 - 代替
<instance_id>與您的身分證件Monitoring實例。 - 更換
metrics包含要擷取的指標 JSON 陣列的欄位。 範例指標是cpu.cores.used。
from sdcclient import IbmAuthHelper, SdMonitorClient
endpoint = "https://<region>.monitoring.cloud.ibm.com"
apikey = "<apikey>"
instanceID = "<instance_id>"
ibm_headers = IbmAuthHelper.get_headers(endpoint, apikey, instanceID)
sdclient = SdMonitorClient(sdc_url=endpoint, custom_headers=ibm_headers)
metrics = [
{
"id": "cpu.cores.used",
"aggregations": {
"time": "avg",
"group": "sum"
}
}
]
filter = None
start = -120
end = 0
sampling = 60
ok, res = sdclient.get_data(metrics, start, end, sampling, filter=filter, datasource_type = "container")
print(ok)
print(res)
return res
範例:從中提取通知Monitoring到Event Streams與Event Streams休息API
-
- 從定價計劃中選擇企業計劃。
- 如有必要,請設定一個 IAM 中的授權為了Event Streams以 KMS 提供者為目標,例如Key Protect,然後重新整理頁面。
- 為了服務端點,選擇帶有 a 的選項私人的網路。
-
等待幾分鐘Event Streams要提供的實例。
-
在你的Event Streams實例,創建一個主題,例如
kafka-java-console-sample-topic。 -
建立一個服務金鑰作為您的憑證Event Streams私有雲服務端點上的實例。
<name>:輸入服務金鑰的名稱。<event_streams_instance>:以 Event Streams 範例的名稱取代。
ibmcloud resource service-key-create <name> Writer --instance-name <event_streams_instance> --service-endpoint private -
在上一個命令的輸出中,取得進行 REST 呼叫所需的詳細資訊。
api_keykafka_http_url
輸出範例:
api_key: 123465123456123465123465123465123465 kafka_http_url: https://mh-<id>.private.us-south.messagehub.appdomain.cloud -
來自 Monitoring儀表板,點選打開儀表板為了Monitoring您要使用的實例。
-
點選開始 > 設定通知頻道 > 設定通知頻道。 設定」頁面會開啟。
-
點選 + 新增通知頻道並選擇網路鉤子選項。
-
配置 Webhook 通知通道。 建立 Webhook 通知頻道後,您必須繼續為該頻道設定驗證。
- 對於 URL,從服務金鑰中輸入
kafka_http_url,並將/topics/<topic>/records路由附加到 URL。<topic>是您在 Event Streams 範例中建立的主題名稱。 - 為了姓名,輸入 Webhook 通知頻道的名稱。
- 將其他欄位保留為預設值。
- 按一下儲存以建立頻道。
- 對於 URL,從服務金鑰中輸入
-
取得 IAM 令牌 驗證您的要求Monitoring API。
-
Curl這Monitoring用於列出通知頻道的 API。
- 代替
<region>與您的地區Monitoring實例,例如us-east。 - 代替
<token>使用您先前檢索到的 IAM 令牌。
curl ’https://<region>.monitoring.cloud.ibm.com/api/notificationChannels' --header ‘Authorization: Bearer <token>’ | jq
在輸出中,請注意 id 的通知頻道,其中 options.url 欄位與您先前建立的 URL 字段與您之前建立的
{
"id": 123,
"version": 6,
...
"options": {
"notifyOnOk": true,
"url": "https://mh-<id>.private.us-south.messagehub.appdomain.cloud",
"notifyOnResolve": true
}
},
- Curl這Monitoring用於將驗證標頭新增至 Webhook 通知頻道的 API。
- 代替
<region>與您的地區Monitoring實例,例如us-east。 - 代替
<id>與您先前檢索到的通知通道的 ID。 - 代替
<token>使用您先前檢索到的 IAM 令牌。
curl -X PUT ’https://<region>.monitoring.cloud.ibm.com/api/notificationChannels/<id>' \
--header ‘Content-Type: application/json’
--header ‘Authorization: Bearer <token>’ -d @/tmp/notification.json
- Curl這Monitoring再次使用 API 來確認通知通道已使用驗證標頭進行更新。
- 代替
<region>與您的地區Monitoring實例,例如us-east。 - 代替
<token>使用您先前檢索到的 IAM 令牌。
curl ’https://<region>.monitoring.cloud.ibm.com/api/notificationChannels' --header ‘Authorization: Bearer <token>’ | jq
在輸出中,請注意 additionalHeaders.X-Auth-Token 欄位已新增至通知通道。
{
"notificationChannel":
{
"id": 123,
....
"options": {
"notifyOnOk": true,
"url": "https://mh-<id>.private.us-south.messagehub.appdomain.cloud/topics/kafka-java-console-sample-topic/records",
"notifyOnResolve": true,
"additionalHeaders": {
"X-Auth-Token": "123456123456123456123456123465123456"
...
- 設定警報 使用您為其建立的 Webhook 通知頻道Event Streams。
現在,當警報被觸發時Event Streams,您可以查看您的詳細信息Monitoring實例。