900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > windows服务器日志文件定期清理 运维编排场景系列-----定时清理Windows服务器日志...

windows服务器日志文件定期清理 运维编排场景系列-----定时清理Windows服务器日志...

时间:2021-03-06 18:08:13

相关推荐

windows服务器日志文件定期清理 运维编排场景系列-----定时清理Windows服务器日志...

本文介绍在运维编排OOS的控制台,通过OOS服务下的定时运维功能,定时执行一个功能性模版,实现某些需定时管理服务器或定时管理其它服务的需求。

应用背景

运行中的实例内部运行了很多服务程序,随时间的推移系统会产生大量的日志文件或一些垃圾文件。其中产生的某些日志文件可能并无实际作用,时间久了产生越来越多的日志文件会占用实例中很大的存储空间,所以需要定期清理无用的日志文件,优化实例存储空间。

前提条件

实例状态为运行中(Running)

模版介绍:

模版功能:

获取实例

根据设置的时间天数,搜索到指定目录下模糊匹配的日志文件,来清理指定天数之前的匹配日志。(例如:清理5天前日志名称包含info.log.的所有日志。)

模版参数:

参数

说明

示例

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--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: '{{ mandOutputs }}'

模版执行效果:

未执行上述模版前,实例内测试文件夹下的文件如下所示。

执行模版后测试文件下模糊匹配的日志文件全部被删除。

控制台操作步骤

步骤一:创建模版

请访问运维编排OOS的控制。单击我的模版,单击创建模板按钮,选取空白模板。将上述提供的模板粘贴到yaml栏,输入名称DeleteWindowsLogFile或自定义名称,点击创建模板。

步骤二:设置定时触发及参数

单击定时运维,在定时运维内部,设置模版的执行周期频率。操作步骤如下图所示。

在选择模版下,选择我的模版,通过搜索框找到刚刚创建完成的模版。

在定时运维下的设置参数一栏,设置模版需要的参数。参数设置完成单击立即执行。

步骤三:查看执行运行情况

在执行管理中查看创建的运维任务。

系列文章

主题文章

最佳实践

场景系列

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。