Check connections for windows
A Visual Basic Script to check how many open connections exists in a certain port.
Here is the code:
'Script Check Number of ESTABLISHED connections
'Author: Felipe Ferreira
'Data: 29/07/2010
'Version: 1.0 (nagios)
'----------NAGIOS VARs
Const intOK = 0
Const intWarning = 1
Const intCritical = 2
Const intError = 3
Const intUnknown = 3
'----------SCRIPT VARS
Dim ofolder,filename
Dim stroutput : stroutput = "OK - "
Dim argcountcommand
Dim arg(8)
Dim Verbose
Dim conections
'----------EDIT LINES
Verbose = 0 '1 FOR DEBUG MODE, 0 FOR SILENT MODE
intWarn = 20 'Warning level in Minutes
intCrit = 60 'Critical level in Minutes
'----------EDIT LINES
GetArgs()
If Wscript.Arguments.Count = 0 then
help()
wscript.quit(intError)
else
intWarn = GetOneArg("-w")
intCrit = GetOneArg("-c")
end if
pt "WARN: " & intWarn &vbcrlf& "CRIT: " & intCrit
'CALL SUB TO CHECK FOLDER FOR ANY LOG FILES
call checkcon
pt "Conexoes = " & conections
conections = cInt(conections)
intWarn = cInt(intWarn)
intCrit = cInt(intCrit)
'Verifica conforme WARN e CRIT
If (conections > intWarn) and (conections < intCrit) Then
stroutput = "WARNING - " & conections & " conections |conections=" &conections
intExit = intWarning
Elseif (conections > intCrit) Then
stroutput = "CRITICAL - " & conections & " conections |conections="&conections
intExit = intCritical
Elseif conections <= intWarn Then
stroutput = "OK - " & conections & " conections |conections="&conections
intExit = intOK
end if
wscript.echo stroutput
wscript.quit(intExit)
Function checkcon
'Deletes Profiles older then 1 year, uses MS delprof tool
'Should have a timeout, sometimes it hangs
dim strCmd
dim objShell : Set objShell = WScript.CreateObject("WScript.Shell")
Dim objExecObject
strCmd = "cmd /c netstat -an |find /i " & chr(34) &"estab" & chr(34)& " /c" & strScriptPath
pt strCmd
Set objExecObject = objShell.Exec(strCmd)
Do While Not objExecObject.StdOut.AtEndOfStream
conections = objExecObject.StdOut.ReadLine() 'gets %username%
loop
trim(conections)
pt conections
end function
Function Help()
'Prints out help
Dim str
str="Verifica o numero de conexoes Estabelecidas de um windows ."&vbCrlF&vbCrlF
str=str&"cscript "& strScriptFile &"-w 30 -c 90"&vbCrlF
str=str&vbCrlF
str=str&"-w min Warning "&vbCrlF
str=str&"-c min Critical"&vbCrlF
str=str&vbCrlF
str=str&" Change Verbose Varible to 1 to Turn on debug mode."&vbCrlF
str=str&vbCrlF
str=str&"By Felipe Ferreira - 08/2010, version 1.0(For Nagios)." & vbCrlF
wscript.echo str
End Function
Function GetArgs()
'Get ALL arguments passed to the script
'On Error Resume Next
Dim f
argcountcommand = WScript.Arguments.Count
for f = 0 to argcountcommand - 1
arg(f)=WScript.Arguments(f)
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
sub pt(txt)
if Verbose = 1 then
wscript.echo txt
end if
end sub
