hobnobs: (12 Wizard Speed Time)
[personal profile] hobnobs
The following WSF/VBScript was written for Nagios/NRPE, and queries Windows WMI and makes a Software Version Check with optional validation against minimum version, with a mismatch allowance.



The things I do to improve my Nagios Server. I had to learn VBScript for this...

Nagios Setup

Command:
I use a custom command for this, as it takes arguments from the service. I also needed to increase the timeout here (as well as in the Nagios/NRPE/NSClient++ configs) because WMI calls can take some time.

define command {
  command_name                    check_nt_software
  command_line                    $USER1$/check_nrpe -H $HOSTADDRESS$ -p 5666 -t 300 -c $ARG1$ -a $ARG2$ $ARG3$ $ARG4$
}


Service:
You'll need a service per package, and I'll use a strict check of Flash Player for my example. (I always tend to give the allowance argument, even though it's not technically required when doing a strict check. I use 0 in these cases.)

define service {
  use                            INSERT-TEMPLATE-HERE
  host_name                       INSERT-HOSTNAME-HERE
  service_description             check_flash_player
  display_name                    Check Flash Player Version
  contact_groups                  INSERT-GROUPS-HERE
  check_command                   check_nt_software!check_software_ver!%flash%player%!11.1.102.62!0
  check_interval                  10080
  notification_interval           10080
}


Client Requirements:
Windows obviously. (2000 and upwards.)
WMI installed and the Win32_Product Class available. (To check, go to an admin command prompt and type "wmic product". If you see a list of software, it's working.)
NSClient++ (or similar)
Arguments allowed for NRPE.

nsc.ini (If using NSClient++)
Ensure the following are set.
[NRPE]
allow_arguments=1
command_timeout=290

[NRPE Handlers]
check_software_ver=cscript.exe //NoLogo //T:280 "check_software_ver.wsf" $ARG1$ $ARG2$ $ARG3$


Notes:
It requires one argument (Package Name - $ARG1$), and you can feed it two optional arguments of a version ($ARG2$) for comparison, and a mismatch allowance ($ARG3$) defining how many figures - starting from the right- can be ignored.

If only $ARG1$ is provided it just returns the Software/Version. ($ARG1$ can use % as a wildcard for matching.)
If only $ARG1$ and $ARG2$ are provided it also makes a strict version check.
If $ARG3$ is also provided it makes a version check according to the leeway provided by the argument. (Number must be 1 less that the component count in version number provided by $ARG2$. Special Cases are "0", meaning strict check, and 99 meaning any later version is OK.)

This will only report software that registers itself with WMI. (All MSI Installations should do this.)


Return Codes:
The following are returned;

Unknown: (3)
Software Package(s) not found.
$ARG3$ Allowance exceeds 3.

Critical: (2)
$ARG1$ missing or blank.
$ARG1$ is literally "$ARG1$" (You've got a Nagios/NRPE/NSClient config problem somewhere.)
Version Check with unsuccessful strict match.
Software found, but Version Allowance equals or exceeds number of components in the Version given in $ARG2$.

Warning: (1)
Version Check with unsuccessful match with Allowance

OK: (0)
Version Report Only
Version Check with successful Strict match
Version Check with successful match with Allowance


Other Execution Examples;
(check_command)

1. Report All Adobe Software Versions
check_nt_software!check_software_ver!%adobe%

2. Check All Flash Player Levels using strict versioning.
check_nt_software!check_software_ver!%Flash%Player%!11.0.1.152

3. Check VMWare Workstation Level ignoring build figure
check_nt_software!check_software_ver!%VMWare%Workstation%!7.1.5.19539!1

4. Check VMWare Workstation Level, using specific major version
check_nt_software!check_software_ver!%VMWare%Workstation%!7.0!1

5. Check Minimum VMWare Workstation Level, also allowing any higher major version
check_nt_software!check_software_ver!%VMWare%Workstation%!7!99


The future:
I might increase the capabilities to be able to query remote machines, and to specify what field I want to query with $ARG1$. (Vendor is a good one I think.)
Of course this means increasing my knowledge of VBScript, and at the moment I just want to avoid said platform. :D

Script/Code
<job>
<runtime>
<description>
check_software_ver.wsf [1.0.0.0]

Checks WMI for Software Version, with optional validation against minimum version plus mismatch allowance.
</description>
<unnamed
name="Software"
helpstring= "Software Package to check. (% can be used as wildcard.)"
type = "string"
required="true"
/>
<unnamed
name="Baseline"
helpstring= "Baseline Version number for validation."
type = "string"
required="false"
/>
<unnamed
name="Allowance"
helpstring= "Mismatch Allowance for Version Check, up to 3 places from the right.
Special cases are '0' (Strict), and 99' (Any Later Version)."
type = "number"
required="false"
/>
<example>
Example: cscript //nologo check_software_ver.wsf "%VMWare%Workstation%" "6.0" 99
</example>
</runtime>

<script language="VBScript">

Const intOK = 0
Const intWarning = 1
Const intCritical = 2
Const intUnknown = 3

MetaDataOK = ""
MetaDataWARN = ""
MetaDataCRIT = ""
MetaDataUNKN = ""

AnyHigherVer = 0
ReportLvl = -1
osep = ""
wsep = ""
csep = ""
usep = ""

strComputer = "."

If (WScript.Arguments.Count < 1) Then
	Wscript.Echo "CRITICAL: Insufficient Arguments Provided"
	Wscript.Quit intCritical
Elseif (WScript.Arguments.Item(0) = "") Then
	Wscript.Echo "CRITICAL: Argument Error. Empty Argument given. Please check you settings"
	Wscript.Quit intCritical
Elseif (WScript.Arguments.Item(0) = "$ARG1$") Then
	Wscript.Echo "CRITICAL: Argument Error. '$ARG1$' provided as Software Package. Please check your Nagios/NRPE/NSClient++ Settings"
	Wscript.Quit intCritical
Else
	Software = WScript.Arguments.Item(0)
End If

If (WScript.Arguments.Count > 1 ) Then
	If NOT (WScript.Arguments.Item(1) = "" or WScript.Arguments.Item(1) = "$ARG2$") Then
		Baseline = WScript.Arguments.Item(1)
	Else
		NoCompare = 1
	End If
	If (WScript.Arguments.Count = 3 ) Then
		If NOT (WScript.Arguments.Item(2) = "" or WScript.Arguments.Item(2) = "$ARG3$") Then
			Allowance = WScript.Arguments.Item(2)
			If (Allowance = 99) Then
				AnyHigherVer = 1
			Elseif (Allowance > 3) Then
				Wscript.Echo "UNKNOWN: Mismatch Allowance of 4+ Places specified. This will match all versions. If no checking is required then only the Package Name should be supplied"
				Wscript.Quit intUnknown
			End If
		Else
			Allowance = 0
		End If
	End If
Else
	NoCompare = 1
	Allowance = 0
End If

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colSoftware = objWMIService.ExecQuery(_
    "Select * from Win32_Product " & _
    "Where Name like '" _
        & Software & "'")

For Each objItem in colSoftware
	Category = ""
	Higher = 0
	If NoCompare = 1 Then
		Category = "OK"
	ElseIf (StrComp(baseline, objItem.Version) = 0) Then
		Category = "OK"
	ElseIf (Allowance = 0) Then
		Category = "Crit"
	Else
		dim Base_Ver : Base_Ver = split(Baseline, ".")
		dim Comp_Ver : Comp_Ver = split(objItem.Version, ".")
		l = UBound(Base_Ver) +1
		c = UBound(Comp_Ver) +1
		If (l > c) Then
			diff = l - c
			ReDim Preserve Comp_Ver(UBound(Comp_Ver) + diff)
		End If
		IF (Allowance = 99) Then
			CheckTo = l
		Else
			CheckTo = l - Allowance
		End If
		If (CheckTo < 0) Then
			Wscript.Echo "CRITICAL: Version Mismatch allowance of " & Allowance & " places is equal to or exceeds Version Length (" & l & ")"
			Wscript.Quit intCritical
		Else
			i = 0
			Do While i < CheckTo
				If (StrComp(Base_Ver(i), Comp_Ver(i)) = 0) Then
					Category = "OK"
					i = i + 1
				Elseif (AnyHigherVer = 1) Then
					If NOT (Higher = 1) Then
						If (Base_Ver(i) < Comp_Ver(i)) Then
							Higher = 1
							Category = "OK"
							i = i + 4
						Else
							Category = "Warn"
							i = i + 4
						End if
					End If
				Else
					Category = "Warn"
					i = i + 4
				End If
			Loop
		End If
	End If

	Select Case Category
	Case "OK"
		MetaDataOK = MetaDataOK & osep & objItem.Name &" [" & objItem.Version & "]"
		osep = ", "
		If (ReportLvl < 1) Then
			ReportLvl = 0
		End If
	Case "Warn"
		MetaDataWARN = MetaDataWARN & wsep & objItem.Name &" [" & objItem.Version & "]"
		wsep = ", "
		If (ReportLvl < 1) Then
			ReportLvl = 1
		End If
	Case "Crit"
		MetaDataCRIT = MetaDataCRIT & csep & objItem.Name &" [" & objItem.Version & "]"
		csep = ", "
		If (ReportLvl < 2) Then
			ReportLvl = 2
		End If
	Case Else
		MetaDataUNKN = MetaDataUNKN & usep & objItem.Name &" [" & objItem.Version & "]"
		usep = ", "
		If (ReportLvl < 3) Then
			ReportLvl = 3
		End If
	End Select

Next

If (ReportLvl = -1) Then
	Wscript.Echo "UNKNOWN: Software Package(s) '" & Software & "' not found!"
	Wscript.Quit intUnknown
End If

sep = ""
If NOT (MetaDataUNKN = "") Then
	MetaData = MetaData & sep & "UNKNOWN: " & MetaDataUNKN
	sep = " / "
End If
If NOT (MetaDataCRIT = "") Then
	MetaData = MetaData & sep & "CRITICAL: " & MetaDataCRIT
	sep = " / "
End If
If NOT (MetaDataWARN = "") Then
	MetaData = MetaData & sep & "WARNING: " & MetaDataWARN
	sep = " / "
End If
If NOT (MetaDataOK = "") Then
	MetaData = MetaData & sep & "OK: " & MetaDataOK
	sep = " / "
End If

If (Len(MetaData) > 1023) Then
	MetaData = Left(MetaData, 1020) & "..."
End If

Wscript.Echo MetaData
Wscript.Quit ReportLvl

</script>
</job>


This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org

Profile

hobnobs: (Default)
hobnobs

June 2016

S M T W T F S
   1234
567891011
12131415161718
19202122232425
26272829 30  

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 16th, 2025 09:04 pm
Powered by Dreamwidth Studios