Friday, April 1, 2011

OMW: Alert when files are older than 30 minutes Part 1

1- Create the needed script

This script is listing the files that are located in certain directory

cscript /nologo checkolderfiles.vbs abc c:\scripts list

I have added two features for the script

One is to display list the files which are not modified within the time specified

file1.txt
file2.txt
listfiles.vbs


Second is to use opcmon to with a count of files that are not modified before 30 minutes


cscript /nologo checkolderfiles.vbs abc c:\scripts alert

2-prepare the file for distribution


Create a folder name it filesmonitoring under <ovdatadir>\shared\Instrumentation\Categories


image


3-Create Measurement Threshold Monitoring Policy


Create Measurement Threshold Policy name it “monitoroldfiles”


Use Source Type as Program


type in the command area


cscript /nologo checkoldfiles.vbs abc c:\scripts alert

this to run the script to check for the files located in c:\scripts and will generate alert with opcmon policyname=<number of files>


Create a matching rule to send alert if the value is greater than 0 to match the value sent by opcmon  and generate the alert


Add automatic command to this alert to list down which policies are not changed within the time assigned.


The command should be


cscript /nologo checkoldfiles.vbs abc c:\scripts list

4-Deploy the the instrumentation for filemonitoring to the nodes where this policy will be used.


the full script is below and name it checkoldfiles.vbs


nodeName = "."
If Wscript.Arguments.Count <; 3 Then
wscript.echo "Usage cscript checkoldfiles.vbs ""PolicyName"" ""<Files direcotory>"" ""<Alert |List >"" "
wscript.echo "Ex: to send the count to the OM"
wscript.echo "cscript listfiles.vbs abc c:\scripts alert"
wscript.echo "Ex: to list the files"
wscript.echo "cscript listfiles.vbs abc c:\scripts list"
Else
policyname = WScript.Arguments.Item(0)
path= WScript.Arguments.Item(1)
action=WScript.Arguments.Item(2)

myPath = Split(path,":")
drive= myPath(0)
if right(myPath(1),1) <;> "\" then
directory = replace(myPath(1),"\","\\") &; "\\"
else
directory = replace(myPath(1),"\","\\")
end if
Set dateTime = CreateObject("WbemScripting.SWbemDateTime")
dateTime.SetVarDate( DateAdd("n",-5,now()))
Set objWMIService = GetObject("winmgmts:" _
&; "{impersonationLevel=impersonate}!\\" & nodeName & "\root\cimv2")
query = "Select * From CIM_DataFile Where Drive = '"&; drive &":' and Path = '"& directory &"' and LastModified < '" & dateTime.Value &"'"
Set colFiles = _
objWMIService.ExecQuery(query)
if action= "list" then
listfiles()
elseif action="alert" then
alert()
end if
end if
Function listfiles()
For Each objFile in colFiles
strNewFile = objFile.FileName &; "." & objFile.Extension
Wscript.Echo strNewFile
next
End function
function alert()
Set objShell = WScript.CreateObject("WScript.Shell")
command = "cmd /c ""%ovinstalldir%\bin\opcmon.exe"" " &; policyname & "=" & colFiles.Count
Wscript.Echo command
Set oExec = objShell.Exec(Command)
Wscript.Echo "The output of the command: " &; Command
Do While Not oExec.StdOut.AtEndOfStream
commandoutput = oExec.StdOut.ReadLine()
Wscript.Echo commandOutput
Loop
end function


 


Continue on part 2