📄 testsnmphandler.cpp
字号:
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions:// // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.////==============================================================================//// Author: Yi Zhou, Hewlett-Packard Company (Yi.Zhou@hp.com) //// Modified By: ////%/////////////////////////////////////////////////////////////////////////////#include <Pegasus/Common/PegasusAssert.h>#include <Pegasus/Common/Thread.h>#include <Pegasus/Common/Constants.h>#include <Pegasus/Common/FileSystem.h>#include <Pegasus/Common/Stopwatch.h>#include <Pegasus/Client/CIMClient.h>PEGASUS_USING_PEGASUS;PEGASUS_USING_STD;// Interop namespace used with PEGASUS_NAMESPACENAME_INTEROP in Constants.hconst CIMNamespaceName SOURCE_NAMESPACE = CIMNamespaceName ("root/SampleProvider");const String INDICATION_CLASS_NAME = String ("RT_TestIndication");const String SNMPV1_HANDLER_NAME = String ("SNMPHandler01");const String SNMPV2C_HANDLER_NAME = String ("SNMPHandler02");const String FILTER_NAME = String ("IPFilter01");enum SNMPVersion {_SNMPV1_TRAP = 2, _SNMPV2C_TRAP = 3};enum TargetHostFormat {_HOST_NAME = 2, _IPV4_ADDRESS = 3};#define PORT_NUMBER 2006Uint32 indicationSendCountTotal = 0;AtomicInt errorsEncountered(0);//////////////////////////////////////////////////////////////////////////////////// Thread Parameters Class//////////////////////////////////////////////////////////////////////////////////class T_Parms{ public: AutoPtr<CIMClient> client; Uint32 indicationSendCount; Uint32 uniqueID;};///////////////////////////////////////////////////////////////////////////CIMObjectPath _getFilterObjectPath (const String & name){ Array<CIMKeyBinding> keyBindings; keyBindings.append (CIMKeyBinding ("SystemCreationClassName", System::getSystemCreationClassName (), CIMKeyBinding::STRING)); keyBindings.append (CIMKeyBinding ("SystemName", System::getFullyQualifiedHostName (), CIMKeyBinding::STRING)); keyBindings.append (CIMKeyBinding ("CreationClassName", PEGASUS_CLASSNAME_INDFILTER.getString(), CIMKeyBinding::STRING)); keyBindings.append (CIMKeyBinding ("Name", name, CIMKeyBinding::STRING)); return(CIMObjectPath("", CIMNamespaceName (), PEGASUS_CLASSNAME_INDFILTER, keyBindings));}CIMObjectPath _getHandlerObjectPath (const String & name){ Array<CIMKeyBinding> keyBindings; keyBindings.append (CIMKeyBinding ("SystemCreationClassName", System::getSystemCreationClassName (), CIMKeyBinding::STRING)); keyBindings.append (CIMKeyBinding ("SystemName", System::getFullyQualifiedHostName (), CIMKeyBinding::STRING)); keyBindings.append (CIMKeyBinding ("CreationClassName", PEGASUS_CLASSNAME_INDHANDLER_SNMP.getString(), CIMKeyBinding::STRING)); keyBindings.append (CIMKeyBinding ("Name", name, CIMKeyBinding::STRING)); return(CIMObjectPath("", CIMNamespaceName (), PEGASUS_CLASSNAME_INDHANDLER_SNMP, keyBindings));}CIMObjectPath _getSubscriptionObjectPath (const String & filterName, const String & handlerName){ CIMObjectPath filterObjectPath = _getFilterObjectPath(filterName); CIMObjectPath handlerObjectPath = _getHandlerObjectPath(handlerName); Array<CIMKeyBinding> subscriptionKeyBindings; subscriptionKeyBindings.append (CIMKeyBinding ("Filter", CIMValue(filterObjectPath))); subscriptionKeyBindings.append (CIMKeyBinding ("Handler", CIMValue(handlerObjectPath))); return(CIMObjectPath("", CIMNamespaceName (), PEGASUS_CLASSNAME_INDSUBSCRIPTION, subscriptionKeyBindings));}CIMObjectPath _createHandlerInstance (CIMClient & client, const String & name, const String & targetHost, const String & securityName, const Uint16 targetHostFormat, const Uint16 snmpVersion){ CIMInstance handlerInstance (PEGASUS_CLASSNAME_INDHANDLER_SNMP); handlerInstance.addProperty (CIMProperty (CIMName ("SystemCreationClassName"), System::getSystemCreationClassName ())); handlerInstance.addProperty (CIMProperty (CIMName ("SystemName"), System::getFullyQualifiedHostName ())); handlerInstance.addProperty (CIMProperty (CIMName ("CreationClassName"), PEGASUS_CLASSNAME_INDHANDLER_SNMP.getString ())); handlerInstance.addProperty (CIMProperty (CIMName ("Name"), name)); handlerInstance.addProperty (CIMProperty (CIMName ("TargetHost"), targetHost)); handlerInstance.addProperty (CIMProperty (CIMName ("TargetHostFormat"), CIMValue ((Uint16) targetHostFormat))); handlerInstance.addProperty (CIMProperty (CIMName ("SNMPSecurityName"), securityName)); handlerInstance.addProperty (CIMProperty (CIMName ("SnmpVersion"), CIMValue ((Uint16) snmpVersion))); handlerInstance.addProperty (CIMProperty (CIMName ("PortNumber"), CIMValue ((Uint32) PORT_NUMBER))); return(client.createInstance (PEGASUS_NAMESPACENAME_INTEROP, handlerInstance));}CIMObjectPath _createFilterInstance (CIMClient & client, const String & name, const String & query, const String & qlang){ CIMInstance filterInstance (PEGASUS_CLASSNAME_INDFILTER); filterInstance.addProperty (CIMProperty (CIMName ("SystemCreationClassName"), System::getSystemCreationClassName ())); filterInstance.addProperty (CIMProperty (CIMName ("SystemName"), System::getFullyQualifiedHostName ())); filterInstance.addProperty (CIMProperty (CIMName ("CreationClassName"), PEGASUS_CLASSNAME_INDFILTER.getString ())); filterInstance.addProperty (CIMProperty (CIMName ("Name"), name)); filterInstance.addProperty (CIMProperty (CIMName ("Query"), query)); filterInstance.addProperty (CIMProperty (CIMName ("QueryLanguage"), String (qlang))); filterInstance.addProperty (CIMProperty (CIMName ("SourceNamespace"), SOURCE_NAMESPACE.getString ())); return(client.createInstance (PEGASUS_NAMESPACENAME_INTEROP, filterInstance));}CIMObjectPath _createSubscriptionInstance (CIMClient & client, const CIMObjectPath & filterPath, const CIMObjectPath & handlerPath){ CIMInstance subscriptionInstance (PEGASUS_CLASSNAME_INDSUBSCRIPTION); subscriptionInstance.addProperty (CIMProperty (CIMName ("Filter"), filterPath, 0, PEGASUS_CLASSNAME_INDFILTER)); subscriptionInstance.addProperty (CIMProperty (CIMName ("Handler"), handlerPath, 0, PEGASUS_CLASSNAME_INDHANDLER_SNMP)); subscriptionInstance.addProperty (CIMProperty (CIMName ("SubscriptionState"), CIMValue ((Uint16) 2))); return(client.createInstance (PEGASUS_NAMESPACENAME_INTEROP, subscriptionInstance));}void _sendTestIndication(CIMClient* client, const CIMName & methodName, Uint32 indicationSendCount){ // // Invoke method to send test indication // Array <CIMParamValue> inParams; Array <CIMParamValue> outParams; Array <CIMKeyBinding> keyBindings; Sint32 result; CIMObjectPath className (String::EMPTY, CIMNamespaceName (), CIMName ("RT_TestIndication"), keyBindings); inParams.append(CIMParamValue(String("indicationSendCount"), CIMValue(indicationSendCount))); CIMValue retValue = client->invokeMethod (SOURCE_NAMESPACE, className, methodName, inParams, outParams); retValue.get (result); PEGASUS_TEST_ASSERT (result == 0);}void _deleteSubscriptionInstance (CIMClient & client, const String & filterName, const String & handlerName){ CIMObjectPath subscriptionObjectPath = _getSubscriptionObjectPath(filterName, handlerName); client.deleteInstance (PEGASUS_NAMESPACENAME_INTEROP, subscriptionObjectPath);}void _deleteHandlerInstance (CIMClient & client, const String & name){ CIMObjectPath handlerObjectPath = _getHandlerObjectPath(name); client.deleteInstance (PEGASUS_NAMESPACENAME_INTEROP, handlerObjectPath);}void _deleteFilterInstance (CIMClient & client, const String & name){ CIMObjectPath filterObjectPath = _getFilterObjectPath(name); client.deleteInstance (PEGASUS_NAMESPACENAME_INTEROP, filterObjectPath);}void _usage (){ cerr << endl << "Usage:" << endl << " TestSnmpHandler setup [ WQL | DMTF:CQL ]\n" << " TestSnmpHandler run <indicationSendCount> " << "[<threads>]\n" << " where: " << endl << " <indicationSendCount> is the number of indications to\n" << " generate and has to be greater than zero." << endl << " <threads> is an optional number of client threads to\n" << " create, default is one." << endl << " TestSnmpHandler cleanup\n" << " TestSnmpHandler removelog" << endl << endl;}void _setup (CIMClient & client, const String& qlang){ CIMObjectPath filterObjectPath; CIMObjectPath snmpv1HandlerObjectPath; CIMObjectPath snmpv2HandlerObjectPath; try { filterObjectPath = _createFilterInstance (client, FILTER_NAME, String ("SELECT * FROM RT_TestIndication"), qlang); } catch (CIMException& e) { if (e.getCode() == CIM_ERR_ALREADY_EXISTS) { filterObjectPath = _getFilterObjectPath(FILTER_NAME); cerr << "----- Warning: Filter Instance Not Created: " << e.getMessage () << endl; } else { cerr << "----- Error: Filter Instance Not Created: " << endl; throw; } } try { // Create SNMPv1 trap handler snmpv1HandlerObjectPath = _createHandlerInstance (client, SNMPV1_HANDLER_NAME, System::getFullyQualifiedHostName(), "", _HOST_NAME, _SNMPV1_TRAP); } catch (CIMException& e) { if (e.getCode() == CIM_ERR_ALREADY_EXISTS) { snmpv1HandlerObjectPath = _getHandlerObjectPath( SNMPV1_HANDLER_NAME); cerr << "----- Warning: SNMPv1 Trap Handler Instance Not Created: " << e.getMessage () << endl; } else { cerr << "----- Error: SNMPv1 Trap Handler Instance Not Created: " << endl; throw; } } try { _createSubscriptionInstance (client, filterObjectPath, snmpv1HandlerObjectPath); } catch (CIMException& e) { if (e.getCode() == CIM_ERR_ALREADY_EXISTS) { cerr << "----- Warning: Client Subscription Instance: " << e.getMessage () << endl; } else { cerr << "----- Error: Client Subscription Instance: " << endl; throw; } } try { // Create SNMPv2 trap handler snmpv2HandlerObjectPath = _createHandlerInstance (client, SNMPV2C_HANDLER_NAME, System::getHostIP(System::getFullyQualifiedHostName ()), "public", _IPV4_ADDRESS, _SNMPV2C_TRAP); } catch (CIMException& e) { if (e.getCode() == CIM_ERR_ALREADY_EXISTS) { snmpv2HandlerObjectPath = _getHandlerObjectPath( SNMPV2C_HANDLER_NAME); cerr << "----- Warning: SNMPv2c Trap Handler Instance Not Created: " << e.getMessage () << endl; } else { cerr << "----- Error: SNMPv2c Trap Handler Instance Not Created: " << endl; throw; } } try { _createSubscriptionInstance (client, filterObjectPath, snmpv2HandlerObjectPath); } catch (CIMException& e) { if (e.getCode() == CIM_ERR_ALREADY_EXISTS) { cerr << "----- Warning: Client Subscription Instance: " << e.getMessage () << endl; } else { cerr << "----- Error: Client Subscription Instance: " << endl; throw; } }}void _cleanup (CIMClient & client){ try { _deleteSubscriptionInstance (client, FILTER_NAME, SNMPV1_HANDLER_NAME); } catch (CIMException& e) { if (e.getCode() != CIM_ERR_NOT_FOUND) { cerr << "----- Error: deleteSubscriptionInstance failure: " << endl; throw; } } try { _deleteSubscriptionInstance (client, FILTER_NAME, SNMPV2C_HANDLER_NAME); } catch (CIMException& e) { if (e.getCode() != CIM_ERR_NOT_FOUND) { cerr << "----- Error: deleteSubscriptionInstance failure: " << endl; throw; } } try { _deleteFilterInstance (client, FILTER_NAME); } catch (CIMException& e) { if (e.getCode() != CIM_ERR_NOT_FOUND) { cerr << "----- Error: deleteFilterInstance failure: " << endl; throw; } } try { _deleteHandlerInstance (client, SNMPV1_HANDLER_NAME); } catch (CIMException& e) { if (e.getCode() != CIM_ERR_NOT_FOUND) { cerr << "----- Error: deleteHandlerInstance failure: " << endl; throw; } } try { _deleteHandlerInstance (client, SNMPV2C_HANDLER_NAME); } catch (CIMException& e) { if (e.getCode() != CIM_ERR_NOT_FOUND) { cerr << "----- Error: deleteHandlerInstance failure: " << endl; throw; } }}static void _testEnd(const String& uniqueID, const double elapsedTime){ cout << "+++++ thread" << uniqueID << ": passed in " << elapsedTime << " seconds" << endl;}ThreadReturnType PEGASUS_THREAD_CDECL _executeTests(void *parm){ Thread *my_thread = (Thread *)parm; T_Parms *parms = (T_Parms *)my_thread->get_parm(); CIMClient *client = parms->client.get(); Uint32 indicationSendCount = parms->indicationSendCount; Uint32 id = parms->uniqueID; char id_[4]; memset(id_,0x00,sizeof(id_)); sprintf(id_,"%i",id); String uniqueID = "_"; uniqueID.append(id_); try { Stopwatch elapsedTime; elapsedTime.start(); try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -