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