How can I find what machine/server my code is running on?
You can determine the name of the server your code is running on, as well as the ColdFusion instance that is executing it. This is very useful if you have code that you only want to execute in a development environment, or to easily tell which server in a cluster is executing your request.
To retrieve the physical server name:
<cfset host = CreateObject("java", "java.net.InetAddress").getLocalHost().getHostName();>
To retrieve the ColdFusion instance:
<cfset cfserv = createObject("java","jrunx.kernel.JRun").getServerName();>
Some sample code for a fairly common setup:
<cfif isNumeric(right(host,1))><!-- This code was executed on the
###right(host,1)# box in the cluster, by the #cfserv# instance of CF.
-->
<cfelse><!-- This code was executed on the dev or test box named
#host#, by the #cfserv# instance of CF. --></cfif>
Of course, you'll probably want to obfuscate that a bit, maybe a switch/case statement renaming them to something recognizable without giving away any of your host or instance names.
Another excellent use of this code is for a generic error handler that emails you when something goes wrong:
<cfmail to="#me#" from="#me#" subject="Problem with #host#-#cfserv# on
#dateformat(NOW(),'m/d/yy')# at #timeformat(NOW(),'h:mm:ss tt')#">
<cfdump var="#cfcatch#" format="text">
<cfdump var="#cgi#" format="text">
... and so on
</cfmail>
This question was written by J.C. Overgaard
It was last updated on July 3, 2008.