Add server uptime to the ColdFusion Administrator

This post is more than 2 years old.

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 (cwigginton@macromedia.com)

  • @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 senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Jim Papaleo posted on 10/24/2011 at 10:46 PM

Ray,

Is the Admin api available to those of us that are running CF Standard? We don't have the monitor function enabled.

Jim

Comment 2 by Raymond Camden posted on 10/24/2011 at 10:52 PM

The Admin API is available: http://www.adobe.com/produc...

Not sure if this particular CFC is restricted though.

Comment 3 by Raymond Camden posted on 10/24/2011 at 10:53 PM

Ah shoot - going down more the grid I see server monitoring API is _not_ available.

Well, in the forum posting Ryan made, I definitely +1 his idea to add this to the CF Admin itself. I think it should be in all versions.

Comment 4 by Adam Cameron posted on 10/25/2011 at 2:19 AM

G'day Ray:
This gives the number of milliseconds since the JVM was started, which I should think would be close enough to the CF uptime too:

createObject("java", "java.lang.management.ManagementFactory").getRuntimeMXBean().getUptime()

(I just googled "JVM uptime" and that was the first match).

--
Adam

Comment 5 by Raymond Camden posted on 10/25/2011 at 6:03 AM

Seems like a reasonable assumption - thanks Adam.

Comment 6 by Peter Tilbrook posted on 10/25/2011 at 11:29 AM

Wow! I did not realise you could customise the CFAdmin so easily - and this "addition" was easily added and quite handy.

Comment 7 by Raymond Camden posted on 10/25/2011 at 5:19 PM

Glad you liked it. Check out the guide I wrote for it:

http://www.coldfusionjedi.c...

Comment 8 by RobW posted on 10/25/2011 at 9:38 PM

Couple of typos in line 20. Missing left paren before DateCompare, and you have DateComparee with an extra e at the end.

Very nice addition to my Administrator though. Thanks Ray!

Comment 9 by Raymond Camden posted on 10/25/2011 at 10:16 PM

That's ColdFish, the code highlighting, screwing up. Try "View Plain." I need to update my copy of ColdFish some day.

Comment 10 by Charlie Arehart posted on 10/26/2011 at 11:57 AM

To those on CF Standard who can't call this Admin API servermonitoring.cfc, note that there are other ways to get the server up time. Adam mentioned above an available jvm method.

There is also a surprising way to get it, using (believe it or not), the built-in variable server.coldfusion.expiration. I explain more (and how to make the result appear better-formatted) in a blog entry:

http://www.carehart.org/blo...

Ray, if you concur that it works, it may be helpful to point it out to people in the post, as an option for those on Std, since some may not bother to read the comments. Your call, of course.

Comment 11 by Raymond Camden posted on 10/26/2011 at 4:57 PM

Interesting post there - but I always feel weary about things that work like that - ie, not documented. Especially when it's advertised as being something else entirely. Then again, if your 'server uptime' code stopped working one day, it wouldn't be the end of the world.

Um - not a big believer in adding 'read the comments!' to blog entries. I expect my readers to be intelligent enough to know that. ;) (Although I do do it sometimes.)