如何使用 vbscript 将 Web 服务(json 响应)中返回的值与阈值进行比较
how to compare the values returned in a web service (json response) against a threshold using vbscript
我有一个网络服务 returns json 响应如下:
"database" ; True
"cpu usage" ; 30%
"connection response" ; 1
"memory" ; 48%
要求是创建一个 vb 脚本来读取结果,将其与设定的阈值进行比较并相应地设置一个标志。
也就是说,如果针对 "database" 的值是 "true",我需要结果说 "green",cpu 使用率小于 80%,连接响应大于 0 和内存使用率低于 80%。
有人可以帮我解决上述问题吗?这个其实是要配合SCOM监控使用的
你的JSON会更像这样。请注意,我已经更改了变量名称以删除空格 - 如果这些猜测是错误的,您将需要相应地修改代码。在 JSON 中,变量名称和任何非数字值都用引号引起来。通常你会使用 JSON 解析器来处理这个,但如果它真的这么简单,你可以使用一些简单的字符串处理代码来继续。
{
"database": "true",
"cpu_usage": 30,
"connection_response": 1,
"memory": 48
}
调用此函数并将您从服务中获取的 JSON 字符串传递给它。它的工作原理是 JSON 字符串是一个字符串,如果它是简单格式,我们可以将其切碎以获得可用值。如果它变得更复杂,那么您将需要为 VB 搜索 JSON 解析器,或者如果接口可以在 XML 中响应,您会发现在 VB。
这是 VB6 代码(对我来说更容易测试)- 您需要从为 [= 声明的变量中删除所有 'as string'、'as integer' 等28=] 脚本。我已经为来自 here 的 VB 脚本包含了一个 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
我有一个网络服务 returns json 响应如下:
"database" ; True
"cpu usage" ; 30%
"connection response" ; 1
"memory" ; 48%
要求是创建一个 vb 脚本来读取结果,将其与设定的阈值进行比较并相应地设置一个标志。 也就是说,如果针对 "database" 的值是 "true",我需要结果说 "green",cpu 使用率小于 80%,连接响应大于 0 和内存使用率低于 80%。
有人可以帮我解决上述问题吗?这个其实是要配合SCOM监控使用的
你的JSON会更像这样。请注意,我已经更改了变量名称以删除空格 - 如果这些猜测是错误的,您将需要相应地修改代码。在 JSON 中,变量名称和任何非数字值都用引号引起来。通常你会使用 JSON 解析器来处理这个,但如果它真的这么简单,你可以使用一些简单的字符串处理代码来继续。
{
"database": "true",
"cpu_usage": 30,
"connection_response": 1,
"memory": 48
}
调用此函数并将您从服务中获取的 JSON 字符串传递给它。它的工作原理是 JSON 字符串是一个字符串,如果它是简单格式,我们可以将其切碎以获得可用值。如果它变得更复杂,那么您将需要为 VB 搜索 JSON 解析器,或者如果接口可以在 XML 中响应,您会发现在 VB。
这是 VB6 代码(对我来说更容易测试)- 您需要从为 [= 声明的变量中删除所有 'as string'、'as integer' 等28=] 脚本。我已经为来自 here 的 VB 脚本包含了一个 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