(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             
 45             PEGASUS_USING_PEGASUS;
 46             PEGASUS_USING_STD;
 47             
 48 karl    1.4 // Interop namespace used with PEGASUS_NAMESPACENAME_INTEROP in Constants.h
 49 yi.zhou 1.1 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             
 65             AtomicInt errorsEncountered(0);
 66             
 67             ////////////////////////////////////////////////////////////////////////////////
 68             //
 69             // Thread Parameters Class
 70 yi.zhou 1.1 //
 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                 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 yi.zhou 1.1         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                 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 yi.zhou 1.1         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                     CIMValue(handlerObjectPath)));
128                 return(CIMObjectPath("", CIMNamespaceName (),
129                     PEGASUS_CLASSNAME_INDSUBSCRIPTION, subscriptionKeyBindings));
130             }
131             
132             CIMObjectPath _createHandlerInstance
133 yi.zhou 1.1     (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                 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 yi.zhou 1.1     handlerInstance.addProperty (CIMProperty (CIMName ("SnmpVersion"),
155                     CIMValue ((Uint16) snmpVersion)));
156                 handlerInstance.addProperty (CIMProperty (CIMName ("PortNumber"),
157                     CIMValue ((Uint32) PORT_NUMBER)));
158             
159 karl    1.4     return(client.createInstance (PEGASUS_NAMESPACENAME_INTEROP, handlerInstance));
160 yi.zhou 1.1 }
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                 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 yi.zhou 1.1 
182 karl    1.4     return(client.createInstance (PEGASUS_NAMESPACENAME_INTEROP, filterInstance));
183 yi.zhou 1.1 }
184             
185             CIMObjectPath _createSubscriptionInstance
186                 (CIMClient & client,
187                  const CIMObjectPath & filterPath,
188                  const CIMObjectPath & handlerPath)
189             {
190                 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 karl    1.4     return(client.createInstance (PEGASUS_NAMESPACENAME_INTEROP, subscriptionInstance));
199 yi.zhou 1.1 }
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             
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 yi.zhou 1.1          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                  const String & handlerName)
233             {
234                 CIMObjectPath subscriptionObjectPath =
235                    _getSubscriptionObjectPath(filterName, handlerName);
236 karl    1.4     client.deleteInstance (PEGASUS_NAMESPACENAME_INTEROP, subscriptionObjectPath);
237 yi.zhou 1.1 }
238             
239             void _deleteHandlerInstance
240                 (CIMClient & client,
241                  const String & name)
242             {
243                 CIMObjectPath handlerObjectPath = _getHandlerObjectPath(name);
244 karl    1.4     client.deleteInstance (PEGASUS_NAMESPACENAME_INTEROP, handlerObjectPath);
245 yi.zhou 1.1 }
246             
247             void _deleteFilterInstance
248                 (CIMClient & client,
249                  const String & name)
250             {
251                 CIMObjectPath filterObjectPath = _getFilterObjectPath(name);
252 karl    1.4     client.deleteInstance (PEGASUS_NAMESPACENAME_INTEROP, filterObjectPath);
253 yi.zhou 1.1 }
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.1     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.1             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.1                 << 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.1         }
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.1         {
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.1         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.1     {
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.1     }
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.1         {
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.1     cout << "+++++ thread" << uniqueID << ": passed in " << elapsedTime
464                     << " seconds" << endl;
465             }
466             
467 mike    1.3 ThreadReturnType PEGASUS_THREAD_CDECL _executeTests(void *parm)
468 yi.zhou 1.1 {
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                     elapsedTime.start();
485                     try
486                     {
487                         _sendTestIndication (client, CIMName ("SendTestIndicationTrap"),
488                         indicationSendCount);
489 yi.zhou 1.1         }
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 mike    1.3     my_thread->exit_self((ThreadReturnType)1);
503 yi.zhou 1.1     return(0);
504             }
505             
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 yi.zhou 1.1 }
525             
526             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 yi.zhou 1.1             receivedTrap1Count++;
546                     }
547                     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 yi.zhou 1.1         {
567                         return (0);
568                     }
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 yi.zhou 1.1         kill(receiverPid, SIGTERM);
588             
589                     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 yi.zhou 1.1     // build snmptrapd cmd options
609                 //
610             
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 yi.zhou 1.1     }
630             
631             #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 yi.zhou 1.1 
651                 throw Exception ("snmptrapd can not be started");
652             }
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 yi.zhou 1.2 void _receiveExpectedTraps(CIMClient& workClient, 
667 yi.zhou 1.1     Uint32 indicationSendCount,
668                 Uint32 runClientThreadCount)
669             {
670             
671                 CIMClient * clientConnections = new CIMClient[runClientThreadCount];
672             
673                 // 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 marek   1.5     Uint32 testTimeout = PEGASUS_DEFAULT_CLIENT_TIMEOUT_MILLISECONDS
683                                     + (indicationSendCountTotal/MSG_PER_SEC)*1000;
684 yi.zhou 1.1 
685                 // connect the clients
686                 for(Uint32 i = 0; i < runClientThreadCount; i++)
687                 {
688                     clientConnections[i].setTimeout(testTimeout);
689                     clientConnections[i].connectLocal();
690                 }
691             
692                 // run tests
693                 Thread ** clientThreads = new Thread *[runClientThreadCount];
694             
695                 Stopwatch trapReceiverElapsedTime;
696             
697                 trapReceiverElapsedTime.start();
698             
699                 for(Uint32 i = 0; i < runClientThreadCount; i++)
700                 {
701                     clientThreads[i] = _runTestThreads(&clientConnections[i],
702                         indicationSendCount, i);
703                 }
704             
705 yi.zhou 1.1     for(Uint32 i=0; i< runClientThreadCount; i++)
706                 {
707                     clientThreads[i]->join();
708                 }
709             
710                 delete[] clientConnections;
711                 delete[] clientThreads;
712             
713                 //
714                 //  Allow time for the trap to be received
715                 //  Wait in SLEEP_SEC second intervals.
716                 //  Put msg out every MSG_SEC intervals
717                 //
718             
719             #define SLEEP_SEC 1
720             #define COUT_TIME_INTERVAL 30
721             #define MAX_NO_CHANGE_ITERATIONS COUT_TIME_INTERVAL*3
722             
723                 Uint32 noChangeIterations = 0;
724                 Uint32 priorReceivedTrap1Count = 0;
725                 Uint32 priorReceivedTrap2Count = 0;
726 yi.zhou 1.1     Uint32 currentReceivedTrap1Count = 0;
727                 Uint32 currentReceivedTrap2Count = 0;
728                 Uint32 totalIterations = 0;
729             
730                 //
731                 // Wait for the trap receiver to receive the expected
732                 // number of Indication traps, indicationSendCountTotal. 
733                 //
734                 // We will continue to wait until either indicationSendCountTotal
735                 // Indications have been received by the trap receiver or no new
736                 // Indications have been received in the previous
737                 // MAX_NO_CHANGE_ITERATIONS.
738                 // iterations. 
739                 //
740             
741                 Boolean receivedTrapCountComplete = false;
742                 Boolean receiverTrap1NoChange = true;
743                 Boolean receiverTrap2NoChange = true;
744             
745                 while (noChangeIterations <= MAX_NO_CHANGE_ITERATIONS)
746                 {
747 yi.zhou 1.1         totalIterations++;
748             
749                     currentReceivedTrap1Count = _getReceivedTrapCount(_SNMPV1_TRAP); 
750                     currentReceivedTrap2Count = _getReceivedTrapCount(_SNMPV2C_TRAP); 
751             
752                     if (totalIterations % COUT_TIME_INTERVAL == 1 &&
753                         !(receivedTrapCountComplete))
754                     {
755                         cout << "++++ The trap receiver has received "
756                         << currentReceivedTrap1Count << " of "
757                         << indicationSendCountTotal << " SNMPv1 trap."
758                         << endl;
759                         cout << "++++ The trap receiver has received "
760                         << currentReceivedTrap2Count << " of "
761                         << indicationSendCountTotal << " SNMPv2c trap."
762                         << endl;
763                     }
764             
765                     if ((indicationSendCountTotal == currentReceivedTrap1Count) &&
766                         (indicationSendCountTotal == currentReceivedTrap2Count))
767                     {
768 yi.zhou 1.1              receivedTrapCountComplete = true;
769                          trapReceiverElapsedTime.stop();
770                     }
771                     if (!(receiverTrap1NoChange =
772                             (priorReceivedTrap1Count == currentReceivedTrap1Count)))
773                     {
774                          priorReceivedTrap1Count = currentReceivedTrap1Count;
775                     }
776             
777                     if (!(receiverTrap2NoChange =
778                             (priorReceivedTrap2Count == currentReceivedTrap2Count)))
779                     {
780                          priorReceivedTrap2Count = currentReceivedTrap2Count;
781                     }
782             
783                     if (receivedTrapCountComplete)
784                     {
785                         cout << "++++ The trap receiver has received "
786                         << currentReceivedTrap1Count << " of "
787                         << indicationSendCountTotal << " SNMPv1 trap."
788                         << endl;
789 yi.zhou 1.1             cout << "++++ The trap receiver has received "
790                         << currentReceivedTrap2Count << " of "
791                         << indicationSendCountTotal << " SNMPv2c trap."
792                         << endl;
793             
794                         break;
795                     }
796                     if (receiverTrap1NoChange || receiverTrap2NoChange)
797                     {
798                        noChangeIterations++;
799                     }
800                     else
801                     {
802                        noChangeIterations = 0;
803                     }
804             
805                     System::sleep (SLEEP_SEC);
806                 }
807             
808                 if (!receivedTrapCountComplete)
809                 {
810 yi.zhou 1.1         trapReceiverElapsedTime.stop();
811                 }
812             
813                 // assert that all indications sent have been received. 
814                 PEGASUS_TEST_ASSERT(indicationSendCountTotal ==
815                    currentReceivedTrap1Count);
816                 PEGASUS_TEST_ASSERT(indicationSendCountTotal ==
817                    currentReceivedTrap2Count);
818 yi.zhou 1.2 }
819             
820             int _beginTest(CIMClient& workClient, 
821                 Uint32 indicationSendCount,
822                 Uint32 runClientThreadCount)
823             {
824 yi.zhou 1.1 
825             #ifdef PEGASUS_USE_NET_SNMP
826 yi.zhou 1.2 
827                 // Stop snmptrapd process if it is running
828                 _stopSnmptrapd();
829             
830                 // if trapLogFile exists, remove it
831                 _removeTrapLogFile();
832             
833                 FILE * trapInfo;
834             
835                 try
836                 {
837                     _startSnmptrapd(&trapInfo);
838                 }
839                 catch (Exception & e)
840                 {
841                     cerr << e.getMessage() << endl;
842                     return (-1);
843                 }
844             
845                 // Extended for all snmp implementation
846                 _receiveExpectedTraps(workClient, indicationSendCount, 
847 yi.zhou 1.2 	runClientThreadCount);
848             
849 yi.zhou 1.1     // Stop snmptrapd process if it is running and remove procIdFile
850                 _stopSnmptrapd();
851             
852                 pclose(trapInfo);
853             
854                 // if error encountered then fail the test.
855                 if (errorsEncountered.get())
856                 {  
857                     cout << "+++++ test failed" << endl;
858                     return (-1);
859                 }
860                 else
861                 {
862                     cout << "+++++ passed all tests" << endl;
863                 }
864 yi.zhou 1.2 
865                 return (0);
866             
867             #else
868                 cerr << "Cannot create a trap receiver." << endl;
869                 return (-1);
870             #endif
871 yi.zhou 1.1 }
872             
873             int main (int argc, char** argv)
874             {
875                 // This client connection is used solely to create and delete subscriptions.
876                 CIMClient workClient;
877                 try
878                 {
879                     workClient.connectLocal();
880             
881                     if (argc <= 1 || argc > 4)
882                     {
883                         cerr << "Invalid argument count: " << argc << endl;
884                         _usage();
885                         return 1;
886                     }
887                     else if (strcmp(argv[1], "setup") == 0)
888                     {
889                         if (argc < 3)
890                         {
891                             cerr << "Missing query language" << endl;
892 yi.zhou 1.1                 _usage();
893                             return -1;
894                         }
895             
896                         if ((strcmp(argv[2], "WQL") != 0) &&
897                             (strcmp(argv[2], "DMTF:CQL") != 0))
898                         {
899                             cerr << "Invalid query language: '" << argv[2] << "'" << endl;
900                             _usage();
901                             return -1;
902                         }
903                     
904                         _setup(workClient, argv[2]);
905             
906                         cout << "+++++ setup completed successfully" << endl;
907                         return 0;
908                     } 
909                     else if (String::equalNoCase(argv[1], "run"))
910                     {
911                         if (argc < 3)
912                         {
913 yi.zhou 1.1                 cerr << "Invalid indicationSendCount." << endl;
914                             _usage ();
915                             return -1;
916                         }
917             
918                         Uint32 indicationSendCount = atoi(argv[2]);
919             
920                         Uint32 runClientThreadCount = 1;
921             
922                         if (argc == 4) 
923                         {
924                             runClientThreadCount = atoi(argv[3]);
925                         }
926             
927                         int rc = _beginTest(workClient, indicationSendCount, 
928                             runClientThreadCount);
929                         return rc;
930                     } 
931                     else if (String::equalNoCase(argv[1], "cleanup"))
932                     {
933                         if (argc > 2)
934 yi.zhou 1.1             {
935                             cerr << "Invalid argument count." << endl;
936                             _usage ();
937                             return -1;
938                         }
939                    
940                         _cleanup (workClient);
941             
942                         cout << "+++++ cleanup completed successfully" << endl;
943                         return 0;
944                     }
945                     else if (String::equalNoCase(argv[1], "removelog"))
946                     {
947                         if (argc > 2)
948                         {
949                             cerr << "Invalid argument count." << endl;
950                             _usage ();
951                             return -1;
952                         }
953                    
954                         _removeTrapLogFile ();
955 yi.zhou 1.1             cout << "+++++ removelog completed successfully" << endl;
956                         return 0;
957                     }
958                     else
959                     {
960                         cerr << "Invalid option: " << argv[1] << endl;
961                         _usage ();
962                         return -1;
963                     }
964                 }
965                 catch (Exception & e)
966                 {
967                     cerr << "Error: " << e.getMessage() << endl;
968                 }
969             
970                 PEGASUS_UNREACHABLE( return 0; )
971             }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2