views:

861

answers:

4

Is there any way in ColdFusion code to determine on what server the code is executing? I have few load-balanced ColdFusion servers. I want to be able to know on which server the code is running when I catch an exception, so I can include that information in the logging / reporting code.

The servers are Windows 2003/IIS, if that matters. I'd love to know how to do it in Linux/Apache too. :-)

+2  A: 

I believe that CGI.SERVER_NAME will get you what you want.

Edit per comment: You might be able to do something a bit more "low level" ...

<cfset inet = CreateObject("java", "java.net.InetAddress")>  
<cfdump var = "#inet.getLocalhost().gethostname()#">

(No CF server here at work, so I can't test that).

JP Alioto
I don't think so. It will contain the client facing server name. No way to find out the actual machine you are on here.
Tomalak
That will not be sufficient in a load balancing scenario?
JP Alioto
The client sees one server name. Let's say there are 2 servers. How do you find out which server you are on using the one server name?
Tomalak
The Java version is more like it. +1 ColdFusion supports the JavaBeans syntax, so <cfdump var = "#inet.Localhost.HostName#"> is enough. Confirmed as working.
Tomalak
+5  A: 

You can use Server Variables like

server.coldfusion.appserver 
server.coldfusion.expiration    
server.coldfusion.productlevel
server.coldfusion.productname   
server.coldfusion.productversion
server.coldfusion.rootdir   
server.coldfusion.serialnumber  
server.coldfusion.supportedlocales
server.os.additionalinformation 
server.os.arch  
server.os.buildnumber   
server.os.name  
server.os.version

to tweak your code to specific platforms. Do a <cfdump var=”#SERVER#” /> to see what's applicable to your version of Coldfusion.

You can get the hostname with a Java call:

<cfscript>
machineName = createObject("java", "java.net.InetAddress").localhost.getHostName();
instanceName = createObject("java", "jrunx.kernel.JRun").getServerName();
</cfscript>
Vincent Buck
+11  A: 

This may help you further...

<cfscript>
machineName = createObject("java", "java.net.InetAddress").localhost.getCanonicalHostName();
hostaddress = createObject("java", "java.net.InetAddress").localhost.getHostAddress();
</cfscript>
<cfdump var="#machineName#"><br />
<cfdump var="#hostaddress#"><br />
Jayson
+1  A: 

Another place to look for information about the executing JRun process is to instance the following:

<cfset oErrorJRun = createObject("java","jrunx.kernel.JRun")/>
<cfset strServerName = oErrorJRun.ServerName />

That will give you the name of the JRun instance where the code is being executed. We've run into occasions where in our cluster environment the IIS on one node will log the page hit, but the JRun on the other node will handle the request. Occasionally, we'll have one node's JRun stop responding, and we'll need to restart some services to get the traffic back to that node. I use the above code in my error handler plugin to stick the server name in an email I send to the admins, and to incorporate it in the filename where I write the debugging info.

Matt Newby