天天看点

运维编排场景系列-----每日统计多Region实例的运行状态系列文章

应用场景

一个账号内存在一个Region或者多个Region时,并且每个Region都存在多个实例。需要自定义指定时间来查看所有Region下或者查看指定的部分Region的全部实例的运行状态,并统计出各种实例状态的数量,将输出的报告以钉钉的形式通知指定用户。

解决方案

任务步骤

1.设置定时器

2.循环多个Region

3.查询每个Region的实例数量

4.计算不同状态的实例数量

5.将结果发送给指定的钉钉用户

一、打开

控制台

,找到运维编排

运维编排场景系列-----每日统计多Region实例的运行状态系列文章

二、创建模版

根据以上的任务步骤来看可以把此任务创建为以下两个模版。点击创建模版**

运维编排场景系列-----每日统计多Region实例的运行状态系列文章

模版一:

此模版是子模版,根据父模版传进来的RegionId来分别计算此RegionId下不同运行状态的实例数量,并将结果传回父模版去做处理。自定义此模版名称或这将此模版命名为:InstanceDifferentStatusCountInfo

FormatVersion: OOS-2019-06-01
Outputs:
  runningCount:
    Type: String
    Value: '{{ regionId }}:{{ runningInstance.count }}'
  stoppedCount:
    Type: String
    Value: '{{ regionId }}:{{ stoppedInstance.count }}'
  totalCount:
    Type: String
    Value: '{{ regionId }}:{{ totalInstance.count }}'
Parameters:
  regionId:
    Description: The region id for instance.
    Type: String
  OOSAssumeRole:
    Description: The RAM role to be assumed by OOS.
    Type: String
    Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: runningInstance
    Action: 'ACS::ExecuteAPI'
    Description: >-
      describe instances with specified parameters, refer them here:
      https://help.aliyun.com/document_detail/63440.html
    Properties:
      Service: Ecs
      API: DescribeInstances
      Parameters:
        Status: Running
        RegionId: '{{ regionId }}'
    Outputs:
      count:
        Type: String
        ValueSelector: .TotalCount
  - Name: stoppedInstance
    Action: 'ACS::ExecuteAPI'
    Description: >-
      describe instances with specified parameters, refer them here:
      https://help.aliyun.com/document_detail/63440.html
    Properties:
      Service: Ecs
      API: DescribeInstances
      Parameters:
        Status: Stopped
        RegionId: '{{ regionId }}'
    Outputs:
      count:
        Type: String
        ValueSelector: .TotalCount
  - Name: totalInstance
    Action: 'ACS::ExecuteAPI'
    Description: >-
      describe instances with specified parameters, refer them here:
      https://help.aliyun.com/document_detail/63440.html
    Properties:
      Service: Ecs
      API: DescribeInstances
      Parameters:
        RegionId: '{{ regionId }}'
    Outputs:
      count:
        Type: String
        ValueSelector: .TotalCount           

模版二:

    此模版在嵌套模版中为父模版,主要作用是定时执行循环输入的RegionId,并给子模版传RegionId,将属于不同Region的模版一输出的结果做聚合处理,聚合处理后的结果通过钉钉发送给指定的用户。自定义此模版名称或将此模版命名为DifferentRegionInstanceStatusCount。

注意:由于此功能使用了嵌套模版,需要先将模版一创建成功后再创建模版二,并将【模版一】的名称作为【模版二】instanceStatusInfo下TemplateName的参数。

FormatVersion: OOS-2019-06-01
Outputs: {}
Parameters:
  regionId:
    Description: The region id for instance.
    Type: List
    Default:
      - cn-hangzhou
      - cn-beijing
  token:
    Description: DingTalk webhook token.
    Type: String
  expression:
    Description: 'Daily task execution time(UTC),for example:0 0 2 ? * *'
    Type: String
  endDate:
    Description: 'The task execution end date(UTC),for example:2019-08-02T08:06:49Z or 2019-08-02'
    Type: String
  OOSAssumeRole:
    Description: The RAM role to be assumed by OOS.
    Type: String
    Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: DailyStatisticsStatus
    Action: 'ACS::TimerTrigger'
    Description: Timer for executing task.
    Properties:
      Type: cron
      Expression: '{{ expression }}'
      EndDate: '{{ endDate }}'
  - Name: instanceStatusInfo
    Action: 'ACS::Template'
    Description: Check if the user has an MFA Device.
    Properties:
      TemplateName: InstanceDifferentStatusCountInfo
      Parameters:
        regionId: '{{ACS::TaskLoopItem}}'
    Outputs:
      runningCount:
        Type: String
        ValueSelector: runningCount
      stoppedCount:
        Type: String
        ValueSelector: stoppedCount
      totalCount:
        Type: String
        ValueSelector: totalCount
    Loop:
      Items: '{{ regionId }}'
      MaxErrors: 100
      Concurrency: 10
      Outputs:
        RunningCount:
          AggregateField: runningCount
          AggregateType: 'Fn::ListJoin'
        StoppedCount:
          AggregateField: stoppedCount
          AggregateType: 'Fn::ListJoin'
        TotalCount:
          AggregateField: totalCount
          AggregateType: 'Fn::ListJoin'
  - Name: NotifyDingTalk
    Action: 'ACS::Notify'
    Description: >-
      Send notification to DingTalk via webhook. Please refer
      https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq for details.
    Properties:
      NotifyType: WebHook
      WebHook:
        URI: 'https://oapi.dingtalk.com/robot/send?access_token={{ token }}'
        Headers:
          Content-Type: application/json
        Content:
          msgtype: text
          text:
            content: >-
              Total Instance Count: {{ instanceStatusInfo.TotalCount }},  
              Running Instance Count: {{ instanceStatusInfo.RunningCount}},  
              Stopped Instance Count: {{ instanceStatusInfo.StoppedCount}}           

三、创建执行

两个模版全部创建成功后就点击创建执行了,此时必须只执行模版二。

运维编排场景系列-----每日统计多Region实例的运行状态系列文章

四、设置参数

如下所示,根据实际情况设置参数。参数输入完毕后点击下一步:确认创建。

参数介绍:

regionId:实例的地区,可以根据实际情况输入一个或多个regionId。

token:报告接收者的token

expression:设置的定期执行时间,此处的设置参数方式如下所示(必须为UTC格式时间,参考云助手的

设置定时执行命令

,UTC时间在平常的基础上 -8h):

0 15 2 ?   每天上午10:15执行任务

0 0 2,6,8 ?  每天上午10:00点、下午14:00以及下午16:00执行任务

0 0/5 6 ? 每天下午14:00到下午14:55时间段内每隔5分钟执行任务

0 0/30 1-9 ?  每天上午09:00到下午17:00时间段内每隔半小时执行任务

0 10,44 4 ? 3 WED 每年3月的每个星期三下午14:10到14:44时间段内执行任务

0 15 2 ? * 6L 2002-2005  2002年至2005年每月最后一个星期五上午10:15执行任务

endDate:设置执行结束时间(必须为UTC格式的时间:2019-08-02T08:06:49Z  or 2019-08-02)

运维编排场景系列-----每日统计多Region实例的运行状态系列文章

五、创建执行

参数设置完毕后,就可以点击创建执行了,此任务开始执行。

运维编排场景系列-----每日统计多Region实例的运行状态系列文章

由于时间定时器的关系,此任务在指定的时间执行任务结束后,此任务继续回到等待中。下图为第一层子执行。

运维编排场景系列-----每日统计多Region实例的运行状态系列文章

账号内包含多个regionId时此执行会有多个子执行,时间定时器下的任务子执行显示如下图所示。

运维编排场景系列-----每日统计多Region实例的运行状态系列文章

六、输出结果

当循环完所有的regionId后,任务执行结束,并将输出的报告发送给指定的钉钉用户,其显示如下所示。

运维编排场景系列-----每日统计多Region实例的运行状态系列文章

总结

此执行主要是为了定时查看某一账号内的所有实例的运行状态。在高压任务下或者是日常检查中方便随时了解账号内所有实例的实际运行情况。通过嵌套模版的方法解决了在一个模版下无法统计不同Region的实例不同运行状态,并根据需要来输出理想的数据格式。目前OOS运维编排处于公测中欢迎试用。

系列文章

主题文章

阿里云重磅发布云上自动化利器——运维编排OOS

最佳实践

玩转运维编排服务的权限:Assume Role+Pass Role

场景系列

运维编排场景系列----更新ECS镜像 运维编排场景系列-----给ECS实例自动打TAG 运维编排场景系列----从实例中拷贝文件到OSS 运维编排场景系列----给实例加到SLS机器组 运维编排场景系列----检测MFA功能状态 阿里云运维编排新功能:一键批量克隆ECS

继续阅读