Back in June I wrote a quick blog entry on using the Server Monitor API. Most folks don't know that the cool Server Monitor in ColdFusion 8 is all driven by an underlying CFC API you can use in your own applications. I wrote up a quick demo of this I thought folks might find interesting.
One of the API calls you can make is getRequestDetails. This returns a (possibly) large array of data about each and every single request your site has served up. Requests are organized into array items by template. You get quite a bit of information. The last time the request was made. How many times it erred. When the last error occurred. The last, average, min, and max request times. Etc. With this in mind I wrote up a file that would grab all requests that were last run in the past 3 minutes. My server is busy so this gave a good result set. You may have to tweak that number for your own box. Here is the code:
<cfset password = "parishiltonismylostlovechild"> <cfinvoke component="CFIDE.adminapi.administrator" method="login" adminpassword="#password#" returnVariable="result">
<cfif not result> <cfabort /> </cfif>
<cfinvoke component="CFIDE.adminapi.servermonitoring" method="getRequestDetails" returnVariable="areq"> <cfset data = queryNew("template,function,timeran,lastresponsetime,avgtime,hitcount,errorcount,lasterror,lasterrorat","varchar,varchar,date,integer,integer,integer,integer,varchar,date")> <cfloop index="req" array="#areq#"> <!--- only use if within past 3 mins ---> <cfif dateDiff("n", req.lasttimeexecuted, now()) lte 3> <cfset queryAddRow(data)> <cfset querySetCell(data, "template", replace(req.templatepath,"","/","all"))> <cfset querySetCell(data, "function", req.functionname)> <cfset querySetCell(data, "timeran", req.lasttimeexecuted)> <cfset querySetCell(data, "lastresponsetime", req.lastresponsetime)> <cfset querySetCell(data, "avgtime", req.avgtime)> <cfset querySetCell(data, "hitcount", req.hitcount)> <cfset querySetCell(data, "errorcount", req.errorcount)> <cfset querySetCell(data, "lasterror", req.lasterror)> <cfset querySetCell(data, "lasterrorat", req.lasterrorat)> </cfif> </cfloop>
I begin by authenticating with the adminapi using my ColdFusion Administrator password. Then I call the ServerMonitor CFC. The result is an array, so I loop over it, and for each item, I check the date of the last hit. If within the last three minutes, I take out some of the values and stuff them into a query. (I'll explain the replace function in a minute.)
Next I sort the items by time:
<!--- sort by time ---> <cfquery name="data" dbtype="query"> select * from data order by timeran desc </cfquery>
And then I pass to a cool HTML grid:
<cfform name="throwaway"> <cfgrid format="html" query="data" name="requests" width="100%"> <cfgridcolumn name="template" header="Name" width="400"> <cfgridcolumn name="lastresponsetime" header="Last Response Time" width="200"> <cfgridcolumn name="avgtime" header="Avg Response Time" width="200"> <cfgridcolumn name="hitcount" header="Times Ran"> <cfgridcolumn name="errorcount" header="Error Count"> <cfgridcolumn name="lasterror" header="Last Error" width="400"> <cfgridcolumn name="lasterrorat" header="Last Error Occured" width="200"> </cfgrid> </cfform>
Here is a screen shot. Click for a large version. I hid my physical paths:
The reason for the replace is to handle the bug I blogged about a few days ago.
Tomorrow I'll demonstrate another example of using the API. Enjoy.