CFLib.org – Common Function Library Project

checkHealth([sCheckType])

Last updated October 10, 2011

Version: 1 | Requires: ColdFusion 9 | Library: UtilityLib

 
Rated 1 time(s). Average Rating: 5.0

Description:
Provides a check of system health for memory usage, queued requests or average requesttime and returns status for Nagios. 0 = ok, 1 = warning, 2 = critical, 3 = unknown/unreachable. Dont forget to change the variable sAdminPwd to yours and adjust the warning levels to your needs.

Return Values:
Returns a struct.

Example:

view plain print about
<cfdump var="#checkHealth()#" />
<cfdump var="#checkHealth('reqTime')#" />
<cfdump var="#checkHealth('queu')#" />

Parameters:

Name Description Required
sCheckType Type to check. Values are jvmMem, reqTime, queu. Defaults to jvmMem. No

Full UDF Source:

view plain print about
<!---
 Serve system checks for nagios or other monitoring solutions.
 
 @param sCheckType      Type to check. Values are jvmMem, reqTime, queu. Defaults to jvmMem. (Optional)
 @return Returns a struct. 
 @author Sigi (siegfried.heckl@siemens.com) 
 @version 1, October 10, 2011 
--->

<cffunction name="checkHealth" access="public" output="false" returntype="struct" hint="serve system checks for nagios or other monitoring solutions">
  <cfargument name="sCheckType" type="string" default="jvmMem" hint="(jvmMem|reqTime|queu)" />

  <cfscript>
    var sAdminPwd  = 'topsecret'; //password for your CF-Admin-Login
    var adminObj   = {}//not defined here to avoid unnecessary overhead
    var runtimeObj = {}//not defined here to avoid unnecessary overhead
    var strHeart   = {}//not defined here to avoid unnecessary overhead
    var strReturn  = { typ = arguments.sCheckType, value = 0, health = 0 };

    switch(arguments.sCheckType) {
      case 'jvmMem': {
        try {
          runtimeObj      = CreateObject("java","java.lang.Runtime").getRuntime();
          strReturn.value = int((runtimeObj.totalMemory()/runtimeObj.maxMemory())*100);
          if(strReturn.value GT 70) { //percent memory userd, warning level
            strReturn.health = 1;
            if(strReturn.value GT 85) { //percent memory userd, critical level
              strReturn.health = 2;
            }
          }
        }
        catch(any err) {
          strReturn.health = 3;
        }
        break;
      }
      case 'reqTime': {
        try {
          adminObj = createObject("component","cfide.adminapi.administrator").login(sAdminPwd);
          strHeart = createObject("component","cfide.adminapi.servermonitoring").getHeartbeat();
          strReturn.value = strHeart.avgTime;
          if(strHeart.avgTime GT 2000) { //average request time, warning level
            strReturn.health = 1;
            if(strHeart.avgTime GT 8000){ //average request time, critical level
              strReturn.health = 2;
            }
          }
        }
        catch(any err) {
          strReturn.health = 3;
        }
        break;
      }
      case 'queu': {
        try {
          adminObj = createObject("component","cfide.adminapi.administrator").login(sAdminPwd);
          strHeart = createObject("component","cfide.adminapi.servermonitoring").getHeartbeat();
          strReturn.value = strHeart.reqQueued;
          if(strHeart.reqQueued GT 3) { //queued requests, warning level
            strReturn.health = 1;
            if(strHeart.reqQueued GT 6) {//queued requests, critical level
              strReturn.health = 2;
            }
          }
        }
        catch(any err) {
          strReturn.health = 3;
        }
        break;
      }
      default: {
        strReturn.health = 3;
        break;
      }
    }
    return strReturn;
  
</cfscript>
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson