Python 클라이언트 사용
Python 클라이언트를 사용하여 IBM Cloud Monitoring 서비스를 관리할 수 있습니다. 클라이언트는 sdcclient라고도 합니다.
이러한 지시사항은 Python 버전 3.x에 적용됩니다.
기본 팀과 연관된 리소스를 관리하려면 IAM을 인증 방법으로 사용하십시오.
팀과 연관된 리소스를 관리하려면 팀에 대해 활성화된 모니터링 API 토큰을 사용해야 합니다.
1단계. Python 클라이언트 설치하기
전제조건
Python 버전 3 이상을 사용하는 경우에는 필요한 다음 Python 패키지를 설치하십시오.
-
다음 명령을 실행하여
request패키지를 설치하십시오.pip3 install requests -
다음 명령을 실행하여
pyyaml패키지를 설치하십시오.python3 -m pip install pyyaml
pip을 사용하여 Python 클라이언트 설치
터미널에서 다음 명령을 실행하십시오.
sddclient 버전이 0.9.x 이상이어야 합니다.
# sddclient version must be at least 0.9.0
pip3 install sdcclient
GitHub 저장소에서 Python 클라이언트 설치
다음 단계를 완료하십시오.
-
클라이언트 코드를 복사할 폴더를 작성하십시오.
mkdir python-client && cd python-client -
저장소를 복제하십시오.
git clone https://github.com/sysdiglabs/sysdig-sdk-python.git -
빈 Python 스크립트를 작성하십시오.
touch script.py -
python-sdc-client/script.py폴더의 내용을 확인할 때python-client가 나열되어 있는지 확인하십시오.ls
2단계. Python 스크립트에서 Python 클라이언트를 참조하세요
pip을 사용하여 설치된 Python 클라이언트 참조
Python 스크립트에 다음 명령문을 추가하여 Python 클라이언트를 참조할 수 있습니다.
from sdcclient import IbmAuthHelper, SdMonitorClient
GitHub 저장소를 복제하여 설치된 Python 클라이언트 참조
다음 항목을 Python 스크립트에 추가하여 GitHub 복제 패키지를 스크립트에 가져오십시오.
import sys
sys.path.append("python-sdc-client")
from sdcclient import IbmAuthHelper, SdMonitorClient
3단계. Monitoring Python 클라이언트 인스턴스화
옵션 1. IAM을 사용하여 사용자 또는 서비스 ID를 인증합니다
Python 클라이언트에서 IBM Cloud IAM 인증을 사용하려면 Monitoring 인스턴스에서 IBM Cloud Monitoring 엔드포인트, API 키 및 GUID를 지정해야 합니다.
터미널에서 다음 단계를 완료하십시오.
-
IBM Cloud Monitoring 인스턴스의 GUID를 가져오십시오. 다음 명령을 실행하십시오.
ibmcloud resource service-instance <NAME> --output json | jq -r '.[].guid' -
API 키를 가져오십시오. 다음 명령을 실행하여 사용자 API 키를 생성하십시오.
ibmcloud iam api-key-create KEY_NAME -
모니터링 인스턴스가 사용 가능한 지역의 엔드포인트를 가져오십시오.
사용 가능한 엔드포인트 목록을 보려면 Monitoring 엔드포인트를 참조하십시오.
예를 들어, 미국에서 사용 가능한 인스턴스의 엔드포인트는
https://us-south.monitoring.cloud.ibm.com입니다. -
Python 스크립트에 다음 항목을 추가하십시오.
from sdcclient import IbmAuthHelper, SdMonitorClient URL = <MONITORING_ENDPOINT> # For example: URL = 'https://us-south.monitoring.cloud.ibm.com' APIKEY = <IAM_APIKEY> GUID = <GUID> ibm_headers = IbmAuthHelper.get_headers(URL, APIKEY, GUID) sdclient = SdMonitorClient(sdc_url=URL, custom_headers=ibm_headers)여기서,
<MONITORING_ENDPOINT>를 모니터링 인스턴스를 사용할 수 있는 엔드포인트로 교체해야 합니다.<IAM_APIKEY>를 유효한 IAM API 키로 대체해야 합니다. 자세히 보기<GUID>를 이전 단계에서 얻은 모니터링 인스턴스의 GUID로 바꿔야 합니다.
이제 sdclient를 사용하여 IAM을 통해 인증할 조치를 수행할 수 있습니다.
오류 400 Client Error: Bad Request for url: https://iam.cloud.ibm.com/identity/token이 발생하면 API 키를 확인하십시오. 전달하는 값은 올바르지 않습니다.
옵션 2. 모니터링 API 토큰을 사용하여 인증하기
Python 클라이언트로 관리하려는 팀과 연관된 Monitoring 엔드포인트 및 모니터링 API 토큰을 지정해야 합니다.
터미널에서 다음 단계를 완료하십시오.
-
모니터링 인스턴스가 사용 가능한 지역의 엔드포인트를 가져오십시오.
사용 가능한 엔드포인트 목록을 보려면 Monitoring 엔드포인트를 참조하십시오.
예를 들어, 미국에서 사용 가능한 인스턴스의 엔드포인트는
https://us-south.monitoring.cloud.ibm.com입니다. -
Python 스크립트에 다음 항목을 추가하십시오.
from sdcclient import IbmAuthHelper, SdMonitorClient URL = <MONITORING_ENDPOINT> # For example: URL = 'https://us-south.monitoring.cloud.ibm.com' MONITOR_TOKEN = <MONITOR_TOKEN> # For example: MONITOR_TOKEN = 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' sdclient = SdMonitorClient(token=MONITOR_TOKEN,sdc_url=URL)여기서,
<MONITORING_ENDPOINT>를 모니터링 인스턴스를 사용할 수 있는 엔드포인트로 교체해야 합니다.<MONITOR_TOKEN>를 모니터링 API 토큰으로 대체해야 합니다.
이제 sdclient를 사용하여 IAM을 통해 인증할 조치를 수행할 수 있습니다.
샘플 1. Python iAM API 키를 사용하는 스크립트
#!/usr/bin/env python3
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
from sdcclient import IbmAuthHelper, SdMonitorClient
# Parse arguments.
def usage():
print('usage: %s <ENDPOINT_URL> <API_KEY> <INSTANCE_GUID>' % sys.argv[0])
print('ENDPOINT_URL: IBM Cloud endpoint URL (e.g. https://us-south.monitoring.cloud.ibm.com')
print('API_KEY: IBM Cloud IAM API key. This key is used to retrieve an IAM access token.')
print('INSTANCE_GUID: GUID of an IBM Cloud Monitoring.')
sys.exit(1)
if len(sys.argv) != 4:
usage()
URL = sys.argv[1]
APIKEY = sys.argv[2]
GUID = sys.argv[3]
# Instantiate the client
ibm_headers = IbmAuthHelper.get_headers(URL, APIKEY, GUID)
sdclient = SdMonitorClient(sdc_url=URL, custom_headers=ibm_headers)
샘플 2. Python 모니터링 API 토큰을 사용하는 스크립트
#!/usr/bin/env python3
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
from sdcclient import IbmAuthHelper, SdMonitorClient
# Parse arguments.
def usage():
print('usage: %s <ENDPOINT_URL> <MONITOR_TOKEN> <INSTANCE_GUID>' % sys.argv[0])
print('ENDPOINT_URL: IBM Cloud endpoint URL (e.g. https://us-south.monitoring.cloud.ibm.com')
print('MONITOR_TOKEN: token that is associated to a team.')
sys.exit(1)
if len(sys.argv) != 3:
usage()
URL = sys.argv[1]
MONITOR_TOKEN = sys.argv[2]
# Instantiate the client
sdclient = SdMonitorClient(token=MONITOR_TOKEN,sdc_url=URL)