1 yi.zhou 1.1 //%2006////////////////////////////////////////////////////////////////////////
2 //
3 // 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 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // 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 // 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 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 yi.zhou 1.1 // 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 // 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 // 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/PegasusAssert.h>
35 #include <Pegasus/Common/Thread.h>
36 #include <Pegasus/Common/Constants.h>
37 #include <Pegasus/Common/FileSystem.h>
38 #include <Pegasus/Common/Stopwatch.h>
39 #include <Pegasus/Client/CIMClient.h>
|
40 dave.sudlik 1.6 #include <Pegasus/Common/HostAddress.h>
|
41 yi.zhou 1.1
42 PEGASUS_USING_PEGASUS;
43 PEGASUS_USING_STD;
44
|
45 karl 1.4 // Interop namespace used with PEGASUS_NAMESPACENAME_INTEROP in Constants.h
|
46 kumpf 1.7 const CIMNamespaceName SOURCE_NAMESPACE =
|
47 kumpf 1.9 CIMNamespaceName ("test/TestProvider");
|
48 yi.zhou 1.1
|
49 kumpf 1.9 const String INDICATION_CLASS_NAME = "Test_IndicationProviderClass";
|
50 yi.zhou 1.1
|
51 kumpf 1.9 const String SNMPV1_HANDLER_NAME = "SNMPHandler01";
52 const String SNMPV2C_HANDLER_NAME = "SNMPHandler02";
53 const String SNMPV2C_IPV6_HANDLER_NAME = "SNMPHandler03";
|
54 denise.eckstein 1.16 const String FILTER_NAME = "SNMPIPFilter01";
|
55 yi.zhou 1.1
56 enum SNMPVersion {_SNMPV1_TRAP = 2, _SNMPV2C_TRAP = 3};
|
57 dave.sudlik 1.6 enum TargetHostFormat {_HOST_NAME = 2, _IPV4_ADDRESS = 3, _IPV6_ADDRESS = 4};
|
58 yi.zhou 1.1
59 #define PORT_NUMBER 2006
60
61 AtomicInt errorsEncountered(0);
62
63 ////////////////////////////////////////////////////////////////////////////////
64 //
65 // Thread Parameters Class
66 //
67 ////////////////////////////////////////////////////////////////////////////////
68
|
69 kumpf 1.7 class T_Parms
70 {
71 public:
|
72 kumpf 1.15 CIMClient* client;
|
73 yi.zhou 1.1 Uint32 indicationSendCount;
74 Uint32 uniqueID;
75 };
76
77 ///////////////////////////////////////////////////////////////////////////
78
|
79 kumpf 1.7 CIMObjectPath _getFilterObjectPath(const String & name)
|
80 yi.zhou 1.1 {
81 Array<CIMKeyBinding> keyBindings;
82 keyBindings.append (CIMKeyBinding ("SystemCreationClassName",
83 System::getSystemCreationClassName (), CIMKeyBinding::STRING));
84 keyBindings.append (CIMKeyBinding ("SystemName",
85 System::getFullyQualifiedHostName (), CIMKeyBinding::STRING));
86 keyBindings.append (CIMKeyBinding ("CreationClassName",
87 PEGASUS_CLASSNAME_INDFILTER.getString(), CIMKeyBinding::STRING));
88 keyBindings.append (CIMKeyBinding ("Name", name,
89 CIMKeyBinding::STRING));
90 return(CIMObjectPath("", CIMNamespaceName (),
91 PEGASUS_CLASSNAME_INDFILTER, keyBindings));
92 }
93
|
94 kumpf 1.7 CIMObjectPath _getHandlerObjectPath(const String & name)
|
95 yi.zhou 1.1 {
96 Array<CIMKeyBinding> keyBindings;
97 keyBindings.append (CIMKeyBinding ("SystemCreationClassName",
98 System::getSystemCreationClassName (), CIMKeyBinding::STRING));
99 keyBindings.append (CIMKeyBinding ("SystemName",
100 System::getFullyQualifiedHostName (), CIMKeyBinding::STRING));
101 keyBindings.append (CIMKeyBinding ("CreationClassName",
102 PEGASUS_CLASSNAME_INDHANDLER_SNMP.getString(),
103 CIMKeyBinding::STRING));
104 keyBindings.append (CIMKeyBinding ("Name", name,
105 CIMKeyBinding::STRING));
106 return(CIMObjectPath("", CIMNamespaceName (),
107 PEGASUS_CLASSNAME_INDHANDLER_SNMP, keyBindings));
108 }
109
|
110 kumpf 1.7 CIMObjectPath _getSubscriptionObjectPath(
111 const String & filterName,
112 const String & handlerName)
|
113 yi.zhou 1.1 {
114 CIMObjectPath filterObjectPath = _getFilterObjectPath(filterName);
115
116 CIMObjectPath handlerObjectPath = _getHandlerObjectPath(handlerName);
117
118 Array<CIMKeyBinding> subscriptionKeyBindings;
119 subscriptionKeyBindings.append (CIMKeyBinding ("Filter",
120 CIMValue(filterObjectPath)));
121 subscriptionKeyBindings.append (CIMKeyBinding ("Handler",
122 CIMValue(handlerObjectPath)));
123 return(CIMObjectPath("", CIMNamespaceName (),
124 PEGASUS_CLASSNAME_INDSUBSCRIPTION, subscriptionKeyBindings));
125 }
126
|
127 kumpf 1.7 CIMObjectPath _createHandlerInstance(
128 CIMClient & client,
129 const String & name,
130 const String & targetHost,
131 const String & securityName,
132 const Uint16 targetHostFormat,
133 const Uint16 snmpVersion)
|
134 yi.zhou 1.1 {
135 CIMInstance handlerInstance (PEGASUS_CLASSNAME_INDHANDLER_SNMP);
136 handlerInstance.addProperty (CIMProperty (CIMName
137 ("SystemCreationClassName"), System::getSystemCreationClassName ()));
138 handlerInstance.addProperty (CIMProperty (CIMName ("SystemName"),
139 System::getFullyQualifiedHostName ()));
140 handlerInstance.addProperty (CIMProperty (CIMName ("CreationClassName"),
141 PEGASUS_CLASSNAME_INDHANDLER_SNMP.getString ()));
142 handlerInstance.addProperty (CIMProperty (CIMName ("Name"), name));
143 handlerInstance.addProperty (CIMProperty (CIMName ("TargetHost"),
144 targetHost));
145 handlerInstance.addProperty (CIMProperty (CIMName ("TargetHostFormat"),
146 CIMValue ((Uint16) targetHostFormat)));
147 handlerInstance.addProperty (CIMProperty (CIMName ("SNMPSecurityName"),
148 securityName));
149 handlerInstance.addProperty (CIMProperty (CIMName ("SnmpVersion"),
150 CIMValue ((Uint16) snmpVersion)));
151 handlerInstance.addProperty (CIMProperty (CIMName ("PortNumber"),
152 CIMValue ((Uint32) PORT_NUMBER)));
153
|
154 kumpf 1.7 return client.createInstance(
155 PEGASUS_NAMESPACENAME_INTEROP, handlerInstance);
|
156 yi.zhou 1.1 }
157
|
158 kumpf 1.7 CIMObjectPath _createFilterInstance(
159 CIMClient & client,
160 const String & name,
161 const String & query,
162 const String & qlang)
|
163 yi.zhou 1.1 {
164 CIMInstance filterInstance (PEGASUS_CLASSNAME_INDFILTER);
165 filterInstance.addProperty (CIMProperty (CIMName
166 ("SystemCreationClassName"), System::getSystemCreationClassName ()));
167 filterInstance.addProperty (CIMProperty (CIMName ("SystemName"),
168 System::getFullyQualifiedHostName ()));
169 filterInstance.addProperty (CIMProperty (CIMName ("CreationClassName"),
170 PEGASUS_CLASSNAME_INDFILTER.getString ()));
171 filterInstance.addProperty (CIMProperty (CIMName ("Name"), name));
172 filterInstance.addProperty (CIMProperty (CIMName ("Query"), query));
173 filterInstance.addProperty (CIMProperty (CIMName ("QueryLanguage"),
174 String (qlang)));
175 filterInstance.addProperty (CIMProperty (CIMName ("SourceNamespace"),
176 SOURCE_NAMESPACE.getString ()));
177
|
178 kumpf 1.7 return client.createInstance(PEGASUS_NAMESPACENAME_INTEROP, filterInstance);
|
179 yi.zhou 1.1 }
180
|
181 kumpf 1.7 CIMObjectPath _createSubscriptionInstance(
182 CIMClient & client,
183 const CIMObjectPath & filterPath,
184 const CIMObjectPath & handlerPath)
|
185 yi.zhou 1.1 {
186 CIMInstance subscriptionInstance (PEGASUS_CLASSNAME_INDSUBSCRIPTION);
187 subscriptionInstance.addProperty (CIMProperty (CIMName ("Filter"),
188 filterPath, 0, PEGASUS_CLASSNAME_INDFILTER));
189 subscriptionInstance.addProperty (CIMProperty (CIMName ("Handler"),
190 handlerPath, 0, PEGASUS_CLASSNAME_INDHANDLER_SNMP));
191 subscriptionInstance.addProperty (CIMProperty
192 (CIMName ("SubscriptionState"), CIMValue ((Uint16) 2)));
193
|
194 kumpf 1.7 return client.createInstance(
195 PEGASUS_NAMESPACENAME_INTEROP, subscriptionInstance);
|
196 yi.zhou 1.1 }
197
|
198 kumpf 1.7 void _sendTestIndication(
199 CIMClient* client,
200 const CIMName & methodName,
201 Uint32 indicationSendCount)
|
202 yi.zhou 1.1 {
203 //
204 // Invoke method to send test indication
205 //
206 Array <CIMParamValue> inParams;
207 Array <CIMParamValue> outParams;
208 Array <CIMKeyBinding> keyBindings;
209 Sint32 result;
210
211 CIMObjectPath className (String::EMPTY, CIMNamespaceName (),
|
212 kumpf 1.9 CIMName ("Test_IndicationProviderClass"), keyBindings);
|
213 yi.zhou 1.1
|
214 kumpf 1.7 inParams.append(CIMParamValue(String("indicationSendCount"),
|
215 yi.zhou 1.1 CIMValue(indicationSendCount)));
216
217 CIMValue retValue = client->invokeMethod
218 (SOURCE_NAMESPACE,
219 className,
220 methodName,
221 inParams,
222 outParams);
223
224 retValue.get (result);
225 PEGASUS_TEST_ASSERT (result == 0);
226 }
227
|
228 kumpf 1.7 void _deleteSubscriptionInstance(
229 CIMClient & client,
230 const String & filterName,
231 const String & handlerName)
|
232 yi.zhou 1.1 {
233 CIMObjectPath subscriptionObjectPath =
234 _getSubscriptionObjectPath(filterName, handlerName);
|
235 kumpf 1.7 client.deleteInstance(
236 PEGASUS_NAMESPACENAME_INTEROP, subscriptionObjectPath);
|
237 yi.zhou 1.1 }
238
|
239 kumpf 1.7 void _deleteHandlerInstance(
240 CIMClient & client,
241 const String & name)
|
242 yi.zhou 1.1 {
243 CIMObjectPath handlerObjectPath = _getHandlerObjectPath(name);
|
244 karl 1.4 client.deleteInstance (PEGASUS_NAMESPACENAME_INTEROP, handlerObjectPath);
|
245 yi.zhou 1.1 }
246
|
247 kumpf 1.7 void _deleteFilterInstance(
248 CIMClient & client,
249 const String & name)
|
250 yi.zhou 1.1 {
251 CIMObjectPath filterObjectPath = _getFilterObjectPath(name);
|
252 karl 1.4 client.deleteInstance (PEGASUS_NAMESPACENAME_INTEROP, filterObjectPath);
|
253 yi.zhou 1.1 }
254
|
255 kumpf 1.7 void _usage()
|
256 yi.zhou 1.1 {
257 cerr << endl
258 << "Usage:" << endl
259 << " TestSnmpHandler setup [ WQL | DMTF:CQL ]\n"
260 << " TestSnmpHandler run <indicationSendCount> "
261 << "[<threads>]\n"
262 << " where: " << endl
263 << " <indicationSendCount> is the number of indications to\n"
264 << " generate and has to be greater than zero." << endl
265 << " <threads> is an optional number of client threads to\n"
266 << " create, default is one." << endl
267 << " TestSnmpHandler cleanup\n"
268 << " TestSnmpHandler removelog"
269 << endl << endl;
270 }
271
272 void _setup (CIMClient & client, const String& qlang)
273 {
274 CIMObjectPath filterObjectPath;
275 CIMObjectPath snmpv1HandlerObjectPath;
276 CIMObjectPath snmpv2HandlerObjectPath;
|
277 yi.zhou 1.8 CIMObjectPath snmpv2IPV6HandlerObjectPath;
|
278 yi.zhou 1.1
279 try
280 {
281 filterObjectPath = _createFilterInstance (client, FILTER_NAME,
|
282 kumpf 1.9 String ("SELECT * FROM Test_IndicationProviderClass"),
|
283 yi.zhou 1.1 qlang);
284 }
285 catch (CIMException& e)
286 {
287 if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
288 {
289 filterObjectPath = _getFilterObjectPath(FILTER_NAME);
290 cerr << "----- Warning: Filter Instance Not Created: "
291 << e.getMessage () << endl;
292 }
293 else
294 {
295 cerr << "----- Error: Filter Instance Not Created: " << endl;
296 throw;
297 }
298 }
299
300 try
301 {
|
302 kumpf 1.7 // Create SNMPv1 trap handler
|
303 yi.zhou 1.1 snmpv1HandlerObjectPath = _createHandlerInstance (client,
304 SNMPV1_HANDLER_NAME,
305 System::getFullyQualifiedHostName(),
306 "",
307 _HOST_NAME,
308 _SNMPV1_TRAP);
309 }
310 catch (CIMException& e)
311 {
312 if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
313 {
314 snmpv1HandlerObjectPath = _getHandlerObjectPath(
315 SNMPV1_HANDLER_NAME);
316 cerr << "----- Warning: SNMPv1 Trap Handler Instance Not Created: "
317 << e.getMessage () << endl;
318 }
319 else
320 {
321 cerr << "----- Error: SNMPv1 Trap Handler Instance Not Created: "
322 << endl;
323 throw;
324 yi.zhou 1.1 }
325 }
326
327 try
328 {
329 _createSubscriptionInstance (client, filterObjectPath,
330 snmpv1HandlerObjectPath);
331 }
332 catch (CIMException& e)
333 {
334 if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
335 {
336 cerr << "----- Warning: Client Subscription Instance: "
337 << e.getMessage () << endl;
338 }
339 else
340 {
341 cerr << "----- Error: Client Subscription Instance: " << endl;
342 throw;
343 }
344 }
345 yi.zhou 1.1
346 try
347 {
|
348 dave.sudlik 1.6 String ipAddress;
349 int af;
350 System::getHostIP(System::getFullyQualifiedHostName (), &af, ipAddress);
|
351 kumpf 1.7 // Create SNMPv2 trap handler
|
352 yi.zhou 1.1 snmpv2HandlerObjectPath = _createHandlerInstance (client,
353 SNMPV2C_HANDLER_NAME,
|
354 dave.sudlik 1.6 ipAddress,
|
355 yi.zhou 1.1 "public",
|
356 dave.sudlik 1.6 af == AF_INET ? _IPV4_ADDRESS : _IPV6_ADDRESS,
|
357 yi.zhou 1.1 _SNMPV2C_TRAP);
358 }
359 catch (CIMException& e)
360 {
361 if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
362 {
363 snmpv2HandlerObjectPath = _getHandlerObjectPath(
364 SNMPV2C_HANDLER_NAME);
365 cerr << "----- Warning: SNMPv2c Trap Handler Instance Not Created: "
366 << e.getMessage () << endl;
367 }
368 else
369 {
370 cerr << "----- Error: SNMPv2c Trap Handler Instance Not Created: "
371 << endl;
372 throw;
373 }
374 }
375
376 try
377 {
378 yi.zhou 1.1 _createSubscriptionInstance (client, filterObjectPath,
379 snmpv2HandlerObjectPath);
380 }
381 catch (CIMException& e)
382 {
383 if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
384 {
385 cerr << "----- Warning: Client Subscription Instance: "
386 << e.getMessage () << endl;
387 }
388 else
389 {
390 cerr << "----- Error: Client Subscription Instance: " << endl;
391 throw;
392 }
393 }
|
394 yi.zhou 1.8
395 #if defined(PEGASUS_ENABLE_IPV6)
396 // create a subscription with trap destination of IPV6 address format
397 try
398 {
399 // Create SNMPv2 IPV6 trap handler
400 snmpv2IPV6HandlerObjectPath = _createHandlerInstance (client,
401 SNMPV2C_IPV6_HANDLER_NAME,
402 String("::1"),
403 "public",
404 _IPV6_ADDRESS,
405 _SNMPV2C_TRAP);
406 }
407 catch (CIMException& e)
408 {
409 if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
410 {
411 snmpv2IPV6HandlerObjectPath = _getHandlerObjectPath(
412 SNMPV2C_IPV6_HANDLER_NAME);
413 cerr << "----- Warning: SNMPv2c IPV6 Trap Handler Instance "
414 "Not Created: " << e.getMessage () << endl;
415 yi.zhou 1.8 }
416 else
417 {
418 cerr << "----- Error: SNMPv2c IPV6 Trap Handler Instance Not "
419 "Created: " << endl;
420 throw;
421 }
422 }
423
424 try
425 {
426 _createSubscriptionInstance (client, filterObjectPath,
427 snmpv2IPV6HandlerObjectPath);
428 }
429 catch (CIMException& e)
430 {
431 if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
432 {
433 cerr << "----- Warning: Client Subscription Instance: "
434 << e.getMessage () << endl;
435 }
436 yi.zhou 1.8 else
437 {
438 cerr << "----- Error: Client Subscription Instance: " << endl;
439 throw;
440 }
441 }
442 #endif
|
443 yi.zhou 1.1 }
444
445 void _cleanup (CIMClient & client)
446 {
447 try
448 {
449 _deleteSubscriptionInstance (client, FILTER_NAME,
450 SNMPV1_HANDLER_NAME);
451 }
452 catch (CIMException& e)
453 {
454 if (e.getCode() != CIM_ERR_NOT_FOUND)
455 {
456 cerr << "----- Error: deleteSubscriptionInstance failure: "
457 << endl;
458 throw;
459 }
460 }
461 try
462 {
463 _deleteSubscriptionInstance (client, FILTER_NAME,
464 yi.zhou 1.1 SNMPV2C_HANDLER_NAME);
465 }
466 catch (CIMException& e)
467 {
468 if (e.getCode() != CIM_ERR_NOT_FOUND)
469 {
470 cerr << "----- Error: deleteSubscriptionInstance failure: "
471 << endl;
472 throw;
473 }
474 }
|
475 yi.zhou 1.8
476 #if defined(PEGASUS_ENABLE_IPV6)
477 try
478 {
479 _deleteSubscriptionInstance (client, FILTER_NAME,
480 SNMPV2C_IPV6_HANDLER_NAME);
481 }
482 catch (CIMException& e)
483 {
484 if (e.getCode() != CIM_ERR_NOT_FOUND)
485 {
486 cerr << "----- Error: deleteSubscriptionInstance failure: "
487 << endl;
488 throw;
489 }
490 }
491 #endif
492
|
493 yi.zhou 1.1 try
494 {
495 _deleteFilterInstance (client, FILTER_NAME);
496 }
497 catch (CIMException& e)
498 {
499 if (e.getCode() != CIM_ERR_NOT_FOUND)
500 {
501 cerr << "----- Error: deleteFilterInstance failure: " << endl;
502 throw;
503 }
504 }
505
506 try
507 {
508 _deleteHandlerInstance (client, SNMPV1_HANDLER_NAME);
509 }
510 catch (CIMException& e)
511 {
512 if (e.getCode() != CIM_ERR_NOT_FOUND)
513 {
514 yi.zhou 1.1 cerr << "----- Error: deleteHandlerInstance failure: " << endl;
515 throw;
516 }
517 }
518 try
519 {
520 _deleteHandlerInstance (client, SNMPV2C_HANDLER_NAME);
521 }
522 catch (CIMException& e)
523 {
524 if (e.getCode() != CIM_ERR_NOT_FOUND)
525 {
526 cerr << "----- Error: deleteHandlerInstance failure: " << endl;
527 throw;
528 }
529 }
|
530 yi.zhou 1.8 #if defined(PEGASUS_ENABLE_IPV6)
531 try
532 {
533 _deleteHandlerInstance (client, SNMPV2C_IPV6_HANDLER_NAME);
534 }
535 catch (CIMException& e)
536 {
537 if (e.getCode() != CIM_ERR_NOT_FOUND)
538 {
539 cerr << "----- Error: deleteHandlerInstance failure: " << endl;
540 throw;
541 }
542 }
543 #endif
|
544 yi.zhou 1.1 }
545
546 static void _testEnd(const String& uniqueID, const double elapsedTime)
547 {
548 cout << "+++++ thread" << uniqueID << ": passed in " << elapsedTime
549 << " seconds" << endl;
550 }
551
|
552 mike 1.3 ThreadReturnType PEGASUS_THREAD_CDECL _executeTests(void *parm)
|
553 yi.zhou 1.1 {
554 Thread *my_thread = (Thread *)parm;
|
555 kumpf 1.15 AutoPtr<T_Parms> parms((T_Parms *)my_thread->get_parm());
556 CIMClient* client = parms->client;
|
557 yi.zhou 1.1 Uint32 indicationSendCount = parms->indicationSendCount;
558 Uint32 id = parms->uniqueID;
559 char id_[4];
560 memset(id_,0x00,sizeof(id_));
|
561 kumpf 1.14 sprintf(id_,"%u",id);
|
562 yi.zhou 1.1 String uniqueID = "_";
563 uniqueID.append(id_);
564
565 try
566 {
567 Stopwatch elapsedTime;
568
569 elapsedTime.start();
570 try
571 {
572 _sendTestIndication (client, CIMName ("SendTestIndicationTrap"),
573 indicationSendCount);
574 }
575 catch (Exception & e)
576 {
|
577 kumpf 1.7 cerr << "----- sendTestIndication failed: " << e.getMessage () <<
578 endl;
|
579 yi.zhou 1.1 exit (-1);
580 }
581 elapsedTime.stop();
582 _testEnd(uniqueID, elapsedTime.getElapsed());
583 }
584 catch(Exception & e)
585 {
586 cout << e.getMessage() << endl;
587 }
|
588 kumpf 1.12
589 return ThreadReturnType(0);
|
590 yi.zhou 1.1 }
591
592 Thread * _runTestThreads(
593 CIMClient* client,
594 Uint32 indicationSendCount,
595 Uint32 uniqueID)
596 {
597 // package parameters, create thread and run...
598 AutoPtr<T_Parms> parms(new T_Parms());
|
599 kumpf 1.15 parms->client = client;
|
600 yi.zhou 1.1 parms->indicationSendCount = indicationSendCount;
601 parms->uniqueID = uniqueID;
602 AutoPtr<Thread> t(new Thread(_executeTests, (void*)parms.release(), false));
603 t->run();
604 return t.release();
605 }
606
|
607 yi.zhou 1.13 Uint32 _getReceivedTrapCount(Uint16 snmpVersion, const String& logFile)
|
608 yi.zhou 1.1 {
609 String trap1 = "Trap Info: TRAP, SNMP v1, community public";
610 String trap2 = "Trap Info: TRAP2, SNMP v2c, community public";
611
612 Uint32 receivedTrap1Count = 0;
613 Uint32 receivedTrap2Count = 0;
|
614 kumpf 1.7
|
615 yi.zhou 1.13 ifstream ifs(logFile.getCString());
|
616 yi.zhou 1.1 if (!ifs)
617 {
618 return (0);
619 }
620
621 String line;
622 while (GetLine(ifs, line))
623 {
624 if (String::compare(line, trap1) == 0)
625 {
626 receivedTrap1Count++;
627 }
628 if (String::compare(line, trap2) == 0)
629 {
630 receivedTrap2Count++;
631 }
632 }
633
634 ifs.close();
635
636 switch (snmpVersion)
637 yi.zhou 1.1 {
638 case _SNMPV1_TRAP:
639 {
640 return (receivedTrap1Count);
641 }
642 case _SNMPV2C_TRAP:
643 {
644 return (receivedTrap2Count);
645 }
646 default:
647 {
648 return (0);
649 }
650 }
651
652 }
653
654 #ifdef PEGASUS_USE_NET_SNMP
|
655 kumpf 1.7 // Stop snmptrapd process if it is running and remove
|
656 yi.zhou 1.1 // procIdFile file if it exists
|
657 kumpf 1.7 //
|
658 yi.zhou 1.13 void _stopSnmptrapd(const String& processIdFile)
|
659 yi.zhou 1.1 {
660 Uint32 receiverPid;
661 FILE *fd;
|
662 yi.zhou 1.13 if ((fd = fopen(processIdFile.getCString(), "r")) != NULL)
|
663 yi.zhou 1.1 {
|
664 kumpf 1.10 if (fscanf(fd, "%d\n", &receiverPid) != 1)
665 {
666 throw Exception("Failed to read trapd pid from procIdFile.");
667 }
|
668 kumpf 1.7
|
669 yi.zhou 1.1 kill(receiverPid, SIGTERM);
670
671 fclose(fd);
672 }
673
|
674 yi.zhou 1.13 if (FileSystem::exists(processIdFile))
|
675 yi.zhou 1.1 {
|
676 yi.zhou 1.13 FileSystem::removeFile(processIdFile);
|
677 yi.zhou 1.1 }
678 }
679
680 static Boolean _startSnmptrapd(
|
681 yi.zhou 1.13 FILE **trapInfo,
682 const String& processIdFile,
683 const String& logFile)
|
684 yi.zhou 1.1 {
685 String snmptrapdCmd;
686
687 Uint32 portNumber = PORT_NUMBER;
688 char portNumberStr[32];
689 sprintf(portNumberStr, "%lu", (unsigned long) portNumber);
690
691 //
692 // build snmptrapd cmd options
693 //
694
|
695 kumpf 1.7 // Specify logging incoming traps to trapLogFile
|
696 yi.zhou 1.1 // Save the process ID of the snmptrapd in procIdFile
697 snmptrapdCmd.append(
|
698 yi.zhou 1.13 "/usr/sbin/snmptrapd -f -Lf ");
699 snmptrapdCmd.append(logFile);
700 snmptrapdCmd.append(" -p ");
701 snmptrapdCmd.append(processIdFile);
|
702 yi.zhou 1.1
703 // Specify incoming trap format
704 snmptrapdCmd.append( " -F \"\nTrap Info: %P\nVariable: %v\n\"");
705
706 // Specify listening address
|
707 yi.zhou 1.8 #if defined(PEGASUS_ENABLE_IPV6)
708 snmptrapdCmd.append(" UDP6:");
709 snmptrapdCmd.append(portNumberStr);
710 snmptrapdCmd.append(",UDP:");
711 #else
|
712 yi.zhou 1.1 snmptrapdCmd.append(" UDP:");
|
713 yi.zhou 1.8 #endif
|
714 yi.zhou 1.1 snmptrapdCmd.append(System::getFullyQualifiedHostName ());
715
716 snmptrapdCmd.append(":");
717 snmptrapdCmd.append(portNumberStr);
718
719 if ((*trapInfo = popen(snmptrapdCmd.getCString(), "r")) == NULL)
720 {
721 throw Exception ("snmptrapd can not be started");
722 }
723
724 #define MAX_ITERATIONS 300
725 #define SLEEP_SEC 1
726
727 Uint32 iterations = 0;
728
|
729 kumpf 1.7 // Wait until snmptrapd started
|
730 yi.zhou 1.1 while (iterations < MAX_ITERATIONS)
731 {
732 iterations++;
|
733 yi.zhou 1.13 if (FileSystem::exists(processIdFile))
|
734 yi.zhou 1.1 {
735 return (true);
736 }
737 else
738 {
739 System::sleep(SLEEP_SEC);
|
740 kumpf 1.7
|
741 yi.zhou 1.1 }
742 }
743
744 throw Exception ("snmptrapd can not be started");
745 }
746 #endif
747
|
748 yi.zhou 1.13 void _removeTrapLogFile(const String& logFile)
|
749 yi.zhou 1.1 {
750 // if trapLogFile exists, remove it
751 if (FileSystem::exists(logFile))
752 {
753 FileSystem::removeFile(logFile);
754 }
755 }
756
|
757 kumpf 1.7 void _receiveExpectedTraps(
758 CIMClient& workClient,
|
759 yi.zhou 1.1 Uint32 indicationSendCount,
|
760 yi.zhou 1.13 Uint32 runClientThreadCount,
761 const String& logFile)
|
762 yi.zhou 1.1 {
|
763 yi.zhou 1.8 Uint32 indicationTrapV1SendCount = 0;
764 Uint32 indicationTrapV2SendCount = 0;
765
|
766 yi.zhou 1.1 CIMClient * clientConnections = new CIMClient[runClientThreadCount];
767
768 // determine total number of indication send count
|
769 yi.zhou 1.8 indicationTrapV1SendCount =
770 indicationSendCount * runClientThreadCount;
771
772 // if IPV6 is enabled, an additional SNMPv2c trap is sent to IPV6 address
773 #if defined(PEGASUS_ENABLE_IPV6)
774 indicationTrapV2SendCount = 2 * indicationTrapV1SendCount;
775 #else
776 indicationTrapV2SendCount = indicationTrapV1SendCount;
777 #endif
|
778 yi.zhou 1.1
779 // calculate the timeout based on the total send count allowing
|
780 kumpf 1.7 // using the MSG_PER_SEC rate
781 // allow 20 seconds of test overhead for very small tests
|
782 yi.zhou 1.1
783 #define MSG_PER_SEC 4
784
|
785 marek 1.5 Uint32 testTimeout = PEGASUS_DEFAULT_CLIENT_TIMEOUT_MILLISECONDS
|
786 yi.zhou 1.8 + (indicationTrapV2SendCount/MSG_PER_SEC)*1000;
|
787 yi.zhou 1.1
788 // connect the clients
789 for(Uint32 i = 0; i < runClientThreadCount; i++)
790 {
791 clientConnections[i].setTimeout(testTimeout);
792 clientConnections[i].connectLocal();
793 }
794
795 // run tests
796 Thread ** clientThreads = new Thread *[runClientThreadCount];
797
798 Stopwatch trapReceiverElapsedTime;
799
800 trapReceiverElapsedTime.start();
801
802 for(Uint32 i = 0; i < runClientThreadCount; i++)
803 {
804 clientThreads[i] = _runTestThreads(&clientConnections[i],
805 indicationSendCount, i);
806 }
807
808 yi.zhou 1.1 for(Uint32 i=0; i< runClientThreadCount; i++)
809 {
810 clientThreads[i]->join();
|
811 kumpf 1.15 delete clientThreads[i];
|
812 yi.zhou 1.1 }
813
814 delete[] clientConnections;
815 delete[] clientThreads;
816
817 //
818 // Allow time for the trap to be received
819 // Wait in SLEEP_SEC second intervals.
820 // Put msg out every MSG_SEC intervals
821 //
822
823 #define SLEEP_SEC 1
824 #define COUT_TIME_INTERVAL 30
825 #define MAX_NO_CHANGE_ITERATIONS COUT_TIME_INTERVAL*3
826
827 Uint32 noChangeIterations = 0;
828 Uint32 priorReceivedTrap1Count = 0;
829 Uint32 priorReceivedTrap2Count = 0;
830 Uint32 currentReceivedTrap1Count = 0;
831 Uint32 currentReceivedTrap2Count = 0;
832 Uint32 totalIterations = 0;
833 yi.zhou 1.1
834 //
835 // Wait for the trap receiver to receive the expected
|
836 yi.zhou 1.8 // number of Indication traps, indicationTrapV1SendCount
837 // and indicationTrapV2SendCount.
|
838 yi.zhou 1.1 //
|
839 yi.zhou 1.8 // We will continue to wait until either (indicationTrapV1SendCount
840 // and indicationTrapV2SendCount) Indications have been received
841 // by the trap receiver or no new
|
842 yi.zhou 1.1 // Indications have been received in the previous
843 // MAX_NO_CHANGE_ITERATIONS.
|
844 kumpf 1.7 // iterations.
|
845 yi.zhou 1.1 //
846
847 Boolean receivedTrapCountComplete = false;
848 Boolean receiverTrap1NoChange = true;
849 Boolean receiverTrap2NoChange = true;
850
851 while (noChangeIterations <= MAX_NO_CHANGE_ITERATIONS)
852 {
853 totalIterations++;
854
|
855 yi.zhou 1.13 currentReceivedTrap1Count =
856 _getReceivedTrapCount(_SNMPV1_TRAP, logFile);
857 currentReceivedTrap2Count =
858 _getReceivedTrapCount(_SNMPV2C_TRAP, logFile);
|
859 yi.zhou 1.1
860 if (totalIterations % COUT_TIME_INTERVAL == 1 &&
861 !(receivedTrapCountComplete))
862 {
863 cout << "++++ The trap receiver has received "
864 << currentReceivedTrap1Count << " of "
|
865 yi.zhou 1.8 << indicationTrapV1SendCount << " SNMPv1 trap."
|
866 yi.zhou 1.1 << endl;
867 cout << "++++ The trap receiver has received "
868 << currentReceivedTrap2Count << " of "
|
869 yi.zhou 1.8 << indicationTrapV2SendCount << " SNMPv2c trap."
|
870 yi.zhou 1.1 << endl;
871 }
872
|
873 yi.zhou 1.8 if ((indicationTrapV1SendCount == currentReceivedTrap1Count) &&
874 (indicationTrapV2SendCount == currentReceivedTrap2Count))
|
875 yi.zhou 1.1 {
876 receivedTrapCountComplete = true;
877 trapReceiverElapsedTime.stop();
878 }
879 if (!(receiverTrap1NoChange =
880 (priorReceivedTrap1Count == currentReceivedTrap1Count)))
881 {
882 priorReceivedTrap1Count = currentReceivedTrap1Count;
883 }
884
885 if (!(receiverTrap2NoChange =
886 (priorReceivedTrap2Count == currentReceivedTrap2Count)))
887 {
888 priorReceivedTrap2Count = currentReceivedTrap2Count;
889 }
890
891 if (receivedTrapCountComplete)
892 {
893 cout << "++++ The trap receiver has received "
894 << currentReceivedTrap1Count << " of "
|
895 yi.zhou 1.8 << indicationTrapV1SendCount << " SNMPv1 trap."
|
896 yi.zhou 1.1 << endl;
897 cout << "++++ The trap receiver has received "
898 << currentReceivedTrap2Count << " of "
|
899 yi.zhou 1.8 << indicationTrapV2SendCount << " SNMPv2c trap."
|
900 yi.zhou 1.1 << endl;
901
902 break;
903 }
904 if (receiverTrap1NoChange || receiverTrap2NoChange)
905 {
906 noChangeIterations++;
907 }
908 else
909 {
910 noChangeIterations = 0;
911 }
912
913 System::sleep (SLEEP_SEC);
914 }
915
916 if (!receivedTrapCountComplete)
917 {
918 trapReceiverElapsedTime.stop();
919 }
920
|
921 kumpf 1.7 // assert that all indications sent have been received.
|
922 yi.zhou 1.8 PEGASUS_TEST_ASSERT(indicationTrapV1SendCount ==
|
923 yi.zhou 1.1 currentReceivedTrap1Count);
|
924 yi.zhou 1.8 PEGASUS_TEST_ASSERT(indicationTrapV2SendCount ==
|
925 yi.zhou 1.1 currentReceivedTrap2Count);
|
926 yi.zhou 1.2 }
927
|
928 kumpf 1.7 int _beginTest(CIMClient& workClient,
|
929 yi.zhou 1.2 Uint32 indicationSendCount,
|
930 yi.zhou 1.13 Uint32 runClientThreadCount,
931 const String& processIdFile,
932 const String& logFile)
|
933 yi.zhou 1.2 {
|
934 yi.zhou 1.1
935 #ifdef PEGASUS_USE_NET_SNMP
|
936 yi.zhou 1.2
937 // Stop snmptrapd process if it is running
|
938 yi.zhou 1.13 _stopSnmptrapd(processIdFile);
|
939 yi.zhou 1.2
940 // if trapLogFile exists, remove it
|
941 yi.zhou 1.13 _removeTrapLogFile(logFile);
|
942 yi.zhou 1.2
943 FILE * trapInfo;
944
945 try
946 {
|
947 yi.zhou 1.13 _startSnmptrapd(&trapInfo, processIdFile, logFile);
|
948 yi.zhou 1.2 }
949 catch (Exception & e)
950 {
951 cerr << e.getMessage() << endl;
952 return (-1);
953 }
954
955 // Extended for all snmp implementation
|
956 kumpf 1.7 _receiveExpectedTraps(workClient, indicationSendCount,
|
957 yi.zhou 1.13 runClientThreadCount, logFile);
|
958 yi.zhou 1.2
|
959 yi.zhou 1.1 // Stop snmptrapd process if it is running and remove procIdFile
|
960 yi.zhou 1.13 _stopSnmptrapd(processIdFile);
|
961 yi.zhou 1.1
962 pclose(trapInfo);
963
964 // if error encountered then fail the test.
965 if (errorsEncountered.get())
|
966 kumpf 1.7 {
|
967 yi.zhou 1.1 cout << "+++++ test failed" << endl;
968 return (-1);
969 }
970 else
971 {
972 cout << "+++++ passed all tests" << endl;
973 }
|
974 yi.zhou 1.2
975 return (0);
976
977 #else
978 cerr << "Cannot create a trap receiver." << endl;
979 return (-1);
980 #endif
|
981 yi.zhou 1.1 }
982
983 int main (int argc, char** argv)
984 {
985 // This client connection is used solely to create and delete subscriptions.
986 CIMClient workClient;
987 try
988 {
989 workClient.connectLocal();
990
|
991 yi.zhou 1.13 String processIdFile = TRAP_DIR;
992 processIdFile.append("/procIdFile");
993
994 String logFile = TRAP_DIR;
995 logFile.append("/trapLogFile");
996
|
997 yi.zhou 1.1 if (argc <= 1 || argc > 4)
998 {
999 cerr << "Invalid argument count: " << argc << endl;
1000 _usage();
1001 return 1;
1002 }
1003 else if (strcmp(argv[1], "setup") == 0)
1004 {
1005 if (argc < 3)
1006 {
1007 cerr << "Missing query language" << endl;
1008 _usage();
1009 return -1;
1010 }
1011
1012 if ((strcmp(argv[2], "WQL") != 0) &&
1013 (strcmp(argv[2], "DMTF:CQL") != 0))
1014 {
1015 cerr << "Invalid query language: '" << argv[2] << "'" << endl;
1016 _usage();
1017 return -1;
1018 yi.zhou 1.1 }
|
1019 kumpf 1.7
|
1020 yi.zhou 1.1 _setup(workClient, argv[2]);
1021
1022 cout << "+++++ setup completed successfully" << endl;
1023 return 0;
|
1024 kumpf 1.7 }
|
1025 yi.zhou 1.1 else if (String::equalNoCase(argv[1], "run"))
1026 {
1027 if (argc < 3)
1028 {
1029 cerr << "Invalid indicationSendCount." << endl;
1030 _usage ();
1031 return -1;
1032 }
1033
1034 Uint32 indicationSendCount = atoi(argv[2]);
1035
1036 Uint32 runClientThreadCount = 1;
1037
|
1038 kumpf 1.7 if (argc == 4)
|
1039 yi.zhou 1.1 {
1040 runClientThreadCount = atoi(argv[3]);
1041 }
1042
|
1043 kumpf 1.7 int rc = _beginTest(workClient, indicationSendCount,
|
1044 yi.zhou 1.13 runClientThreadCount, processIdFile, logFile);
|
1045 yi.zhou 1.1 return rc;
|
1046 kumpf 1.7 }
|
1047 yi.zhou 1.1 else if (String::equalNoCase(argv[1], "cleanup"))
1048 {
1049 if (argc > 2)
1050 {
1051 cerr << "Invalid argument count." << endl;
1052 _usage ();
1053 return -1;
1054 }
|
1055 kumpf 1.7
|
1056 yi.zhou 1.1 _cleanup (workClient);
1057
1058 cout << "+++++ cleanup completed successfully" << endl;
1059 return 0;
1060 }
1061 else if (String::equalNoCase(argv[1], "removelog"))
1062 {
1063 if (argc > 2)
1064 {
1065 cerr << "Invalid argument count." << endl;
1066 _usage ();
1067 return -1;
1068 }
|
1069 kumpf 1.7
|
1070 yi.zhou 1.13 _removeTrapLogFile(logFile);
|
1071 yi.zhou 1.1 cout << "+++++ removelog completed successfully" << endl;
1072 return 0;
1073 }
1074 else
1075 {
1076 cerr << "Invalid option: " << argv[1] << endl;
1077 _usage ();
1078 return -1;
1079 }
1080 }
1081 catch (Exception & e)
1082 {
1083 cerr << "Error: " << e.getMessage() << endl;
1084 }
1085
|
1086 kumpf 1.11 return -1;
|
1087 yi.zhou 1.1 }
|