使用脚本调用LTS接口实现自定义操作
背景信息
您在LTS上经常会执行配置相关的操作,例如接入日志、创建告警、配置转储等。有些配置操作是需要重复多次配置的,但目前LTS还没有提供控制台批量操作功能,这时您可以通过Python脚本结合LTS API接口实现自定义的批量操作。
使用场景
- 当用户创建了1000条日志转储的OBS规则,但转储时文件时区全部选择(UTC) 协调世界时间,现在需要根据实际情况修改为(UTC+08:00) 北京。
- 目前LTS控制台没有提供批量修改功能,如果手动修改每条转储规则,会导致人工耗时非常长。
前提条件
- Linux环境
- 查询API相关接口文档
- 通过查询日志转储API获取到所有转储任务的信息
- 通过更新日志转储API将转储任务配置的时区修改
- 在API Explorer中测试API功能
API Explorer提供API检索及平台调试能力,测试查询日志转储API效果如下:
- 参考API Explorer示例代码,安装Python sdk
- Python sdk依赖包地址以及SDK使用说明
pip install huaweicloudsdklts
- API Explore提供Python调用API的示例代码,参考如下:
# coding: utf-8 from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdklts.v2.region.lts_region import LtsRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdklts.v2 import * if __name__ == "__main__": AK = "your ak" SK = "your sk" PROJECT_ID = "your project id" REGION = "your region" IAM_ENDPOINT = "iam_endpoint" credentials = BasicCredentials(AK, SK, PROJECT_ID).with_iam_endpoint(IAM_ENDPOINT) client = LtsClient.new_builder() .with_credentials(credentials) .with_region(LtsRegion.value_of(REGION)) .build() try: request = ListTransfersRequest() request.log_transfer_type = "OBS" response = client.list_transfers(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
- Python sdk依赖包地址以及SDK使用说明
操作步骤
- 获取参数,并将实际值替换到代码中。
- 获取AK&SK
- 获取项目ID(project id),详细步骤请参考API凭证。
图1 获取Project_ID
- 请参考地区和终端节点获取Region&iam_Endpoint。
表1 Endpoint表 区域名称
区域
终端节点(Endpoint)
协议类型
华北-北京三
cn-north-3
lts.cn-north-3.myhuaweicloud.com
HTTPS
华北-北京四
cn-north-4
lts.cn-north-4.myhuaweicloud.com
HTTPS
华北-北京一
cn-north-1
lts.cn-north-1.myhuaweicloud.com
HTTPS
华东-上海二
cn-east-2
lts.cn-east-2.myhuaweicloud.com
HTTPS
华东-上海一
cn-east-3
lts.cn-east-3.myhuaweicloud.com
HTTPS
华南-广州
cn-south-1
lts.cn-south-1.myhuaweicloud.com
HTTPS
华南-深圳
cn-south-2
lts.cn-south-2.myhuaweicloud.com
HTTPS
亚太-曼谷
ap-southeast-2
lts.ap-southeast-2.myhuaweicloud.com
HTTPS
亚太-新加坡
ap-southeast-3
lts.ap-southeast-3.myhuaweicloud.com
HTTPS
中国-香港
ap-southeast-1
lts.ap-southeast-1.myhuaweicloud.com
HTTPS
- 时区和时区ID
表2 常用时区表 时区
时区ID
UTC-12:00
Etc/GMT+12
UTC-11:00
Etc/GMT+11
UTC-10:00
Pacific/Honolulu
UTC-09:00
America/Anchorage
UTC-08:00
America/Santa_Isabel
UTC-07:00
America/Chihuahua
UTC-06:00
America/Chicago
UTC-05:00
America/New_York
UTC-04:00
America/Santiago
UTC-03:00
America/Sao_Paulo
UTC-02:00
Etc/GMT+2
UTC-01:00
Atlantic/Azoresjavik
UTC+00:00
Europe/London
UTC+01:00
Europe/Parist
UTC+02:00
Europe/Istanbul
UTC+03:00
Europe/Minsk
UTC+04:00
Europe/Moscow
UTC+05:00
Asia/Tashkent
UTC+06:00
Asia/Almaty
UTC+07:00
Asia/Bangkok
UTC+08:00
Asia/Shanghai
UTC+09:00
Asia/Tokyo
UTC+10:00
Asia/Yakutsk
UTC+11:00
Asia/Vladivostok
UTC+12:00
Pacific/Fiji
UTC+13:00
Pacific/Apia
- 确认是否已安装 huaweicloudsdkcore和huaweicloudsdklts包。
pip list | grep huaweicloudsdk
若未安装请执行如下操作:pip install huaweicloudsdkcore huaweicloudsdklts
- 新建lts_python.py文件,将如下代码复制到该文件中。
# coding: utf-8 from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdklts.v2 import * from huaweicloudsdklts.v2.region.lts_region import LtsRegion if __name__ == "__main__": AK = "your ak" SK = "your sk" PROJECT_ID = "your project id" REGION = "your region" IAM_ENDPOINT = "iam_endpoint" OBS_TIME_ZONE = "the time_zone you want to change" OBS_TIME_ZONE_ID = "time_zone_id" credentials = BasicCredentials(AK, SK, PROJECT_ID).with_iam_endpoint(IAM_ENDPOINT) client = LtsClient.new_builder() \ .with_credentials(credentials) \ .with_region(LtsRegion.value_of(REGION)) \ .build() # 1.get obs transfer task try: request = ListTransfersRequest() request.log_transfer_type = "OBS" response = client.list_transfers(request) obs_transfer_num = len(response.log_transfers) task_list = response.log_transfers print("#### get {} obs transfer task ####".format(obs_transfer_num)) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) # 2.set obs transfer task obs_time_zone to UTC+08:00 CNT = 1 while len(task_list): transfer_task = task_list.pop() print("There are still {} progress: \n".format(len(task_list)), transfer_task) try: if transfer_task.log_transfer_info.log_transfer_detail.obs_time_zone == OBS_TIME_ZONE: CNT += 1 continue request = UpdateTransferRequest() transfer_task.log_transfer_info.log_transfer_detail.obs_time_zone = OBS_TIME_ZONE transfer_task.log_transfer_info.log_transfer_detail.obs_time_zone_id = OBS_TIME_ZONE_ID request.body = UpdateTransferRequestBody( log_transfer_info=transfer_task.log_transfer_info, log_transfer_id=transfer_task.log_transfer_id ) response = client.update_transfer(request) CNT += 1 except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) task_list.append(transfer_task) except exceptions.ServerResponseException as e: print({ "target": transfer_task.log_streams, "reason": e }) task_list.append(transfer_task)
- 执行Python脚本,批量修改OBS转储文件时区。
nohup python lts_python.py > lts_python.log &
查看执行日志。
tail -f lts_python.log
图2 查看执行日志