天天看點

基于OOS實作定時清理磁盤

OOS簡介

Operation Orchestration Service,簡稱OOS,是全面、免費的雲上自動化運維平台,提供運維任務的管理和執行。典型使用場景包括:事件驅動運維,批量操作運維,定時運維任務,跨地域運維等,OOS為重要運維場景提供審批,通知等功能。OOS幫您實作标準化運維任務,進而實踐運維即代碼(Operations as Code)的先進理念。關于OOS更詳細的介紹請參見

什麼是運維編排服務

操作步驟

1.登陸

OOS控制台

,找到我的模闆,點選建立模闆。

基于OOS實作定時清理磁盤

2.選擇空白模版。

基于OOS實作定時清理磁盤

3.複制下文提供的模闆到YAML欄中,輸入模闆名稱(CleanUpDisks),點選建立模闆。

基于OOS實作定時清理磁盤

模闆内容:

FormatVersion: OOS-2019-06-01
Description:
  en: Clean up disks.
  zh-cn: 定時清理磁盤。
  name-en: CleanUpDisks
  name-zh-cn: 清理磁盤
Parameters:
  regionId:
    Type: String
    Description:
      en: The id of region.
      zh-cn: 地域ID。
    AssociationProperty: RegionId
    Default: '{{ ACS::RegionId }}'
  tagValues:
    Description:
      en: The value list of tag.
      zh-cn: 标簽值清單。
    Type: List
  cleanUpInfos:
    Description:
      en: 'The infos of disk to clean up.Format: 3d|/root/log/|*.log,3h|/root/log/|*.access,30m|C:\Users\Administrator\log\|*.log.'
      zh-cn: 磁盤清理資訊。格式:3d|/root/log/|*.log,3h|/root/log/|*.access,30m|C:\Users\Administrator\log\|*.log
    Type: String
  threshold:
    Description:
      en: The disk usage threshold.
      zh-cn: 磁盤使用率門檻值。
    Type: Number
    MaxValue: 100
  rateControl:
    Description:
      en: Concurrency ratio of task execution.
      zh-cn: 任務執行的并發比率。
    Type: Json
    AssociationProperty: RateControl
    Default:
      Mode: Concurrency
      MaxErrors: 0
      Concurrency: 10
  OOSAssumeRole:
    Description:
      en: The RAM role to be assumed by OOS.
      zh-cn: OOS扮演的RAM角色。
    Type: String
    Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: getInstance
    Description:
      en: Views the ECS instances.
      zh-cn: 擷取ECS執行個體。
    Action: 'ACS::ExecuteApi'
    Properties:
      Service: ECS
      API: DescribeInstances
      Parameters:
        RegionId: '{{ regionId }}'
        Tags:
          - Key: appgroup
            Value: "{{ ACS::TaskLoopItem }}"
    Loop:
      RateControl: '{{ rateControl }}'
      Items: '{{ tagValues }}'
      Outputs:
        instanceIds:
          AggregateType: Fn::ListJoin
          AggregateField: instanceId
    Outputs:
      instanceId:
        Type: List
        ValueSelector: 'Instances.Instance[].InstanceId'
  - Name: cleanUpDisk
    Action: 'ACS::ECS::CleanUpDisk'
    Description:
      en: Execute cloud assistant command to clean up disk.
      zh-cn: 執行雲助手指令清理磁盤。
    Properties:
      regionId: '{{ regionId }}'
      instanceId: '{{ ACS::TaskLoopItem }}'
      threshold: '{{ threshold }}'
      cleanUpInfos: '{{ cleanUpInfos }}'
    Loop:
      RateControl: '{{ rateControl }}'
      Items:
        'Fn::Jq':
          - First
          - '[.[][]]'
          - '{{ getInstance.instanceIds }}'           

執行模闆

1.找到CleanUpDisks,點選建立執行,點選下一步:設定參數。

基于OOS實作定時清理磁盤

2.根據提示設定參數,點選下一步:确定。

基于OOS實作定時清理磁盤

參數解釋:

  • regionId: 地域。
  • tagValues: 标簽值,List類型,每行表示不同的值。TagKey已固定為:appgroup。
  • cleanUpInfos: 磁盤清理資訊,3d|/root/log/|.log表示:删除/root/log/比對.log的修改于3天前的所有檔案,可輸入過個清理規則,各個規則間用","隔開。
  • threshold: 磁盤使用率閥值,磁盤使用率超過閥值後會清理磁盤。
  • rateControl: 任務執行并發速率,使用預設值即可。

3.确認參數無誤後,點選建立。執行完成後,可以在執行詳情檢視執行結果。

基于OOS實作定時清理磁盤