This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

SAM VBscript Issues/Question

Let me start out by saying I'm not a coder anymore and at this point fail in all aspects. For this particular script I outsourced to talented folks that are not me. I've ran the script thru the CLI on the SAM server and everything appears to work correctly. I think I've directed the coder incorrectly for what to echo or how to echo. Could someone provide some insight in what I'm doing incorrect?

----------Code Begins-----------

Dim strFilePath, objFSO, objFile, objRegExp, strPatternLine, strPatternContent, strLine, objMatches, Statistic

Const ForReading = 1, ForWriting = 2, ForAppending = 8

strFilePath = "C:\Testing.htm"

' Create File System and File objects

Set objFSO = CreateObject("Scripting.FileSystemObject")

' Create regular expression object

Set objRegExp = new regexp

' Set RegExp properties

objRegExp.IgnoreCase = true

strPatternLine = "type generic"

' strPatternContent = "\((.*)\)"

strPatternContent = "^.*?\([^\d]*(\d+)[^\d]*\).*$"

If objFSO.FileExists(strFilePath) Then

  ' Open file

  Set objFile = objFSO.OpenTextFile(strFilePath, ForReading)

  ' Parse file

  Do While objFile.AtEndOfStream <> True

  strLine = objFile.ReadLine

  ' Set the appropriate RegExp pattern

  objRegExp.Pattern = strPatternLine

  ' Check for a match

  If objRegExp.Test(strLine) Then

  ' WScript.Echo strLine

  ' Set the appropriate RegExp pattern

  objRegExp.Pattern = strPatternContent

  ' Clear previous matches, if any

  Set objMatches = Nothing

  ' Check for a match

  Set objMatches = objRegExp.Execute(strLine)

  Statistic = ""

  ' Iterate the matches

  For Each objMatch in objMatches

  For i = 0 to (objMatch.SubMatches.Count - 1)

  Statistic = Statistic & objMatch.SubMatches(0) & vbCrlf

  Next

  Next

  WScript.Echo "Statistic.NAT:" & Statistic

  End If

  Loop

  ' Close file

  objFile.Close

Else

  WScript.Echo "File Not Found"

End If

----------Code Ends -----------

When I press the "Get script output" button I get this below message.

pastedImage_0.png

If this looks correct and there isn't any feedback for the code, does anyone know where to look to debug the application as Orion executes it?

  • Hi,

    I'm not sure if I got exactly the logic - specially how it should works, when file contains more then one line, but I did some modification of your script.

    It should be clear, how the output of statistic works:

    Dim strFilePath, objFSO, objFile, objRegExp, strPatternLine, strPatternContent, strLine, objMatches, Statistic

    Const ForReading = 1, ForWriting = 2, ForAppending = 8

    strFilePath = "C:\Testing.htm"

    Const FILEFOUND = 0, FAILED = 1

    sub EchoStats(stats, message)

        WScript.Echo "Message.NAT: - " & Trim(message)

        WScript.Echo "Statistic.NAT: " & stats

    end sub

    sub FailAndExit(message)

        call EchoStats(0, message)

        WScript.Quit( FAILED )

    end sub

    ' Create File System and File objects

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Create regular expression object

    Set objRegExp = new regexp

    ' Set RegExp properties

    objRegExp.IgnoreCase = true

    strPatternLine = "type generic"

    ' strPatternContent = "\((.*)\)"

    strPatternContent = "^.*?\([^\d]*(\d+)[^\d]*\).*$"

    If objFSO.FileExists(strFilePath) Then

      ' Open file

      Set objFile = objFSO.OpenTextFile(strFilePath, ForReading)

      ' Parse file

      Do While objFile.AtEndOfStream <> True

      strLine = objFile.ReadLine

      ' Set the appropriate RegExp pattern

      objRegExp.Pattern = strPatternLine

      ' Check for a match

      If objRegExp.Test(strLine) Then

      ' WScript.Echo strLine

      ' Set the appropriate RegExp pattern

      objRegExp.Pattern = strPatternContent

      ' Clear previous matches, if any

      Set objMatches = Nothing

      ' Check for a match

      Set objMatches = objRegExp.Execute(strLine)

      Statistic = ""

      ' Iterate the matches

      For Each objMatch in objMatches

      For i = 0 to (objMatch.SubMatches.Count - 1)

      Statistic = Statistic & objMatch.SubMatches(0) & vbCrlf

      Next

      Next

      call EchoStats(Statistic,"")

      objFile.Close

      WScript.Quit( FILEFOUND )

      End If

      Loop

      ' Close file

      objFile.Close

      call FailAndExit("File doesn't contain expected content.")

    End If

    call FailAndExit("File Not Found")

  • This is getting me closer. I had put you at a disadvantage because you were not aware of the contents of the file that I'm intending to monitor. The file has three lines that we're interested in. This will be a constant for the file. Based on the code snip "strPatternLine = "type generic"  " we will have the below three lines left to evaluate.

    type generic, total addresses 64, allocated 1 (100%), misses 0<br>

    type generic, total addresses 126, allocated 2 (10%), misses 0<br>

    type generic, total addresses 126, allocated 7 (5%), misses 45255952<br>

    The regex that is in the code should then trim this down to the digits left between the () so that should be 100, 10, and 5.

    The code you provided in the cli exits after the first line and in Orion it doesn't provide any values, it outputs based on the failandexit call with the message "file doesn't contain expected content."

    I think based on the output that I'm seeing I may need to load each lines value into an array that is then echo'd for the output.

  • I would take a look at this if you haven't already;

    Creating a Windows Script Monitor

    All the scripts will require an exit code and a statistic.  You can also include a message with the statistic, but that is optional.  Scripts can output 10 different statistics if you're looking to report on multiple statistics.

  • After sitting down with the programmer that did the first iteration and showing him how Solarwinds handles the script we were able to get the script working the way that I had intended. See below for the final script.

    Dim strFilePath, objFSO, objFile, objRegExp, strContent, objMatches, counter

    Const ForReading = 1, ForWriting = 2, ForAppending = 8

    strFilePath = "c:/Testing.htm"

    ' Create File System and File objects

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Create regular expression object

    Set objRegExp = new regexp

    ' Set RegExp properties

    objRegExp.IgnoreCase = True

    objRegExp.Global = True

    objRegExp.Pattern = "\s*type generic.*\((\d*)\%\).*"

    If objFSO.FileExists(strFilePath) Then

        ' Open file

        Set objFile = objFSO.OpenTextFile(strFilePath, ForReading)

       

        ' Read file

        If Not objFile.AtEndOfStream Then

            strContent = objFile.ReadAll

           

            ' Close file

            objFile.Close

                   

            ' Check for a match

            Set objMatches = objRegExp.Execute(strContent)

                   

            If objMatches.Count > 0 Then

                ' Iterate the matches

                counter = 0

                For Each objMatch in objMatches

                    For i = 0 to (objMatch.SubMatches.Count - 1)

                        WScript.Echo "Statistic.NAT" & counter & ": " & objMatch.SubMatches(i)

                        counter = counter + 1

                    Next

                Next           

            End If

        Else

            WScript.Echo "File Empty"

        End If

       

    Else

        WScript.Echo "File Not Found"

    End If

  • Well, Unfortunately I got my hopes up on this. It ran once, I was able to get the alert which I had executed a remediation script through NCM. The next time it ran it failed and I'm getting results that don't make any sense now. The script runs flawlessly through CLI against the file that gets created via the NCM job. The script is indicating that there is no matches, however, I know there are matches because I can run it via the CLI and see them. See below the most updated script, cli output, output from get output result, and contents of the file being searched.

    ----------- Code Begins --------------

    Dim strFilePath, objFSO, objFile, objRegExp, strContent, objMatches, counter, errorcheck

    Const ForReading = 1, ForWriting = 2, ForAppending = 8

    strFilePath = "C:\NAT\nat.txt"

    ' Create File System and File objects

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Create regular expression object

    Set objRegExp = new regexp

    Set errorcheck = new regexp

    ' Set RegExp properties

    objRegExp.IgnoreCase = True

    objRegExp.Global = True

    objRegExp.Pattern = "\s*type generic.*\((\d*)\%\).*"

    errorcheck.IgnoreCase = True

    errorcheck.Global = True

    errorcheck.Pattern = "\berror\b"

    If objFSO.FileExists(strFilePath) Then

      ' Open file

      Set objFile = objFSO.OpenTextFile(strFilePath, ForReading)

      ' Read file

      If Not objFile.AtEndOfStream Then

      strContent = objFile.ReadAll

      ' Close file

      objFile.Close

      Set errors = errorcheck.Execute(strContent)

      ' Check for a match

      Set objMatches = objRegExp.Execute(strContent)

      If errors.Count = 0 Then

      If objMatches.Count > 0 Then

      ' Iterate the matches

      counter = 0

      For Each objMatch in objMatches

      For i = 0 to (objMatch.SubMatches.Count - 1)

      WScript.Echo "Statistic.NAT" & counter & ": " & objMatch.SubMatches(i)

      counter = counter + 1

      Next

      Next

      WScript.Echo "Finished"

                            WScript.Quit(0)  

      Else

                                    Wscript.Echo "Message: No Match"

                     Wscript.Echo "Statistic: 0"

                                    WScript.Quit(1)

      End If

      Else

      Wscript.Echo "Message: Errors"

      Wscript.Echo "Statistic: 0"

      Wscript.Quit(1)

      End If

      End If

    Else

            Wscript.Echo "Message: File not Found"

      Wscript.Echo "Statistic: 0"

      Wscript.Quit(1)

    End If

    ----------- Code Ends --------------

    pastedImage_0.png

    pastedImage_1.png

    ----------- File contents Begins --------------

    ___________________________________________________________________________

    Job Engine: ORION-MP

    4/22/2016 9:22:05 AM : Started natstats Copy : JobDescription_6bfaa4cb-c767-404a-9055-fd47043639cf

    Execute Command Script on Devices

    2 devices selected

    Devices : 2

    Errors  : 0

    ___________________________________________________________________________

    <Scrubbed>:

      type generic, total addresses 64, allocated 1 (1%), misses 0

      type generic, total addresses 126, allocated 1 (0%), misses 4

      type generic, total addresses 126, allocated 6 (4%), misses 4724

    ___________________________________________________________________________

    <Scrubbed>:

      type generic, total addresses 64, allocated 1 (1%), misses 0

      type generic, total addresses 126, allocated 1 (0%), misses 0

      type generic, total addresses 126, allocated 1 (0%), misses 45255952

    ___________________________________________________________________________

    4/22/2016 9:22:11 AM : Completed natstats Copy : JobDescription_6bfaa4cb-c767-404a-9055-fd47043639cf

    Execution time : 6 seconds

    ___________________________________________________________________________

    ----------- File contents Ends --------------

  • Ok, The issue was with how I was monitoring the node that I was running against. Even though it was my main poller, I had it being monitored by my additional poller, so it was actually running the script on the Additional poller not the Main Poller. After changing the main poller to be monitored by itself I'm working. I have no explanation as to why this worked the first time through. Thanks to jeff.stewart​ for his offline assistance sparking me to look at this.