Add server uptime to the ColdFusion Administrator

On a private list, Ryan Guill asked if it was possible to get the ColdFusion’s server uptime in the administrator. You can see this now in the Server Monitor as well as the Server Manager. Here’s a quick screen shot of a few of my servers.

But if you want to put it in the CF Admin itself, it's pretty simple using the Admin API. The servermonitoring CFC has a method called getHeartBeat. This method returns a set of values about the server one of which is the uptime in milliseconds. Here's a quick snippet that shows this inaction.

<cfset admin = createObject("component", "CFIDE.adminapi.administrator")> <cfset admin.login("admin")> <cfset sm = createObject("component", "CFIDE.adminapi.servermonitoring")> <cfset hb = sm.getHeartBeat()> <!--- miliseconds ---> <cfset uptime = hb.serveruptime>

And that's basically it depending on how you want to display it. So how do you get this in the admin? I've talked about this before, but the first thing you do is open custommenu.xml in your CFADMIN folder:

<submenu label="Custom"> <menuitem href="uptime.cfm" target="content">Uptime</menuitem> </submenu>

And here is uptime.cfm. I added the common header/footer elements used by CF Admin files and made use of Duration from CFLib.

<cfscript> /** * Duration(dateObj1, dateObj2) Takes two date objects and returns a structure containing the duration of days, hours, and minutes. * v2 mod by James Moberg to support seconds. * * @param dateObj1 CF Date Object to compare (Required) * @param dateObj2 CF Date Object to compare (Required) * @return Returns a structure containing the keys Days, Hours, and Minutes with their associated values. * @author Chris Wigginton ( * @version 2, September 29, 2011 */ function Duration(dateObj1, dateObj2){ var dateStorage = dateObj2; var DayHours = 0; var DayMinutes = 0; var HourMinutes = 0; var timeStruct = structNew(); if (DateCompare(dateObj1, dateObj2) IS 1) { dateObj2 = dateObj1; dateObj1 = dateStorage; } timeStruct.days = DateDiff("d",dateObj1,dateObj2); DayHours = timeStruct.days * 24; timeStruct.hours = DateDiff("h",dateObj1,dateObj2); timeStruct.hours = timeStruct.hours - DayHours; DayMinutes = timeStruct.days * 1440; HourMinutes = timeStruct.hours * 60; timeStruct.minutes = DateDiff("n",dateObj1,dateObj2); timeStruct.minutes = timeStruct.minutes - (DayMinutes + HourMinutes); DayMinutes = timeStruct.days * 86400; HourMinutes = (timeStruct.hours * 3600) + (timeStruct.minutes * 60); timeStruct.seconds = DateDiff("s",dateObj1,dateObj2); timeStruct.seconds = timeStruct.seconds - (DayMinutes + HourMinutes); return timeStruct; } </cfscript> <cfset sm = createObject("component", "CFIDE.adminapi.servermonitoring")> <cfset hb = sm.getHeartBeat()> <!--- miliseconds ---> <cfset uptime = hb.serveruptime> <cfset upat = dateAdd("l", -1 * uptime, now())> <cfset dur = duration(upat, now())> <cfinclude template="header.cfm"> <h2 class="pageHeader">Uptime</h2> <cfoutput> Up for #dur.days# days, #dur.hours# hours, and #dur.minutes# minutes. </cfoutput> <cfinclude template="footer.cfm">

Here's a screen shot:

Notice I chose to ignore the seconds value. It seems a bit silly to be concerned about that level of detail. Feel free to disagree with me though. The Duration UDF returns it.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA