使用OpenAPI自动化处理ECS系统事件

本文涉及的产品
云服务器 ECS,每月免费额度200元 3个月
云服务器ECS,u1 2核4GB 1个月
简介: 什么是系统事件 当您将业务系统部署到阿里云ECS后,阿里云保证ECS计算服务的高可用。在极少情况下,比如探测到ECS实例所在的硬件发生故障,会产生有计划的维护事件并通知您。 深入了解系统事件,请参考: 实例系统事件 让运维更高效:关于ECS系统事件 监控和应对系统事件的方式 为了业务的平稳运行,您需要监控ECS系统事件并及时合理地应对系统事件。

什么是系统事件

当您将业务系统部署到阿里云ECS后,阿里云保证ECS计算服务的高可用。在极少情况下,比如探测到ECS实例所在的硬件发生故障,会产生有计划的维护事件并通知您。

深入了解系统事件,请参考:

监控和应对系统事件的方式

为了业务的平稳运行,您需要监控ECS系统事件并及时合理地应对系统事件。

从控制台处理ECS主动运维事件请参考 ECS主动运维事件--让你HOLD住全场

相对于收到通知后登陆ECS控制台人工处理系统事件,通过程序自动化监控和处理系统事件,能够提高您的运维效率,消除遗漏或出错的可能性,让您的运维人员不用再为半夜的故障通知而烦恼。如果您保有较多的ECS实例,自动化程序的优点将会更加突出。

ECS为您提供了两个OpenAPI来监控实例的健康状态和系统事件。

1. DescribeInstancesFullStatus 查询实例的全状态信息

ECS实例全状态信息包括:

  • 实例的生命周期状态,比如实例处于Running还是Stopped状态
  • 实例的健康状态,比如您的实例处于Ok还是Warning状态
  • 处于待执行状态(Scheduled)的所有系统事件

这个OpenAPI关注实例的当前状态,它不会返回已经完结的历史事件。对于事前运维来说,我们只需要关注Scheduled状态的事件。事件处于Scheduled状态意味着现在仍处在用户操作窗口期。在事件的计划执行时间NotBefore之前,我们可以通过程序处理来避免事件执行。

首先,我们调用DescribeInstancesFullStatus OpenAPI来查询当前是否存在待执行的SystemMaintenance.Reboot事件。

def build_instance_full_status_request():
    request = DescribeInstancesFullStatusRequest()
    request.set_EventType('SystemMaintenance.Reboot')
    return request


# send open api request
def _send_request(request):
    request.set_accept_format('json')
    try:
        response_str = client.do_action_with_exception(request)
        logging.info(response_str)
        response_detail = json.loads(response_str)
        return response_detail
    except Exception as e:
        logging.error(e)


# only_check=True时仅检查是否存在SystemMaintenance.Reboot事件,为False时对SystemMaintenance.Reboot事件进行处理
def check_scheduled_reboot_events(only_check=False, instance_id=None):
    request = build_instance_full_status_request()
    if instance_id:
        request.set_InstanceIds([instance_id])
    response = _send_request(request)
    if response.get('Code') is None:
        instance_full_status_list = response.get('InstanceFullStatusSet').get('InstanceFullStatusType')
        # 因为指定了事件类型查询,无SystemMaintenance.Reboot系统事件的实例不会返回
        exist_reboot_event = len(instance_full_status_list) > 0
        if not exist_reboot_event:
            print "No scheduled SystemMaintenance.Reboot event found"
        if only_check:
            return exist_reboot_event
        for instance_full_status in instance_full_status_list:
            instance_id = instance_full_status.get('InstanceId')
            scheduled_reboot_events = instance_full_status.get('ScheduledSystemEventSet').get(
                'ScheduledSystemEventType')
            for scheduled_reboot_event in scheduled_reboot_events:
                handle_reboot_event(instance_id, scheduled_reboot_event)
    else:
        logging.error(str(response))

Tip:主动运维系统事件会留出足够长的用户操作窗口期,一般以天为单位。所以并不需要频繁的去轮询待执行的系统事件。未来我们将会提供基于消息队列的系统事件消费接口

如果发现存在SystemMaintenance.Reboot系统事件,您应该根据实例上运行的业务类型来决定是否需要自行处理。

Tip:即使由ECS系统执行重启,对您的重要数据进行提前备份也是一个好主意。

如果实例重启对业务有影响,你可能需要选择一个NotBefore之前的更合适的业务低谷时间点。您需要设定一个定时任务,在这个时间点执行重启操作。


def handle_reboot_event(instance_id, reboot_event):
    not_before_str = reboot_event.get('NotBefore')
    not_before = datetime.strptime(not_before_str, '%Y-%m-%dT%H:%M:%SZ')
    print "Instance %s has a SystemMaintenance.Reboot event scheduled to execute at %s" % (instance_id, str(not_before))
    # 根据你的业务特性选择not_before之前的影响最小的时间点
    # 使用定时任务在该时间点进行实例重启

    # 示例中简化为立即重启
    pre_reboot(instance_id)
    reboot_instance(instance_id)
    post_reboot(instance_id)


def reboot_instance(instance_id):
    print "Reboot instance %s now..." % instance_id
    reboot_request = RebootInstanceRequest()
    reboot_request.set_InstanceId(instance_id)
    _send_request(reboot_request)


def pre_reboot(instance_id):
    # 重启前做backup等等准备工作
    print "Do pre-reboot works..."


def post_reboot(instance_id):
    # 重启后做健康检查等等善后工作
    # 检查重启是否成功
    print "Do post-reboot works..."

    # 一般情况下重启成功后几秒后SystemMaintenance.Reboot事件将变为Avoided状态
    # 再次查询DescribeInstancesFullStatus确认SystemMaintenance.Reboot事件无法查询到
    wait_event_disappear(instance_id)

重启成功完成后,系统事件将在短时间内变为Avoided状态。

def wait_event_disappear(instance_id):
    wait_sec = 0
    while wait_sec < TIME_OUT:
        exist = check_scheduled_reboot_events(only_check=True, instance_id=instance_id)
        if not exist:
            print "SystemMaintenance.Reboot system event is avoided"
            return
        time.sleep(10)
        wait_sec += 10

您的自动化处理程序需要妥善处理各种异常情况,保证定时重启的及时性和稳定性。尤其注意的是,在事件状态变化前不要重复处理,以避免不必要的重启。

2. DescribeInstanceHistoryEvents 查询实例的历史事件

查询指定ECS实例的系统事件,默认查询已经处于非活跃状态的历史事件。如果指定全部的事件状态,可以查询包含活跃事件在内的所有事件。

此API默认只查询历史事件,它的用途是对实例的历史事件进行分析、复盘,追溯问题原因。某些事件类型比如SystemFailure.Reboot发生时,不一定会留出用户操作窗口期。比如非预期的紧急故障发生后,阿里云立刻进行了恢复并重启了您的实例。此类事件可以在历史事件中查询到。

总结

  1. 使用DescribeInstancesFullStatus来查询实例状态和Scheduled状态的系统事件
  2. 使用DescribeInstanceHistoryEvents对历史事件进行复盘。如果指定系统事件状态,也可以查询未结束的系统事件(Scheduled和Executing状态)。
  3. 使用自动化程序对Scheduled状态的系统事件进行处理
  4. 如果只需要查询系统事件,推荐使用DescribeInstanceHistoryEvents接口,性能更好。

未来我们将会发布更多类型的ECS实例和存储相关系统事件,覆盖更多运维场景,敬请期待!

完整的示例代码如下

#  coding=utf-8

# if the python sdk is not install using 'sudo pip install aliyun-python-sdk-ecs'
# if the python sdk is install using 'sudo pip install --upgrade aliyun-python-sdk-ecs'
# make sure the sdk version is 4.4.3, you can use command 'pip show aliyun-python-sdk-ecs' to check

import json
import logging
from datetime import datetime
import time

from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526.DescribeInstancesFullStatusRequest import DescribeInstancesFullStatusRequest
from aliyunsdkecs.request.v20140526.RebootInstanceRequest import RebootInstanceRequest

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S')

# your access key Id
ak_id = "YOU_ACCESS_KEY_ID"
# your access key secret
ak_secret = "YOU_ACCESS_SECRET"
region_id = "cn-shanghai"
TIME_OUT = 5 * 60

client = client.AcsClient(ak_id, ak_secret, region_id)


def build_instance_full_status_request():
    request = DescribeInstancesFullStatusRequest()
    request.set_EventType('SystemMaintenance.Reboot')
    return request


# send open api request
def _send_request(request):
    request.set_accept_format('json')
    try:
        response_str = client.do_action_with_exception(request)
        logging.info(response_str)
        response_detail = json.loads(response_str)
        return response_detail
    except Exception as e:
        logging.error(e)


# only_check=True时仅检查是否存在SystemMaintenance.Reboot事件,为False时对SystemMaintenance.Reboot事件进行处理
def check_scheduled_reboot_events(only_check=False, instance_id=None):
    request = build_instance_full_status_request()
    if instance_id:
        request.set_InstanceIds([instance_id])
    response = _send_request(request)
    if response.get('Code') is None:
        instance_full_status_list = response.get('InstanceFullStatusSet').get('InstanceFullStatusType')
        # 因为指定了事件类型查询,无SystemMaintenance.Reboot系统事件的实例不会返回
        exist_reboot_event = len(instance_full_status_list) > 0
        if not exist_reboot_event:
            print "No scheduled SystemMaintenance.Reboot event found"
        if only_check:
            return exist_reboot_event
        for instance_full_status in instance_full_status_list:
            instance_id = instance_full_status.get('InstanceId')
            scheduled_reboot_events = instance_full_status.get('ScheduledSystemEventSet').get(
                'ScheduledSystemEventType')
            for scheduled_reboot_event in scheduled_reboot_events:
                handle_reboot_event(instance_id, scheduled_reboot_event)
    else:
        logging.error(str(response))


def handle_reboot_event(instance_id, reboot_event):
    not_before_str = reboot_event.get('NotBefore')
    not_before = datetime.strptime(not_before_str, '%Y-%m-%dT%H:%M:%SZ')
    print "Instance %s has a SystemMaintenance.Reboot event scheduled to execute at %s" % (instance_id, str(not_before))
    # 根据你的业务特性选择not_before之前的影响最小的时间点
    # 使用定时任务在该时间点进行实例重启

    # 示例中简化为立即重启
    pre_reboot(instance_id)
    reboot_instance(instance_id)
    post_reboot(instance_id)


def reboot_instance(instance_id):
    print "Reboot instance %s now..." % instance_id
    reboot_request = RebootInstanceRequest()
    reboot_request.set_InstanceId(instance_id)
    _send_request(reboot_request)


def pre_reboot(instance_id):
    # 重启前做backup等等准备工作
    print "Do pre-reboot works..."


def post_reboot(instance_id):
    # 重启后做健康检查等等善后工作
    # 检查重启是否成功
    print "Do post-reboot works..."

    # 一般情况下重启成功后几秒后SystemMaintenance.Reboot事件将变为Avoided状态
    # 再次查询DescribeInstancesFullStatus确认SystemMaintenance.Reboot事件无法查询到
    wait_event_disappear(instance_id)


def wait_event_disappear(instance_id):
    wait_sec = 0
    while wait_sec < TIME_OUT:
        exist = check_scheduled_reboot_events(only_check=True, instance_id=instance_id)
        if not exist:
            print "SystemMaintenance.Reboot system event is avoided"
            return
        time.sleep(10)
        wait_sec += 10


if __name__ == '__main__':
    check_scheduled_reboot_events(only_check=False)
相关实践学习
一小时快速掌握 SQL 语法
本实验带您学习SQL的基础语法,快速入门SQL。
7天玩转云服务器
云服务器ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,可降低 IT 成本,提升运维效率。本课程手把手带你了解ECS、掌握基本操作、动手实操快照管理、镜像管理等。了解产品详情:&nbsp;https://www.aliyun.com/product/ecs
目录
相关文章
|
1月前
|
运维 Prometheus 监控
构建高效自动化运维系统的关键策略
【2月更文挑战第30天】随着云计算和微服务架构的兴起,现代IT运维环境变得愈加复杂多变。为保持业务连续性、提高响应速度并降低成本,企业亟需构建一个高效的自动化运维系统。本文将深入探讨自动化运维系统构建过程中的关键策略,包括工具和技术选型、流程优化、监控与告警体系搭建以及持续集成/持续部署(CI/CD)实践,旨在为读者提供一个清晰的构建蓝图和实用的实施建议。
|
1月前
|
人工智能 运维 监控
构建高性能微服务架构:现代后端开发的挑战与策略构建高效自动化运维系统的关键策略
【2月更文挑战第30天】 随着企业应用的复杂性增加,传统的单体应用架构已经难以满足快速迭代和高可用性的需求。微服务架构作为解决方案,以其服务的细粒度、独立性和弹性而受到青睐。本文将深入探讨如何构建一个高性能的微服务系统,包括关键的设计原则、常用的技术栈选择以及性能优化的最佳实践。我们将分析微服务在处理分布式事务、数据一致性以及服务发现等方面的挑战,并提出相应的解决策略。通过实例分析和案例研究,我们的目标是为后端开发人员提供一套实用的指南,帮助他们构建出既能快速响应市场变化,又能保持高效率和稳定性的微服务系统。 【2月更文挑战第30天】随着信息技术的飞速发展,企业对于信息系统的稳定性和效率要求
|
3天前
|
监控 安全 Linux
Linux系统之安装ServerBee服务器监控工具
【4月更文挑战第22天】Linux系统之安装ServerBee服务器监控工具
41 2
|
9天前
|
JavaScript 前端开发 UED
Vue工具和生态系统: Vue.js和服务器端渲染(SSR)有关系吗?请解释。
Vue.js是一个渐进式JavaScript框架,常用于开发单页面应用,但其首屏加载较慢影响用户体验和SEO。为解决此问题,Vue.js支持服务器端渲染(SSR),在服务器预生成HTML,加快首屏速度。Vue.js的SSR可手动实现或借助如Nuxt.js的第三方库简化流程。Nuxt.js是基于Vue.js的服务器端渲染框架,整合核心库并提供额外功能,帮助构建高效的应用,改善用户体验。
12 0
|
1月前
|
机器学习/深度学习 测试技术 API
iOS系统下轻松构建自动化数据收集流程
iOS系统下轻松构建自动化数据收集流程
26 0
|
1月前
|
弹性计算 运维 Kubernetes
云原生K8S场景自动化响应ECS系统事件
客户云原生K8S场景下,通过社区开源NPD+Draino+Autoscaler零开发,对接响应ECS主动运维事件,通过自动响应事件减少非预期宕机。
|
2月前
|
机器学习/深度学习 人工智能 安全
PyRIT:主动发现生成式 AI 系统潜在风险的开放式自动化框架
【2月更文挑战第9天】PyRIT:主动发现生成式 AI 系统潜在风险的开放式自动化框架
22 3
PyRIT:主动发现生成式 AI 系统潜在风险的开放式自动化框架
|
2月前
|
弹性计算 安全 Linux
阿里云ECS Linux系统漏洞修复详细教程
阿里云ECS Linux系统漏洞修复详细教程
|
2月前
|
数据安全/隐私保护
2012及其以上系统修改服务器密码指南
2012及其以上系统修改服务器密码指南
|
2月前
|
弹性计算 网络安全
阿里云OpenAPI提供了多种接口来管理ECS实例
【2月更文挑战第1天】阿里云OpenAPI提供了多种接口来管理ECS实例
32 0

热门文章

最新文章

相关产品

  • 云服务器 ECS