1 '------------------------------------------------------------------------------
3 ' This script can be used as local check for the Check_MK windows agent.
4 ' The script takes several arguments:
5 ' 1. The name of the service
6 ' 1. The name/address of the DNS host
7 ' 2. The address to lookup
8 ' 3. The direction to resolve (forward or reverse)
9 ' *. The expected answers
11 ' The script executes nslookup, parses the output and returns status output
12 ' and state information depending on the given parameters.
16 ' Place a small batch script e.g. nslookup_domain.de in the local/ subdirectory
17 ' of the check_mk_agent. and add the following line:
19 ' cscript //NoLogo lib/nslookup_domain.vbs nslookup_domain.de domain.de 192.168.123.1
21 ' The script checks for the domain "domain.de" and expects the answer "192.168.123.1".
22 ' Another answer or more resolved addresses will result in a CRITICAL state.
25 ' Author: Lars Michelsen <lm@mathias-kettner.de>, 2010-07-20
26 '------------------------------------------------------------------------------
30 Dim strName
, strHost
, checkName
, status
, output
, i
31 Dim objShell
, objExec
, strOutput
, arrLines
, strLine
32 Dim addresses
, address
, expected
, direction
, label
33 Dim aExpected(), inAddresses
, additionalAddresses
, add
38 If Wscript
.Arguments
.Count
< 3 Then
39 wscript
.echo
"Wrong launch options."
43 checkName
= Wscript
.Arguments(0)
44 strHost
= Wscript
.Arguments(1)
45 strName
= Wscript
.Arguments(2)
46 direction
= Wscript
.Arguments(3)
48 For i
= 4 to Wscript
.Arguments
.Count
- 1
49 Redim Preserve aExpected(i
)
50 aExpected(i
) = Wscript
.Arguments(i
)
54 If direction
= "reverse" Then
58 Set objShell
= CreateObject("WScript.Shell")
59 Set objExec
= objShell
.Exec("cmd /c nslookup " & strName
& " " & strHost
)
64 strOutput
= objExec
.StdOut
.ReadAll
65 arrLines
= Split(strOutput
, VbCrLf
)
69 For Each strLine
In arrLines
70 If direction
= "forward" AND Left(strLine
, 10) = "Addresses:" Then
71 addresses
= Split(Trim(Mid(strLine
, 11)), ", ")
73 ElseIf direction
= "reverse" AND Left(strLine
, 5) = "Name:" Then
74 addresses
= Split(Trim(Mid(strLine
, 6)), ", ")
77 ' Maybe the answer continues in the following line(s).
78 ' Add until next line with a ":" in it
79 If InStr(strLine
, ":") = 0 Then
80 additionalAddresses
= Split(Trim(Replace(strLine
, vbTab
, "")), ", ")
81 For Each add
in additionalAddresses
82 Redim Preserve addresses(ubound(addresses
) + 1)
83 addresses(ubound(addresses
)) = Trim(add
)
88 ' Are all found addresses expected?
89 If UBound(addresses
) > -1 Then
90 For Each address
in addresses
91 If InStr(1, vbNullChar
& Join(aExpected
, vbNullChar
) , vbNullChar
& address
) = 0 Then
93 output
= output
& label
& " is NOT expected """ & address
& """, "
98 ' Are all expected addresses found?
99 For Each expected
in aExpected
100 If InStr(1, vbNullChar
& Join(addresses
, vbNullChar
) , vbNullChar
& expected
) = 0 Then
102 output
= output
& "Expected " & label
& " NOT found """ & expected
& """, "
107 If direction
= "forward" Then
108 output
= "OK - All expected addresses were found in ""nslookup " & strName
& """"
110 output
= "OK - All expected names were found in ""nslookup " & strName
& """"
113 output
= Mid(output
, 1, Len(output
)-2)
116 wscript
.echo status
& " " & checkName
& " - " & output