-1

我有一个 Web 服务,它返回一个 json 响应,如下所示:

"database" ; True
"cpu usage" ; 30%
"connection response" ; 1
"memory" ; 48%

要求是创建一个 vb 脚本,该脚本将读取结果,将其与设置的阈值进行比较并相应地设置一个标志。也就是说,如果“数据库”的值为“真”,cpu 使用率小于 80%,连接响应大于 0,内存使用率小于 80%,我需要结果显示“绿色”。

有人可以帮我解决上述要求。这实际上是与 SCOM 监控一起使用的。

4

1 回答 1

-1

你的 JSON 会更像这样。请注意,我已更改变量名称以删除空格 - 如果这些猜测错误,您将需要相应地修改代码。在 JSON 中,变量名称和任何非数字值都用引号引起来。通常你会使用 JSON 解析器来处理这个,但如果它真的这么简单,你可以使用一些简单的字符串处理代码来继续。

{
"database": "true",
"cpu_usage": 30,
"connection_response": 1,
"memory": 48
}

调用此函数,将您从服务中获得的 JSON 字符串传递给它。它的工作原理是 JSON 字符串是一个字符串,如果它是简单格式,我们可以将其切碎以获得可用值。如果它变成更复杂的消息,那么您将需要为 VB 搜索 JSON 解析器,或者如果接口可以以 XML 响应,您会发现在 VB 中处理起来更容易。

这是 VB6 代码(对我来说更容易测试) - 您需要从为 VB 脚本声明的变量中删除所有“作为字符串”、“作为整数”等。我已经为来自这里的 VBScript 包含了一个 val() 函数,尽管没有用我的函数进行测试。您需要 val() 因为 JSON 是字符串格式的,如果您尝试将数值与字符串进行比较,您会得到意想不到的结果。

'
' Function to return RED or GREEN depending on values in simple JSON
'
Function checkStatus(sJSON As String) As String

Dim aVals() As String, aParams() As String, i As Integer, sName As String, sVal As String
Dim bDatabase As Boolean, bCPU As Boolean, bConnection As Boolean, bMemory As Boolean

aVals = Split(sJSON, ",")
For i = 0 To UBound(aVals)

    aVals(i) = Trim(aVals(i)) ' remove any leading & trailing spaces
    aVals(i) = Replace(aVals(i), "{", "") ' remove braces open
    aVals(i) = Replace(aVals(i), "}", "") ' remove braces close
    aVals(i) = Replace(aVals(i), """", "") ' remove quotes > "database: true"
    Debug.Print "vals[" & i & "]=" & aVals(i)

    If Len(aVals(i)) > 0 Then ' should catch any dodgy JSON formatting but may need refinement

        aParams = Split(aVals(i), ":") ' split the line e.g. "database: true" > "database" and " true"
        If UBound(aParams) > 0 Then

            sName = LCase(Trim(aParams(0)))  ' now we have sName = "database"
            sVal = LCase(Trim(aParams(1)))   ' and sVal = "true"

            Select Case sName
                Case "database"

                    bDatabase = False
                    If sVal = "true" Then
                        bDatabase = True
                    End If

                Case "cpu_usage"
                    bCPU = False
                    If Val(sVal) > 80 Then
                        bCPU = True
                    End If

                Case "connection_response"
                    bConnection = False
                    If Val(sVal) > 0 Then
                        bConnection = True
                    End If


                Case "memory"
                    bMemory = False
                    If Val(sVal) < 80 Then
                        bMemory = True
                    End If


            End Select
        End If
    End If
Next i

checkStatus = "RED" ' default return value to indicate an issue

' compare the flags to decide if all is well.
If bDatabase And bCPU Then  'And bConnection And bMemory Then
    checkStatus = "GREEN"
End If


End Function

Function Val( myString )
' Val Function for VBScript (aka ParseInt Function in VBScript).
' By Denis St-Pierre.
' Natively VBScript has no function to extract numbers from a string.
' Based shamelessly on MS' Helpfile example on RegExp object.
' CAVEAT: Returns only the *last* match found
'         (or, with objRE.Global = False, only the *first* match)

    Dim colMatches, objMatch, objRE, strPattern

    ' Default if no numbers are found
    Val = 0

    strPattern = "[-+0-9]+"       ' Numbers positive and negative; use
                                  ' "ˆ[-+0-9]+" to emulate Rexx' Value()
                                  ' function, which returns 0 unless the
                                  ' string starts with a number or sign.
    Set objRE = New RegExp        ' Create regular expression object.
    objRE.Pattern    = strPattern ' Set pattern.
    objRE.IgnoreCase = True       ' Set case insensitivity.
    objRE.Global     = True       ' Set global applicability:
                                  '   True  => return last match only,
                                  '   False => return first match only.
    Set colMatches = objRE.Execute( myString )  ' Execute search.
    For Each objMatch In colMatches             ' Iterate Matches collection.
        Val = objMatch.Value
    Next
    Set objRE= Nothing
End Function
于 2016-12-14T13:10:14.203 回答