Thursday, May 8, 2008

MOM 2005: Disk defrag analysis

Use this script to analyse the defragmentation levels of disk drives on the servers.



'*******************************************************************************
' Script Name - Analysis disk defragmentation
'
' Version - 0.1 (14-Mar-2008)
'
' Purpose - Analyses the defragmentation of a disk
' An event is generated if the disk requires fragmentation.
'
'
' Events - 1301: Disk requires fragmentation (Warning)
' 1302: Disk does not require fragmentation (Info)
'
'
'*************************************************************************

' Event types
Const Event_Type_Success = 0
Const Event_Type_Error = 1
Const Event_Type_Warning = 2
Const Event_Type_Info = 4
Const Event_Type_Audit_Success = 8
Const Event_Type_Audit_Failure = 16

strComputer = "."
wmiNS = "\root\cimv2"
wmiQuery = "Select * from win32_Volume where DriveType ='3'"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

For Each objItem in colItems
strDrive = objItem.DriveLetter

objItem.DefragAnalysis defrag,objitem1
SubEvalDefrag
Next

Sub SubEvalDefrag
If defrag = 0 then

strMessage = "Disk " & strDrive & ": This disk does not need defragmentation"
CreateEvent 1302, 4, strMessage

Else
'WScript.echo defrag
strMessage = "Disk " & strDrive & ": This disk needs defragmentation" & vbcr &_
"AverageFileSize: " & objitem1.AverageFileSize & vbcr &_
"FilePercentFragmentation: " & objitem1.FilePercentFragmentation & "%" & vbcr &_
"FragmentedFolders: " & objitem1.FragmentedFolders & vbcrlf &_
"TotalExcessFragments: " & objitem1.TotalExcessFragments & vbcrlf &_
"MFTPercentInUse: " & objitem1.MFTPercentInUse & "%" & vbcrlf &_
"TotalPageFileFragments: " & objitem1.TotalPageFileFragments
CreateEvent 1301, 2, strMessage
End if

End Sub

Sub CreateEvent (ByVal StrEventID, strEventType, strMessage)
' Create new event and submit event objects
set objCE_NewEvent = ScriptContext.CreateEvent ' Set event properties
objCE_NewEvent.Message = strMessage
objCE_NewEvent.EventNumber = StrEventID
objCE_NewEvent.EventType = strEventType
objCE_NewEvent.EventSource = "Custom Degrag Analysis"
' Submit the event
ScriptContext.Submit(objCE_NewEvent)
set objCE_NewEvent = Nothing
End Sub



I would not suggest running this on a daily basis, but rather on a weekly or even monthly basis in off-peak periods, as it can cause slight degradation in service.

The script runs defrag analysis and will generate an Error event should the drive require defragmentation.

To implement this script, create a timed event rule to run the script, and then the corresponding event response rules.

To change the event id generated, these lines should be modified:

CreateEvent 1302, 4, strMessage
CreateEvent 1301, 2, strMessage

You can also modify the message generated as it suits your environment.

No comments:

Related Posts with Thumbnails