'Script to check the status of a DOMAIN controller and report to Nagios 'requires DCDIAG.EXE 'Author: Felipe Ferreira 'TODO: 'Get from args what to check, update status to 2 and also build the cmd line 'loop thru args and with a select update tests status to 2 'OK - for now just check all and report critical if one fails Dim verbose : verbose = 1 '----------NAGIOS VARs Const intOK = 0 Const intWarning = 1 Const intCritical = 2 Const intError = 3 '----------ARGUMENT VALUES Dim argcountcommand Dim arg(20) Dim arrTest(5) Dim cmd dim hostname 'State of Checks ( 1 = OK, 0 = Crtitical) Dim services : services = "CRITICAL" Dim replications : replications = "CRITICAL" Dim advertising : advertising = "CRITICAL" Dim fsmocheck : smocheck = "CRITICAL" Dim ridmanager : ridmanager = "CRITICAL" Dim machineaccount : machineaccount = "CRITICAL" GetArgs() If Wscript.Arguments.Count = 0 then help() wscript.quit(intError) else arrTest(0)=GetOneArg("-T1") arrTest(1)=GetOneArg("-T2") arrTest(2)=GetOneArg("-T3") arrTest(3)=GetOneArg("-T4") arrTest(4)=GetOneArg("-T5") arrTest(5)=GetOneArg("-T6") end if '@@@@@@@@@@ MAIN CALLS @@@@@@@@@@@@@@ 'CMD Options are: dcdiag /test:services /test:replications /test:advertising /test:fsmocheck /test:ridmanager /test:machineaccount cmd = "dcdiag /test:services /test:replications /test:advertising /test:fsmocheck /test:ridmanager /test:machineaccount" call getname() call exec(cmd) call printout() '@@@@@@@@@@ FUNCTIONS @@@@@@@@@@@@@@@ function exec(strCmd) dim objShell : Set objShell = WScript.CreateObject("WScript.Shell") call pt( strCmd ) Dim objExecObject,lineout Set objExecObject = objShell.Exec(strCmd) Do While Not objExecObject.StdOut.AtEndOfStream lineout=LCASE(objExecObject.StdOut.ReadLine()) 'Each line output is sent to parse function to check for keywords and update status call parse(lineout) loop if err.number = 0 and objExecObject.Status = 1 then call pt("done") elseif (err.number <> 0 ) then wscript.echo("Error " & err.Description & " " & Err.number) end if end function Function GetArgs() 'Get ALL arguments passed to the script On Error Resume Next Dim i argcountcommand=WScript.Arguments.Count for i=0 to argcountcommand-1 arg(i)=WScript.Arguments(i) DisplayMsg i & " - " & arg(i) next End Function function parse(txtp) 'Parse output of dcdiag command and change state of checks if instr(txtp,"passed test") or instr(txtp,"failed test") then txtp = Replace(txtp,".","") pt txtp txtp = Replace(txtp,"passed test","") txtp = Replace(txtp,"failed test","") txtp = Replace(txtp,hostname,"") txtp = trim(txtp) Select Case txtp case "services" services = "OK" case "replications" replications = "OK" case "advertising" advertising = "OK" case "fsmocheck" fsmocheck = "OK" case "advertising" advertising = "OK" case "ridmanager" ridmanager = "OK" case "machineaccount" machineaccount = "OK" end select end if end function function printout() 'outputs result dim msg1,msg msg = "Services: " & services & " Replications: " & replications & " Advertising: " & advertising & " Fsmocheck: " &_ fsmocheck & " Ridmanager: " & ridmanager & " Machineaccount: " & machineaccount if instr(msg,"CRITICAL") then msg1 = "CRITICAL - " wscript.echo msg1 & msg wscript.quit(intCritical) else msg1 = "OK - " wscript.echo msg1 & msg wscript.quit(intOK) end if end function function getname() 'get NAME OF LOCAL PC dim objShell2 : Set objShell2 = WScript.CreateObject("WScript.Shell") Dim objExecObject2 Set objExecObject2 = objShell2.Exec("cmd /c hostname") Do While Not objExecObject2.StdOut.AtEndOfStream hostname = LCASE(objExecObject2.StdOut.ReadLine()) 'gets %userdomain% loop hostname=trim(hostname) pt hostname end function Function GetArgs() 'Get ALL arguments passed to the script On Error Resume Next Dim i argcountcommand=WScript.Arguments.Count for i=0 to argcountcommand-1 arg(i)=WScript.Arguments(i) ' pt i & " - " & arg(i) next End Function Function GetOneArg(strName) On Error Resume Next Dim i for i=0 to argcountcommand-1 if (Ucase(arg(i))=Ucase(strName)) then GetOneArg=arg(i+1) Exit Function end if next End Function Function help() wscript.echo "checkdc.vbs - Nagios NRPE Plugin for Active Directory Health Check" & vbcrlf & _ "Version 1.9, By Felipe Ferreira www.felipeferreira.net" & vbcrlf & _ "Usage:" & vbcrlf & _ "check_ad [-T1 -T2 -T3 -T4 -T5 -T6 ][--help]" & vbcrlf & _ " :services, replications, advertising, fsmocheck, ridmanager, machineaccount" & vbcrlf & vbcrlf & _ "Checks domain controller functionality by using dcdiag tool from" & vbcrlf & _ "Windows Support Tools. Following dcdiag tests are performed" end function Function pt(msgTxt) 'Prints msg on screen only if Verbose is set if Verbose = 1 then wscript.echo msgTXT end if end Function