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.
Archived Comments
Ray,
Is the Admin api available to those of us that are running CF Standard? We don't have the monitor function enabled.
Jim
The Admin API is available: http://www.adobe.com/produc...
Not sure if this particular CFC is restricted though.
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.
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
Seems like a reasonable assumption - thanks Adam.
Wow! I did not realise you could customise the CFAdmin so easily - and this "addition" was easily added and quite handy.
Glad you liked it. Check out the guide I wrote for it:
http://www.coldfusionjedi.c...
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!
That's ColdFish, the code highlighting, screwing up. Try "View Plain." I need to update my copy of ColdFish some day.
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.
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.)