''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' "check_dell_sensors.wsf"
'
' This script was initially developed by Anstat Pty Ltd for internal use,
' and has kindly been made available to the Open Source community for
' redistribution and further development under the terms of the
' GNU General Public License: http://www.gnu.org/licenses/gpl.html
' 
' This script is supplied 'as-is', in the hope that it will be useful, but
' neither Anstat Pty Ltd nor the authors make any warranties or guarantees
' as to its correct operation, including its intended function.
' 
' Or in other words:
'         Test it yourself, and make sure it works for YOU.
' 
' Author: George Hansper               e-mail:  Name.Surname@anstat.com.au
'
<job>
<runtime>
  <description>
Check status of Dell Temperature/Fan sensors using Dell OpenManage Server Administrator utility "omreport"

Example NRPE_NT Usage:
	command[check_dell_sensors]=c:\winnt\system32\cscript.exe //NoLogo //T:10 c:\nrpe_nt\check_dell_sensors.wsf
	
Requires omreport version 1.7 or 2.0 or later which is included with Dell OpenManage Server Administrator version 3.x, or 4.x
  </description>
  <named
    name="h"
    helpstring="This Help"
    type="simple"
    required="false" 
  />
  <example>
Example: check_dell_sensors.wsf
  </example>
  </runtime>
<script language="VBScript">

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Help
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Wscript.Arguments.Named.Exists("h")  Then
      Wscript.Echo "Plugin help screen:"
      Wscript.Arguments.ShowUsage()
      Wscript.Quit(intUnknown)
End If

Dim strMessage, strLine
Dim ndx
Const intOK = 0
Const intWarning = 1
Const intCritical = 2
Dim intExit

Set objRegexp = New RegExp
With objRegexp
.Global = False
.IgnoreCase = True
.Pattern = "."
End With

Set objShell = WScript.CreateObject("WScript.Shell")

Dim strPathToOmreport
' The strPathToOmreport variable may be used if the omreport CLI command is not on the PATH
strPathToOmreport = "C:\Program Files\Dell\SysMgt\oma\bin\"
strPathToOmreport = ""

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Read sensors using "omreport chassis"
Dim nCtrlrs
Dim strSensorName(), strSensorState()
Dim strField, strValue
nSensors = 0
objRegexp.Pattern = "([^:]*):(.*)"

Set objExecObject = objShell.Exec(strPathToOmreport & "omreport.exe chassis -fmt lst")

Do Until objExecObject.StdOut.AtEndOfStream
	strLine = objExecObject.StdOut.ReadLine()
	strField = ""
	if objRegexp.Test(strLine) = True Then
		strField = objRegexp.Replace(strLine, "$2")
		strField = Trim(strField)
		strValue = objRegexp.Replace(strLine, "$1")
		strValue = Trim(strValue)
	Else
		' Wscript.Echo ":( " & strLine 
	End If
	If ( NOT Lcase(strField) = "component" AND NOT strField = "" AND NOT strValue = "" ) Then
		nSensors = nSensors + 1
		ReDim Preserve strSensorName(nSensors-1)
		ReDim Preserve strSensorState(nSensors-1)
		strSensorName(nSensors-1) = strField
		strSensorState(nSensors-1) = strValue
	End If

	' Wscript.Echo ">> " & strField & "=" & strValue
Loop

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Now we've collected the data, we will analyse the results
Dim nAlarms, sensorNdx
Dim strState
nAlarms=0

' Loop through sensors
sensorNdx = 0
strMessage = ""

Do Until ( sensorNdx >= nSensors )
	' Wscript.Echo ">> " & ctrlrID(ctrlNdx)
	strState = strSensorState(sensorNdx)
	if ( NOT Lcase(strState) = "ok" ) Then
		nAlarms = nAlarms + 1
		strSensorState(sensorNdx) = strSensorState(sensorNdx) & "**"
	End If
	strMessage = strMessage & strSensorName(sensorNdx) & "=" & strSensorState(sensorNdx) & ": "
	sensorNdx = sensorNdx+1
Loop
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Print the message, and exit with the appropriate exit code
if( nAlarms > 0 ) Then
	intExit = intCritical
	strMessage = "CRITICAL: " & strMessage
Else
	intExit = intOK
	strMessage = "OK: " & strMessage
End If

WScript.Echo strMessage

WScript.Quit(intExit)

Set objRegexp = Nothing
Set objShell = Nothing
Set objExecObject = Nothing
Set Slot() = Nothing
Set Array_resync() = Nothing
Set Array_failed() = Nothing
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

</script>
</job>