(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.4.18.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.4.18.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.4.18.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.4.18.1             ipAddress,
355 yi.zhou     1.1                  "public",
356 dave.sudlik 1.4.18.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                          Uint32 testTimeout = 20000+(indicationSendCountTotal/MSG_PER_SEC)*1000;
687                      
688                          // connect the clients
689                          for(Uint32 i = 0; i < runClientThreadCount; i++)
690                          {
691                              clientConnections[i].setTimeout(testTimeout);
692 yi.zhou     1.1              clientConnections[i].connectLocal();
693                          }
694                      
695                          // run tests
696                          Thread ** clientThreads = new Thread *[runClientThreadCount];
697                      
698                          Stopwatch trapReceiverElapsedTime;
699                      
700                          trapReceiverElapsedTime.start();
701                      
702                          for(Uint32 i = 0; i < runClientThreadCount; i++)
703                          {
704                              clientThreads[i] = _runTestThreads(&clientConnections[i],
705                                  indicationSendCount, i);
706                          }
707                      
708                          for(Uint32 i=0; i< runClientThreadCount; i++)
709                          {
710                              clientThreads[i]->join();
711                          }
712                      
713 yi.zhou     1.1          delete[] clientConnections;
714                          delete[] clientThreads;
715                      
716                          //
717                          //  Allow time for the trap to be received
718                          //  Wait in SLEEP_SEC second intervals.
719                          //  Put msg out every MSG_SEC intervals
720                          //
721                      
722                      #define SLEEP_SEC 1
723                      #define COUT_TIME_INTERVAL 30
724                      #define MAX_NO_CHANGE_ITERATIONS COUT_TIME_INTERVAL*3
725                      
726                          Uint32 noChangeIterations = 0;
727                          Uint32 priorReceivedTrap1Count = 0;
728                          Uint32 priorReceivedTrap2Count = 0;
729                          Uint32 currentReceivedTrap1Count = 0;
730                          Uint32 currentReceivedTrap2Count = 0;
731                          Uint32 totalIterations = 0;
732                      
733                          //
734 yi.zhou     1.1          // Wait for the trap receiver to receive the expected
735                          // number of Indication traps, indicationSendCountTotal. 
736                          //
737                          // We will continue to wait until either indicationSendCountTotal
738                          // Indications have been received by the trap receiver or no new
739                          // Indications have been received in the previous
740                          // MAX_NO_CHANGE_ITERATIONS.
741                          // iterations. 
742                          //
743                      
744                          Boolean receivedTrapCountComplete = false;
745                          Boolean receiverTrap1NoChange = true;
746                          Boolean receiverTrap2NoChange = true;
747                      
748                          while (noChangeIterations <= MAX_NO_CHANGE_ITERATIONS)
749                          {
750                              totalIterations++;
751                      
752                              currentReceivedTrap1Count = _getReceivedTrapCount(_SNMPV1_TRAP); 
753                              currentReceivedTrap2Count = _getReceivedTrapCount(_SNMPV2C_TRAP); 
754                      
755 yi.zhou     1.1              if (totalIterations % COUT_TIME_INTERVAL == 1 &&
756                                  !(receivedTrapCountComplete))
757                              {
758                                  cout << "++++ The trap receiver has received "
759                                  << currentReceivedTrap1Count << " of "
760                                  << indicationSendCountTotal << " SNMPv1 trap."
761                                  << endl;
762                                  cout << "++++ The trap receiver has received "
763                                  << currentReceivedTrap2Count << " of "
764                                  << indicationSendCountTotal << " SNMPv2c trap."
765                                  << endl;
766                              }
767                      
768                              if ((indicationSendCountTotal == currentReceivedTrap1Count) &&
769                                  (indicationSendCountTotal == currentReceivedTrap2Count))
770                              {
771                                   receivedTrapCountComplete = true;
772                                   trapReceiverElapsedTime.stop();
773                              }
774                              if (!(receiverTrap1NoChange =
775                                      (priorReceivedTrap1Count == currentReceivedTrap1Count)))
776 yi.zhou     1.1              {
777                                   priorReceivedTrap1Count = currentReceivedTrap1Count;
778                              }
779                      
780                              if (!(receiverTrap2NoChange =
781                                      (priorReceivedTrap2Count == currentReceivedTrap2Count)))
782                              {
783                                   priorReceivedTrap2Count = currentReceivedTrap2Count;
784                              }
785                      
786                              if (receivedTrapCountComplete)
787                              {
788                                  cout << "++++ The trap receiver has received "
789                                  << currentReceivedTrap1Count << " of "
790                                  << indicationSendCountTotal << " SNMPv1 trap."
791                                  << endl;
792                                  cout << "++++ The trap receiver has received "
793                                  << currentReceivedTrap2Count << " of "
794                                  << indicationSendCountTotal << " SNMPv2c trap."
795                                  << endl;
796                      
797 yi.zhou     1.1                  break;
798                              }
799                              if (receiverTrap1NoChange || receiverTrap2NoChange)
800                              {
801                                 noChangeIterations++;
802                              }
803                              else
804                              {
805                                 noChangeIterations = 0;
806                              }
807                      
808                              System::sleep (SLEEP_SEC);
809                          }
810                      
811                          if (!receivedTrapCountComplete)
812                          {
813                              trapReceiverElapsedTime.stop();
814                          }
815                      
816                          // assert that all indications sent have been received. 
817                          PEGASUS_TEST_ASSERT(indicationSendCountTotal ==
818 yi.zhou     1.1             currentReceivedTrap1Count);
819                          PEGASUS_TEST_ASSERT(indicationSendCountTotal ==
820                             currentReceivedTrap2Count);
821 yi.zhou     1.2      }
822                      
823                      int _beginTest(CIMClient& workClient, 
824                          Uint32 indicationSendCount,
825                          Uint32 runClientThreadCount)
826                      {
827 yi.zhou     1.1      
828                      #ifdef PEGASUS_USE_NET_SNMP
829 yi.zhou     1.2      
830                          // Stop snmptrapd process if it is running
831                          _stopSnmptrapd();
832                      
833                          // if trapLogFile exists, remove it
834                          _removeTrapLogFile();
835                      
836                          FILE * trapInfo;
837                      
838                          try
839                          {
840                              _startSnmptrapd(&trapInfo);
841                          }
842                          catch (Exception & e)
843                          {
844                              cerr << e.getMessage() << endl;
845                              return (-1);
846                          }
847                      
848                          // Extended for all snmp implementation
849                          _receiveExpectedTraps(workClient, indicationSendCount, 
850 yi.zhou     1.2      	runClientThreadCount);
851                      
852 yi.zhou     1.1          // Stop snmptrapd process if it is running and remove procIdFile
853                          _stopSnmptrapd();
854                      
855                          pclose(trapInfo);
856                      
857                          // if error encountered then fail the test.
858                          if (errorsEncountered.get())
859                          {  
860                              cout << "+++++ test failed" << endl;
861                              return (-1);
862                          }
863                          else
864                          {
865                              cout << "+++++ passed all tests" << endl;
866                          }
867 yi.zhou     1.2      
868                          return (0);
869                      
870                      #else
871                          cerr << "Cannot create a trap receiver." << endl;
872                          return (-1);
873                      #endif
874 yi.zhou     1.1      }
875                      
876                      int main (int argc, char** argv)
877                      {
878                          // This client connection is used solely to create and delete subscriptions.
879                          CIMClient workClient;
880                          try
881                          {
882                              workClient.connectLocal();
883                      
884                              if (argc <= 1 || argc > 4)
885                              {
886                                  cerr << "Invalid argument count: " << argc << endl;
887                                  _usage();
888                                  return 1;
889                              }
890                              else if (strcmp(argv[1], "setup") == 0)
891                              {
892                                  if (argc < 3)
893                                  {
894                                      cerr << "Missing query language" << endl;
895 yi.zhou     1.1                      _usage();
896                                      return -1;
897                                  }
898                      
899                                  if ((strcmp(argv[2], "WQL") != 0) &&
900                                      (strcmp(argv[2], "DMTF:CQL") != 0))
901                                  {
902                                      cerr << "Invalid query language: '" << argv[2] << "'" << endl;
903                                      _usage();
904                                      return -1;
905                                  }
906                              
907                                  _setup(workClient, argv[2]);
908                      
909                                  cout << "+++++ setup completed successfully" << endl;
910                                  return 0;
911                              } 
912                              else if (String::equalNoCase(argv[1], "run"))
913                              {
914                                  if (argc < 3)
915                                  {
916 yi.zhou     1.1                      cerr << "Invalid indicationSendCount." << endl;
917                                      _usage ();
918                                      return -1;
919                                  }
920                      
921                                  Uint32 indicationSendCount = atoi(argv[2]);
922                      
923                                  Uint32 runClientThreadCount = 1;
924                      
925                                  if (argc == 4) 
926                                  {
927                                      runClientThreadCount = atoi(argv[3]);
928                                  }
929                      
930                                  int rc = _beginTest(workClient, indicationSendCount, 
931                                      runClientThreadCount);
932                                  return rc;
933                              } 
934                              else if (String::equalNoCase(argv[1], "cleanup"))
935                              {
936                                  if (argc > 2)
937 yi.zhou     1.1                  {
938                                      cerr << "Invalid argument count." << endl;
939                                      _usage ();
940                                      return -1;
941                                  }
942                             
943                                  _cleanup (workClient);
944                      
945                                  cout << "+++++ cleanup completed successfully" << endl;
946                                  return 0;
947                              }
948                              else if (String::equalNoCase(argv[1], "removelog"))
949                              {
950                                  if (argc > 2)
951                                  {
952                                      cerr << "Invalid argument count." << endl;
953                                      _usage ();
954                                      return -1;
955                                  }
956                             
957                                  _removeTrapLogFile ();
958 yi.zhou     1.1                  cout << "+++++ removelog completed successfully" << endl;
959                                  return 0;
960                              }
961                              else
962                              {
963                                  cerr << "Invalid option: " << argv[1] << endl;
964                                  _usage ();
965                                  return -1;
966                              }
967                          }
968                          catch (Exception & e)
969                          {
970                              cerr << "Error: " << e.getMessage() << endl;
971                          }
972                      
973                          PEGASUS_UNREACHABLE( return 0; )
974                      }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2