2016-12-14 73 views
-1

我有如下返回一個JSON響應Web服務進行比較的閾值在Web服務(JSON響應)返回值:如何使用VBScript

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

要求是創建一個VB腳本它會讀取結果,將其與設定的閾值進行比較並相應地設置一個標誌。 也就是說,如果針對「數據庫」的值爲「true」,cpu使用率小於80%,連接響應大於0且內存使用率低於80%,則需要結果來說「綠色」。

有人可以幫助我的上述要求。這實際上是用於SCOM監視。

+0

JSON格式錯誤,應該是屬性名稱:屬性值。分號*(';')*是JavaScript中的命令終止字符,您應該使用冒號*(':')*代替。 – Lankymart

+0

感謝Lankymart - 這是一個建議的迴應,我將從Web服務接收,因此我寫了一些我認爲會從Web服務中出來的東西。該webservice本身尚未建成。但我試圖找出一種方法來監控它。 –

+0

對不起,我沒有從 - *「我有一個web服務返回一個json響應」*。如果目前這純粹是概念性的,那麼這不是一個正確的問題,因爲問題應該有一個明確的問題,並且可以通過[mcve]輕鬆地爲任何想要幫助的人重新創建。發佈前請檢查[問]。 – Lankymart

回答

-1

你的JSON會更像這樣。請注意,我已更改變量名稱以刪除空格 - 如果這些猜測錯誤,則需要相應地修改代碼。在JSON中,變量名稱和任何非數字值都在引號中。通常你會使用JSON解析器來處理這個問題,但如果真的這麼簡單,你可以使用一些簡單的字符串處理代碼來繼續。

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

調用此函數將它從服務中獲取的JSON字符串傳遞給它。它的工作原理是JSON字符串是一個字符串,如果它是簡單的格式,我們可以將它切斷以獲取可用值。如果它變成一個更復雜的消息,那麼你將需要搜索一個用於VB的JSON解析器,或者如果接口可以用XML響應,你會發現在VB中處理起來要容易得多。

這是VB6代碼(我更容易測試) - 您將需要從變量聲明的VB腳本中刪除所有'as string','as integer'等。我已經包含了一個來自here的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 
+0

請! ** 1。**不要在不完整的問題/離題問題上發佈答案,因爲它只是鼓勵更多答案*(好的指導是,OP已經表明努力自己解決問題)*。 ** 2。**不同語言的源代碼示例*(並且是VB不同於VBScript)*它足夠令人困惑,試圖使它們保持原樣。 – Lankymart

+0

想想你說什麼 - 如果你會考慮更具包容性。我寫這個答案的原因有一半是因爲OP對他的JSON示例不準確,然後是他的英語而引發的火焰。我想我會告訴他,在這裏有一個歡迎,同時給他一個前進的方向,給他留下一些問題來解決他自己。 –

+0

這不是他們說他們有網絡服務的火焰,後來說他們沒有。你提出的是錯誤的假設,我之所以提到這個問題是因爲這是題外話題和題外話題之間的區別。如果他們沒有任何代碼可以顯示,並且一切都是概念性的,那麼他們需要至少嘗試一些事情,然後在他們遇到特定問題時再回來。 – Lankymart