批量修改LTS日志文件转储时区
您在LTS上经常会执行日志接入、日志告警、日志转储等配置操作。有些操作是需要重复多次配置,但目前LTS还没有提供控制台批量操作功能,这时您可以通过Python脚本结合LTS API接口实现自定义的批量操作。
使用场景
当用户创建了1000条日志转储的OBS规则,但转储时文件时区全部选择(UTC) 协调世界时间,现在需要根据实际情况修改为(UTC+08:00) 北京。目前LTS控制台没有提供批量修改功能,如果手动修改每条转储规则,会导致人工耗时非常长。
前提条件
- Linux系统的主机。
- 查询API相关接口文档。
- 通过查询日志转储API获取到所有转储任务的信息。
- 通过更新日志转储API将转储任务配置的时区修改。
- 在API Explorer中测试API功能,API Explorer提供API检索及平台调试能力。
- 参考API Explorer示例代码,在主机上安装Python SDK。
- Python的SDK依赖包地址以及SDK使用说明。
pip install huaweicloudsdklts
- API Explore提供Python调用API的示例代码,以下示例仅供参考:
# coding: utf-8 import os 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__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment /* 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险, 建议在配置文件或者环境变量中密文存放, 使用时解密, 确保安全; ak = os.environ["CLOUD_SDK_AK"] sk = os.environ["CLOUD_SDK_SK"] credentials = BasicCredentials(ak, sk) client = LtsClient.new_builder() \ .with_credentials(credentials) \ .with_region(LtsRegion.value_of("xx")) \ .build() try: request = ListTransfersRequest() 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使用说明。
批量修改LTS日志文件转储时区
- 获取参数,并将实际值替换到代码中。
- 获取华为账号的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
- 如果已安装,执行结果会返回huaweicloudsdk相关信息。如果没有安装,则不会返回任何内容。
- 若未安装在主机上请执行如下操作进行安装:
pip install huaweicloudsdkcore huaweicloudsdklts
- 在主机上执行“vi lts_python.py”新建lts_python.py文件,将如下代码复制到该文件中,用于实现批量修改OBS转储文件时区。
# 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 /* 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险, 建议在配置文件或者环境变量中密文存放, 使用时解密, 确保安全; 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 &
- 查看执行日志确认Python脚本运行成功,文件时区已修改。
tail -f lts_python.log
图2 查看执行日志