用脚本管理服务器日志

简介:
       您是否经常碰到在登录系统时“系统日志已满,请清空日志”的警告消息呢?如果您的是终端服务器还可能造成终端用户无法登录等问题,即使将日志存储空间加大也会有满的一天,但是作为一名专业的管理员,服务器的日志又是那么的重要,就算你没时间全部阅读一遍,最起码也要保留一份作为日后排错用。那么以下的小脚本就可以帮你解除这些烦恼:
 
1:备份并清空系统日志,复制代码另存为SysEvtLog_Clear_bak_C.vbs
 
  1. On Error Resume Next  
  2. strYear = Year(Date)  
  3. strMonth = Month(Date)  
  4. If strMonth < 10 Then strMonth = 0 & strMonth  
  5. strDay = Day(Date)  
  6. If strDay < 10 Then strDay = 0 & strDay  
  7. strDate = strYear & strMonth & strDay '得到当前日期  
  8. strLogfileName = strYear & strMonth & strDay  
  9. Set objFSO = CreateObject("Scripting.FileSystemObject")  
  10. strPath = "c:\"  '日志保存位置  
  11. strComputer = "." 
  12. Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Backup)}!\\" & strComputer & "\root\cimv2")  
  13. Set colLogFiles = objWMIService.ExecQuery("Select * from Win32_NTEventLogFile where LogFileName='System'")  
  14. For Each objLogfile in colLogFiles  
  15.  objLogFile.BackupEventLog(strPath & "System(" & strLogfileName & ").evt")  
  16.  objLogFile.ClearEventLog()  
  17. Next 
 
 
2:备份并清空应用程序日志,复制代码另存为AppEvtLog_Clear_bak_C.vbs
 
  1. On Error Resume Next  
  2. strYear = Year(Date)  
  3. strMonth = Month(Date)  
  4. If strMonth < 10 Then strMonth = 0 & strMonth  
  5. strDay = Day(Date)  
  6. If strDay < 10 Then strDay = 0 & strDay  
  7. strDate = strYear & strMonth & strDay '得到当前日期  
  8. strLogfileName = strYear & strMonth & strDay  
  9. Set objFSO = CreateObject("Scripting.FileSystemObject")  
  10. strPath = "c:\" '日志保存位置  
  11. strComputer = "." 
  12. Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Backup)}!\\" & strComputer & "\root\cimv2")  
  13. Set colLogFiles = objWMIService.ExecQuery("Select * from Win32_NTEventLogFile where LogFileName='Application'")  
  14. For Each objLogfile in colLogFiles  
  15.  objLogFile.BackupEventLog(strPath & "Application(" & strLogfileName & ").evt")  
  16.  objLogFile.ClearEventLog()  
  17. Next 
 
 
3:备份并清空安全日志,复制代码另存为SecEvtLog_Clear_bak_C.vbs
 
  1. On Error Resume Next  
  2. strYear = Year(Date)  
  3. strMonth = Month(Date)  
  4. If strMonth < 10 Then strMonth = 0 & strMonth  
  5. strDay = Day(Date)  
  6. If strDay < 10 Then strDay = 0 & strDay  
  7. strDate = strYear & strMonth & strDay '得到当前日期  
  8. strLogfileName = strYear & strMonth & strDay  
  9. Set objFSO = CreateObject("Scripting.FileSystemObject")  
  10. strPath = "c:\" '日志保存位置  
  11. strComputer = "." 
  12. Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Backup, Security)}!\\" & strComputer & "\root\cimv2")  
  13. Set colLogFiles = objWMIService.ExecQuery("Select * from Win32_NTEventLogFile where LogFileName='Security'")  
  14. For Each objLogfile in colLogFiles  
  15.  objLogFile.BackupEventLog(strPath & "Security(" & strLogfileName & ").evt")  
  16.  objLogFile.ClearEventLog()  
  17. Next 
 
将上面的脚本设置计划任务,每月最后一天运行即可(自行定义备份频率)。
其中代码 objLogFile.ClearEventLog() 为清空日志,如果只需要备份而不清空日志的话删除此行即可。


本文转自yangye1985 51CTO博客,原文链接:http://blog.51cto.com/yangye/265076,如需转载请自行联系原作者

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
25天前
|
弹性计算 Shell Perl
ecs服务器shell常用脚本练习(二)
【4月更文挑战第1天】shell代码训练(二)
106 1
|
25天前
|
Linux Shell
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
78 1
|
1月前
|
弹性计算 运维 监控
ECS监控与管理
ECS监控与管理
36 5
|
14天前
|
存储 弹性计算 Shell
ecs服务器shell常用脚本练习(十)
【4月更文挑战第11天】shell代码训练(十)
144 0
|
14天前
|
弹性计算 Shell Go
ecs服务器shell常用脚本练习(九)
【4月更文挑战第10天】shell代码训练(八)
141 0
|
18天前
|
弹性计算 Shell Linux
ecs服务器shell常用脚本练习(六)
【4月更文挑战第4天】shell代码训练(六)
110 0
|
23天前
|
弹性计算 Shell 应用服务中间件
ecs服务器shell常用脚本练习(四)
【4月更文挑战第4天】shell代码训练(四)
97 0
|
1月前
|
Shell Linux 网络安全
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 httpd命令 使用指南
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 httpd命令 使用指南
30 0
|
1月前
|
Shell Linux Apache
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 apachectl命令 使用教程
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 apachectl命令 使用教程
162 1
|
1月前
|
Linux Shell 调度
PBS任务脚本模板及常用服务器PBS命令介绍
【2月更文挑战第21天】本文介绍在Linux服务器中,通过PBS(Portable Batch System)作业管理系统脚本的方式,提交任务到服务器队列,并执行任务的方法~
134 4
PBS任务脚本模板及常用服务器PBS命令介绍

热门文章

最新文章