Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

rmEvent.cpp

Go to the documentation of this file.
00001 #include "rmEvent.h"
00002 #include "ResourceMonitor/MonitoringEventSchema.h"
00003 #include "rmClient.h"
00004 #include "rmClientError.h"
00005 #include "rmSysInfo.h"
00006 
00007 extern "C" {
00008 #include <ucd-snmp/ucd-snmp-config.h>
00009 #include <netinet/in.h>
00010 
00011 #include <ucd-snmp/ucd-snmp-includes.h>
00012 #include <ucd-snmp/ucd-snmp-agent-includes.h>
00013 #include <ucd-snmp/agent_trap.h>
00014 }
00015 
00016 #include <string>
00017 
00018 
00019 int
00020 rmEvent::init()
00021 {
00022   int ret;
00023 
00024   size_t buf(256);
00025   char query_str[buf];
00026   char error_str[buf];
00027   int flag = POSIX_LOG_SEND_RECID & POSIX_LOG_SEND_SIGVAL;
00028 
00029   posix_log_query_t query;
00030   posix_log_notify_t nfhandle;
00031   struct sigevent notification;
00032 
00033   snprintf(query_str, buf,
00034       "data contains \"Subsystem Added\" || data contains \"Subsystem Removed\" || data contains \"Data Capture Library Added\" || data contains \"Data Capture Library Removed\" || data contains \"Resource Added\" || data contains \"Resource Removed\" ");
00035 
00036   ret = posix_log_query_create(query_str, POSIX_LOG_PRPS_NOTIFY,
00037       &query, error_str, buf);
00038   if(ret)
00039     return RMCLIENT_ERROR_QUERY;
00040 
00041 
00042   notification.sigev_notify = SIGEV_SIGNAL;
00043   notification.sigev_signo = SIGRTMIN + 1;
00044   notification.sigev_value.sival_int = 0;
00045 
00046   ret = posix_log_notify_add(&query, &notification, flag, &nfhandle);
00047   if(ret)
00048     return RMCLIENT_ERROR_NOTIFY_CREATE;
00049 
00050   mSystemNotifyHandle = nfhandle;
00051 
00052   return RMCLIENT_SUCCESS;
00053 }
00054 
00060 void
00061 rmEvent::addEvent(posix_log_recid_t aRecID, unsigned long aMonID)
00062 {
00063   eventData event;
00064 
00065   event.recid = aRecID;
00066   event.monid = aMonID;
00067 
00068   mEventQueue.push(event);
00069 }
00070 
00079 void
00080 rmEvent::processEvents()
00081 {
00082   int ret;
00083 
00084   eventData event;
00085   long eType;
00086 
00087   while(!mEventQueue.empty()) {
00088     event = mEventQueue.front();
00089 
00090     ret = getEventType(event.recid, eType);
00091     if(ret) {
00092       mEventQueue.pop();
00093       continue; // let's just ignore the error and continue.
00094     }
00095 
00096     switch(eType) {
00097       case ME_EVENT_CANCELED:
00098         sendMonitorEvent(THRESHOLDCANCEL, event);
00099         break;
00100 
00101       case ME_EVENT_VALUE_ABOVE:
00102       case ME_EVENT_VALUE_AT_OR_ABOVE:
00103       case ME_EVENT_VALUE_AT:
00104       case ME_EVENT_VALUE_AT_OR_BELOW:
00105       case ME_EVENT_VALUE_BELOW:
00106       case ME_EVENT_VALUE_NOT_AT_BELOW:
00107       case ME_EVENT_VALUE_NOT_AT_ABOVE:
00108         sendMonitorEvent(THRESHOLD, event);
00109         break;
00110 
00111       case ME_EVENT_COUNTER_RATE_EXCEEDED:
00112         sendMonitorEvent(LEAKYBUCKET, event);
00113         break;
00114 
00115       case ME_EVENT_SUBSYSTEM_CONFIGURATION_CHANGE:
00116         processSystemEvent(SUBSYSTEM, event);
00117         break;
00118 
00119       case ME_EVENT_RESOURCE_CONFIGURATION_CHANGE:
00120         processSystemEvent(RESOURCE, event);
00121         break;
00122     }
00123 
00124     mEventQueue.pop();
00125   }
00126 }
00127 
00128 int
00129 rmEvent::getEventType(posix_log_recid_t aRecID, long & aType)
00130 {
00131   return getInteger32(EVENTLOG, EVENTLOG_TYPE, aRecID, &aType);
00132 }
00133 
00134 int
00135 rmEvent::getEventText(posix_log_recid_t aRecID, char * * aText)
00136 {
00137   return getString(EVENTLOG, EVENTLOG_STRING, aRecID, aText);
00138 }
00139 
00140 int
00141 rmEvent::sendMonitorEvent(EventType aType, eventData & aEvent)
00142 {
00143   size_t oidlen;
00144 
00145   static struct variable_list var_trap;
00146   static struct variable_list var_obj, var_obj2, var_obj3, var_obj4;
00147 
00148   static char logBuf[SPRINT_MAX_LEN];
00149 
00150   oid objid_snmptrap[] = { 1,3,6,1,6,3,1,1,4,1,0}; /* snmpTrapOID.0 */
00151 
00152   oid threshold[] = { 1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 911, 1 };
00153   oid thresholdCanel[] = { 1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 911, 2 };
00154   oid leakyBucket[] = { 1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 911, 3 };
00155   oid eventLog[] = { 1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 1, 5, 3, 1, 15, 0};
00156   oid monitorSubsystemID[] = { 1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 4, 2, 1, 7, 0 };
00157   oid monitorControlIndex[] = { 1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 4, 2, 1, 3, 0 };
00158   oid monitorConfigIndex[] = { 1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 4, 2, 1, 4, 0 };
00159 
00160   unsigned long subSystemID, configID, controlID;
00161 
00162   oidlen = sizeof(monitorSubsystemID)/sizeof(oid);
00163   monitorSubsystemID[oidlen-1] = aEvent.monid;
00164   monitorControlIndex[oidlen-1] = aEvent.monid;
00165   monitorConfigIndex[oidlen-1] = aEvent.monid;
00166 
00167   oidlen = sizeof(eventLog)/sizeof(oid);
00168   eventLog[oidlen -1] = aEvent.recid;
00169 
00170   getUnsigned(MONITOR, MONITOR_SUBSYSTEMID, aEvent.monid, &subSystemID);
00171   getUnsigned(MONITOR, MONITOR_CONTROLID, aEvent.monid, &controlID);
00172   getUnsigned(MONITOR, MONITOR_CONFIGID, aEvent.monid, &configID);
00173 
00174   char * eText;
00175   getEventText(aEvent.recid, &eText);
00176   strncpy(logBuf, eText, SPRINT_MAX_LEN);
00177   free(eText);
00178 
00179   /* trap definition objects */
00180   var_trap.next_variable = &var_obj; /* next variable */
00181   var_trap.name = objid_snmptrap; /* snmpTrapOID.0 */
00182   var_trap.name_length = sizeof(objid_snmptrap)/sizeof(oid); /* number of sub-ids */
00183   var_trap.type = ASN_OBJECT_ID;
00184 
00185   switch(aType) {
00186 
00187     case THRESHOLDCANCEL:
00188       var_trap.val.objid = thresholdCanel;
00189       var_trap.val_len = sizeof(thresholdCanel);
00190       break;
00191 
00192     case THRESHOLD:
00193       var_trap.val.objid = threshold;
00194       var_trap.val_len = sizeof(threshold);
00195       break;
00196 
00197     case LEAKYBUCKET:
00198       var_trap.val.objid = leakyBucket;
00199       var_trap.val_len = sizeof(leakyBucket);
00200       break;
00201 
00202     default:
00203       return RMCLIENT_ERROR_NOSUCHNAME;
00204   }
00205 
00206   /* additional objects */
00207   var_obj.next_variable = &var_obj2;
00208   var_obj.name = monitorSubsystemID;
00209   var_obj.name_length = sizeof(monitorSubsystemID)/sizeof(oid);
00210   var_obj.type = ASN_UNSIGNED;
00211   var_obj.val.integer = (long*) &subSystemID;
00212   var_obj.val_len = sizeof(unsigned long);
00213 
00214   var_obj2.next_variable = &var_obj3;
00215   var_obj2.name = monitorControlIndex;
00216   var_obj2.name_length = sizeof(monitorControlIndex)/sizeof(oid);
00217   var_obj2.type = ASN_UNSIGNED;
00218   var_obj2.val.integer = (long*) &controlID;
00219   var_obj2.val_len = sizeof(unsigned long);
00220 
00221   var_obj3.next_variable = &var_obj4;
00222   var_obj3.name = monitorConfigIndex;
00223   var_obj3.name_length = sizeof(monitorConfigIndex)/sizeof(oid);
00224   var_obj3.type = ASN_UNSIGNED;
00225   var_obj3.val.integer = (long*) &configID;
00226   var_obj3.val_len = sizeof(unsigned long);
00227 
00228   var_obj4.next_variable = NULL;
00229   var_obj4.name = eventLog;
00230   var_obj4.name_length = sizeof(eventLog)/sizeof(oid);
00231   var_obj4.type = ASN_OCTET_STR;
00232   var_obj4.val.string = (u_char *) logBuf;
00233   var_obj4.val_len = strlen(logBuf);
00234 
00235   send_v2trap(&var_trap);
00236 
00237   return RMCLIENT_SUCCESS;
00238 }
00239 
00240 int
00241 rmEvent::sendSubSystemEvent(EventName aEvent, uuid_t aUUID)
00242 {
00243   size_t oidlen;
00244 
00245   static struct variable_list var_trap;
00246   static struct variable_list var_obj, var_obj2;
00247 
00248   oid objid_snmptrap[] = { 1,3,6,1,6,3,1,1,4,1,0}; /* snmpTrapOID.0 */
00249 
00250   oid subsystemAdd[] = { 1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 911, 4 };
00251   oid subsystemRemove[] = { 1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 911, 5 };
00252   oid subSystemUUID[] = {  1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 1, 3, 1, 2, 0 };
00253   oid subSystemName[] = {  1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 1, 3, 1, 3, 0 };
00254 
00255   static char name[SPRINT_MAX_LEN];
00256   static char uuid[UUID_STRING_LENGTH];
00257 
00258   unsigned long id = mSubsystemInfo->getSubsystemIDByUUID(aUUID);
00259 
00260   oidlen = sizeof(subSystemUUID)/sizeof(oid);
00261   subSystemUUID[oidlen - 1] = id;
00262   subSystemName[oidlen - 1] = id;
00263 
00264   char * buf;
00265   getString(SUBSYSTEMINFO, SUBSYSTEMINFO_NAME, id, &buf);
00266   strncpy(name, buf, SPRINT_MAX_LEN);
00267   free(buf);
00268 
00269   uuid_unparse(aUUID, uuid);
00270 
00271   /* trap definition objects */
00272   var_trap.next_variable = &var_obj; /* next variable */
00273   var_trap.name = objid_snmptrap; /* snmpTrapOID.0 */
00274   var_trap.name_length = sizeof(objid_snmptrap)/sizeof(oid); /* number of sub-ids */
00275   var_trap.type = ASN_OBJECT_ID;
00276 
00277   if(aEvent == SUBSYSTEM_REMOVED) {
00278     var_trap.val.objid = subsystemRemove;
00279     var_trap.val_len = sizeof(subsystemRemove);
00280   } else if (aEvent == SUBSYSTEM_ADDED) {
00281     var_trap.val.objid = subsystemAdd;
00282     var_trap.val_len = sizeof(subsystemAdd);
00283   }
00284 
00285   /* additional objects */
00286   var_obj.next_variable = &var_obj2;
00287   var_obj.name = subSystemUUID;
00288   var_obj.name_length = sizeof(subSystemUUID)/sizeof(oid);
00289   var_obj.type = ASN_OCTET_STR;
00290   var_obj.val.string = (u_char *) uuid;
00291   var_obj.val_len = UUID_STRING_LENGTH;
00292 
00293   var_obj2.next_variable = NULL;
00294   var_obj2.name = subSystemName;
00295   var_obj2.name_length = sizeof(subSystemName)/sizeof(oid);
00296   var_obj2.type = ASN_OCTET_STR;
00297   var_obj2.val.string = (u_char *) name;
00298   var_obj2.val_len = strlen(name);
00299 
00300   send_v2trap(&var_trap);
00301 
00302   return RMCLIENT_SUCCESS;
00303 }
00304 
00305 int
00306 rmEvent::sendResourceEvent(EventName aEvent, uuid_t aUUID, unsigned long aID, string aName)
00307 {
00308   static struct variable_list var_trap;
00309   static struct variable_list var_obj, var_obj2, var_obj3;
00310 
00311   oid objid_snmptrap[] = { 1,3,6,1,6,3,1,1,4,1,0}; /* snmpTrapOID.0 */
00312 
00313   oid resourceAdd[] = { 1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 911, 6 };
00314   oid resourceRemove[] = { 1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 911, 7 };
00315   oid resourceName[] = { 1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 1, 4, 1, 2, 0, 0 };
00316   oid subSystemUUID[] = {  1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 1, 3, 1, 2, 0 };
00317   oid subSystemName[] = {  1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 1, 3, 1, 3, 0 };
00318 
00319   static char rname[SPRINT_MAX_LEN];
00320   static char sname[SPRINT_MAX_LEN];
00321   static char uuid[UUID_STRING_LENGTH];
00322 
00323   unsigned long id = mSubsystemInfo->getSubsystemIDByUUID(aUUID);
00324   size_t oidlen = sizeof(resourceName)/sizeof(oid);
00325   resourceName[oidlen - 2] = id;
00326   resourceName[oidlen - 1] = aID;
00327    
00328   oidlen = sizeof(subSystemUUID)/sizeof(oid);
00329   subSystemUUID[oidlen - 1] = id;
00330   subSystemName[oidlen - 1] = id;
00331 
00332   char * buf;
00333   getString(SUBSYSTEMINFO, SUBSYSTEMINFO_NAME, id, &buf);
00334   strncpy(sname, buf, SPRINT_MAX_LEN);
00335   free(buf);
00336 
00337   uuid_unparse(aUUID, uuid);
00338 
00339   strncpy(rname, aName.c_str(), SPRINT_MAX_LEN);
00340 
00341   /* trap definition objects */
00342   var_trap.next_variable = &var_obj; /* next variable */
00343   var_trap.name = objid_snmptrap; /* snmpTrapOID.0 */
00344   var_trap.name_length = sizeof(objid_snmptrap)/sizeof(oid); /* number of sub-ids */
00345   var_trap.type = ASN_OBJECT_ID;
00346 
00347   if (aEvent == RESOURCE_REMOVED) {
00348     var_trap.val.objid = resourceRemove;
00349     var_trap.val_len = sizeof(resourceRemove);
00350   } else if (aEvent == RESOURCE_ADDED) {
00351     var_trap.val.objid = resourceAdd;
00352     var_trap.val_len = sizeof(resourceAdd);
00353   }
00354 
00355   /* additional objects */
00356   var_obj.next_variable = &var_obj2;
00357   var_obj.name = resourceName;
00358   var_obj.name_length = sizeof(resourceName)/sizeof(oid);
00359   var_obj.type = ASN_OCTET_STR;
00360   var_obj.val.string = (u_char *) rname;
00361   var_obj.val_len = strlen(rname);
00362 
00363   var_obj2.next_variable = &var_obj3;
00364   var_obj2.name = subSystemUUID;
00365   var_obj2.name_length = sizeof(subSystemUUID)/sizeof(oid);
00366   var_obj2.type = ASN_OCTET_STR;
00367   var_obj2.val.string = (u_char *) uuid;
00368   var_obj2.val_len = UUID_STRING_LENGTH;
00369 
00370   var_obj3.next_variable = NULL;
00371   var_obj3.name = subSystemName;
00372   var_obj3.name_length = sizeof(subSystemName)/sizeof(oid);
00373   var_obj3.type = ASN_UNSIGNED;
00374   var_obj3.val.string = (u_char *) sname;
00375   var_obj3.val_len = strlen(sname);
00376 
00377   send_v2trap(&var_trap);
00378 
00379   return RMCLIENT_SUCCESS;
00380 }
00381 
00382 int
00383 rmEvent::processSystemEvent(EventType aType, eventData & aEvent)
00384 {
00385   int ret;
00386   char * eText;
00387   string eventLog, uuidString, residString, resnameString, pluginString;
00388   unsigned long pluginID;
00389   char * str;
00390 
00391   uuid_t suuid;
00392 
00393   getEventText(aEvent.recid, &eText);
00394   eventLog.assign(eText);
00395   free(eText);
00396 
00397   string::size_type idx = eventLog.find("Subsystem_ID=");
00398   idx = eventLog.find('=', idx);
00399   uuidString = eventLog.substr(idx+1, 36);
00400 
00401   uuid_clear(suuid);
00402   uuid_parse((char *) uuidString.c_str(), suuid);
00403 
00404   if (aType == SUBSYSTEM) {
00405 
00406     if (eventLog.find(ME_RM_SubsystemAdded) != string::npos) {
00407 
00408       ret = mSubsystemInfo->addSubsystemByUUID(suuid);
00409       if(ret)
00410         return ret;
00411 
00412       return sendSubSystemEvent(SUBSYSTEM_ADDED, suuid);
00413       
00414     } else if (eventLog.find(ME_RM_SubsystemRemoved) != string::npos) {
00415 
00416       ret = mSubsystemInfo->removeSubsystemByUUID(suuid);
00417       if(ret)
00418         return ret;
00419 
00420       return sendSubSystemEvent(SUBSYSTEM_REMOVED, suuid);
00421 
00422     } else if (eventLog.find(ME_RM_DataCaptureAdded) != string::npos) {
00423       ret = mDataCaptureInfo->init();
00424       if(ret)
00425         return ret;
00426 
00427       pluginID = mDataCaptureInfo->getIDByUUID(suuid);
00428       if(pluginID) {
00429         mDataCaptureInfo->getString(DATACAPTUREPLUGININFO_NAME, pluginID, str);
00430         pluginString = str;
00431         free(str);
00432 
00433         sendPluginEvent(DATACAPTURE_ADDED, suuid, pluginID, pluginString);
00434       }
00435 
00436     } else if (eventLog.find(ME_RM_DataCaptureRemoved) != string::npos) {
00437 
00438       pluginID = mDataCaptureInfo->getIDByUUID(suuid);
00439       if(pluginID) {
00440         mDataCaptureInfo->getString(DATACAPTUREPLUGININFO_NAME, pluginID, str);
00441         pluginString = str;
00442         free(str);
00443       }
00444 
00445       ret = mDataCaptureInfo->init();
00446       if(ret)
00447         return ret;
00448 
00449       sendPluginEvent(DATACAPTURE_REMOVED, suuid, pluginID, pluginString);
00450     }
00451 
00452   } else if (aType == RESOURCE) {
00453 
00454     idx = eventLog.find("Resource_ID=");
00455     residString = eventLog.substr(idx);
00456     idx = residString.find('=');
00457     residString = residString.substr(idx+1);
00458     idx = residString.find('|');
00459     residString = residString.substr(0, idx);
00460 
00461     idx = eventLog.find("Resource_Name=");
00462     resnameString = eventLog.substr(idx);
00463     idx = resnameString.find('=');
00464     resnameString = resnameString.substr(idx+1);
00465 
00466     unsigned long rid = strtol(residString.c_str(), NULL, 10);
00467 
00468     if (eventLog.find(ME_RM_ResourceAdded) != string::npos) {
00469 
00470       ret = mResourceInfo->addResource(suuid, rid);
00471       if(ret)
00472         return ret;
00473 
00474       return sendResourceEvent(RESOURCE_ADDED, suuid, rid, resnameString);
00475       
00476     } else if (eventLog.find(ME_RM_ResourceRemoved) != string::npos) {
00477 
00478       ret = mResourceInfo->removeResource(suuid, rid);
00479       if(ret)
00480         return ret;
00481 
00482       return sendResourceEvent(RESOURCE_REMOVED, suuid, rid, resnameString);
00483     }
00484   }
00485 
00486   return RMCLIENT_SUCCESS;
00487 }
00488 
00489 int
00490 rmEvent::sendPluginEvent(EventName aEvent, uuid_t aUUID, unsigned long aID, string aName)
00491 {
00492   static struct variable_list var_trap;
00493   static struct variable_list var_obj, var_obj2;
00494 
00495   oid objid_snmptrap[] = { 1,3,6,1,6,3,1,1,4,1,0}; /* snmpTrapOID.0 */
00496 
00497   oid pluginAdd[] = { 1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 911, 8 };
00498   oid pluginRemove[] = { 1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 911, 9 };
00499   oid pluginName[] = { 1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 1, 6, 1, 3, 0 };
00500   oid pluginUUID[] = {  1, 3, 6, 1, 4, 1, 343, 2, 15, 1, 2, 1, 6, 1, 2, 0 };
00501 
00502   static char pname[SPRINT_MAX_LEN];
00503   static char uuid[UUID_STRING_LENGTH];
00504 
00505   size_t oidlen = sizeof(pluginName)/sizeof(oid);
00506   pluginName[oidlen - 1] = aID;
00507   pluginUUID[oidlen - 1] = aID;
00508 
00509   strncpy(pname, aName.c_str(), SPRINT_MAX_LEN);
00510 
00511   uuid_unparse(aUUID, uuid);
00512 
00513   /* trap definition objects */
00514   var_trap.next_variable = &var_obj; /* next variable */
00515   var_trap.name = objid_snmptrap; /* snmpTrapOID.0 */
00516   var_trap.name_length = sizeof(objid_snmptrap)/sizeof(oid); /* number of sub-ids */
00517   var_trap.type = ASN_OBJECT_ID;
00518 
00519   if (aEvent == DATACAPTURE_REMOVED) {
00520     var_trap.val.objid = pluginRemove;
00521     var_trap.val_len = sizeof(pluginRemove);
00522   } else if (aEvent == DATACAPTURE_ADDED) {
00523     var_trap.val.objid = pluginAdd;
00524     var_trap.val_len = sizeof(pluginAdd);
00525   }
00526 
00527   /* additional objects */
00528   var_obj.next_variable = &var_obj2;
00529   var_obj.name = pluginUUID;
00530   var_obj.name_length = sizeof(pluginUUID)/sizeof(oid);
00531   var_obj.type = ASN_OCTET_STR;
00532   var_obj.val.string = (u_char *) uuid;
00533   var_obj.val_len = UUID_STRING_LENGTH;
00534 
00535   var_obj2.next_variable = NULL;
00536   var_obj2.name = pluginName;
00537   var_obj2.name_length = sizeof(pluginName)/sizeof(oid);
00538   var_obj2.type = ASN_OCTET_STR;
00539   var_obj2.val.string = (u_char *) pname;
00540   var_obj2.val_len = strlen(pname);
00541 
00542   send_v2trap(&var_trap);
00543 
00544   return RMCLIENT_SUCCESS;
00545 }
00546 
00547 
00548 

Generated on Tue Sep 10 16:46:32 2002 for ResourceMonitorSNMPSubagent by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002