(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 w.otsuka 1.37         PEG_TRACE ((TRC_INDICATION_GENERATION, Tracer::LEVEL3,
127                           "snmpIndicationHandler %s:%s.%s processing %s Indication",
128                          (const char*)(nameSpace.getCString()),
129                          (const char*)(handler.getClassName().getString().getCString()),
130                          (const char*)(handler.getProperty(
131                          handler.findProperty(PEGASUS_PROPERTYNAME_NAME)).
132                          getValue().toString().getCString()),
133                          (const char*)(indication.getClassName().getString().
134                          getCString())));
135 kumpf    1.34         CIMClass indicationClass = _repository->getClass(
136                           nameSpace, indication.getClassName(), false, true,
137                           false, CIMPropertyList());
138 kumpf    1.20 
139 kumpf    1.34         Uint32 propertyCount = indication.getPropertyCount();
140 kumpf    1.13 
141 kumpf    1.34         for (Uint32 i=0; i < propertyCount; i++)
142                       {
143 yi.zhou  1.36             CIMProperty prop = indication.getProperty(i);
144 mike     1.2  
145 yi.zhou  1.36             Uint32 propDeclPos = indicationClass.findProperty(prop.getName());
146                           if (propDeclPos != PEG_NOT_FOUND)
147 kumpf    1.4              {
148 yi.zhou  1.36                 CIMProperty propDecl = indicationClass.getProperty(propDeclPos);
149               
150                               Uint32 qualifierPos =
151                                   propDecl.findQualifier(CIMName("MappingStrings"));
152                               if (qualifierPos != PEG_NOT_FOUND)
153 kumpf    1.20                 {
154 yi.zhou  1.36                     //
155                                   // We are looking for following fields:
156                                   // MappingStrings {"OID.IETF | SNMP." oidStr, 
157                                   //     "DataType.IETF |" dataType}
158                                   // oidStr is the object identifier (e.g. "1.3.6.1.2.1.5..."
159                                   // dataType is either Integer, or OctetString, 
160                                   // or OID
161                                   // Following is one example:
162                                   // MappingStrings {"OID.IETF | SNMP.1.3.6.6.3.1.1.5.2",
163                                   //    "DataType.IETF | Integer"}
164                                   //
165               
166                                   propDecl.getQualifier(qualifierPos).getValue().get(
167                                       mapStr);
168                
169                                   String oidStr, dataType;
170                                   String mapStr1, mapStr2;
171                                   Boolean isValidAuthority = false;
172                                   Boolean isValidDataType = false;
173 mike     1.2  
174 yi.zhou  1.36                     for (Uint32 j=0; j < mapStr.size(); j++)
175 kumpf    1.20                     {
176 yi.zhou  1.36                         Uint32 barPos = mapStr[j].find("|");
177                                           
178                                       if (barPos != PEG_NOT_FOUND) 
179                                       {
180                                           mapStr1 = mapStr[j].subString(0, barPos);
181                                           mapStr2 = mapStr[j].subString(barPos + 1);
182 kumpf    1.34 
183 yi.zhou  1.36                             _trimWhitespace(mapStr1);
184                                           _trimWhitespace(mapStr2);
185                                               
186                                           if ((mapStr1 == "OID.IETF") &&
187                                               (String::compare(mapStr2, 
188                                                String("SNMP."), 5) == 0))
189                                           {
190                                               isValidAuthority = true;
191                                               oidStr = mapStr2.subString(5);
192                                           }
193                                           else if (mapStr1 == "DataType.IETF")
194                                           {
195                                               isValidDataType = true;
196                                               dataType = mapStr2;
197                                           }
198 kumpf    1.34 
199 yi.zhou  1.36                             if (isValidAuthority && isValidDataType) 
200 kumpf    1.34                             {
201 yi.zhou  1.36                                 propOIDs.append(oidStr);
202                                               propTYPEs.append(dataType);
203                                               propVALUEs.append(prop.getValue().toString());
204               
205                                               break;
206 kumpf    1.34                             }
207                                       }
208                                   }
209 kumpf    1.20                 }
210 kumpf    1.4              }
211 mike     1.2          }
212               
213                       // Collected complete data in arrays and ready to send the trap.
214                       // trap destination and SNMP type are defined in handlerInstance
215                       // and passing this instance as it is to deliverTrap() call
216               
217 kumpf    1.34         Uint32 targetHostPos = handler.findProperty(CIMName("TargetHost"));
218                       Uint32 targetHostFormatPos =
219                           handler.findProperty(CIMName("TargetHostFormat"));
220                       Uint32 otherTargetHostFormatPos =
221                           handler.findProperty(CIMName("OtherTargetHostFormat"));
222                       Uint32 portNumberPos = handler.findProperty(CIMName("PortNumber"));
223                       Uint32 snmpVersionPos = handler.findProperty(CIMName("SNMPVersion"));
224                       Uint32 securityNamePos =
225                           handler.findProperty(CIMName("SNMPSecurityName"));
226                       Uint32 engineIDPos = handler.findProperty(CIMName("SNMPEngineID"));
227 kumpf    1.20 
228 w.otsuka 1.37         if (targetHostPos == PEG_NOT_FOUND)
229                       {
230                           PEG_TRACE((TRC_DISCARDED_DATA, Tracer::LEVEL3,
231                               "Target host is not set for IndicationHandlerSNMPMapper %s"
232                               " Indication.",
233                               (const char*)(indication.getClassName().getString().
234                               getCString())));
235                           PEG_METHOD_EXIT();
236                           throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
237                               "Handler.snmpIndicationHandler.snmpIndicationHandler."
238                               "INVALID_SNMP_INSTANCE",
239                               "Invalid IndicationHandlerSNMPMapper instance"));
240                       }
241                       if (targetHostFormatPos == PEG_NOT_FOUND)
242                       {
243                           PEG_TRACE((TRC_DISCARDED_DATA, Tracer::LEVEL3,
244                               "Target host format is not set for IndicationHandlerSNMPMapper"
245                               " %s Indication.",
246                               (const char*)(indication.getClassName().getString().
247                               getCString())));
248                           PEG_METHOD_EXIT();
249 w.otsuka 1.37             throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
250                               "Handler.snmpIndicationHandler.snmpIndicationHandler."
251                               "INVALID_SNMP_INSTANCE",
252                               "Invalid IndicationHandlerSNMPMapper instance"));
253                       }
254                       if (snmpVersionPos == PEG_NOT_FOUND)
255                       {
256                           PEG_TRACE((TRC_DISCARDED_DATA, Tracer::LEVEL3,
257                               "SNMP Version is not set for IndicationHandlerSNMPMapper %s"
258                               " Indication.",
259                               (const char*)(indication.getClassName().getString().
260                               getCString())));
261                           PEG_METHOD_EXIT();
262                           throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
263                               "Handler.snmpIndicationHandler.snmpIndicationHandler."
264                               "INVALID_SNMP_INSTANCE",
265                               "Invalid IndicationHandlerSNMPMapper instance"));
266                       }
267                       else
268 kumpf    1.16         {
269 kumpf    1.34             // properties from the handler instance
270 kumpf    1.20             String targetHost;
271 kumpf    1.34             String otherTargetHostFormat = String();
272                           String securityName = String();
273                           String engineID = String();
274                           Uint16 targetHostFormat = 0;
275                           Uint16 snmpVersion = 0;
276                           Uint32 portNumber;
277 kumpf    1.20 
278 kumpf    1.34             String trapOid;
279 yi.zhou  1.36             Boolean trapOidAvailable = false;
280                           String exceptionStr;
281 kumpf    1.34             //
282 kumpf    1.20             //  Get snmpTrapOid from context
283                           //
284 kumpf    1.34             if (context.contains(SnmpTrapOidContainer::NAME))
285                           {
286                               SnmpTrapOidContainer trapContainer =
287                                   context.get(SnmpTrapOidContainer::NAME);
288 kumpf    1.16 
289 kumpf    1.20                 trapOid = trapContainer.getSnmpTrapOid();
290 yi.zhou  1.36                 trapOidAvailable = true;
291 kumpf    1.34             }
292                           else
293 kumpf    1.20             {
294 kumpf    1.34                 // get trapOid from indication Class
295 kumpf    1.16 
296 kumpf    1.34                 Uint32 pos =
297                                   indicationClass.findQualifier(CIMName("MappingStrings"));
298                               if (pos != PEG_NOT_FOUND)
299                               {
300 yi.zhou  1.36                     Array<String> classMapStr;
301                                   indicationClass.getQualifier(pos).getValue().
302                                       get(classMapStr);
303 kumpf    1.20 
304 yi.zhou  1.36                     for (Uint32 i=0; i < classMapStr.size(); i++)
305                                   {
306                                       Uint32 barPos = classMapStr[i].find("|");
307 kumpf    1.20 
308 yi.zhou  1.36                         if (barPos != PEG_NOT_FOUND) 
309                                       {
310                                           String authorityName = 
311                                               classMapStr[i].subString(0, barPos);
312                                           String oidStr = classMapStr[i].subString(
313                                               barPos+1, PEG_NOT_FOUND);
314               
315                                           _trimWhitespace(authorityName);
316                                           _trimWhitespace(oidStr);
317               
318                                           if ((authorityName == "OID.IETF") &&
319                                               (String::compare(oidStr, 
320                                                String("SNMP."), 5) == 0))
321                                           {
322                                               trapOid = oidStr.subString(5); 
323                                               trapOidAvailable = true;
324                                               break;
325                                           }
326                                       }
327 kumpf    1.20                     }
328 yi.zhou  1.36 
329                                   if (!trapOidAvailable)
330 kumpf    1.34                     {
331 yi.zhou  1.36                         exceptionStr = "No MappingStrings for snmp trap"
332                                           "is specified for class: ";
333                                     
334                                       exceptionStr.append(
335                                           indication.getClassName().getString());
336               
337 kumpf    1.34                         PEG_TRACE_STRING(TRC_IND_HANDLER, Tracer::LEVEL4,
338 yi.zhou  1.36                             exceptionStr);
339 kumpf    1.34                         PEG_METHOD_EXIT();
340 yi.zhou  1.36 
341 kumpf    1.34                         throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
342                                           MessageLoaderParms(
343                                               "Handler.snmpIndicationHandler."
344 yi.zhou  1.36                                 "snmpIndicationHandler.NO_MS_FOR_SNMP_TRAP",
345                                               exceptionStr));
346 kumpf    1.34                     }
347                               }
348                               else
349                               {
350 marek    1.35                     PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL4,
351 kumpf    1.34                         "Qualifier MappingStrings can not be found.");
352                                   PEG_METHOD_EXIT();
353                                   MessageLoaderParms parms(
354                                       "Handler.snmpIndicationHandler.snmpIndicationHandler."
355                                           "QUALIFIER_MAPPINGS_NOT_FOUND",
356                                       "Qualifier MappingStrings can not be found");
357                                   throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, parms);
358                               }
359                           }
360               
361                           handler.getProperty(targetHostPos).getValue().get(targetHost);
362                           handler.getProperty(targetHostFormatPos).getValue().get(
363                               targetHostFormat);
364                           if (otherTargetHostFormatPos != PEG_NOT_FOUND)
365                           {
366                               handler.getProperty(otherTargetHostFormatPos).getValue().get(
367                                   otherTargetHostFormat);
368                           }
369                           if (portNumberPos != PEG_NOT_FOUND)
370                           {
371                               handler.getProperty(portNumberPos).getValue().get(portNumber);
372 kumpf    1.34             }
373                           else
374                           {
375                               // default port
376                               portNumber = SNMP_TRAP_DEFAULT_PORT;
377                           }
378               
379                           handler.getProperty(snmpVersionPos).getValue().get(snmpVersion);
380                           if (securityNamePos != PEG_NOT_FOUND)
381                           {
382                               handler.getProperty(securityNamePos).getValue().get(
383                                   securityName);
384                           }
385                           if (engineIDPos != PEG_NOT_FOUND)
386                           {
387                               handler.getProperty(engineIDPos).getValue().get(engineID);
388                           }
389 kumpf    1.13 
390 w.otsuka 1.37             PEG_TRACE ((TRC_INDICATION_GENERATION, Tracer::LEVEL3,
391                              "snmpIndicationHandler sending %s Indication trap %s to target"
392                              " host %s target port %d",
393                              (const char*)(indication.getClassName().getString().
394                              getCString()),
395                              (const char*)(trapOid.getCString()),
396                              (const char*)(targetHost.getCString()),portNumber));
397                          _snmpTrapSender->deliverTrap(
398 kumpf    1.20                 trapOid,
399                               securityName,
400                               targetHost,
401                               targetHostFormat,
402 kumpf    1.34                 otherTargetHostFormat,
403                               portNumber,
404                               snmpVersion,
405                               engineID,
406                               propOIDs,
407                               propTYPEs,
408 kumpf    1.20                 propVALUEs);
409 w.otsuka 1.37 
410                       PEG_TRACE ((TRC_INDICATION_GENERATION, Tracer::LEVEL3,
411                          "%s Indication trap %s sent to target host %s target port %d "
412                          "successfully",
413                          (const char*)(indication.getClassName().getString().getCString()),
414                          (const char*)(trapOid.getCString()),
415                          (const char*)(targetHost.getCString()),portNumber));
416 kumpf    1.20         }
417 kumpf    1.34     }
418                   catch (CIMException& c)
419 kumpf    1.20     {
420 kumpf    1.34         PEG_TRACE_STRING(TRC_IND_HANDLER, Tracer::LEVEL4, c.getMessage());
421                       PEG_METHOD_EXIT();
422                       throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, c.getMessage());
423 kumpf    1.20     }
424                   catch (Exception& e)
425                   {
426 kumpf    1.34         PEG_TRACE_STRING(TRC_IND_HANDLER, Tracer::LEVEL4, e.getMessage());
427                       PEG_METHOD_EXIT();
428 kumpf    1.20 
429 kumpf    1.34         throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, e.getMessage());
430 kumpf    1.20     }
431                   catch (...)
432                   {
433 marek    1.35         PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL4,
434 kumpf    1.34             "Failed to deliver trap.");
435                       PEG_METHOD_EXIT();
436               
437                       throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
438                           "Handler.snmpIndicationHandler.snmpIndicationHandler."
439                               "FAILED_TO_DELIVER_TRAP",
440                           "Failed to deliver trap."));
441 kumpf    1.13     }
442 yi.zhou  1.30     PEG_METHOD_EXIT();
443 mike     1.2  }
444               
445 yi.zhou  1.36 void snmpIndicationHandler::_trimWhitespace(
446                   String & nameStr)
447               {
448                   PEG_METHOD_ENTER(TRC_IND_HANDLER,
449                       "snmpIndicationHandler::_trimWhitespace");
450               
451                   Uint32 ps = 0;
452                   // skip begining whitespace
453                   for (ps = 0; ps < nameStr.size(); ps++)
454                   {
455                       if (nameStr[ps] != ' ')
456                       {
457                           break;
458                       }
459                   }
460               
461                   if (ps != 0)
462                   {
463                       nameStr.remove(0, ps);
464                   }
465               
466 yi.zhou  1.36     // skip the appended whitespace
467                   for (ps = nameStr.size(); ps != 0; ps--)
468                   {
469                       if (nameStr[ps-1] != ' ')
470                       {
471                           break;
472                       }
473                   }
474               
475                   if (ps !=  nameStr.size())
476                   {
477                       nameStr.remove(ps);
478                   }
479               
480                   PEG_METHOD_EXIT();
481               }
482               
483 kumpf    1.32 PEGASUS_NAMESPACE_END
484               
485               PEGASUS_USING_PEGASUS;
486               
487 kumpf    1.33 // This is the entry point into this dynamic module.
488 mike     1.2  
489 kumpf    1.33 extern "C" PEGASUS_EXPORT CIMHandler* PegasusCreateHandler(
490                   const String& handlerName)
491 kumpf    1.32 {
492 kumpf    1.33     if (handlerName == "snmpIndicationHandler")
493                   {
494                       return new snmpIndicationHandler;
495                   }
496               
497                   return 0;
498 mike     1.2  }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2