'**********************************************************************************
' Script Name - Huawei HostGroup Properites Discovery
' Author: Muralidhar L, Vignesh Pandian
'
' Description:
' Discovery of Huawei HostGroup and all it's attributes.
'
' (c) Copyright Huawei Inc. 2013-2015. All rights reserved
'**********************************************************************************
Option Explicit
SetLocale("en-us")
Dim oAPI, oDiscoveryData, oInst
Set oAPI = CreateObject("MOM.ScriptAPI")
Dim oArgs
set oArgs = wscript.arguments
If oArgs.Count > 10 Then
Call oAPI.LogScriptEvent("DiscoverHostGroupProperites.vbs",101,0, _
"script was called with fewer than three arguments and was not executed.")
Wscript.Quit -1
End If
' Declaration of Script argument and File logging variables
Dim logMessage, logLevel, debugLevel, objLogTextFile, objLogFSO,isOpen
logLevel = oArgs(6)
isOpen = 0
' Begin Logheader
Sub OpenLogFile()
If logLevel <> 0 Then
Dim fileSize, strOutFile
On Error Resume Next
Const strFileName = "Discovery_HostGroupProperites.log"
Const ForAppending = 8
Const ForWriting = 2
Const MaxFileSize = 5242880 'File Size is limited to 512 KB
dim wsShell : Set wsShell = Createobject("WScript.Shell")
dim tFolder : tFolder = wsShell.ExpandEnvironmentStrings("%ProgramFiles%")
Set objLogFSO = CreateObject("Scripting.FileSystemObject")
If Not objLogFSO.FolderExists(tFolder) Then
objLogFSO.CreateFolder(tFolder)
End If
tFolder = tFolder & "\Huawei"
If Not objLogFSO.FolderExists(tFolder) Then
objLogFSO.CreateFolder(tFolder)
End If
tFolder = tFolder & "\SCOM"
If Not objLogFSO.FolderExists(tFolder) Then
objLogFSO.CreateFolder(tFolder)
End If
strOutFile = tFolder & "\" & strFileName
If Not objLogFSO.FileExists(strOutFile) Then
Set objLogTextFile = objLogFSO.CreateTextFile(strOutFile)
objLogTextFile.Close()
End If
fileSize = objLogFSO.GetFile(strOutFile).Size
Dim k
k = 0
Do
Err.Clear
WScript.Sleep(1000)
On Error Resume Next
If (fileSize > MaxFileSize) Then
Set objLogTextFile = objLogFSO.OpentextFile(strOutFile, ForWriting, True)
Else
Set objLogTextFile = objLogFSO.OpentextFile(strOutFile, ForAppending, True)
End If
k = k + 1
Loop While Err.Number <> 0
logMessage = "INFO: Script - HostGroup Properites Discovery : Start()"& k
MOMDebugLog 1, logMessage
logMessage = "INFO: Script - HostGroup(" & oArgs(3) &") Belongs to Huawei Device IP Address :"& oArgs(8)
MOMDebugLog 1, logMessage
Err.Clear
isOpen = 1
End If
End Sub
Sub MOMDebugLog (ByVal debugLevel, ByVal Message)
On Error Resume Next
If (debugLevel > 0) and (debugLevel <= logLevel) Then
objLogTextFile.WriteLine(Now() & " ---- " & Message)
Err.Clear
End If
End Sub
' End of Logheader
Dim SourceID, ManagedEntityID, deviceKey, hostGroupID, hostGroupName, idList
Set oDiscoveryData = oAPI.CreateDiscoveryData(0, SourceID, ManagedEntityID)
Dim oFSo
Set oFso = CreateObject("Scripting.FileSystemObject")
Dim arVol, Vol, arHostGroupName,aridList, i
arVol = split(hostGroupID,Chr(32))
arHostGroupName = split(hostGroupName,Chr(32))
aridList = split(idList,Chr(32))
i =0
For Each Vol in arVol
Set oInst = oDiscoveryData.CreateClassInstance("$MPElement[Name='Huawei.ISM.Management.Pack.huawei.Class.HostGroup']$")
Call oInst.AddProperty ("$MPElement[Name='SNL!System.NetworkManagement.Node']/DeviceKey$", deviceKey)
Call oInst.AddProperty ("$MPElement[Name='Huawei.ISM.Management.Pack.huawei.Class.HostGroup']/id$", Vol)
If UBound(arHostGroupName) < i Then
Call oInst.AddProperty("$MPElement[Name='Huawei.ISM.Management.Pack.huawei.Class.HostGroup']/name$", "")
Else
Call oInst.AddProperty("$MPElement[Name='Huawei.ISM.Management.Pack.huawei.Class.HostGroup']/name$", UTF8BadDecode(arHostGroupName(i)))
End If
If UBound(aridList) < i Then
Call oInst.AddProperty("$MPElement[Name='Huawei.ISM.Management.Pack.huawei.Class.HostGroup']/idList$", "")
Else
Call oInst.AddProperty("$MPElement[Name='Huawei.ISM.Management.Pack.huawei.Class.HostGroup']/idList$", aridList(i))
End If
Call oDiscoveryData.AddInstance(oInst)
i = i+1
Next
Public Function OS(system)
select Case system
Case 0
OS="Linux"
Case 1
OS="Windows"
Case 2
OS="Solaris"
Case 3
OS="HP-UX"
Case 4
OS="AIX"
Case 5
OS="enServer"
Case 6
OS="Mac OS"
Case 7
OS="VMware ESX"
Case 8
OS="Linux"
Case 9
OS="Windows Server 2012"
Case Else
OS=""
End select
End Function
'形式类如code=E7-0x-0x93-0x9C-0xE7-0x93-0x9C-0x5F-0x30-0x30-0x39
Public Function UTF8BadDecode(ByVal code)
If code = "" Then
UTF8BadDecode = ""
Exit Function
End If
Dim tmp
Dim decodeStr
Dim codelen
Dim leftStr
Dim result
Dim str
str = code
result = ""
On Error Resume Next
code = Trim(code)
'将字符串中的 “-0x”替换为“.”
code = Replace(code, "-0x", ".")
'获取左边的第一个字符
leftStr = Left(code, 1)
If leftStr = "" Then
UTF8BadDecode = ""
Exit Function
ElseIf leftStr <> "." Then
result = str
ElseIf leftStr = "." Then
codelen = Len(code)
If (Mid(code, 2, 1) = "C" Or Mid(code, 2, 1) = "B") Then
decodeStr = Replace(Mid(code, 1, 6), ".", "")
tmp = c10ton(CLng("&H" & Hex(CLng("&H" & decodeStr) And &H1F3F)), 2)
tmp = String(16 - Len(tmp), "0") & tmp
result = result & ChrW(CLng("&H" & c2to16(Mid(tmp, 3, 4)) & c2to16(Mid(tmp, 7, 2) & Mid(tmp, 11, 2)) & Right(decodeStr, 1))) & UTF8BadDecode(Right(code, codelen - 6))
ElseIf (Mid(code, 2, 1) = "E") Then
decodeStr = Replace(Mid(code, 1, 9), ".", "")
tmp = c10ton((CLng("&H" & Mid(Hex(CLng("&H" & decodeStr) And &HF3F3F), 2, 3))), 2)
tmp = String(10 - Len(tmp), "0") & tmp
result = result & ChrW(CLng("&H" & (Mid(decodeStr, 2, 1) & c2to16(Mid(tmp, 1, 4)) & c2to16(Mid(tmp, 5, 2) & Right(tmp, 2)) & Right(decodeStr, 1)))) & UTF8BadDecode(Right(code, codelen - 9))
Else
result = Chr(CLng("&H" & (Mid(code, 2, 2)))) & UTF8BadDecode(Right(code, codelen - 3))
End If
End If
If Err.Number <> 0 Then
result = str
If isOpen = 0 Then
OpenLogFile()
End IF
logMessage = "ERROR : Script -Hexadecimal string parsing error.code:" & str
MOMDebugLog 1, logMessage
End If
Err.Clear
UTF8BadDecode = result
End Function
'二进制代码转换为十六进制代码
Public Function c2to16(ByVal x)
On Error Resume Next
Dim i, strinfo
i = 1
strinfo = ""
For i = 1 To Len(x) Step 4
strinfo = strinfo & Hex(c2to10(Mid(x, i, 4)))
Next
c2to16 = strinfo
If Err.Number <> 0 Then
If isOpen = 0 Then
OpenLogFile()
End IF
logMessage = "ERROR : Script -Binary code into hex code error.code:" & x
MOMDebugLog 1, logMessage
End If
Err.Clear
End Function
'二进制代码转换为十进制代码
Public Function c2to10(ByVal x)
On Error Resume Next
c2to10 = 0
If x = "0" Then Exit Function
Dim i
i = 0
For i = 0 To Len(x) - 1
If Mid(x, Len(x) - i, 1) = "1" Then c2to10 = c2to10 + 2 ^ (i)
Next
If Err.Number <> 0 Then
If isOpen = 0 Then
OpenLogFile()
End IF
logMessage = "ERROR : Script -Binary to decimal code error.code:" & x
MOMDebugLog 1, logMessage
End If
Err.Clear
End Function
'10进制转n进制(默认2)
Public Function c10ton(ByVal x, ByVal n)
On Error Resume Next
Dim i
i = x \ n
If i > 0 Then
If x Mod n > 10 Then
c10ton = c10ton(i, n) + Chr(x Mod n + 55)
Else
c10ton = c10ton(i, n) + CStr(x Mod n)
End If
Else
If x > 10 Then
c10ton = Chr(x + 55)
Else
c10ton = CStr(x)
End If
End If
If Err.Number <> 0 Then
If isOpen = 0 Then
OpenLogFile()
End IF
logMessage = "ERROR : Script -Decimal code into binary code error.code:" & x
MOMDebugLog 1, logMessage
End If
Err.Clear
End Function
Call oAPI.Return(oDiscoveryData)
Set oDiscoveryData = Nothing
Set oAPI =Nothing
set oArgs = Nothing
' Close file used for logs
If logLevel <> 0 And isOpen <> 0 Then
objLogTextFile.Close
Set objLogTextFile = Nothing
Set objLogFSO = Nothing
End If