Application scenarios: Mainly used with virtual hosts, but also for personal servers
Background: a vm that has always been running normally crashed on a certain day of June 1, 2005, so that the personnel on duty in the IDC cannot restart the host several times. The system displays the VM and prompts that the disk C is insufficient, in the middle of the night, I had to go to the data center for processing. after going to the data center, I first cut off the network and went into the system to find two problems. The C: \ WINDOWS \ system32 \ LogFiles file had 6 GB, another one is the Symantec isolation virus. After finding it online, the greatest possibility is that all the logs of our VM are written here, and no one knows how to write them here, after reading this in IIS, the log is growing every day. At that time, there were a lot of orders in the company and no one paid attention to this. At that time, the system was normal, after returning to the company, I changed the IIS log to another disk.
Solution: But this is not the final solution. If a VM has hundreds of sites, some sites can generate hundreds of M log files in a day, and they must be cleaned up in time.
There are two solutions:
1. Clean the logs of the previous 60 days every day
2. Clear the logs 60 days ago in a short time.
However, either method requires technical processing. If you manually delete it, you can find the log file that was deleted 60 days ago unless you are very professional. However, even if your technology is good, this method is also very time-consuming. The best method is to use DOS batch processing or script implementation. The scripts available are mainly vbs and js.
There are several methods in the following solution that you can choose to suit yourself. Their overall design idea is as follows:
The format of IIS log files is: ex year month day. log for example: ex071116.log
IIS log file storage location: by default, it is at: % windir % \ system32 \ LogFiles. If you are using professional IIS management software, it usually allows you to set the corresponding log directory
IIS log cleanup CMD: calculates the date of the previous N days based on the current time, for example, today is, the date of the first 60 days is 20070916-9-16 (the program can automatically identify 30 days or 31 days or months), and then process it into a format like, then combine the IIS log file format such as ex070916.log, so that we can get the log file name to be cleared, and then use del/s/f d: \ iislog \ ex070916.log is used to clear all files with this file name in the directory where the log is located and all the files under the subdirectory, so as to clear the log, but this is only a day's log, therefore, we have to add the batch processing to the scheduled task so that it can be regularly executed every day. In this way, we can ignore all computer log problems.
IIS log cleanup VBS version: the theory of VBS version is that it is not as fast as the iis version, because it also needs to use the Script driver, instead of using the batch processing function of the DOS system directly in cmd version (guessed ), after all, VBS is a high-level language. The ability to process dates is achieved in one sentence, while the CMD version has to write half a page. IIS log cleanup Implementation of VBS uses VBS to traverse all files and folders in the directory where the IIS log is located, and then combines the file names into date types, and then the current date-this date, to check whether the number of days exceeds the specified number of days. If the number of days exceeds the value of delete, one advantage is that all records from the past N days can be cleared at a time, instead of just one day, the script can be written to a scheduled task and run every day. It can also be run manually once in a period of time. This code is obviously less than the CMD version for IIS log cleanup.
IIS log cleanup JS version: This version is actually not much different from IIS log cleanup VBS version. The idea is the same, but the script language is different, in addition, there are two parameters in the call: Directory, which must be written as: D: \ iislog. In the past, vbs was used as the main script, this time I want to learn C #. I heard that both of these languages are similar. I just practiced and didn't spend much time.
IIS log cleanup WSH version: WSH version is actually the simplest, because it is highly integrated and the operation process is as follows: Use vbs or js to generate the file name to be processed, then use WScript. shell executes the cmd command to process IIS logs. It uses the advantages of IIS logs in CMD and IIS logs in VBS. This is also a log that can only be processed for one day at a time, of course, you can also change it to processing logs for multiple days. Because WSH is highly integrated and can perform many operations, hackers like this very much, and WScript is the most used. shell. Generally, server providers with high security awareness will disable this component. As a result, this best-to-use function becomes the least usable, with the worst versatility.
Instance code:
IIS log cleanup CMD code (DelIISLog. cmd ):Copy codeThe Code is as follows: @ echo off
Title
: Set the number of days before or after the current date
Set/a beforedays =-3
: Set the directory location
Set dir = "F: \ log \"
: Converts the current date to the number of days for calculation.
Call: Date2Day % date :~ 0, 10% days
Set/a days = % days % beforedays %
Call: Day2Date % days % lastdate
: After calculation, generate the desired character combination.
Set okstr = ex % lastdate :~ 2,6%. log
: Delete these files
Del/f/s/q % dir % \ % okstr %
Cmd/k
: Date2Day
Setlocal ENABLEEXTENSIONS
For/f "tokens = 1-3 delims =/-," % a in ('echo/% 1') do (
Set yy = % a & set mm = % B & set dd = % c
)
Set/a dd = 100% dd % 100, mm = 100% mm % 100
Set/a z = 14-mm, z/= 12, y = yy + 4800-z, m = mm + 12 * z-3, j = 153 * m + 2
Set/a j = j/5 + dd + y * 365 + y/4-y/100 + y/400-2472633
Endlocal & set % 2 = % j % & goto: EOF
: Day2Date
Setlocal ENABLEEXTENSIONS
Set/a I = % 1, a = I + 2472632, B = 4 * a + 3, B/= 146097, c =-B * 146097, c/= 4, c + =
Set/a d = 4 * c + 3, d/= 1461, e =-1461 * d, e/= 4, e + = c, m = 5 * e + 2, m/= 153, dd = 153 * m + 2, dd/= 5
Set/a dd =-dd + e + 1, mm =-m/10, mm * = 12, mm + = m + 3, yy = B * 100 + d-4800 + m/10
(If % mm % LSS 10 set mm = 0% mm %) & (if % dd % LSS 10 set dd = 0% dd %)
Endlocal & set % 2 = % yy % mm % dd % & goto: EOF
IIS log cleanup VBS code (DelIISLog. vbs ):Copy codeThe Code is as follows: 'iis log cleanup VBS version code (DelIISLog. vbs)
'Call method: DelIISLog "IIS Log Path", how many days of IIS logs are retained
'Traverse all files in the IIS log folder and files in the subfolders
Function DelIISLog (IISLogPath, KeepDays)
On error resume next
Set oFso = CreateObject ("Scripting. FileSystemObject ")
Set oFolder = oFso. GetFolder (IISLogPath)
Set oSubFolders = oFolder. SubFolders to get the Set of all folders in the directory.
Set oFiles = oFolder. Files '.
'Step 1: process all files in the current directory
For Each oFile In oFiles 'traverse all files
If right (oFile. name, 3) = "log" then
ODate = cdate ("20" & mid (oFile. name, 3, 2) & "-" & mid (oFile. name, 5, 2) & "-" & mid (oFile. name, 7,2 ))
If date-oDate> KeepDays then oFile. delete' determines whether the IIS log file is to be processed. if yes, delete it directly.
End if
Next
'Step 2: process all the directories in the current directory and perform recursive calls
For Each oSubFolder In oSubFolders
DelIISLog oSubFolder. Path, KeepDays 'recursion
Next
End Function
DelIISLog "D: \ IISLogTest", 20' Traversal
IIS log cleanup JS Code (DelIISLog. js ):Copy codeThe Code is as follows: // IIS log cleanup JS version code (DelIISLog. js)
// Call method: DelIISLog ("IIS Log Path", number of days of IIS log retained );
// Traverse all files in the IIS log folder and files in the subfolders
Function DelIISLog (IISLogPath, KeepDays ){
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
Var f = fso. GetFolder (IISLogPath );
Var Folders = new Enumerator (f. SubFolders); // get the set of all Folders in the directory
Var Files = new Enumerator (f. Files); // obtain the set of all Files in the directory
// Process all files in the current directory
For (;! Files. atEnd (); Files. moveNext ()){
Var fileName = Files. item (). name;
Var year = "20" + fileName. substr (2, 2 );
Var mouth = fileName. substr (4, 2 );
Var day = fileName. substr (6, 2 );
Var days = Math. round (new Date (). getTime ()-Date. UTC (year, mouth-1, day)/1000/60/60/24 );
If (days> KeepDays) Files. item (). Delete (); // you can check whether the IIS log file is to be processed. if yes, Delete it directly.
}
// Step 2 process all the directories in the current directory and perform recursive calls
For (;! Folders. atEnd (); Folders. moveNext ()){
DelIISLog (Folders. item (), KeepDays );
}
}
// Call a function, for example, "F: \ log", 5, or "C: \ windows \ system32 \ LogFiles", 5
DelIISLog ("D: \ IISLogTest", 2 );
IIS log cleanup WSH code (DelIISLog. wsf ):Copy codeThe Code is as follows: <job id = "IIS log cleanup WSH code (DelIISLog. wsf)">
<Script language = "vbscript">
'Author: Liu yongfa (yongfa365) 'Blog
'Modified:
'Operation instructions: This file can only clear logs for one day. You have to use a scheduled task to execute it once a day. Because WScript. Shell is generally disabled on the server, it is not recommended to use
Function DelIISLog (IISLogPath, beforedays)
D = Now-beforedays
If Right (IISLogPath, 1) <> "\" Then IISLogPath = IISLogPath &"\"
P = IISLogPath & "ex" & Right (Year (d), 2) & Right ("0" & Month (d), 2) & Right ("0" & Day (d), 2 )&". log"
Set WshShell = WScript. CreateObject ("WScript. Shell ")
Wscript. echo p
WshShell. Run ("cmd.exe/c del/s" & p)
Set WshShell = Nothing
End Function
DelIISLog "D: \ IISLogTest", 2
</Script>
</Job>
Sometimes when I get someone else's code, I have to write a bunch of things to test. Now, if you want to test it, you won't test it directly on the server. So, the following is a test script of Liu yongfa, which is used to generate a test folder and some IIS test log files on drive D,
IIS log generation system (CreateIISLog. vbs) for IIS log cleaning ):Copy codeThe Code is as follows: 'iis log cleansing-IIS log generation system (CreateIISLog. vbs)
'Create a folder
Function CreateFolder (Folder)
On Error Resume Next
Set FSO = CreateObject ("Scripting. FileSystemObject ")
FSO. CreateFolder (Folder)
If Err> 0 Then
Err. Clear
CreateFolder = False
Else
CreateFolder = True
End If
End Function
'Create a file
Function CreateFile (FileName, Content)
On Error Resume Next
Set FSO = CreateObject ("Scripting. FileSystemObject ")
Set fd = FSO. CreateTextFile (FileName, True)
Fd. WriteLine Content
If Err> 0 Then
Err. Clear
CreateFile = False
Else
CreateFile = True
End If
End Function
CreateFolder "D: \ IISLogTest"
CreateFolder "D: \ IISLogTest \ IISLogs001"
CreateFolder "D: \ IISLogTest \ IISLogs002"
CreateFolder "D: \ IISLogTest \ IISLogs003"
For I = 1 to 30
D = date-I
Filename = "ex" & right (year (d), 2) & right ("0" & month (d), 2) & right ("0" & day (d), 2 )&". log"
CreateFile "D: \ IISLogTest \" & filename, Content
CreateFile "D: \ IISLogTest \ IISLogs001 \" & filename, Content
CreateFile "D: \ IISLogTest \ IISLogs002 \" & filename, Content
CreateFile "D: \ IISLogTest \ IISLogs003 \" & filename, Content
Postscript: This method can be used not only for IIS log processing, but also for Serv-U log processing, the precondition is that the format of the Serv-U log file must also be set to ex071115.log in the format of IIS log.