Sunday, April 24, 2011

Load balance web application without an hardware load balancer

Here is a vb script to access web application load balancing the user requests . you don't need a hardware load balancer but the users will be accessing different servers depending on the number of processes for the application on each server.

On Error Resume Next
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
strComputer1 = "x.x.x.x"
strComputer2 = "y.y.y.y"
strx = "domain-user"
stry = "password"
strDomain = "domain-name"
dim objShell
set objShell = CreateObject("Shell.Application")
str1 = "applicationlink1"
str2 = "applicationlink2"
str3 = "http://www.google.com"
str4 = "applicationlink3"

//Locate the computers

Set objWMIService1 = objLocator.ConnectServer(strComputer1, "root\cimv2",strx,stry,"MS_409","ntlmdomain:" + strDomain)
Set objWMIService2 = objLocator.ConnectServer(strComputer2, "root\cimv2",strx,stry,"MS_409","ntlmdomain:" + strDomain)

//Ping the computers

Set colPingedComputers1 = objWMIService1.ExecQuery ("Select * from Win32_PingStatus Where Address = 'x.x.x.x'")
Set colPingedComputers2 = objWMIService2.ExecQuery ("Select * from Win32_PingStatus Where Address = 'y.y.y.y'")

//check if both servers are offline open http://www.google.com

For each objComputer1 in colPingedComputers1
If objComputer1.StatusCode <> 0 Then
For each objComputer2 in colPingedComputers2
If objComputer2.StatusCode <> 0 Then
strMessage = "No Servers are Online"
MsgBox strMessage
objShell.ShellExecute str3, "", "", "open", 3
set objShell = nothing
set objComputer2 = nothing
End If
Next
set objComputer1 = nothing
End If
Next

//if server2 is online and server1 is offline execute application link2

For each objComputer1 in colPingedComputers1
If objComputer1.StatusCode <> 0 Then
For each objComputer2 in colPingedComputers2
If objComputer2.StatusCode = 0 Then
objShell.ShellExecute str2, "", "", "open", 3
set objShell = nothing
set objComputer2 = nothing
End If
Next
set objComputer1 = nothing
End If
Next

//if server1 is online and server2 is offline execute application link1

For each objComputer2 in colPingedComputers2
If objComputer2.StatusCode <> 0 Then
For each objComputer1 in colPingedComputers1
If objComputer1.StatusCode = 0 Then
objShell.ShellExecute str1, "", "", "open", 3
set objShell = nothing
set objComputer1 = nothing
End If
Next
set objComputer2 = nothing
End If
Next

//if server1 and server2 are online execute link checking for the number of processes on each computer and also specify a maximum limit.

For each objComputer1 in colPingedComputers1
If objComputer1.StatusCode = 0 Then
Set colProcessList1 = objWMIService1.ExecQuery ("Select * from Win32_Process Where Name = 'application-name.exe'")
For each objComputer2 in colPingedComputers2
If objComputer2.StatusCode = 0 Then
Set colProcessList2 = objWMIService2.ExecQuery ("Select * from Win32_Process Where Name = 'application-name.exe'")
End If
set objComputer2 = nothing
Next
If colProcessList1.Count > 100 And colProcessList2.Count > 100 Then
objShell.ShellExecute str4, "", "", "open", 3
set objShell = nothing
Else If colProcessList1.Count < colProcessList2.Count Then
objShell.ShellExecute str1, "", "", "open", 3
set objShell = nothing
Else if colProcessList2.Count < colProcessList1.Count Then
objShell.ShellExecute str2, "", "", "open", 3
set objShell = nothing
Else
objShell.ShellExecute str1, "", "", "open", 3
set objShell = nothing
End If
End If
End If
set objComputer1 = nothing
End If
Next

No comments:

Post a Comment