3 Replies Latest reply on Nov 20, 2013 10:13 AM by Justin Walker

    Monitoring a heartbeat file

    Ian Morris

      Hi,

       

      I have a script (on a windows box) that writes out a ping to a text file every 42 seconds,  I would like to alert if this files modified date has not changed in say the last hour (suggesting the script is stopped somehow) but we cannot work out a way to do this in Solar Winds.  Has anyone done this before?

       

      Thanks

       

      Ian

        • Re: Monitoring a heartbeat file
          Justin Walker

          I need something like this too did you ever figure out how to do this?

           

          Justin

          • Re: Monitoring a heartbeat file
            stripet

            Create a SAM Template "File Age Monitor" Component.  It returns the number of hours since the file was last modified (in decimal format so it would report .10 if the file was modified 6 minutes ago):

             

            The path to the file is a UNC Path like as follows:  \\${IP}\c$\temp\filename.txt   (where c$ is the name of the admin share)

             

            File Age Monitor:  This component monitor determines when a file was last modified. The statistic is the number of elapsed hours since the file modification date.

             

            You can also go the VBScript route.  For this script, make sure your logfile is the only file in the directory and you have to change the strPath variable in the script to the UNC path to the directory where your logfile resides (not the file!).  You will also have to modify the following line to the number of minutes old you want to test for (defaults to 5 minutes)   If DiffMinutes > 5 Then

             

            Script Arguments:    ${IP} ${USER} ${PASSWORD} False 1000

            Script Engine:  vbscript

            Script Body:

             

            Option Explicit

            Dim ObjFSO, objArgs, strIP, strUser, strPwd, strComputer

            Dim strMessage, objFolder

            Dim StartDateTime, ModDateTime, DiffMinutes

            Dim strUNCPath, strPath, file, recentFile, DirFileCount

            Dim FileCountThresh, IgnoreDirFileCountCheck

             

            Set ObjFSO = CreateObject("Scripting.FileSystemObject")

             

            strMessage = ""

            FileCountThresh = CLng(1000)

            IgnoreDirFileCountCheck = "False"

             

            strPath = "\C$\windows\temp"

             

            Set objArgs = WScript.Arguments

            'strMessage = strMessage & " -" & Date & " " & Time & " Argument count is: " & ObjArgs.Count & " -"

             

            If ObjArgs.Count < 5 Then

              strIP = objArgs(0)

              strUser = objArgs(1)

              strPwd = objArgs(2)

                    IgnoreDirFileCountCheck = objArgs(3)

                    If IgnoreDirFileCountCheck = "False" Then

                 WScript.Echo "Message: " & strMessage & "------" & Date & " " & Time & "  ERROR You must supply a FileCountThresh value which would be exactly five arguments IP USER PASSWORD IgnoreDirFileCountCheck (True|False) FileCountThreshold (int default is " & FileCountThresh & ")"

                 WScript.Echo "Statistic: 2"

                 WScript.Quit(2)

                    End If

            End If

            If ObjArgs.Count = 5 Then

              strIP = objArgs(0)

              strUser = objArgs(1)

              strPwd = objArgs(2)

                    IgnoreDirFileCountCheck = objArgs(3)

                    FileCountThresh = objArgs(4)

            Else

                    If ObjArgs.Count < 4 Then

                 WScript.Echo "Message: " & strMessage & "------" & Date & " " & Time & "  ERROR You must supply exactly five arguments IP USER PASSWORD IgnoreDirFileCountCheck (True|False) FileCountThreshold (int default is " & FileCountThresh & ")"

                 WScript.Echo "Statistic: 2"

                 WScript.Quit(2)

                    End If

            End If

             

            strComputer = strIP

             

            StartDateTime = Now

             

            strUNCPath = "\\" & strComputer & strPath

             

            'strMessage = strMessage & "--FileCountThresh is: " & FileCountThresh & "--"

            'strMessage = strMessage & "--IgnoreDirFileCountCheck is: " & IgnoreDirFileCountCheck & "--"

             

            Set recentFile = Nothing

             

             

            Set objFolder = ObjFSO.GetFolder(strUNCPath).Files

            DirFileCount = objFolder.Count

            strMessage = strMessage & "  --Number of Files in " & strUNCPath & " is: " & DirFileCount & "--  "

             

            If IgnoreDirFileCountCheck = "False" Then

              If CLng(DirFileCount) > CLng(FileCountThresh) Then

              strMessage = strMessage & "  ---" & Date & " " & Time & " ERROR:  There are more than " & FileCountThresh & " files in " & strUNCPath & " so will not process this directory and exiting and doing nothing---  "

              WScript.Echo "Message: " & strMessage & VbCrLf

              WScript.Echo "Statistic: 2" & VbCrLf

              WScript.Quit(2)

              End If

            End If

             

            'strMessage = strMessage & " --" & Date & " " & Time & " Checking to see if most recent file in " & strUNCPath & " is older than 5 minutes-- "

             

            For Each file in objFolder

                If (recentFile is Nothing) Then

              Set recentFile = file

              ModDateTime = file.DateLastModified

                ElseIf (file.DateLastModified > recentFile.DateLastModified) Then

              Set recentFile = file

              ModDateTime = file.DateLastModified

                End If

            Next

             

            strMessage = strMessage & "  --The most recent file " & recentFile.Name & " has a DateLastModified of " & ModDateTime & "--  "

             

            If recentFile is Nothing Then

              strMessage = strMessage & " ---" & Date & " " & Time & " CRITICAL: There are no files in the directory " & strUNCPath & "!"

              WScript.Echo "Message: " & strMessage & VbCrLf

              WScript.Echo "Statistic: 3"

              WScript.Quit(3)

            Else

              DiffMinutes = DateDiff("n", ModDateTime, StartDateTime)

            ' strMessage = strMessage & "  --The most recent file " & recentFile.Name & " is " & DiffMinutes & " Minutes old.--"

              If DiffMinutes > 5 Then

              strMessage = strMessage & " ------" & Date & " " & Time & " ERROR:  The most recent file " & strUNCPath & "\" & recentFile.Name & " is OLDER THAN 5 MINUTES OLD so you need to check and verify logfiles are getting generated regularly!-----  "

              WScript.Echo "Message: " & strMessage & VbCrLf

              WScript.Echo "Statistic: 3" & VbCrLf

              WScript.Quit(3)

              Else

              strMessage = strMessage & "  --" & Date & " " & Time & " NORMAL:  The most recent file " & strUNCPath & "\" & recentFile.Name & " is " & DiffMinutes & " Minutes old which is less than the threshold of >5 Minutes"

              WScript.Echo "Message: " & strMessage & VbCrLf

              WScript.Echo "Statistic: 0" & VbCrLf

              WScript.Quit(0)

              End If

            End If

              • Re: Monitoring a heartbeat file
                Justin Walker

                Disclaimer I am not a VBscripter so this thing is ugly. This is the route i went and it has tested successfully from SolarWinds

                 

                the arguments are "\\${IP}\C$\directoryofFileToBeMonitored" "File.filename" 5

                The last argument is in minutes so anything older than five minutes alert. Still need some additional testing but I got it to work so far

                 

                 

                Option Explicit

                 

                ' Description

                '--------------------------------------------------------------------------------------------------

                ' Vbscript-based File Age Comparison Monitoring

                '  - a directory name

                '  - a filename

                '  - File Age Threshold in Minutes

                ' to be used by SolarWinds SAM.

                '

                ' Author: Justin Walker

                ' Date: 2013-11-13

                ' Revisions:

                '--------------------------------------------------------------------------------------------------

                ' 2013-11-13 - Original Creation

                '

                ' Declare variables

                '--------------------------------------------------------------------------------------------------

                Dim lstArgs

                Dim FolderToScan, FileName, FileLastMod, MinuteThresh

                Dim objFSO, objFolder, objFile

                Dim objRE, bMatch

                Dim thefile, newestdate, TempFile, MyFile, PreviousTimeArray

                Dim EndOfServer,ServerName, PreviousDateTime, PreviousLogFileSize, PreviousTime,CurrentTime

                Dim CurrentTimeHour, CurrentTimeMin, PreviousTimeHour, PreviousTimeMin, PreviousDate, CurrentDate

                Dim dD, mD, yD, hT, mT

                 

                '--------------------------------------------------------------------------------------------------

                ' Set initial elements, read incoming variables, etc.

                '--------------------------------------------------------------------------------------------------

                Const ForReading = 1

                Set lstArgs = WScript.Arguments

                If lstArgs.Count = 3 Then

                                FolderToScan = Trim(lstArgs(0))

                                FileName = Trim(lstArgs(1))

                                MinuteThresh = Trim(lstArgs(2))

                                MinuteThresh = CInt(MinuteThresh)

                Else

                                WScript.Echo "Message: Usage: wscript.exe <thisscript> <FolderToScan> <FileName> <MinuteThreshold>"

                                WScript.Echo "Statistic: 0"

                                WScript.Quit(1)

                End If

                 

                Set objFSO = CreateObject("Scripting.FileSystemObject")

                '--------------------------------------------------------------------------------------------------

                ' Retrieve the System Name for Temp

                '--------------------------------------------------------------------------------------------------

                EndofServer = instr(3,Wscript.Arguments(0),"\",0)

                ServerName = mid(Wscript.Arguments(0),1,EndofServer)

                 

                TempFile = ServerName & "C$\tmp\SWLastMod.txt"

                 

                '--------------------------------------------------------------------------------------------------

                ' Checking last modified time

                '--------------------------------------------------------------------------------------------------

                 

                'WScript.Echo FolderToScan

                FileLastMod = LastModified(FolderToScan)

                'Wscript.Echo "Statistic.Found: '" & FileLastMod & "'"

                 

                 

                '--------------------------------------------------------------------------------------------------

                ' Check for the temp file, if it doesn't exist this is the first time the script is running

                ' Creates temp file on local machine

                '--------------------------------------------------------------------------------------------------

                If Not objFSO.FileExists(TempFile) then

                                Set MyFile = objFSO.CreateTextFile(TempFile, True)

                                MyFile.Writeline(FileLastMod)

                                MyFile.Close

                                WScript.Echo "Message: VBScript Log Parser Initialized"

                                WScript.Echo "Statistic: 0"

                                Wscript.Quit(0)

                else

                                ' --------------------------------------------------------------------------------

                                ' Check the Temp file for last modified time then updates it

                                ' with new modified time

                                ' --------------------------------------------------------------------------------

                                Set objFile = objFSO.OpenTextFile(TempFile, 1)

                                PreviousDateTime = objFile.Readline() 'Converts to Integer for Comparison below

                                objFile.Close

                                Set objFile = objFSO.OpenTextFile(TempFile,2)

                                objFile.Writeline(FileLastMod)

                                objFile.Close

                end if

                 

                '--------------------------------------------------------------------------------------------------

                ' Comparison of previous time and current time

                '--------------------------------------------------------------------------------------------------

                 

                ' Gets Current Date and Time

                CurrentTime = Time

                CurrentDate  = Date

                 

                ' Splits array of previous time and date into seperate variables

                PreviousTimeArray = Split(PreviousDateTime)

                PreviousDate = PreviousTimeArray(0)

                PreviousTime = PreviousTimeArray(1) & " " & PreviousTimeArray(2)

                 

                'Commented out used for trouble shooting

                'WScript.Echo "Previous Time " & PreviousTime

                'WScript.Echo "Previous Date " & PreviousDate

                'WScript.Echo "Current Time " & CurrentTime

                'WScript.Echo "Current Date " & CurrentDate

                 

                'Gets the difference from the previous date and times to the current date and times

                mD = DateDiff("m",PreviousDate, CurrentDate)

                mD = CInt(mD)

                yD = DateDiff("yyyy",PreviousDate, CurrentDate)

                yD = CInt(yD)

                dD = DateDiff("d",PreviousDate, CurrentDate)

                dD = CInt(dD)

                hT = DateDiff("h", PreviousTime, CurrentTime)

                hT = CInt(hT)

                mT = DateDiff("n", PreviousTime, CurrentTime)

                mT = CInt(mT)

                 

                'Commented out used for trouble shooting

                'WScript.Echo "Month " & mD

                'WScript.Echo "Year " & yD

                'WScript.Echo "Day " & dD

                'WScript.Echo "Hour " & hT

                'WScript.Echo "Minute " & mT

                'WScript.Echo "MinuteThresh " & MinuteThresh

                 

                '--------------------------------------------------------------------------------------------------

                ' If Then Logic to determine if file updated with in MinuteThreshold Variable

                '--------------------------------------------------------------------------------------------------

                 

                If yD = 0 then

                    if mD = 0 then

                        if dD = 0 then

                            if ((hT < 2) and (mT < MinuteThresh)) then

                                if ((hT < 1) and (mT < MinuteThresh)) then

                                    WScript.Echo "Message: File " & mT & " Minutes Old"

                                    WScript.Echo "Statistic: 0"

                                    WScript.Quit(0)

                                Else

                                    WScript.Echo "Message: ERROR! File " & mT & " Minutes Old!"

                                    WScript.Echo "Statistic: 1"

                                    WScript.Quit(0)

                                end if

                            Else

                                WScript.Echo "Message: ERROR! File " & mT & " Minutes Old!"

                                WScript.Echo "Statistic: 1"

                                WScript.Quit(0)

                            end if

                        Else

                            WScript.Echo "Message: ERROR! File " & mT & " Minutes Old!"

                            WScript.Echo "Statistic: 1"

                            WScript.Quit(0)

                        end if

                    Else

                        WScript.Echo "Message: ERROR! File " & mT & " Minutes Old!"

                        WScript.Echo "Statistic: 1"

                        WScript.Quit(0)

                    end if

                Else

                    WScript.Echo "Message: ERROR! File " & mT & " Minutes Old!"

                    WScript.Echo "Statistic: 1"

                    WScript.Quit(0)

                end if

                 

                 

                '--------------------------------------------------------------------------------------------------

                ' LastModified: find and return last modified file matching the pattern specified

                '--------------------------------------------------------------------------------------------------

                Function LastModified(FolderToScan)

                                Set objFolder = objFSO.GetFolder(FolderToScan)

                                Dim arrFileName(), arrFileDateLastModified()

                                Set objRE = New RegExp

                                objRE.Global     = True

                                objRE.IgnoreCase = False

                                objRE.Pattern    = FileName

                                newestdate = ""

                                thefile = ""

                                For Each objFile In objFolder.Files

                                                bMatch = objRE.Test(objFile.Name)

                                                If bMatch Then

                                                                If objFile.DateLastModified > newestdate OR newestdate = "" Then

                                                                                thefile=objFile.Name

                                                                                newestdate=objFile.DateLastModified

                                                                End If

                                                End If

                                Next

                                If thefile = "" Then

                                                Wscript.Echo "Message: No files matched '" & FolderToScan & FileName

                                                Wscript.Echo "Statistic: " & 0

                                                Wscript.Quit(0)

                                End If

                'LastModified = thefile

                LastModified = newestdate

                End Function