天天看點

運維編排場景系列-----每日統計多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

繼續閱讀