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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2