TCE-虛擬機器建立、雲審計日誌
阿新 • • 發佈:2021-08-11
TCE-虛擬機器建立
- setting.ini
[defaults]
SecretId=AKIDEI6dvwhCVYKp31i8G9k47uQfdKlumNim
SecretKey=oKaZGgphIdhU0ECtJSJtCEyRearsc5GA
[params]
Limit = 100
MetricName = [
"CPUUsage",
"MemUsage",
"CvmDiskUsage"
]
[label]
label_endpoint=tag.tencentcloudapi.com
- 虛擬機器建立
import json import configparser from tencentcloud.common import credential from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException from tencentcloud.cvm.v20170312 import cvm_client, models class TceCvmCreate(object): def __init__(self, SecretId, SecretKey): self.SecretId = SecretId self.SecretKey = SecretKey def crate_cvm(self, params): try: cred = credential.Credential(self.SecretId, self.SecretKey) httpProfile = HttpProfile() httpProfile.endpoint = "cvm.tencentcloudapi.com" clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile client = cvm_client.CvmClient(cred, "ap-guangzhou", clientProfile) req = models.RunInstancesRequest() req.from_json_string(json.dumps(params)) resp = client.RunInstances(req) print(resp.to_json_string()) except TencentCloudSDKException as err: print(err) if __name__ == '__main__': config = configparser.ConfigParser() config.read("setting.ini", encoding="utf-8") cvm = TceCvmCreate(config.get("defaults", "SecretId"),config.get("defaults", "SecretKey")) params = { "InstanceChargeType": "POSTPAID_BY_HOUR", "Placement": { "Zone": "ap-guangzhou-6" }, "InstanceType": "S5.SMALL2", "ImageId": "img-l8og963d", "SystemDisk": { "DiskType": "CLOUD_PREMIUM", "DiskSize": 50 }, "DataDisks": [ { "DiskType": "CLOUD_PREMIUM", "DiskSize": 10, "DeleteWithInstance": True } ], "VirtualPrivateCloud": { "VpcId": "vpc-9pdjw5tz", "SubnetId": "subnet-he80xx2q", "AsVpcGateway": False }, "InstanceCount": 1, "InstanceName": "test-liuzhx", "LoginSettings": { "Password": "Root123456789" }, "SecurityGroupIds": [ "sg-i8ou5lah" ], "EnhancedService": { "SecurityService": { "Enabled": True }, "MonitorService": { "Enabled": True } }, "HostName": "liuzhx", "DryRun": False } cvm.crate_cvm(params)
- 雲審計日誌獲取
import json import configparser from tencentcloud.common import credential from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException from tencentcloud.cloudaudit.v20190319 import cloudaudit_client, models class CloudAuditGet(object): def __init__(self, SecretId, SecretKey): self.SecretId = SecretId self.SecretKey = SecretKey def getCloudAudit(self, params): try: cred = credential.Credential(self.SecretId, self.SecretKey) httpProfile = HttpProfile() httpProfile.endpoint = "cloudaudit.tencentcloudapi.com" clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile client = cloudaudit_client.CloudauditClient(cred, "ap-guangzhou", clientProfile) req = models.LookUpEventsRequest() req.from_json_string(json.dumps(params)) resp = client.LookUpEvents(req) event_data = json.loads(resp.to_json_string()) params['NextToken'] = event_data.get('NextToken') if event_data.get('ListOver'): event_list = event_data.get('Events') else: event_list = event_data.get('Events') while True: req.from_json_string(json.dumps(params)) resp = client.LookUpEvents(req) event_tmp = json.loads(resp.to_json_string()) event_list += event_tmp.get('Events') if event_tmp.get('ListOver'): break print(len(event_list)) return event_list except TencentCloudSDKException as err: print(err) if __name__ == '__main__': config = configparser.ConfigParser() config.read("setting.ini", encoding="utf-8") event_list = [] params = { "MaxResults": 20, # 最大查詢日誌量50 "StartTime": 1625123992, # 只能查詢一個月內的日誌 "EndTime": 1625728792 } cloudaudit = CloudAuditGet(config.get('defaults', 'SecretId'), config.get('defaults', 'SecretKey')) event = cloudaudit.getCloudAudit(params) print(event)