Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Function calculation Automation Operation and maintenance practice 3-event trigger automatically create Snapshot

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

function calculation

Aliyun Function Computing is an event-driven fully hosted computing service. With functional computing, you don't have to manage infrastructure like servers, just write code and upload it. Function computation prepares compute resources for you to run your code in a resilient, reliable manner, and provides log queries, performance monitoring, alerts, and more. With functional computing, you can quickly build any type of application and service without management and operations. Even better, you only pay for the resources your code actually consumes to run, and you don't incur a charge if your code doesn't run.

Cloud Monitor

Alibaba Cloud Monitoring provides out-of-the-box enterprise-level open one-stop monitoring solutions for cloud users. It covers IT infrastructure monitoring, external network quality monitoring, business monitoring based on events, custom metrics and logs. To provide you with a full range of more efficient, comprehensive and cost-effective monitoring services.

Cloud Monitor provides a wealth of events, and the events are still being enriched (cloud product system event monitoring). Rich events trigger custom processing functions, which can achieve more perfect automatic O & M.

Thematic portal => Function calculation for automated operation and maintenance Thematic example scenario

In this article, we focus on the function calculation to deal with the restart events of ecs, because these ecs restart events are the ones that users need to respond to with high priority. Assuming that an ECS was restarted due to a system error, the user may get up urgently to do some validation or create a snapshot. In this example, we automatically process a machine that restarts due to a system error instance or due to an instance error, such as creating a snapshot after a successful restart.

ECS system events

cdn.com/31eeef9826151824881ec02ea99b81c2962a640c.png">

Cloud Product System Event Monitoring

Steps Create a function (function code at the end of the text), function creation can refer to the function calculation helloworld

Note: Remember to set permissions to operate ecs for the role of the service function

Log in to Cloud Monitor console, create alarm rules, monitor events for ecs due to instance error or sirong error restart start and end

mock debugging

Simulate real ecs events

Please refer to walkthrough system event handler? So Easy~

Code # -*- coding: utf-8 -*-import loggingimport json, random, string, timefrom aliyunsdkcore import clientfrom aliyunsdkecs.request.v20140526.DeleteSnapshotRequest import DeleteSnapshotRequestfrom aliyunsdkecs.request.v20140526.CreateSnapshotRequest import CreateSnapshotRequestfrom aliyunsdkecs.request.v20140526.DescribeDisksRequest import DescribeDisksRequestfrom aliyunsdkcore.auth.credentials import StsTokenCredentialLOGGER = logging.getLogger()clt = Nonedef handler(event, context): creds = context.credentials sts_token_credential = StsTokenCredential(creds.access_key_id, creds.access_key_secret, creds.security_token) ''' { "product": "ECS", "content": { "executeFinishTime": "2018-06-08T01:25:37Z", "executeStartTime": "2018-06-08T01:23:37Z", "ecsInstanceName": "timewarp", "eventId": "e-t4nhcpqcu8fqushpn3mm", "eventType": "InstanceFailure.Reboot", "ecsInstanceId": "i-bp18l0uopocfc98xxxx" }, "resourceId": "acs:ecs:cn-hangzhou:123456789:instance/i-bp18l0uopocfc98xxxx", "level": "CRITICAL", "instanceName": "instanceName", "status": "Executing", "name": "Instance:SystemFailure.Reboot:Executing", "regionId": "cn-hangzhou" } ''' evt = json.loads(event) content = evt.get("content"); ecsInstanceId = content.get("ecsInstanceId"); regionId = evt.get("regionId"); global clt clt = client.AcsClient(region_id=regionId, credential=sts_token_credential) name = evt.get("name"); name = name.lower() if name in ['Instance:SystemFailure.Reboot:Executing'.lower(), "Instance:InstanceFailure.Reboot:Executing".lower()]: pass # do other things if name in ['Instance:SystemFailure.Reboot:Executed'.lower(), "Instance:InstanceFailure.Reboot:Executed".lower()]: request = DescribeDisksRequest() request.add_query_param("RegionId", "cn-shenzhen") request.set_InstanceId(ecsInstanceId) response = _send_request(request) disks = response.get('Disks').get('Disk', []) for disk in disks: diskId = disk["DiskId"] SnapshotId = create_ecs_snap_by_id(diskId) LOGGER.info("Create ecs snap sucess, ecs id = %s , disk id = %s ", ecsInstanceId, diskId)def create_ecs_snap_by_id(disk_id): LOGGER.info("Create ecs snap, disk id is %s ", disk_id) request = CreateSnapshotRequest() request.set_DiskId(disk_id) request.set_SnapshotName("reboot_" + ''.join(random.choice(string.ascii_lowercase) for _ in range(6))) response = _send_request(request) return response.get("SnapshotId")# send open api requestdef _send_request(request): request.set_accept_format('json') try: response_str = clt.do_action_with_exception(request) LOGGER.info(response_str) response_detail = json.loads(response_str) return response_detail except Exception as e: LOGGER.error(e)

"Alibaba Cloud Native Weixin Official Accounts (ID: Alicloudnative) focuses on micro-services, Serverless, containers, Service Mesh and other technical fields, focuses on cloud native popular technology trends, cloud native large-scale landing practice, and is the technical public account that best understands cloud native developers. "

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report