本文介紹在 運維編排OOS的控制台 ,通過OOS服務下的定時運維功能,定時執行一個功能性模版,實作某些需定時管理伺服器或定時管理其它服務的需求。
應用背景
運作中的執行個體内部運作了很多服務程式,随時間的推移系統會産生大量的日志檔案或一些垃圾檔案。其中産生的某些日志檔案可能并無實際作用,時間久了産生越來越多的日志檔案會占用執行個體中很大的存儲空間,是以需要定期清理無用的日志檔案,優化執行個體存儲空間。
前提條件
- 執行個體狀态為運作中(
)Running
模版介紹:
模版功能:
模版參數:
參數 | 說明 | 示例 |
---|---|---|
LogFilePath | 存放日志檔案的絕對路徑。 | C:\Users\Administrator\testlogway |
LogKeepTime | 清理幾天之前的日志檔案。 | 5 |
Targets | 根據執行個體标簽(tags)或執行個體ID(InstanceId)標明需要的執行個體。 | i-11111111111 |
RateControl | 固定參數。 - 速率控制類型:并發控制 - 并發速率:1 - 最大錯誤次數:0 | |
OOSAssumeRole | 可選參數。 - (預設設定)目前賬号的已有權限:執行您使用的賬号的權限動作。請確定您擁有建立自定義鏡像涉及的所有ECS API調用權限。 - 指定RAM角色,使用該角色的權限:如果指定了RAM角色名稱,OOS扮演該RAM角色執行運維任務。 | 目前賬号的已有權限 |
模版内容:
(注:模版設定的測試删除的檔案名為模糊比對的方式,本模版比對的名稱為“ info.log. ”,請根據實際的日志檔案名稱更改模版内的需要删除檔案的模糊名稱。)
FormatVersion: OOS-2019-06-01
Description:
en: Compared to the current time, specify the period of time to keep log files and delete log files outside the time period.
zh-cn: 相比于目前時間,指定保留日志檔案的時間段,并删除時間段之外的日志檔案。
Parameters:
LogFilePath:
Type: String
Description:
en: Log file storage location.
zh-cn: 日志檔案存放位置。
LogKeepTime:
Type: Number
Description:
en: How long to pre-retain log files compared to the current time.
zh-cn: 相比于目前時間,預保留日志檔案的時間天數。
Default: 5
Targets:
Type: Json
AssociationProperty: Targets
AssociationPropertyMetadata:
ResourceType: 'ALIYUN::ECS::Instance'
RateControl:
Description:
en: Concurrency ratio of task execution.
zh-cn: 任務執行的并發比率。
Type: Json
AssociationProperty: RateControl
Default:
Mode: Concurrency
MaxErrors: 0
Concurrency: 10
OOSAssumeRole:
Description: The RAM role to be assumed by OOS.
Type: String
Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
- Name: getInstance
Description:
en: Views the ECS instances.
zh-cn: 擷取ECS執行個體。
Action: 'ACS::SelectTargets'
Properties:
ResourceType: 'ALIYUN::ECS::Instance'
Filters:
- '{{ Targets }}'
Outputs:
instanceIds:
Type: List
ValueSelector: 'Instances.Instance[].InstanceId'
- Name: runCommand
Action: 'ACS::ECS::RunCommand'
Properties:
commandContent: |-
$pathExistOrNot = Test-Path -Path "{{ LogFilePath }}"
if ($pathExistOrNot)
{
$timeOutDay = {{ LogKeepTime }}
$filePath = "{{ LogFilePath }}"
$allFile = Get-ChildItem -Path $filePath
foreach($file in $allFile)
{
$daySpan = ((Get-Date) - $file.LastWriteTime).Days
if ($daySpan -gt $timeOutDay -and $file.FullName.contains("info.log."))
{
Remove-Item $file.FullName -Recurse -Force
echo "delete:" $file.FullName
}
}
} else
{
echo "path not exist"
}
instanceId: '{{ ACS::TaskLoopItem }}'
commandType: RunPowerShellScript
Loop:
Items: '{{ getInstance.instanceIds }}'
RateControl: '{{ RateControl }}'
Outputs:
commandOutputs:
AggregateType: 'Fn::ListJoin'
AggregateField: commandOutput
Outputs:
commandOutput:
Type: String
ValueSelector: invocationOutput
Outputs:
commandOutput:
Type: String
Value: '{{ runCommand.commandOutputs }}'
模版執行效果:
- 未執行上述模版前,執行個體内測試檔案夾下的檔案如下所示。
運維編排場景系列-----定時清理Windows伺服器日志 - 執行模版後測試檔案下模糊比對的日志檔案全部被删除。
運維編排場景系列-----定時清理Windows伺服器日志
控制台操作步驟
步驟一:建立模版
- 請通路 運維編排OOS的控制 。單擊我的模版,單擊建立模闆按鈕,選取空白模闆。将上述提供的模闆粘貼到yaml欄,輸入名稱DeleteWindowsLogFile或自定義名稱,點選建立模闆。
運維編排場景系列-----定時清理Windows伺服器日志 運維編排場景系列-----定時清理Windows伺服器日志
步驟二:設定定時觸發及參數
- 單擊定時運維,在定時運維内部,設定模版的執行周期頻率。操作步驟如下圖所示。
運維編排場景系列-----定時清理Windows伺服器日志 - 在選擇模版下,選擇我的模版,通過搜尋框找到剛剛建立完成的模版。
運維編排場景系列-----定時清理Windows伺服器日志 - 在定時運維下的設定參數一欄,設定模版需要的參數。參數設定完成單擊立即執行。
運維編排場景系列-----定時清理Windows伺服器日志
步驟三:檢視執行運作情況
- 在執行管理中檢視建立的運維任務。
運維編排場景系列-----定時清理Windows伺服器日志