PI Services

Le blog des collaborateurs de PI Services

SCOM – VbScript de surveillance de modification régulière d’un dossier

 

Le script suivant surveille qu’un dossier donné en paramètre (chemin au format wmi) est bien modifié régulièrement (Laps de temps en minutes au delà duquel un état KO est généré si le dossier n'a pas été modifié).

Le script est directement utilisable dans un script monitor en passant en paramètre les paramètres strDirName et MinuteOffset.

Lien du script plus bas.

 

Option Explicit '####################################################### 'Script verifiant qu un dossier est bien modifié regulierement 'Le script affiche OK ou KO et repercute l état a SCOM 'ARGUMENTS: 'strDirName(Nom complet du repertoire au format wmi => Ex: D:\\Test\\MyDirectory 'MinuteOffSet(Laps de temps en minutes pendant lequel le dossier n est pas modifié) '####################################################### Dim oArgs Set oArgs = Wscript.Arguments Dim strDirName Dim strComputer Dim objSWbemServices Dim colDirectories Dim objDirectory Dim MinuteOffSet Dim dateNow Dim dateNowMinus Dim oAPI Dim oBag Dim strStatus 'CREATION OBJET SCOM Set oAPI = CreateObject("MOM.ScriptAPI") if oArgs.Count <2 Then 'If the script is called without the required argument, 'create an information event and then quit. wscript.echo "Le script a ete appele sans le nombre minimum d'argument (2)" Call oAPI.LogScriptEvent(WScript.ScriptName,851,0,WScript.ScriptName+" Le script a été appelé sans le nombre minimum d'argument (2)") Wscript.Quit -1 Else 'Log evenement: lancement du script OK Call oAPI.LogScriptEvent(WScript.ScriptName,852,0,WScript.ScriptName+" script was launched successfully") End If '###FONCTION DE FORMATAGE DE DATE########################### function formatDate(format, intTimeStamp) dim unUDate, A ' Test to see if intTimeStamp looks valid. If not, they have passed a normal date if not (isnumeric(intTimeStamp)) then if isdate(intTimeStamp) then intTimeStamp = DateDiff("S", "01/01/1970 00:00:00", intTimeStamp) else response.write "Date Invalid" exit function end if end if if (intTimeStamp=0) then unUDate = now() else unUDate = DateAdd("s", intTimeStamp, "01/01/1970 00:00:00") end if unUDate = trim(unUDate) dim startM : startM = InStr(1, unUDate, "/", vbTextCompare) + 1 dim startY : startY = InStr(startM, unUDate, "/", vbTextCompare) + 1 dim startHour : startHour = InStr(startY, unUDate, " ", vbTextCompare) + 1 dim startMin : startMin = InStr(startHour, unUDate, ":", vbTextCompare) + 1 dim dateDay : dateDay = mid(unUDate, 1, 2) dim dateMonth : dateMonth = mid(unUDate, startM, 2) dim dateYear : dateYear = mid(unUDate, startY, 4) dim dateHour : dateHour = mid(unUDate, startHour, 2) dim dateMinute : dateMinute = mid(unUDate, startMin, 2) dim dateSecond : dateSecond = mid(unUDate, InStr(startMin, unUDate, ":", vbTextCompare) + 1, 2) format = replace(format, "%Y", right(dateYear, 4)) format = replace(format, "%y", right(dateYear, 2)) format = replace(format, "%m", dateMonth) format = replace(format, "%n", cint(dateMonth)) format = replace(format, "%F", monthname(cint(dateMonth))) format = replace(format, "%M", left(monthname(cint(dateMonth)), 3)) format = replace(format, "%d", dateDay) format = replace(format, "%j", cint(dateDay)) format = replace(format, "%h", mid(unUDate, startHour, 2)) format = replace(format, "%g", cint(mid(unUDate, startHour, 2))) if (cint(dateHour) > 12) then A = "PM" else A = "AM" end if format = replace(format, "%A", A) format = replace(format, "%a", lcase(A)) if (A = "PM") then format = replace(format, "%H", left("0" & dateHour - 12, 2)) format = replace(format, "%H", dateHour) if (A = "PM") then format = replace(format, "%G", left("0" & cint(dateHour) - 12, 2)) format = replace(format, "%G", cint(dateHour)) format = replace(format, "%i", dateMinute) format = replace(format, "%I", cint(dateMinute)) format = replace(format, "%s", dateSecond) format = replace(format, "%S", cint(dateSecond)) format = replace(format, "%L", WeekDay(unUDate)) format = replace(format, "%D", left(WeekDayName(WeekDay(unUDate)), 3)) format = replace(format, "%l", WeekDayName(WeekDay(unUDate))) format = replace(format, "%U", intTimeStamp) format = replace(format, "11%O", "11th") format = replace(format, "1%O", "1st") format = replace(format, "12%O", "12th") format = replace(format, "2%O", "2nd") format = replace(format, "13%O", "13th") format = replace(format, "3%O", "3rd") format = replace(format, "%O", "th") formatDate = format end function '##################################################################### '###FONCTION DE CONVERSION DE DATE WMI EN DATE STANDARD (UTC)######### Function WMIDateStringToDate(dtmInstallDate) WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & "/" & _ Mid(dtmInstallDate, 7, 2) & "/" & Left(dtmInstallDate, 4) _ & " " & Mid (dtmInstallDate, 9, 2) & ":" & _ Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate, _ 13, 2)) End Function '##################################################################### 'NOM DU DOSSIER AU FORMAT WMI strDirName=oArgs(0) 'NOMBRE DE MINUTES MinuteOffSet=oArgs(1) dateNowMinus=DateAdd("n",-MinuteOffSet,formatDate("%n/%d/%Y %h:%i:%s", Now())) wscript.Echo "FORMAT DE DATE US (MM/dd/YYYY) => SI BESOIN MODIFIER LE FORMATAGE EFFECTUEE PAR LA FONCTION formatDate SUR LA VARIABLE Now" wscript.Echo "" & vbCrLf wscript.Echo "H:" wscript.Echo formatDate("%n/%d/%Y %h:%i:%s", Now()) wscript.Echo "" & vbCrLf wscript.Echo "H-"&MinuteOffSet&"minutes:" wscript.Echo dateNowMinus wscript.Echo "" & vbCrLf strComputer = "." Set objSWbemServices = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colDirectories = objSWbemServices.ExecQuery("select * from win32_directory where name='" & strDirName & "'") For each objDirectory in colDirectories Wscript.Echo objDirectory.Name Wscript.Echo "LastModified: "& WMIDateStringToDate(objDirectory.LastModified) &"" If WMIDateStringToDate(objDirectory.LastModified) > dateNowMinus Then Wscript.Echo "Dossier modifié plus recemment que "& dateNowMinus &" => OK" strStatus="OK" Else Wscript.Echo "Dossier non modifié depuis plus de "& MinuteOffSet &" minutes => KO" strStatus="KO" End If Next Set oBag = oAPI.CreatePropertyBag() If strStatus = "OK" then Call oBag.AddValue("Status","Success") wscript.echo "Healthy" Call oAPI.Return(oBag) Else strStatus = "KO" Call oBag.AddValue("Status","Critical") wscript.echo "Critical" Call oAPI.Return(oBag) End if