GUI CSS: Refactored wato styles to nested structure meeting sasslint requirements...
[check_mk.git] / doc / windows / nslookup_local_check_example / nslookup.vbs
blobd683e36e06ed6f308d0526d56df99c27bc976e86
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.
14 ' Example:
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 '------------------------------------------------------------------------------
28 Option Explicit
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
35 status = 0
36 output = ""
38 If Wscript.Arguments.Count < 3 Then
39 wscript.echo "Wrong launch options."
40 wscript.quit 2
41 End If
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)
51 Next
53 label = "address"
54 If direction = "reverse" Then
55 label = "name"
56 End If
58 Set objShell = CreateObject("WScript.Shell")
59 Set objExec = objShell.Exec("cmd /c nslookup " & strName & " " & strHost)
60 While objExec.Status
61 WScript.Sleep 100
62 Wend
64 strOutput = objExec.StdOut.ReadAll
65 arrLines = Split(strOutput, VbCrLf)
67 addresses = Array()
68 inAddresses = False
69 For Each strLine In arrLines
70 If direction = "forward" AND Left(strLine, 10) = "Addresses:" Then
71 addresses = Split(Trim(Mid(strLine, 11)), ", ")
72 inAddresses = True
73 ElseIf direction = "reverse" AND Left(strLine, 5) = "Name:" Then
74 addresses = Split(Trim(Mid(strLine, 6)), ", ")
75 inAddresses = True
76 End If
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)
84 Next
85 End If
86 Next
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
92 status = 2
93 output = output & label & " is NOT expected """ & address & """, "
94 End If
95 Next
96 End If
98 ' Are all expected addresses found?
99 For Each expected in aExpected
100 If InStr(1, vbNullChar & Join(addresses, vbNullChar) , vbNullChar & expected) = 0 Then
101 status = 2
102 output = output & "Expected " & label & " NOT found """ & expected & """, "
103 End If
104 Next
106 If output = "" Then
107 If direction = "forward" Then
108 output = "OK - All expected addresses were found in ""nslookup " & strName & """"
109 Else
110 output = "OK - All expected names were found in ""nslookup " & strName & """"
111 End If
112 Else
113 output = Mid(output, 1, Len(output)-2)
114 End If
116 wscript.echo status & " " & checkName & " - " & output
117 wscript.quit status