(file) Return to snmpIndicationHandler.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Handler / snmpIndicationHandler

  1 karl  1.29 //%2006////////////////////////////////////////////////////////////////////////
  2 mike  1.2  //
  3 karl  1.24 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  4            // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  5            // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  6 karl  1.21 // IBM Corp.; EMC Corporation, The Open Group.
  7 karl  1.24 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8            // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9 karl  1.25 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10            // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11 karl  1.29 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 12            // EMC Corporation; Symantec Corporation; The Open Group.
 13 mike  1.2  //
 14            // Permission is hereby granted, free of charge, to any person obtaining a copy
 15 kumpf 1.9  // of this software and associated documentation files (the "Software"), to
 16            // deal in the Software without restriction, including without limitation the
 17            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 18 mike  1.2  // sell copies of the Software, and to permit persons to whom the Software is
 19            // furnished to do so, subject to the following conditions:
 20            // 
 21 kumpf 1.9  // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 22 mike  1.2  // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 23            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 24 kumpf 1.9  // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 25            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 26            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 27 mike  1.2  // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 28            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 29            //
 30            //==============================================================================
 31            //
 32            //%/////////////////////////////////////////////////////////////////////////////
 33            
 34            #include <Pegasus/Common/Config.h>
 35 kumpf 1.7  #include <Pegasus/Common/PegasusVersion.h>
 36            
 37 mike  1.2  #include <iostream>
 38            #include <Pegasus/Handler/CIMHandler.h>
 39            #include <Pegasus/Repository/CIMRepository.h>
 40 kumpf 1.23 #include <Pegasus/Common/Tracer.h>
 41 mike  1.2  
 42            #include "snmpIndicationHandler.h"
 43            
 44            #ifdef HPUX_EMANATE
 45 kumpf 1.34 # include "snmpDeliverTrap_emanate.h"
 46 yi.zhou 1.27 #elif defined (PEGASUS_USE_NET_SNMP)
 47 kumpf   1.34 # include "snmpDeliverTrap_netsnmp.h"
 48 mike    1.2  #else
 49 kumpf   1.34 # include "snmpDeliverTrap_stub.h"
 50 mike    1.2  #endif
 51              
 52 humberto 1.15 #include <Pegasus/Common/MessageLoader.h>
 53               
 54 kumpf    1.32 PEGASUS_USING_STD;
 55               
 56 mike     1.2  PEGASUS_NAMESPACE_BEGIN
 57               
 58 kumpf    1.32 snmpIndicationHandler::snmpIndicationHandler()
 59               {
 60                   PEG_METHOD_ENTER (TRC_IND_HANDLER,
 61                       "snmpIndicationHandler::snmpIndicationHandler");
 62 mike     1.2  
 63 yi.zhou  1.30 #ifdef HPUX_EMANATE
 64 kumpf    1.32     _snmpTrapSender = new snmpDeliverTrap_emanate();
 65 yi.zhou  1.30 #elif defined (PEGASUS_USE_NET_SNMP)
 66 kumpf    1.32     _snmpTrapSender = new snmpDeliverTrap_netsnmp();
 67 yi.zhou  1.30 #else
 68 kumpf    1.32     _snmpTrapSender = new snmpDeliverTrap_stub();
 69 yi.zhou  1.30 #endif
 70               
 71                   PEG_METHOD_EXIT();
 72               }
 73               
 74 mike     1.2  void snmpIndicationHandler::initialize(CIMRepository* repository)
 75               {
 76 yi.zhou  1.30     PEG_METHOD_ENTER (TRC_IND_HANDLER,
 77                       "snmpIndicationHandler::initialize");
 78               
 79 mike     1.2      _repository = repository;
 80 yi.zhou  1.30 
 81 kumpf    1.32     _snmpTrapSender->initialize();
 82 yi.zhou  1.30 
 83                   PEG_METHOD_EXIT();
 84               }
 85               
 86               void snmpIndicationHandler::terminate()
 87               {
 88 kumpf    1.34     PEG_METHOD_ENTER(TRC_IND_HANDLER,
 89 yi.zhou  1.30         "snmpIndicationHandler::terminate");
 90               
 91 kumpf    1.32     _snmpTrapSender->terminate();
 92 yi.zhou  1.30 
 93                   PEG_METHOD_EXIT();
 94               }
 95               
 96               snmpIndicationHandler::~snmpIndicationHandler()
 97               {
 98 kumpf    1.34     PEG_METHOD_ENTER(TRC_IND_HANDLER,
 99 yi.zhou  1.30         "snmpIndicationHandler::~snmpIndicationHandler");
100               
101 kumpf    1.32     delete _snmpTrapSender;
102 yi.zhou  1.30 
103                   PEG_METHOD_EXIT();
104 mike     1.2  }
105               
106 chuck    1.14 // l10n - note: ignoring indication language
107 kumpf    1.16 void snmpIndicationHandler::handleIndication(
108                   const OperationContext& context,
109 yi.zhou  1.26     const String nameSpace,
110                   CIMInstance& indication,
111 kumpf    1.34     CIMInstance& handler,
112 yi.zhou  1.26     CIMInstance& subscription,
113 kumpf    1.28     ContentLanguageList & contentLanguages)
114 mike     1.2  {
115                   Array<String> propOIDs;
116                   Array<String> propTYPEs;
117                   Array<String> propVALUEs;
118               
119 yi.zhou  1.36     Array<String> mapStr;
120 mike     1.2  
121 kumpf    1.34     PEG_METHOD_ENTER(TRC_IND_HANDLER,
122                       "snmpIndicationHandler::handleIndication");
123 mike     1.2  
124 kumpf    1.20     try
125                   {
126 kumpf    1.34         CIMClass indicationClass = _repository->getClass(
127                           nameSpace, indication.getClassName(), false, true,
128                           false, CIMPropertyList());
129 kumpf    1.20 
130 kumpf    1.34         Uint32 propertyCount = indication.getPropertyCount();
131 kumpf    1.13 
132 kumpf    1.34         for (Uint32 i=0; i < propertyCount; i++)
133                       {
134 yi.zhou  1.36             CIMProperty prop = indication.getProperty(i);
135 mike     1.2  
136 yi.zhou  1.36             Uint32 propDeclPos = indicationClass.findProperty(prop.getName());
137                           if (propDeclPos != PEG_NOT_FOUND)
138 kumpf    1.4              {
139 yi.zhou  1.36                 CIMProperty propDecl = indicationClass.getProperty(propDeclPos);
140               
141                               Uint32 qualifierPos =
142                                   propDecl.findQualifier(CIMName("MappingStrings"));
143                               if (qualifierPos != PEG_NOT_FOUND)
144 kumpf    1.20                 {
145 yi.zhou  1.36                     //
146                                   // We are looking for following fields:
147                                   // MappingStrings {"OID.IETF | SNMP." oidStr, 
148                                   //     "DataType.IETF |" dataType}
149                                   // oidStr is the object identifier (e.g. "1.3.6.1.2.1.5..."
150                                   // dataType is either Integer, or OctetString, 
151                                   // or OID
152                                   // Following is one example:
153                                   // MappingStrings {"OID.IETF | SNMP.1.3.6.6.3.1.1.5.2",
154                                   //    "DataType.IETF | Integer"}
155                                   //
156               
157                                   propDecl.getQualifier(qualifierPos).getValue().get(
158                                       mapStr);
159                
160                                   String oidStr, dataType;
161                                   String mapStr1, mapStr2;
162                                   Boolean isValidAuthority = false;
163                                   Boolean isValidDataType = false;
164 mike     1.2  
165 yi.zhou  1.36                     for (Uint32 j=0; j < mapStr.size(); j++)
166 kumpf    1.20                     {
167 yi.zhou  1.36                         Uint32 barPos = mapStr[j].find("|");
168                                           
169                                       if (barPos != PEG_NOT_FOUND) 
170                                       {
171                                           mapStr1 = mapStr[j].subString(0, barPos);
172                                           mapStr2 = mapStr[j].subString(barPos + 1);
173 kumpf    1.34 
174 yi.zhou  1.36                             _trimWhitespace(mapStr1);
175                                           _trimWhitespace(mapStr2);
176                                               
177                                           if ((mapStr1 == "OID.IETF") &&
178                                               (String::compare(mapStr2, 
179                                                String("SNMP."), 5) == 0))
180                                           {
181                                               isValidAuthority = true;
182                                               oidStr = mapStr2.subString(5);
183                                           }
184                                           else if (mapStr1 == "DataType.IETF")
185                                           {
186                                               isValidDataType = true;
187                                               dataType = mapStr2;
188                                           }
189 kumpf    1.34 
190 yi.zhou  1.36                             if (isValidAuthority && isValidDataType) 
191 kumpf    1.34                             {
192 yi.zhou  1.36                                 propOIDs.append(oidStr);
193                                               propTYPEs.append(dataType);
194                                               propVALUEs.append(prop.getValue().toString());
195               
196                                               break;
197 kumpf    1.34                             }
198                                       }
199                                   }
200 kumpf    1.20                 }
201 kumpf    1.4              }
202 mike     1.2          }
203               
204                       // Collected complete data in arrays and ready to send the trap.
205                       // trap destination and SNMP type are defined in handlerInstance
206                       // and passing this instance as it is to deliverTrap() call
207               
208 kumpf    1.34         Uint32 targetHostPos = handler.findProperty(CIMName("TargetHost"));
209                       Uint32 targetHostFormatPos =
210                           handler.findProperty(CIMName("TargetHostFormat"));
211                       Uint32 otherTargetHostFormatPos =
212                           handler.findProperty(CIMName("OtherTargetHostFormat"));
213                       Uint32 portNumberPos = handler.findProperty(CIMName("PortNumber"));
214                       Uint32 snmpVersionPos = handler.findProperty(CIMName("SNMPVersion"));
215                       Uint32 securityNamePos =
216                           handler.findProperty(CIMName("SNMPSecurityName"));
217                       Uint32 engineIDPos = handler.findProperty(CIMName("SNMPEngineID"));
218 kumpf    1.20 
219                       if ((targetHostPos != PEG_NOT_FOUND) &&
220 kumpf    1.34             (targetHostFormatPos != PEG_NOT_FOUND) &&
221 yi.zhou  1.31             (snmpVersionPos != PEG_NOT_FOUND))
222 kumpf    1.16         {
223 kumpf    1.34             // properties from the handler instance
224 kumpf    1.20             String targetHost;
225 kumpf    1.34             String otherTargetHostFormat = String();
226                           String securityName = String();
227                           String engineID = String();
228                           Uint16 targetHostFormat = 0;
229                           Uint16 snmpVersion = 0;
230                           Uint32 portNumber;
231 kumpf    1.20 
232 kumpf    1.34             String trapOid;
233 yi.zhou  1.36             Boolean trapOidAvailable = false;
234                           String exceptionStr;
235 kumpf    1.34             //
236 kumpf    1.20             //  Get snmpTrapOid from context
237                           //
238 kumpf    1.34             if (context.contains(SnmpTrapOidContainer::NAME))
239                           {
240                               SnmpTrapOidContainer trapContainer =
241                                   context.get(SnmpTrapOidContainer::NAME);
242 kumpf    1.16 
243 kumpf    1.20                 trapOid = trapContainer.getSnmpTrapOid();
244 yi.zhou  1.36                 trapOidAvailable = true;
245 kumpf    1.34             }
246                           else
247 kumpf    1.20             {
248 kumpf    1.34                 // get trapOid from indication Class
249 kumpf    1.16 
250 kumpf    1.34                 Uint32 pos =
251                                   indicationClass.findQualifier(CIMName("MappingStrings"));
252                               if (pos != PEG_NOT_FOUND)
253                               {
254 yi.zhou  1.36                     Array<String> classMapStr;
255                                   indicationClass.getQualifier(pos).getValue().
256                                       get(classMapStr);
257 kumpf    1.20 
258 yi.zhou  1.36                     for (Uint32 i=0; i < classMapStr.size(); i++)
259                                   {
260                                       Uint32 barPos = classMapStr[i].find("|");
261 kumpf    1.20 
262 yi.zhou  1.36                         if (barPos != PEG_NOT_FOUND) 
263                                       {
264                                           String authorityName = 
265                                               classMapStr[i].subString(0, barPos);
266                                           String oidStr = classMapStr[i].subString(
267                                               barPos+1, PEG_NOT_FOUND);
268               
269                                           _trimWhitespace(authorityName);
270                                           _trimWhitespace(oidStr);
271               
272                                           if ((authorityName == "OID.IETF") &&
273                                               (String::compare(oidStr, 
274                                                String("SNMP."), 5) == 0))
275                                           {
276                                               trapOid = oidStr.subString(5); 
277                                               trapOidAvailable = true;
278                                               break;
279                                           }
280                                       }
281 kumpf    1.20                     }
282 yi.zhou  1.36 
283                                   if (!trapOidAvailable)
284 kumpf    1.34                     {
285 yi.zhou  1.36                         exceptionStr = "No MappingStrings for snmp trap"
286                                           "is specified for class: ";
287                                     
288                                       exceptionStr.append(
289                                           indication.getClassName().getString());
290               
291 kumpf    1.34                         PEG_TRACE_STRING(TRC_IND_HANDLER, Tracer::LEVEL4,
292 yi.zhou  1.36                             exceptionStr);
293 kumpf    1.34                         PEG_METHOD_EXIT();
294 yi.zhou  1.36 
295 kumpf    1.34                         throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
296                                           MessageLoaderParms(
297                                               "Handler.snmpIndicationHandler."
298 yi.zhou  1.36                                 "snmpIndicationHandler.NO_MS_FOR_SNMP_TRAP",
299                                               exceptionStr));
300 kumpf    1.34                     }
301                               }
302                               else
303                               {
304 marek    1.35                     PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL4,
305 kumpf    1.34                         "Qualifier MappingStrings can not be found.");
306                                   PEG_METHOD_EXIT();
307                                   MessageLoaderParms parms(
308                                       "Handler.snmpIndicationHandler.snmpIndicationHandler."
309                                           "QUALIFIER_MAPPINGS_NOT_FOUND",
310                                       "Qualifier MappingStrings can not be found");
311                                   throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, parms);
312                               }
313                           }
314               
315                           handler.getProperty(targetHostPos).getValue().get(targetHost);
316                           handler.getProperty(targetHostFormatPos).getValue().get(
317                               targetHostFormat);
318                           if (otherTargetHostFormatPos != PEG_NOT_FOUND)
319                           {
320                               handler.getProperty(otherTargetHostFormatPos).getValue().get(
321                                   otherTargetHostFormat);
322                           }
323                           if (portNumberPos != PEG_NOT_FOUND)
324                           {
325                               handler.getProperty(portNumberPos).getValue().get(portNumber);
326 kumpf    1.34             }
327                           else
328                           {
329                               // default port
330                               portNumber = SNMP_TRAP_DEFAULT_PORT;
331                           }
332               
333                           handler.getProperty(snmpVersionPos).getValue().get(snmpVersion);
334                           if (securityNamePos != PEG_NOT_FOUND)
335                           {
336                               handler.getProperty(securityNamePos).getValue().get(
337                                   securityName);
338                           }
339                           if (engineIDPos != PEG_NOT_FOUND)
340                           {
341                               handler.getProperty(engineIDPos).getValue().get(engineID);
342                           }
343 kumpf    1.13 
344 kumpf    1.34             _snmpTrapSender->deliverTrap(
345 kumpf    1.20                 trapOid,
346                               securityName,
347                               targetHost,
348                               targetHostFormat,
349 kumpf    1.34                 otherTargetHostFormat,
350                               portNumber,
351                               snmpVersion,
352                               engineID,
353                               propOIDs,
354                               propTYPEs,
355 kumpf    1.20                 propVALUEs);
356                       }
357                       else
358                       {
359 marek    1.35             PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL4,
360 kumpf    1.34                 "Invalid IndicationHandlerSNMPMapper instance.");
361                           PEG_METHOD_EXIT();
362                           throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
363                               "Handler.snmpIndicationHandler.snmpIndicationHandler."
364                                   "INVALID_SNMP_INSTANCE",
365                               "Invalid IndicationHandlerSNMPMapper instance"));
366 kumpf    1.20         }
367 kumpf    1.34     }
368                   catch (CIMException& c)
369 kumpf    1.20     {
370 kumpf    1.34         PEG_TRACE_STRING(TRC_IND_HANDLER, Tracer::LEVEL4, c.getMessage());
371                       PEG_METHOD_EXIT();
372                       throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, c.getMessage());
373 kumpf    1.20     }
374                   catch (Exception& e)
375                   {
376 kumpf    1.34         PEG_TRACE_STRING(TRC_IND_HANDLER, Tracer::LEVEL4, e.getMessage());
377                       PEG_METHOD_EXIT();
378 kumpf    1.20 
379 kumpf    1.34         throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, e.getMessage());
380 kumpf    1.20     }
381                   catch (...)
382                   {
383 marek    1.35         PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL4,
384 kumpf    1.34             "Failed to deliver trap.");
385                       PEG_METHOD_EXIT();
386               
387                       throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
388                           "Handler.snmpIndicationHandler.snmpIndicationHandler."
389                               "FAILED_TO_DELIVER_TRAP",
390                           "Failed to deliver trap."));
391 kumpf    1.13     }
392 yi.zhou  1.30 
393                   PEG_METHOD_EXIT();
394 mike     1.2  }
395               
396 yi.zhou  1.36 void snmpIndicationHandler::_trimWhitespace(
397                   String & nameStr)
398               {
399                   PEG_METHOD_ENTER(TRC_IND_HANDLER,
400                       "snmpIndicationHandler::_trimWhitespace");
401               
402                   Uint32 ps = 0;
403                   // skip begining whitespace
404                   for (ps = 0; ps < nameStr.size(); ps++)
405                   {
406                       if (nameStr[ps] != ' ')
407                       {
408                           break;
409                       }
410                   }
411               
412                   if (ps != 0)
413                   {
414                       nameStr.remove(0, ps);
415                   }
416               
417 yi.zhou  1.36     // skip the appended whitespace
418                   for (ps = nameStr.size(); ps != 0; ps--)
419                   {
420                       if (nameStr[ps-1] != ' ')
421                       {
422                           break;
423                       }
424                   }
425               
426                   if (ps !=  nameStr.size())
427                   {
428                       nameStr.remove(ps);
429                   }
430               
431                   PEG_METHOD_EXIT();
432               }
433               
434 kumpf    1.32 PEGASUS_NAMESPACE_END
435               
436               PEGASUS_USING_PEGASUS;
437               
438 kumpf    1.33 // This is the entry point into this dynamic module.
439 mike     1.2  
440 kumpf    1.33 extern "C" PEGASUS_EXPORT CIMHandler* PegasusCreateHandler(
441                   const String& handlerName)
442 kumpf    1.32 {
443 kumpf    1.33     if (handlerName == "snmpIndicationHandler")
444                   {
445                       return new snmpIndicationHandler;
446                   }
447               
448                   return 0;
449 mike     1.2  }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2