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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2