NiCE Library Computer Class Discovery Probe

NiCE.Library.X.Computer.Discovery.ExecuteShellCommand.Probe (ProbeActionModuleType)

Probe Action to Discover NiCE Library Computer Class

Element properties:

TypeProbeActionModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
InputTypeSystem.BaseData
OutputTypeSystem.Discovery.Data

Member Modules:

ID Module Type TypeId RunAs 
Shell ProbeAction NiCE.Library.X.ShellCommand.Probe Default
VBS ProbeAction Microsoft.Windows.ScriptDiscoveryProbe Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
DiscoverRollupbool$Config/DiscoverRollup$Enable health rollup discoveryThis override can be used to enable/disable the discovery of the health rollup between Unix/Linux Computer and NiCE Library computer.
Tracingbool$Config/Tracing$Tracing

Source Code:

<ProbeActionModuleType ID="NiCE.Library.X.Computer.Discovery.ExecuteShellCommand.Probe" Accessibility="Public" Batching="false" PassThrough="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="TargetSystem" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="0" name="DiscoverRollup" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="Tracing" type="xsd:boolean"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="DiscoverRollup" Selector="$Config/DiscoverRollup$" ParameterType="bool"/>
<OverrideableParameter ID="Tracing" Selector="$Config/Tracing$" ParameterType="bool"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<ProbeAction ID="Shell" TypeID="NiCE.Library.X.ShellCommand.Probe">
<Command>echo UNAME_S=`uname -s`; echo '~~~'; echo UNAME_M=`uname -m`; echo '~~~'; grep NICE_BIN_DIR /var/opt/NiCE/conf/nice_base.env; echo '~~~'; grep NICE_DATA_DIR /var/opt/NiCE/conf/nice_base.env</Command>
<TargetSystem>$Config/TargetSystem$</TargetSystem>
</ProbeAction>
<ProbeAction ID="VBS" TypeID="Windows!Microsoft.Windows.ScriptDiscoveryProbe">
<ScriptName>NiCELibraryComputerDiscoveryShell.vbs</ScriptName>
<Arguments>"$Data[Default='']///*[local-name()='StdOut' or local-name()='ErrorCode']$" "$Data[Default='']///*[local-name()='StdErr' or local-name()='Message']$"</Arguments>
<ScriptBody><Script>'-------------------------------------------------------------------------------
'* VBScript Name: NiCELibraryComputerDiscoveryShell.vbs
'* Description : Discover NiCEBinDirectory, NiCEDataDirectory, NiCEPerlDir
'-------------------------------------------------------------------------------
Option explicit
SetLocale("en-us")

Dim oAPI, SourceId, ManagedEntityId, oDiscoveryData, oDiscoveryInstance, oUnixDiscoveryInstance, oRelationshipInstance
Dim g_szComputerName, szValue, g_szBaseEnvLocation, g_bDiscoverRollup, szUname_s, szUname_m
Dim aData, szData, szArgument
Dim Logger

'Start by setting up API object and creating a discovery data object.
Set oAPI = CreateObject("MOM.ScriptAPI")

'Setup variables sent by SCOM

SourceId = CStr("$MPElement$") 'GUID of discovery calling the script. Provided by the MPElement variable.
ManagedEntityId = CStr("$Target/Id$") 'GUID of target object. Provided by the Target/Id variable.
g_szComputerName = CStr("$Config/TargetSystem$")
g_szBaseEnvLocation = CStr("/var/opt/NiCE/conf/nice_base.env")

g_bDiscoverRollup = true
If InStr(1,"$Config/DiscoverRollup$","False", vbTextCompare) Then
g_bDiscoverRollup = false
End If

Set Logger = new NiCELogger.Init("","","$Config/Tracing$")

'******************************************************************
' MAIN
'******************************************************************
Dim FUNCTIONNAME : FUNCTIONNAME = "MAIN"
Set oDiscoveryData = oAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)

Call Logger.LogDebug(FUNCTIONNAME,"Script started with Arguments Count:" &amp; WScript.Arguments.Count)

'Need to check arguments for all scripts.
If WScript.Arguments.Count &gt; 0 then
Call Logger.LogDebug(FUNCTIONNAME,"Arguments(0):" &amp; WScript.Arguments.Item(0))

If WScript.Arguments.Item(1) &lt;&gt; "" Then
Call Logger.LogError(FUNCTIONNAME,"The NiCE Library Computer Discovery for Computer '" &amp; g_szComputerName &amp; "' failed with error:" &amp; VBCrLf &amp; WScript.Arguments.Item(1))
ElseIf WScript.Arguments.Item(0) &lt;&gt; "" Then
Call Logger.LogDebug(FUNCTIONNAME,"Discovery for Computer: " &amp; g_szComputerName)
Set oDiscoveryInstance = oDiscoveryData.CreateClassInstance("$MPElement[Name='NiCE.Library.X.Computer']$")
Call oDiscoveryInstance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", g_szComputerName)
Call oDiscoveryInstance.AddProperty("$MPElement[Name='Unix!Microsoft.Unix.Computer']/PrincipalName$", g_szComputerName)
Call oDiscoveryInstance.AddProperty("$MPElement[Name='NiCE.Library.X.Computer']/NiCEBaseEnvLocation$", g_szBaseEnvLocation)

szArgument = WScript.Arguments.Item(0)
szArgument = Replace(szArgument, "\n", "")
Call Logger.LogDebug(FUNCTIONNAME,"szArgument: " &amp; szArgument)
aData = Split(szArgument, "~~~")
'aData = Split(WScript.Arguments.Item(0), "\n")
For Each szData in aData
If inStr(szData,"NICE_BIN_DIR=") &gt; 0 Then
szData = Trim(szData)
szValue = Right(szData,Len(szData) - Len("NICE_BIN_DIR="))
Call Logger.LogDebug(FUNCTIONNAME,"NiCEBinDirectory: " &amp; szValue)
Call oDiscoveryInstance.AddProperty("$MPElement[Name='NiCE.Library.X.Computer']/NiCEBinDirectory$", szValue)
End If
If inStr(szData,"NICE_DATA_DIR=") &gt; 0 Then
szData = Trim(szData)
szValue = Right(szData,Len(szData) - Len("NICE_DATA_DIR="))
Call Logger.LogDebug(FUNCTIONNAME,"NiCEDataDirectory: " &amp; szValue)
Call oDiscoveryInstance.AddProperty("$MPElement[Name='NiCE.Library.X.Computer']/NiCEDataDirectory$", szValue)
End If
If inStr(szData,"UNAME_S=") &gt; 0 Then
szData = Trim(szData)
szUname_s = Right(szData,Len(szData) - Len("UNAME_S="))
Call Logger.LogDebug(FUNCTIONNAME,"Uname_s: " &amp; szUname_s)

End If
If inStr(szData,"UNAME_M=") &gt; 0 Then
szData = Trim(szData)
szUname_m = Right(szData,Len(szData) - Len("UNAME_M="))
Call Logger.LogDebug(FUNCTIONNAME,"Uname_m: " &amp; szUname_m)
End If
Next

szValue = "Unknown"
Select Case szUname_s
Case "HP-UX"
If inStr(szUname_m,"ia64") &gt; 0 Then
szValue = "hpia"
Else
szValue = "hppa"
End If
Case "Linux"
If inStr(szUname_m,"86_64") &gt; 0 Then
szValue = "linux_x86_64"
Else
If inStr(szUname_m,"86") &gt; 0 Then
szValue = "linux_x86"
End If
If inStr(szUname_m,"s390x") &gt; 0 Then
szValue = "linux_s390x"
End If
If inStr(szUname_m,"ppc64le") &gt; 0 Then
szValue = "linux_ppc64le"
End If
End If
Case "SunOS"
If inStr(szUname_m,"sun") &gt; 0 Then
szValue = "sol"
End If
If inStr(szUname_m,"86") &gt; 0 Then
szValue = "sol_x86"
End If
Case "AIX"
szValue = "aix"
End Select

Call Logger.LogDebug(FUNCTIONNAME,"Architecture: " &amp; szValue)
Call oDiscoveryInstance.AddProperty("$MPElement[Name='NiCE.Library.X.Computer']/Architecture$", szValue)

Call oDiscoveryData.AddInstance(oDiscoveryInstance)

If g_bDiscoverRollup Then
Set oUnixDiscoveryInstance = oDiscoveryData.CreateClassInstance("$MPElement[Name='NiCE.Library.Unix.Computer']$")
Call oUnixDiscoveryInstance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", g_szComputerName)
Call oUnixDiscoveryInstance.AddProperty("$MPElement[Name='Unix!Microsoft.Unix.Computer']/PrincipalName$", g_szComputerName)
Call oDiscoveryData.AddInstance(oUnixDiscoveryInstance)

Set oRelationshipInstance = oDiscoveryData.CreateRelationshipInstance("$MPElement[Name='NiCE.Library.X.Unix.Computer.Contains.NiCE.LocalApplication']$")
oRelationshipInstance.Source = oUnixDiscoveryInstance
oRelationshipInstance.Target = oDiscoveryInstance
Call oDiscoveryData.AddInstance(oRelationshipInstance)
End If
End If
End If


'And now we just return the discovery data
Call oAPI.Return(oDiscoveryData)

'******************************************************************
' Classes
'******************************************************************

'==================================================================================
' Class: NiCELogger
' Usage: Set Logger = new NiCELogger.Init("NiCEDataDirectory","ProductName","Tracing")
' Set Logger = new NiCELogger.Init("","","Tracing")
' Call Logger.LogDebug("funcname","message")
' Call Logger.LogError("funcname","message")
' Call Logger.LogWarning("funcname","message")
'==================================================================================
Class NiCELogger
Private m_TracingEnabled
Private m_ProductName
Private m_NiCEDataDirectory
Private m_LogDirectory
Private m_ConfDirectory
Private m_LogFileName
Private m_objFSO
Private m_WshShell
Private m_oAPI

'==================================================================================
' Property: TracingEnabled
' Returns: true or false
'==================================================================================
Public Property Get TracingEnabled
TracingEnabled = m_TracingEnabled
End Property

Public Default Function Init(NiCEDataDirectory, ProductName, Tracing)
Set m_objFSO = CreateObject("Scripting.FileSystemObject")
Set m_oAPI = CreateObject("MOM.ScriptAPI")
Set m_WshShell = WScript.CreateObject("WScript.Shell")
m_TracingEnabled = false

If InStr(1,Tracing,"True", vbTextCompare) Then
m_TracingEnabled = true
End If

m_LogFileName = Replace(WScript.ScriptName,".vbs","_Trace.log")
m_NiCEDataDirectory = RemoveSlash(CStr(NiCEDataDirectory))
m_ProductName = CStr(ProductName)
if m_ProductName &lt;&gt; "" And m_NiCEDataDirectory &lt;&gt; "" Then
m_LogDirectory = m_NiCEDataDirectory &amp; "\log\" &amp; m_ProductName &amp; "\"
m_ConfDirectory = m_NiCEDataDirectory &amp; "\conf\" &amp; m_ProductName &amp; "\"
Else
m_LogDirectory = RemoveSlash(m_WshShell.ExpandEnvironmentStrings("%temp%")) &amp; "\"
m_ConfDirectory = RemoveSlash(m_WshShell.ExpandEnvironmentStrings("%temp%")) &amp; "\"
End If

Call InitSpiCfg()

Set Init = Me
End Function

Private Sub InitSpiCfg()
Dim FUNCTIONNAME : FUNCTIONNAME = "NiCELogger::InitSpiCfg"
If m_TracingEnabled = false Then
Dim SpiCfgobjFile, SpiCfgFile, szSpiCfgLine
Dim TraceEnabled : TraceEnabled = false
Dim TraceProcess : TraceProcess = false
Dim SpiCfgPath : SpiCfgPath = m_ConfDirectory &amp; m_ProductName &amp; "_spi.cfg"

If not m_objFSO.FileExists(SpiCfgPath) Then
Dim temppath : temppath = RemoveSlash(m_WshShell.Environment("System")("TEMP"))
SpiCfgPath = temppath &amp; "\" &amp; m_ProductName &amp; "_spi.cfg"
m_LogDirectory = temppath &amp; "\"
m_ConfDirectory = temppath &amp; "\"
End If

If m_objFSO.FileExists(SpiCfgPath) Then
Set SpiCfgobjFile = m_objFSO.GetFile(SpiCfgPath)
If SpiCfgobjFile.Size &gt; 0 Then
Set SpiCfgFile = m_objFSO.OpenTextFile(SpiCfgPath,1)
Do While SpiCfgFile.AtEndOfStream &lt;&gt; True
szSpiCfgLine = SpiCfgFile.ReadLine
If InStr(1,szSpiCfgLine,"SPI_TRACE_STATUS", vbTextCompare) and InStr(1,szSpiCfgLine,"ON", vbTextCompare) Then
TraceEnabled = true
End If

If InStr(1,szSpiCfgLine,"SPI_TRACE_PROCESS", vbTextCompare) and (InStr(1,szSpiCfgLine,"ALL", vbTextCompare) or InStr(1,szSpiCfgLine,WScript.ScriptName, vbTextCompare)) Then
TraceProcess = true
End If
Loop
End If
End If

If TraceEnabled = true and TraceProcess = true Then
m_TracingEnabled = true
End If
End If
End Sub

'==================================================================================
' Sub: LogError
' Purpose: Logs an error event to the operations manager event log
'==================================================================================
Public Sub LogError(FUNCTIONNAME, szMessage)
Call LogEvent(FUNCTIONNAME,6121,1,szMessage)
Call LogDebugInternal(FUNCTIONNAME, "ERROR", szMessage)
End Sub

'==================================================================================
' Sub: LogWarning
' Purpose: Logs a warning event to the operations manager event log
'==================================================================================
Public Sub LogWarning(FUNCTIONNAME, szMessage)
Call LogEvent(FUNCTIONNAME,6122,2,szMessage)
Call LogDebugInternal(FUNCTIONNAME, "WARNING", szMessage)
End Sub

'==================================================================================
' Sub: LogInfo
' Purpose: Logs an info event to the operations manager event log
'==================================================================================
Public Sub LogInfo(FUNCTIONNAME, szMessage)
Call LogEvent(FUNCTIONNAME,6123,4,szMessage)
Call LogDebugInternal(FUNCTIONNAME, "INFO", szMessage)
End Sub

'==================================================================================
' Sub: LogDebug
' Purpose: Logs a debug message in the log file
'==================================================================================
Public Sub LogDebug(FUNCTIONNAME, szMessage)
Call LogDebugInternal(FUNCTIONNAME, "DEBUG", szMessage)
End Sub

Private Sub LogDebugInternal(FUNCTIONNAME, szServerity, szMessage)
If m_TracingEnabled = True Then
Dim LogFile
szMessage = Now &amp; " - " &amp; szServerity &amp; " - " &amp; FUNCTIONNAME &amp; " - " &amp; szMessage
If not m_objFSO.FolderExists(m_LogDirectory) then
CreateFolders(m_LogDirectory)
End If

If m_objFSO.FileExists(m_LogDirectory &amp; m_LogFileName) Then
Set LogFile = m_objFSO.OpenTextFile(m_LogDirectory &amp; m_LogFileName,8)
Call LogFile.WriteLine(szMessage)
Else
Set LogFile = m_objFSO.CreateTextFile(m_LogDirectory &amp; m_LogFileName,true)
Call LogFile.WriteLine(szMessage)
End If
End If
End Sub

'==================================================================================
' Sub: LogEvent
' Purpose: Logs an event to the Operations Manager event log
'==================================================================================
Private Sub LogEvent(FUNCTIONNAME,EventNumber, EventLevel, szMessage)
szMessage = FUNCTIONNAME &amp; VbCrLf &amp; szMessage
Dim nLength, nNum, nStart, szNewMessage, i
nLength = Len(szMessage) / 30000
nNum = CInt(nLength + 0.5)

If nNum &gt; 1 Then ' we have more than 30000 chars, split them into multiple parts'
For i = 0 to nNum-1
szNewMessage = "Part " &amp; i+1 &amp; "/" &amp; nNum &amp; VbCrLf
nStart = 1+(i*30000)
szNewMessage = szNewMessage &amp; Mid(szMessage, nStart, 30000)
Call m_oAPI.LogScriptEvent(WScript.ScriptName, EventNumber, EventLevel, szNewMessage)
Next
Else
Call m_oAPI.LogScriptEvent(WScript.ScriptName, EventNumber, EventLevel, szMessage)
End If
End Sub

'==================================================================================
' Function: RemoveSlash
' Purpose: removes the last char of a string if it a forward or backward slash
'==================================================================================
Public Function RemoveSlash(Path)
Dim szLastChar
szLastChar = Right(Path,1)
If szLastChar = "\" OR szLastChar = "/" Then
removeSlash = Left(Path,Len(Path)-1)
Else
removeSlash = Path
End If
End Function

Public Sub CreateFolders(Path)
Dim FolderArray : FolderArray = Split(Path, "\")
Dim Folder,i

For i=0 to UBound(FolderArray)
If i &lt;&gt; 0 Then
Folder = FolderArray(i-1)
End If

Folder = Folder &amp; FolderArray(i) &amp; "\"

If not m_objFSO.FolderExists(Folder) Then
m_objFSO.CreateFolder(Folder)
End If

FolderArray(i) = Folder
Next
End Sub
End Class


'******************************************************************
' Subs and Functions
'******************************************************************
</Script></ScriptBody>
<TimeoutSeconds>300</TimeoutSeconds>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="VBS">
<Node ID="Shell"/>
</Node>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.Discovery.Data</OutputType>
<InputType>System!System.BaseData</InputType>
</ProbeActionModuleType>