📄 testsnmphandler.cpp
字号:
_sendTestIndication (client, CIMName ("SendTestIndicationTrap"), indicationSendCount); } catch (Exception & e) { cerr << "----- sendTestIndication failed: " << e.getMessage () << endl; exit (-1); } elapsedTime.stop(); _testEnd(uniqueID, elapsedTime.getElapsed()); } catch(Exception & e) { cout << e.getMessage() << endl; } my_thread->exit_self((ThreadReturnType)1); return(0);}Thread * _runTestThreads( CIMClient* client, Uint32 indicationSendCount, Uint32 uniqueID){ // package parameters, create thread and run... AutoPtr<T_Parms> parms(new T_Parms()); parms->client.reset(client); parms->indicationSendCount = indicationSendCount; parms->uniqueID = uniqueID; AutoPtr<Thread> t(new Thread(_executeTests, (void*)parms.release(), false)); t->run(); return t.release();}String _getLogFile(){ return("trapLogFile");}Uint32 _getReceivedTrapCount(Uint16 snmpVersion){ String trap1 = "Trap Info: TRAP, SNMP v1, community public"; String trap2 = "Trap Info: TRAP2, SNMP v2c, community public"; Uint32 receivedTrap1Count = 0; Uint32 receivedTrap2Count = 0; ifstream ifs(_getLogFile().getCString()); if (!ifs) { return (0); } String line; while (GetLine(ifs, line)) { if (String::compare(line, trap1) == 0) { receivedTrap1Count++; } if (String::compare(line, trap2) == 0) { receivedTrap2Count++; } } ifs.close(); switch (snmpVersion) { case _SNMPV1_TRAP: { return (receivedTrap1Count); } case _SNMPV2C_TRAP: { return (receivedTrap2Count); } default: { return (0); } }}#ifdef PEGASUS_USE_NET_SNMP// Stop snmptrapd process if it is running and remove // procIdFile file if it exists// void _stopSnmptrapd(){ String procIdFileName = "procIdFile"; Uint32 receiverPid; FILE *fd; if ((fd = fopen(procIdFileName.getCString(), "r")) != NULL) { fscanf(fd, "%d\n", &receiverPid); kill(receiverPid, SIGTERM); fclose(fd); } if (FileSystem::exists(procIdFileName)) { FileSystem::removeFile(procIdFileName); }}static Boolean _startSnmptrapd( FILE **trapInfo){ String snmptrapdCmd; Uint32 portNumber = PORT_NUMBER; char portNumberStr[32]; sprintf(portNumberStr, "%lu", (unsigned long) portNumber); // // build snmptrapd cmd options // // Specify logging incoming traps to trapLogFile // Save the process ID of the snmptrapd in procIdFile snmptrapdCmd.append( "/usr/sbin/snmptrapd -f -Lf trapLogFile -p procIdFile"); // Specify incoming trap format snmptrapdCmd.append( " -F \"\nTrap Info: %P\nVariable: %v\n\""); // Specify listening address snmptrapdCmd.append(" UDP:"); snmptrapdCmd.append(System::getFullyQualifiedHostName ()); snmptrapdCmd.append(":"); snmptrapdCmd.append(portNumberStr); if ((*trapInfo = popen(snmptrapdCmd.getCString(), "r")) == NULL) { throw Exception ("snmptrapd can not be started"); }#define MAX_ITERATIONS 300#define SLEEP_SEC 1 Uint32 iterations = 0; // Wait until snmptrapd startted while (iterations < MAX_ITERATIONS) { iterations++; if (FileSystem::exists("procIdFile")) { return (true); } else { System::sleep(SLEEP_SEC); } } throw Exception ("snmptrapd can not be started");}#endifvoid _removeTrapLogFile (){ String logFile = _getLogFile(); // if trapLogFile exists, remove it if (FileSystem::exists(logFile)) { FileSystem::removeFile(logFile); }}void _receiveExpectedTraps(CIMClient& workClient, Uint32 indicationSendCount, Uint32 runClientThreadCount){ CIMClient * clientConnections = new CIMClient[runClientThreadCount]; // determine total number of indication send count indicationSendCountTotal = indicationSendCount * runClientThreadCount; // calculate the timeout based on the total send count allowing // using the MSG_PER_SEC rate // allow 20 seconds of test overhead for very small tests #define MSG_PER_SEC 4 Uint32 testTimeout = 20000+(indicationSendCountTotal/MSG_PER_SEC)*1000; // connect the clients for(Uint32 i = 0; i < runClientThreadCount; i++) { clientConnections[i].setTimeout(testTimeout); clientConnections[i].connectLocal(); } // run tests Thread ** clientThreads = new Thread *[runClientThreadCount]; Stopwatch trapReceiverElapsedTime; trapReceiverElapsedTime.start(); for(Uint32 i = 0; i < runClientThreadCount; i++) { clientThreads[i] = _runTestThreads(&clientConnections[i], indicationSendCount, i); } for(Uint32 i=0; i< runClientThreadCount; i++) { clientThreads[i]->join(); } delete[] clientConnections; delete[] clientThreads; // // Allow time for the trap to be received // Wait in SLEEP_SEC second intervals. // Put msg out every MSG_SEC intervals //#define SLEEP_SEC 1#define COUT_TIME_INTERVAL 30#define MAX_NO_CHANGE_ITERATIONS COUT_TIME_INTERVAL*3 Uint32 noChangeIterations = 0; Uint32 priorReceivedTrap1Count = 0; Uint32 priorReceivedTrap2Count = 0; Uint32 currentReceivedTrap1Count = 0; Uint32 currentReceivedTrap2Count = 0; Uint32 totalIterations = 0; // // Wait for the trap receiver to receive the expected // number of Indication traps, indicationSendCountTotal. // // We will continue to wait until either indicationSendCountTotal // Indications have been received by the trap receiver or no new // Indications have been received in the previous // MAX_NO_CHANGE_ITERATIONS. // iterations. // Boolean receivedTrapCountComplete = false; Boolean receiverTrap1NoChange = true; Boolean receiverTrap2NoChange = true; while (noChangeIterations <= MAX_NO_CHANGE_ITERATIONS) { totalIterations++; currentReceivedTrap1Count = _getReceivedTrapCount(_SNMPV1_TRAP); currentReceivedTrap2Count = _getReceivedTrapCount(_SNMPV2C_TRAP); if (totalIterations % COUT_TIME_INTERVAL == 1 && !(receivedTrapCountComplete)) { cout << "++++ The trap receiver has received " << currentReceivedTrap1Count << " of " << indicationSendCountTotal << " SNMPv1 trap." << endl; cout << "++++ The trap receiver has received " << currentReceivedTrap2Count << " of " << indicationSendCountTotal << " SNMPv2c trap." << endl; } if ((indicationSendCountTotal == currentReceivedTrap1Count) && (indicationSendCountTotal == currentReceivedTrap2Count)) { receivedTrapCountComplete = true; trapReceiverElapsedTime.stop(); } if (!(receiverTrap1NoChange = (priorReceivedTrap1Count == currentReceivedTrap1Count))) { priorReceivedTrap1Count = currentReceivedTrap1Count; } if (!(receiverTrap2NoChange = (priorReceivedTrap2Count == currentReceivedTrap2Count))) { priorReceivedTrap2Count = currentReceivedTrap2Count; } if (receivedTrapCountComplete) { cout << "++++ The trap receiver has received " << currentReceivedTrap1Count << " of " << indicationSendCountTotal << " SNMPv1 trap." << endl; cout << "++++ The trap receiver has received " << currentReceivedTrap2Count << " of " << indicationSendCountTotal << " SNMPv2c trap." << endl; break; } if (receiverTrap1NoChange || receiverTrap2NoChange) { noChangeIterations++; } else { noChangeIterations = 0; } System::sleep (SLEEP_SEC); } if (!receivedTrapCountComplete) { trapReceiverElapsedTime.stop(); } // assert that all indications sent have been received. PEGASUS_TEST_ASSERT(indicationSendCountTotal == currentReceivedTrap1Count); PEGASUS_TEST_ASSERT(indicationSendCountTotal == currentReceivedTrap2Count);}int _beginTest(CIMClient& workClient, Uint32 indicationSendCount, Uint32 runClientThreadCount){#ifdef PEGASUS_USE_NET_SNMP // Stop snmptrapd process if it is running _stopSnmptrapd(); // if trapLogFile exists, remove it _removeTrapLogFile(); FILE * trapInfo; try { _startSnmptrapd(&trapInfo); } catch (Exception & e) { cerr << e.getMessage() << endl; return (-1); } // Extended for all snmp implementation _receiveExpectedTraps(workClient, indicationSendCount, runClientThreadCount); // Stop snmptrapd process if it is running and remove procIdFile _stopSnmptrapd(); pclose(trapInfo); // if error encountered then fail the test. if (errorsEncountered.get()) { cout << "+++++ test failed" << endl; return (-1); } else { cout << "+++++ passed all tests" << endl; } return (0);#else cerr << "Cannot create a trap receiver." << endl; return (-1);#endif}int main (int argc, char** argv){ // This client connection is used solely to create and delete subscriptions. CIMClient workClient; try { workClient.connectLocal(); if (argc <= 1 || argc > 4) { cerr << "Invalid argument count: " << argc << endl; _usage(); return 1; } else if (strcmp(argv[1], "setup") == 0) { if (argc < 3) { cerr << "Missing query language" << endl; _usage(); return -1; } if ((strcmp(argv[2], "WQL") != 0) && (strcmp(argv[2], "DMTF:CQL") != 0)) { cerr << "Invalid query language: '" << argv[2] << "'" << endl; _usage(); return -1; } _setup(workClient, argv[2]); cout << "+++++ setup completed successfully" << endl; return 0; } else if (String::equalNoCase(argv[1], "run")) { if (argc < 3) { cerr << "Invalid indicationSendCount." << endl; _usage (); return -1; } Uint32 indicationSendCount = atoi(argv[2]); Uint32 runClientThreadCount = 1; if (argc == 4) { runClientThreadCount = atoi(argv[3]); } int rc = _beginTest(workClient, indicationSendCount, runClientThreadCount); return rc; } else if (String::equalNoCase(argv[1], "cleanup")) { if (argc > 2) { cerr << "Invalid argument count." << endl; _usage (); return -1; } _cleanup (workClient); cout << "+++++ cleanup completed successfully" << endl; return 0; } else if (String::equalNoCase(argv[1], "removelog")) { if (argc > 2) { cerr << "Invalid argument count." << endl; _usage (); return -1; } _removeTrapLogFile (); cout << "+++++ removelog completed successfully" << endl; return 0; } else { cerr << "Invalid option: " << argv[1] << endl; _usage (); return -1; } } catch (Exception & e) { cerr << "Error: " << e.getMessage() << endl; } PEGASUS_UNREACHABLE( return 0; )}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -