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

  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 yi.zhou     1.1     CIMNamespaceName ("root/SampleProvider");
 48                 
 49                 const String INDICATION_CLASS_NAME = String ("RT_TestIndication");
 50                 
 51                 const String SNMPV1_HANDLER_NAME = String ("SNMPHandler01");
 52                 const String SNMPV2C_HANDLER_NAME = String ("SNMPHandler02");
 53                 const String FILTER_NAME = String ("IPFilter01");
 54                 
 55                 enum SNMPVersion {_SNMPV1_TRAP = 2, _SNMPV2C_TRAP = 3};
 56 dave.sudlik 1.6 enum TargetHostFormat {_HOST_NAME = 2, _IPV4_ADDRESS = 3, _IPV6_ADDRESS = 4};
 57 yi.zhou     1.1 
 58                 #define PORT_NUMBER 2006
 59                 
 60                 Uint32 indicationSendCountTotal = 0;
 61                 
 62                 AtomicInt errorsEncountered(0);
 63                 
 64                 ////////////////////////////////////////////////////////////////////////////////
 65                 //
 66                 // Thread Parameters Class
 67                 //
 68                 ////////////////////////////////////////////////////////////////////////////////
 69                 
 70 kumpf       1.7 class T_Parms
 71                 {
 72                 public:
 73 yi.zhou     1.1     AutoPtr<CIMClient> client;
 74                     Uint32 indicationSendCount;
 75                     Uint32 uniqueID;
 76                 };
 77                 
 78                 ///////////////////////////////////////////////////////////////////////////
 79                 
 80 kumpf       1.7 CIMObjectPath _getFilterObjectPath(const String & name)
 81 yi.zhou     1.1 {
 82                     Array<CIMKeyBinding> keyBindings;
 83                     keyBindings.append (CIMKeyBinding ("SystemCreationClassName",
 84                         System::getSystemCreationClassName (), CIMKeyBinding::STRING));
 85                     keyBindings.append (CIMKeyBinding ("SystemName",
 86                         System::getFullyQualifiedHostName (), CIMKeyBinding::STRING));
 87                     keyBindings.append (CIMKeyBinding ("CreationClassName",
 88                         PEGASUS_CLASSNAME_INDFILTER.getString(), CIMKeyBinding::STRING));
 89                     keyBindings.append (CIMKeyBinding ("Name", name,
 90                         CIMKeyBinding::STRING));
 91                     return(CIMObjectPath("", CIMNamespaceName (),
 92                         PEGASUS_CLASSNAME_INDFILTER, keyBindings));
 93                 }
 94                 
 95 kumpf       1.7 CIMObjectPath _getHandlerObjectPath(const String & name)
 96 yi.zhou     1.1 {
 97                     Array<CIMKeyBinding> keyBindings;
 98                     keyBindings.append (CIMKeyBinding ("SystemCreationClassName",
 99                         System::getSystemCreationClassName (), CIMKeyBinding::STRING));
100                     keyBindings.append (CIMKeyBinding ("SystemName",
101                         System::getFullyQualifiedHostName (), CIMKeyBinding::STRING));
102                     keyBindings.append (CIMKeyBinding ("CreationClassName",
103                         PEGASUS_CLASSNAME_INDHANDLER_SNMP.getString(),
104                         CIMKeyBinding::STRING));
105                     keyBindings.append (CIMKeyBinding ("Name", name,
106                         CIMKeyBinding::STRING));
107                     return(CIMObjectPath("", CIMNamespaceName (),
108                         PEGASUS_CLASSNAME_INDHANDLER_SNMP, keyBindings));
109                 }
110                 
111 kumpf       1.7 CIMObjectPath _getSubscriptionObjectPath(
112                     const String & filterName,
113                     const String & handlerName)
114 yi.zhou     1.1 {
115                     CIMObjectPath filterObjectPath = _getFilterObjectPath(filterName);
116                 
117                     CIMObjectPath handlerObjectPath = _getHandlerObjectPath(handlerName);
118                 
119                     Array<CIMKeyBinding> subscriptionKeyBindings;
120                     subscriptionKeyBindings.append (CIMKeyBinding ("Filter",
121                         CIMValue(filterObjectPath)));
122                     subscriptionKeyBindings.append (CIMKeyBinding ("Handler",
123                         CIMValue(handlerObjectPath)));
124                     return(CIMObjectPath("", CIMNamespaceName (),
125                         PEGASUS_CLASSNAME_INDSUBSCRIPTION, subscriptionKeyBindings));
126                 }
127                 
128 kumpf       1.7 CIMObjectPath _createHandlerInstance(
129                     CIMClient & client,
130                     const String & name,
131                     const String & targetHost,
132                     const String & securityName,
133                     const Uint16 targetHostFormat,
134                     const Uint16 snmpVersion)
135 yi.zhou     1.1 {
136                     CIMInstance handlerInstance (PEGASUS_CLASSNAME_INDHANDLER_SNMP);
137                     handlerInstance.addProperty (CIMProperty (CIMName
138                         ("SystemCreationClassName"), System::getSystemCreationClassName ()));
139                     handlerInstance.addProperty (CIMProperty (CIMName ("SystemName"),
140                         System::getFullyQualifiedHostName ()));
141                     handlerInstance.addProperty (CIMProperty (CIMName ("CreationClassName"),
142                         PEGASUS_CLASSNAME_INDHANDLER_SNMP.getString ()));
143                     handlerInstance.addProperty (CIMProperty (CIMName ("Name"), name));
144                     handlerInstance.addProperty (CIMProperty (CIMName ("TargetHost"),
145                         targetHost));
146                     handlerInstance.addProperty (CIMProperty (CIMName ("TargetHostFormat"),
147                         CIMValue ((Uint16) targetHostFormat)));
148                     handlerInstance.addProperty (CIMProperty (CIMName ("SNMPSecurityName"),
149                         securityName));
150                     handlerInstance.addProperty (CIMProperty (CIMName ("SnmpVersion"),
151                         CIMValue ((Uint16) snmpVersion)));
152                     handlerInstance.addProperty (CIMProperty (CIMName ("PortNumber"),
153                         CIMValue ((Uint32) PORT_NUMBER)));
154                 
155 kumpf       1.7     return client.createInstance(
156                         PEGASUS_NAMESPACENAME_INTEROP, handlerInstance);
157 yi.zhou     1.1 }
158                 
159 kumpf       1.7 CIMObjectPath _createFilterInstance(
160                     CIMClient & client,
161                     const String & name,
162                     const String & query,
163                     const String & qlang)
164 yi.zhou     1.1 {
165                     CIMInstance filterInstance (PEGASUS_CLASSNAME_INDFILTER);
166                     filterInstance.addProperty (CIMProperty (CIMName
167                         ("SystemCreationClassName"), System::getSystemCreationClassName ()));
168                     filterInstance.addProperty (CIMProperty (CIMName ("SystemName"),
169                         System::getFullyQualifiedHostName ()));
170                     filterInstance.addProperty (CIMProperty (CIMName ("CreationClassName"),
171                         PEGASUS_CLASSNAME_INDFILTER.getString ()));
172                     filterInstance.addProperty (CIMProperty (CIMName ("Name"), name));
173                     filterInstance.addProperty (CIMProperty (CIMName ("Query"), query));
174                     filterInstance.addProperty (CIMProperty (CIMName ("QueryLanguage"),
175                         String (qlang)));
176                     filterInstance.addProperty (CIMProperty (CIMName ("SourceNamespace"),
177                         SOURCE_NAMESPACE.getString ()));
178                 
179 kumpf       1.7     return client.createInstance(PEGASUS_NAMESPACENAME_INTEROP, filterInstance);
180 yi.zhou     1.1 }
181                 
182 kumpf       1.7 CIMObjectPath _createSubscriptionInstance(
183                     CIMClient & client,
184                     const CIMObjectPath & filterPath,
185                     const CIMObjectPath & handlerPath)
186 yi.zhou     1.1 {
187                     CIMInstance subscriptionInstance (PEGASUS_CLASSNAME_INDSUBSCRIPTION);
188                     subscriptionInstance.addProperty (CIMProperty (CIMName ("Filter"),
189                         filterPath, 0, PEGASUS_CLASSNAME_INDFILTER));
190                     subscriptionInstance.addProperty (CIMProperty (CIMName ("Handler"),
191                         handlerPath, 0, PEGASUS_CLASSNAME_INDHANDLER_SNMP));
192                     subscriptionInstance.addProperty (CIMProperty
193                         (CIMName ("SubscriptionState"), CIMValue ((Uint16) 2)));
194                 
195 kumpf       1.7     return client.createInstance(
196                         PEGASUS_NAMESPACENAME_INTEROP, subscriptionInstance);
197 yi.zhou     1.1 }
198                 
199 kumpf       1.7 void _sendTestIndication(
200                     CIMClient* client,
201                     const CIMName & methodName,
202                     Uint32 indicationSendCount)
203 yi.zhou     1.1 {
204                     //
205                     //  Invoke method to send test indication
206                     //
207                     Array <CIMParamValue> inParams;
208                     Array <CIMParamValue> outParams;
209                     Array <CIMKeyBinding> keyBindings;
210                     Sint32 result;
211                 
212                     CIMObjectPath className (String::EMPTY, CIMNamespaceName (),
213                         CIMName ("RT_TestIndication"), keyBindings);
214                 
215 kumpf       1.7     inParams.append(CIMParamValue(String("indicationSendCount"),
216 yi.zhou     1.1         CIMValue(indicationSendCount)));
217                 
218                     CIMValue retValue = client->invokeMethod
219                         (SOURCE_NAMESPACE,
220                          className,
221                          methodName,
222                          inParams,
223                          outParams);
224                 
225                     retValue.get (result);
226                     PEGASUS_TEST_ASSERT (result == 0);
227                 }
228                 
229 kumpf       1.7 void _deleteSubscriptionInstance(
230                     CIMClient & client,
231                     const String & filterName,
232                     const String & handlerName)
233 yi.zhou     1.1 {
234                     CIMObjectPath subscriptionObjectPath =
235                        _getSubscriptionObjectPath(filterName, handlerName);
236 kumpf       1.7     client.deleteInstance(
237                         PEGASUS_NAMESPACENAME_INTEROP, subscriptionObjectPath);
238 yi.zhou     1.1 }
239                 
240 kumpf       1.7 void _deleteHandlerInstance(
241                     CIMClient & client,
242                     const String & name)
243 yi.zhou     1.1 {
244                     CIMObjectPath handlerObjectPath = _getHandlerObjectPath(name);
245 karl        1.4     client.deleteInstance (PEGASUS_NAMESPACENAME_INTEROP, handlerObjectPath);
246 yi.zhou     1.1 }
247                 
248 kumpf       1.7 void _deleteFilterInstance(
249                     CIMClient & client,
250                     const String & name)
251 yi.zhou     1.1 {
252                     CIMObjectPath filterObjectPath = _getFilterObjectPath(name);
253 karl        1.4     client.deleteInstance (PEGASUS_NAMESPACENAME_INTEROP, filterObjectPath);
254 yi.zhou     1.1 }
255                 
256 kumpf       1.7 void _usage()
257 yi.zhou     1.1 {
258                    cerr << endl
259                         << "Usage:" << endl
260                         << "    TestSnmpHandler setup [ WQL | DMTF:CQL ]\n"
261                         << "    TestSnmpHandler run <indicationSendCount> "
262                         << "[<threads>]\n"
263                         << "    where: " << endl
264                         << "       <indicationSendCount> is the number of indications to\n"
265                         << "            generate and has to be greater than zero." << endl
266                         << "       <threads> is an optional number of client threads to\n"
267                         << "            create, default is one." << endl
268                         << "    TestSnmpHandler cleanup\n"
269                         << "    TestSnmpHandler removelog"
270                         << endl << endl;
271                 }
272                 
273                 void _setup (CIMClient & client, const String& qlang)
274                 {
275                     CIMObjectPath filterObjectPath;
276                     CIMObjectPath snmpv1HandlerObjectPath;
277                     CIMObjectPath snmpv2HandlerObjectPath;
278 yi.zhou     1.1 
279                     try
280                     {
281                         filterObjectPath = _createFilterInstance (client, FILTER_NAME,
282                             String ("SELECT * FROM RT_TestIndication"),
283                             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 yi.zhou     1.1 
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                 }
395                 
396                 void _cleanup (CIMClient & client)
397                 {
398                     try
399 yi.zhou     1.1     {
400                         _deleteSubscriptionInstance (client, FILTER_NAME,
401                             SNMPV1_HANDLER_NAME);
402                     }
403                     catch (CIMException& e)
404                     {
405                         if (e.getCode() != CIM_ERR_NOT_FOUND)
406                         {
407                             cerr << "----- Error: deleteSubscriptionInstance failure: "
408                                  << endl;
409                             throw;
410                         }
411                     }
412                     try
413                     {
414                         _deleteSubscriptionInstance (client, FILTER_NAME,
415                             SNMPV2C_HANDLER_NAME);
416                     }
417                     catch (CIMException& e)
418                     {
419                         if (e.getCode() != CIM_ERR_NOT_FOUND)
420 yi.zhou     1.1         {
421                             cerr << "----- Error: deleteSubscriptionInstance failure: "
422                                  << endl;
423                             throw;
424                         }
425                     }
426                     try
427                     {
428                         _deleteFilterInstance (client, FILTER_NAME);
429                     }
430                     catch (CIMException& e)
431                     {
432                         if (e.getCode() != CIM_ERR_NOT_FOUND)
433                         {
434                             cerr << "----- Error: deleteFilterInstance failure: " << endl;
435                             throw;
436                         }
437                     }
438                 
439                     try
440                     {
441 yi.zhou     1.1         _deleteHandlerInstance (client, SNMPV1_HANDLER_NAME);
442                     }
443                     catch (CIMException& e)
444                     {
445                         if (e.getCode() != CIM_ERR_NOT_FOUND)
446                         {
447                             cerr << "----- Error: deleteHandlerInstance failure: " << endl;
448                             throw;
449                         }
450                     }
451                     try
452                     {
453                         _deleteHandlerInstance (client, SNMPV2C_HANDLER_NAME);
454                     }
455                     catch (CIMException& e)
456                     {
457                         if (e.getCode() != CIM_ERR_NOT_FOUND)
458                         {
459                             cerr << "----- Error: deleteHandlerInstance failure: " << endl;
460                             throw;
461                         }
462 yi.zhou     1.1     }
463                 }
464                 
465                 static void _testEnd(const String& uniqueID, const double elapsedTime)
466                 {
467                     cout << "+++++ thread" << uniqueID << ": passed in " << elapsedTime
468                         << " seconds" << endl;
469                 }
470                 
471 mike        1.3 ThreadReturnType PEGASUS_THREAD_CDECL _executeTests(void *parm)
472 yi.zhou     1.1 {
473                     Thread *my_thread = (Thread *)parm;
474                     T_Parms *parms = (T_Parms *)my_thread->get_parm();
475                     CIMClient *client = parms->client.get();
476                     Uint32 indicationSendCount = parms->indicationSendCount;
477                     Uint32 id = parms->uniqueID;
478                     char id_[4];
479                     memset(id_,0x00,sizeof(id_));
480                     sprintf(id_,"%i",id);
481                     String uniqueID = "_";
482                     uniqueID.append(id_);
483                 
484                     try
485                     {
486                         Stopwatch elapsedTime;
487                 
488                         elapsedTime.start();
489                         try
490                         {
491                             _sendTestIndication (client, CIMName ("SendTestIndicationTrap"),
492                             indicationSendCount);
493 yi.zhou     1.1         }
494                         catch (Exception & e)
495                         {
496 kumpf       1.7             cerr << "----- sendTestIndication failed: " << e.getMessage () <<
497                                 endl;
498 yi.zhou     1.1             exit (-1);
499                         }
500                         elapsedTime.stop();
501                         _testEnd(uniqueID, elapsedTime.getElapsed());
502                     }
503                     catch(Exception  & e)
504                     {
505                         cout << e.getMessage() << endl;
506                     }
507 mike        1.3     my_thread->exit_self((ThreadReturnType)1);
508 yi.zhou     1.1     return(0);
509                 }
510                 
511                 Thread * _runTestThreads(
512                     CIMClient* client,
513                     Uint32 indicationSendCount,
514                     Uint32 uniqueID)
515                 {
516                     // package parameters, create thread and run...
517                     AutoPtr<T_Parms> parms(new T_Parms());
518                     parms->client.reset(client);
519                     parms->indicationSendCount = indicationSendCount;
520                     parms->uniqueID = uniqueID;
521                     AutoPtr<Thread> t(new Thread(_executeTests, (void*)parms.release(), false));
522                     t->run();
523                     return t.release();
524                 }
525                 
526                 String _getLogFile()
527                 {
528                     return("trapLogFile");
529 yi.zhou     1.1 }
530                 
531                 Uint32 _getReceivedTrapCount(Uint16 snmpVersion)
532                 {
533                     String trap1 = "Trap Info: TRAP, SNMP v1, community public";
534                     String trap2 = "Trap Info: TRAP2, SNMP v2c, community public";
535                 
536                     Uint32 receivedTrap1Count = 0;
537                     Uint32 receivedTrap2Count = 0;
538 kumpf       1.7 
539 yi.zhou     1.1     ifstream ifs(_getLogFile().getCString());
540                     if (!ifs)
541                     {
542                         return (0);
543                     }
544                 
545                     String line;
546                     while (GetLine(ifs, line))
547                     {
548                         if (String::compare(line, trap1) == 0)
549                         {
550                             receivedTrap1Count++;
551                         }
552                         if (String::compare(line, trap2) == 0)
553                         {
554                             receivedTrap2Count++;
555                         }
556                     }
557                 
558                     ifs.close();
559                 
560 yi.zhou     1.1     switch (snmpVersion)
561                     {
562                         case _SNMPV1_TRAP:
563                         {
564                             return (receivedTrap1Count);
565                         }
566                         case _SNMPV2C_TRAP:
567                         {
568                             return (receivedTrap2Count);
569                         }
570                         default:
571                         {
572                             return (0);
573                         }
574                     }
575                 
576                 }
577                 
578                 #ifdef PEGASUS_USE_NET_SNMP
579 kumpf       1.7 // Stop snmptrapd process if it is running and remove
580 yi.zhou     1.1 // procIdFile file if it exists
581 kumpf       1.7 //
582 yi.zhou     1.1 void _stopSnmptrapd()
583                 {
584                     String procIdFileName = "procIdFile";
585                 
586                     Uint32 receiverPid;
587                     FILE *fd;
588                     if ((fd = fopen(procIdFileName.getCString(), "r")) != NULL)
589                     {
590                         fscanf(fd, "%d\n", &receiverPid);
591 kumpf       1.7 
592 yi.zhou     1.1         kill(receiverPid, SIGTERM);
593                 
594                         fclose(fd);
595                     }
596                 
597                     if (FileSystem::exists(procIdFileName))
598                     {
599                         FileSystem::removeFile(procIdFileName);
600                     }
601                 }
602                 
603                 static Boolean _startSnmptrapd(
604                     FILE **trapInfo)
605                 {
606                     String snmptrapdCmd;
607                 
608                     Uint32 portNumber = PORT_NUMBER;
609                     char portNumberStr[32];
610                     sprintf(portNumberStr, "%lu", (unsigned long) portNumber);
611                 
612                     //
613 yi.zhou     1.1     // build snmptrapd cmd options
614                     //
615                 
616 kumpf       1.7     // Specify logging incoming traps to trapLogFile
617 yi.zhou     1.1     // Save the process ID of the snmptrapd in procIdFile
618                     snmptrapdCmd.append(
619                         "/usr/sbin/snmptrapd -f -Lf trapLogFile -p procIdFile");
620                 
621                     // Specify incoming trap format
622                     snmptrapdCmd.append( " -F \"\nTrap Info: %P\nVariable: %v\n\"");
623                 
624                     // Specify listening address
625                     snmptrapdCmd.append(" UDP:");
626                     snmptrapdCmd.append(System::getFullyQualifiedHostName ());
627                 
628                     snmptrapdCmd.append(":");
629                     snmptrapdCmd.append(portNumberStr);
630                 
631                     if ((*trapInfo = popen(snmptrapdCmd.getCString(), "r")) == NULL)
632                     {
633                         throw Exception ("snmptrapd can not be started");
634                     }
635                 
636                 #define MAX_ITERATIONS 300
637                 #define SLEEP_SEC 1
638 yi.zhou     1.1 
639                     Uint32 iterations = 0;
640                 
641 kumpf       1.7     // Wait until snmptrapd started
642 yi.zhou     1.1     while (iterations < MAX_ITERATIONS)
643                     {
644                         iterations++;
645                         if (FileSystem::exists("procIdFile"))
646                         {
647                             return (true);
648                         }
649                         else
650                         {
651                             System::sleep(SLEEP_SEC);
652 kumpf       1.7 
653 yi.zhou     1.1         }
654                     }
655                 
656                     throw Exception ("snmptrapd can not be started");
657                 }
658                 #endif
659                 
660                 void _removeTrapLogFile ()
661                 {
662                     String logFile = _getLogFile();
663                 
664                     // if trapLogFile exists, remove it
665                     if (FileSystem::exists(logFile))
666                     {
667                         FileSystem::removeFile(logFile);
668                     }
669                 }
670                 
671 kumpf       1.7 void _receiveExpectedTraps(
672                     CIMClient& workClient,
673 yi.zhou     1.1     Uint32 indicationSendCount,
674                     Uint32 runClientThreadCount)
675                 {
676                     CIMClient * clientConnections = new CIMClient[runClientThreadCount];
677                 
678                     // determine total number of indication send count
679                     indicationSendCountTotal = indicationSendCount * runClientThreadCount;
680                 
681                     // calculate the timeout based on the total send count allowing
682 kumpf       1.7     // using the MSG_PER_SEC rate
683                     // allow 20 seconds of test overhead for very small tests
684 yi.zhou     1.1 
685                 #define MSG_PER_SEC 4
686                 
687 marek       1.5     Uint32 testTimeout = PEGASUS_DEFAULT_CLIENT_TIMEOUT_MILLISECONDS
688                                         + (indicationSendCountTotal/MSG_PER_SEC)*1000;
689 yi.zhou     1.1 
690                     // connect the clients
691                     for(Uint32 i = 0; i < runClientThreadCount; i++)
692                     {
693                         clientConnections[i].setTimeout(testTimeout);
694                         clientConnections[i].connectLocal();
695                     }
696                 
697                     // run tests
698                     Thread ** clientThreads = new Thread *[runClientThreadCount];
699                 
700                     Stopwatch trapReceiverElapsedTime;
701                 
702                     trapReceiverElapsedTime.start();
703                 
704                     for(Uint32 i = 0; i < runClientThreadCount; i++)
705                     {
706                         clientThreads[i] = _runTestThreads(&clientConnections[i],
707                             indicationSendCount, i);
708                     }
709                 
710 yi.zhou     1.1     for(Uint32 i=0; i< runClientThreadCount; i++)
711                     {
712                         clientThreads[i]->join();
713                     }
714                 
715                     delete[] clientConnections;
716                     delete[] clientThreads;
717                 
718                     //
719                     //  Allow time for the trap to be received
720                     //  Wait in SLEEP_SEC second intervals.
721                     //  Put msg out every MSG_SEC intervals
722                     //
723                 
724                 #define SLEEP_SEC 1
725                 #define COUT_TIME_INTERVAL 30
726                 #define MAX_NO_CHANGE_ITERATIONS COUT_TIME_INTERVAL*3
727                 
728                     Uint32 noChangeIterations = 0;
729                     Uint32 priorReceivedTrap1Count = 0;
730                     Uint32 priorReceivedTrap2Count = 0;
731 yi.zhou     1.1     Uint32 currentReceivedTrap1Count = 0;
732                     Uint32 currentReceivedTrap2Count = 0;
733                     Uint32 totalIterations = 0;
734                 
735                     //
736                     // Wait for the trap receiver to receive the expected
737 kumpf       1.7     // number of Indication traps, indicationSendCountTotal.
738 yi.zhou     1.1     //
739                     // We will continue to wait until either indicationSendCountTotal
740                     // Indications have been received by the trap receiver or no new
741                     // Indications have been received in the previous
742                     // MAX_NO_CHANGE_ITERATIONS.
743 kumpf       1.7     // iterations.
744 yi.zhou     1.1     //
745                 
746                     Boolean receivedTrapCountComplete = false;
747                     Boolean receiverTrap1NoChange = true;
748                     Boolean receiverTrap2NoChange = true;
749                 
750                     while (noChangeIterations <= MAX_NO_CHANGE_ITERATIONS)
751                     {
752                         totalIterations++;
753                 
754 kumpf       1.7         currentReceivedTrap1Count = _getReceivedTrapCount(_SNMPV1_TRAP);
755                         currentReceivedTrap2Count = _getReceivedTrapCount(_SNMPV2C_TRAP);
756 yi.zhou     1.1 
757                         if (totalIterations % COUT_TIME_INTERVAL == 1 &&
758                             !(receivedTrapCountComplete))
759                         {
760                             cout << "++++ The trap receiver has received "
761                             << currentReceivedTrap1Count << " of "
762                             << indicationSendCountTotal << " SNMPv1 trap."
763                             << endl;
764                             cout << "++++ The trap receiver has received "
765                             << currentReceivedTrap2Count << " of "
766                             << indicationSendCountTotal << " SNMPv2c trap."
767                             << endl;
768                         }
769                 
770                         if ((indicationSendCountTotal == currentReceivedTrap1Count) &&
771                             (indicationSendCountTotal == currentReceivedTrap2Count))
772                         {
773                              receivedTrapCountComplete = true;
774                              trapReceiverElapsedTime.stop();
775                         }
776                         if (!(receiverTrap1NoChange =
777 yi.zhou     1.1                 (priorReceivedTrap1Count == currentReceivedTrap1Count)))
778                         {
779                              priorReceivedTrap1Count = currentReceivedTrap1Count;
780                         }
781                 
782                         if (!(receiverTrap2NoChange =
783                                 (priorReceivedTrap2Count == currentReceivedTrap2Count)))
784                         {
785                              priorReceivedTrap2Count = currentReceivedTrap2Count;
786                         }
787                 
788                         if (receivedTrapCountComplete)
789                         {
790                             cout << "++++ The trap receiver has received "
791                             << currentReceivedTrap1Count << " of "
792                             << indicationSendCountTotal << " SNMPv1 trap."
793                             << endl;
794                             cout << "++++ The trap receiver has received "
795                             << currentReceivedTrap2Count << " of "
796                             << indicationSendCountTotal << " SNMPv2c trap."
797                             << endl;
798 yi.zhou     1.1 
799                             break;
800                         }
801                         if (receiverTrap1NoChange || receiverTrap2NoChange)
802                         {
803                            noChangeIterations++;
804                         }
805                         else
806                         {
807                            noChangeIterations = 0;
808                         }
809                 
810                         System::sleep (SLEEP_SEC);
811                     }
812                 
813                     if (!receivedTrapCountComplete)
814                     {
815                         trapReceiverElapsedTime.stop();
816                     }
817                 
818 kumpf       1.7     // assert that all indications sent have been received.
819 yi.zhou     1.1     PEGASUS_TEST_ASSERT(indicationSendCountTotal ==
820                        currentReceivedTrap1Count);
821                     PEGASUS_TEST_ASSERT(indicationSendCountTotal ==
822                        currentReceivedTrap2Count);
823 yi.zhou     1.2 }
824                 
825 kumpf       1.7 int _beginTest(CIMClient& workClient,
826 yi.zhou     1.2     Uint32 indicationSendCount,
827                     Uint32 runClientThreadCount)
828                 {
829 yi.zhou     1.1 
830                 #ifdef PEGASUS_USE_NET_SNMP
831 yi.zhou     1.2 
832                     // Stop snmptrapd process if it is running
833                     _stopSnmptrapd();
834                 
835                     // if trapLogFile exists, remove it
836                     _removeTrapLogFile();
837                 
838                     FILE * trapInfo;
839                 
840                     try
841                     {
842                         _startSnmptrapd(&trapInfo);
843                     }
844                     catch (Exception & e)
845                     {
846                         cerr << e.getMessage() << endl;
847                         return (-1);
848                     }
849                 
850                     // Extended for all snmp implementation
851 kumpf       1.7     _receiveExpectedTraps(workClient, indicationSendCount,
852                         runClientThreadCount);
853 yi.zhou     1.2 
854 yi.zhou     1.1     // Stop snmptrapd process if it is running and remove procIdFile
855                     _stopSnmptrapd();
856                 
857                     pclose(trapInfo);
858                 
859                     // if error encountered then fail the test.
860                     if (errorsEncountered.get())
861 kumpf       1.7     {
862 yi.zhou     1.1         cout << "+++++ test failed" << endl;
863                         return (-1);
864                     }
865                     else
866                     {
867                         cout << "+++++ passed all tests" << endl;
868                     }
869 yi.zhou     1.2 
870                     return (0);
871                 
872                 #else
873                     cerr << "Cannot create a trap receiver." << endl;
874                     return (-1);
875                 #endif
876 yi.zhou     1.1 }
877                 
878                 int main (int argc, char** argv)
879                 {
880                     // This client connection is used solely to create and delete subscriptions.
881                     CIMClient workClient;
882                     try
883                     {
884                         workClient.connectLocal();
885                 
886                         if (argc <= 1 || argc > 4)
887                         {
888                             cerr << "Invalid argument count: " << argc << endl;
889                             _usage();
890                             return 1;
891                         }
892                         else if (strcmp(argv[1], "setup") == 0)
893                         {
894                             if (argc < 3)
895                             {
896                                 cerr << "Missing query language" << endl;
897 yi.zhou     1.1                 _usage();
898                                 return -1;
899                             }
900                 
901                             if ((strcmp(argv[2], "WQL") != 0) &&
902                                 (strcmp(argv[2], "DMTF:CQL") != 0))
903                             {
904                                 cerr << "Invalid query language: '" << argv[2] << "'" << endl;
905                                 _usage();
906                                 return -1;
907                             }
908 kumpf       1.7 
909 yi.zhou     1.1             _setup(workClient, argv[2]);
910                 
911                             cout << "+++++ setup completed successfully" << endl;
912                             return 0;
913 kumpf       1.7         }
914 yi.zhou     1.1         else if (String::equalNoCase(argv[1], "run"))
915                         {
916                             if (argc < 3)
917                             {
918                                 cerr << "Invalid indicationSendCount." << endl;
919                                 _usage ();
920                                 return -1;
921                             }
922                 
923                             Uint32 indicationSendCount = atoi(argv[2]);
924                 
925                             Uint32 runClientThreadCount = 1;
926                 
927 kumpf       1.7             if (argc == 4)
928 yi.zhou     1.1             {
929                                 runClientThreadCount = atoi(argv[3]);
930                             }
931                 
932 kumpf       1.7             int rc = _beginTest(workClient, indicationSendCount,
933 yi.zhou     1.1                 runClientThreadCount);
934                             return rc;
935 kumpf       1.7         }
936 yi.zhou     1.1         else if (String::equalNoCase(argv[1], "cleanup"))
937                         {
938                             if (argc > 2)
939                             {
940                                 cerr << "Invalid argument count." << endl;
941                                 _usage ();
942                                 return -1;
943                             }
944 kumpf       1.7 
945 yi.zhou     1.1             _cleanup (workClient);
946                 
947                             cout << "+++++ cleanup completed successfully" << endl;
948                             return 0;
949                         }
950                         else if (String::equalNoCase(argv[1], "removelog"))
951                         {
952                             if (argc > 2)
953                             {
954                                 cerr << "Invalid argument count." << endl;
955                                 _usage ();
956                                 return -1;
957                             }
958 kumpf       1.7 
959 yi.zhou     1.1             _removeTrapLogFile ();
960                             cout << "+++++ removelog completed successfully" << endl;
961                             return 0;
962                         }
963                         else
964                         {
965                             cerr << "Invalid option: " << argv[1] << endl;
966                             _usage ();
967                             return -1;
968                         }
969                     }
970                     catch (Exception & e)
971                     {
972                         cerr << "Error: " << e.getMessage() << endl;
973                     }
974                 
975                     PEGASUS_UNREACHABLE( return 0; )
976                 }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2