/**
    MAIN
*/
int main(int argc, char** argv)
{
    OptionManager om;
    TestWrapperStressClient wc;
    pid_t clientPid = getpid();
    Uint32 validArg = 0;
    Boolean verboseTest;

    //
    // Varriables needed for loging and status checking.
    //
    String clientId;
    String pidFile;
    String clientLog, stopClient;
    char pid_str[15];
    int status = CLIENT_UNKNOWN;

    //
    // Number of times the client command succeeded.
    //
    Uint32 successCount = 0;

    //
    // Number of iterations after which logErrorPercentage() is called.
    //
    Uint32 iteration = 0;

    //
    // Total number of times the client command was executed.
    //
    Uint32 totalCount = 0;

    //
    // Variables needed for Command operation.
    //
    String command;

    String options;
    String help;

    try
    {
        struct OptionRow *newOptionsTable = 0;
        Uint32 newOptionsCount;

        struct OptionRow cOptionTable[] =
        {
            {"clientname", "", true, Option::STRING, 0, 0, "clientname",
                "Client name" },

            {"options", "", true, Option::STRING, 0, 0, "options",
                "Corresponding Client program's options" }
        };

        Uint32 cOptionCount = sizeof(cOptionTable) / sizeof(cOptionTable[0]);
        newOptionsCount = cOptionCount;

        try
        {
            //
            // Generate new option table for this client using OptionManager
            //
            newOptionsTable = wc.generateClientOptions(
                                  cOptionTable,
                                  cOptionCount,
                                  newOptionsCount);
            validArg = wc.GetOptions(
                           om,
                           argc,
                           argv,
                           newOptionsTable,
                           newOptionsCount);
        }
        catch (Exception& e)
        {
            cerr << argv[0] << ": " << e.getMessage() << endl;
            exit(1);
        }
        catch (...)
        {
            cerr << argv[0] << ": Error in Options operations "<< endl;
            exit(1);
        }

        verboseTest = om.isTrue("verbose");

        om.lookupValue("clientid", clientId);

        om.lookupValue("pidfile", pidFile);

        om.lookupValue("clientlog", clientLog);

        om.lookupValue("clientname", command);

        om.lookupValue("options", options);

        om.lookupValue("help", help);
    } /** end of option Try block. */
    catch (Exception& e)
    {
        cerr << argv[0] << ": " << e.getMessage() <<endl;
        exit(1);
    }
    catch (...)
    {
        cerr << argv[0] << ": Unknown Error gathering options "
             << "in Wrapper Client " << endl;
        exit(1);
    }

    /** Checking whether the user asked for HELP Info...
    if (om.valueEquals("help", "true"))
    {
        String header = "Usage ";
        header.append(argv[0]);
        header.append(" -parameters -clientName [clientName]
        header.append(" -options [options] -clientid [clientId] ");
        header.append(" -pidfile [pidFile] -clientlog [clientLog]");
        String trailer = "Assumes localhost:5988 if host not specified";
        trailer.append("\nHost may be of the form name or name:port");
        trailer.append("\nPort 5988 assumed if port number missing.");
        om.printOptionsHelpTxt(header, trailer);
        exit(0);
    }
    */

    try
    {
        if (options.getCString())
        {
            command.append(" " + options);
        }
        if (verboseTest)
        {
            errorInfo.append("client command :  ");
            errorInfo.append(command);
            wc.errorLog(clientPid, clientLog, errorInfo);
            errorInfo.clear();
        }

        //
        // Signal Handling - SIGINT.
        //
        signal(SIGINT, endTest);

        //
        // Timer Start.
        //
        wc.startTime();

        wc.logInfo(clientId, clientPid, status, pidFile);
        sprintf(pid_str, "%d", clientPid);

        stopClient = String::EMPTY;
        stopClient.append(FileSystem::extractFilePath(pidFile));
        stopClient.append("STOP_");
        stopClient.append(pid_str);

        //
        // This loop executes till the client gets stop signal from
        // controller.
        //
        while (!quit)
        {
            if (FileSystem::exists(stopClient))
            {
                if (verboseTest)
                {
                    String mes("Ending client. ");
                    wc.errorLog(clientPid, clientLog, mes);
                }
                break;
            }

                if (!verboseTest)
                {
#ifdef PEGASUS_OS_TYPE_WINDOWS
                    freopen("nul","w",stdout);
#else
                    FILE * file = freopen("/dev/null","w",stdout);
                    if (0 == file)
                    {
                        // ignore possible error, not having stdout redirected
                        // to /dev/null doesn't hurt the test
                        continue;
                    }
#endif
                }

            int i = system(command.getCString());

            iteration++;
            totalCount++;
            switch (i)
            {
                case 0:
                    if (status != CLIENT_PASS)
                    {
                        status = CLIENT_PASS;
                        wc.logInfo(clientId, clientPid, status, pidFile);
                    }
                    successCount++;
                    break;

                case 1:
                    status = CLIENT_UNKNOWN;
                    wc.logInfo(clientId, clientPid, status, pidFile);
                    break;

                default:
                    status = CLIENT_FAIL;
                    wc.logInfo(clientId, clientPid, status, pidFile);
                    break;
            }

            nextCheck = wc.checkTime();
            if (nextCheck)
            {
                wc.logInfo(clientId, clientPid, status, pidFile);
                nextCheck = false;
            }

            //
            // If verbose is set, log success percentage for every 100
            // iterations.  If verbose is not set, log success percentage
            // for every 1000 iterations.
            //
            if (verboseTest)
            {
                if (iteration == 100)
                {
                    wc.logErrorPercentage(
                        successCount,
                        totalCount,
                        clientPid,
                        clientLog,
                        thisClient);
                    iteration = 0;
                }
            }
            else
            {
                if (iteration == 1000)
                {
                    wc.logErrorPercentage(
                        successCount,
                        totalCount,
                        clientPid,
                        clientLog,
                        thisClient);
                    iteration = 0;
                }
            }
        } /** end of while(!quit). */
    } /** end of command execution try block. */
    catch (Exception &exp)
    {
        String expStr("Exception in WrapperClient causing it to exit: ");
        expStr.append(exp.getMessage());
        wc.errorLog(clientPid, clientLog, expStr);

        if (verboseTest)
        {
            cerr << expStr.getCString() << endl;
        }
    }
    catch (...)
    {
        String expStr("General Exception in WrapperClient causing it to exit");
        wc.errorLog(clientPid, clientLog, expStr);

        if (verboseTest)
        {
            cerr << expStr.getCString() << endl;
        }
    }

//
// second delay before shutdown.
//
#ifndef PEGASUS_OS_TYPE_WINDOWS
    sleep(1);
#else
    Sleep(1000);
#endif

    if(FileSystem::exists(stopClient))
    {
        //
        // Remove STOP file here.
        //
        FileSystem::removeFile(stopClient);
    }
    if (verboseTest)
    {
        errorInfo.append(
            "++++ TestWrapperStressClient Terminated Normally +++++");
        wc.errorLog(clientPid, clientLog, errorInfo);
        errorInfo.clear();
    }
    return 0;
}
Exemplo n.º 2
0
int main(int argc, char** argv)
{
    OptionManager om;
    TestModelWalkStressClient tmsc;
    Uint32 validArg = 0;
    struct OptionRow *newOptionsTable = 0;
    Uint32 newOptionsCount = 0;
    Uint32 cOptionCount =  0;

    //
    // Variables need to connect to server. 
    //
    Boolean useSSL;
    String host;
    Uint32 portNumber = 0;
    String portStr;
    String userName;
    String password;
    String help;
    Boolean connectedToHost = false;

    //
    // Variables needed to do loging and status checking.
    //
    String pidFile;
    String clientId;
    pid_t clientPid;
    String clientLog, stopClient;
    char pid_str[15];

    //
    // Number of times the command succeeded. 
    //
    Uint32 successCount=0;

    //
    // Number of iterations after which logErrorPercentage() is called.
    //
    Uint32 iteration=0;

    //
    // Total number of times the command was executed.
    //
    Uint32 totalCount=0;

    //
    // timeout.
    //
    Uint32 timeout = 30000;

    //
    // This try block includes all the options gathering function.
    //
    try
    {
        //
        // client specific options if any.
        //
        struct OptionRow *cOptionTable = 0; 
        newOptionsCount = cOptionCount;

        try
        {
            //
            // Generate new option table for this client using
            // the OptionManager.
            //
            
            newOptionsTable = tmsc.generateClientOptions(cOptionTable,
                cOptionCount,newOptionsCount);
            validArg = tmsc.GetOptions(om, argc, argv, newOptionsTable,
                newOptionsCount);
        }
        catch (Exception& e)
        {
            PEGASUS_STD(cerr) << argv[0] << ": " << e.getMessage()
                              << PEGASUS_STD(endl);
            exit(1);
        }
        catch (...)
        {
            PEGASUS_STD(cerr) << argv[0] << ": Error in Options operation "
                              << PEGASUS_STD(endl);
            exit(1);
        }

        verboseTest = om.isTrue("verbose");

        om.lookupValue("username", userName);

        om.lookupValue("password", password);

        om.lookupIntegerValue("timeout",timeout);

        useSSL =  om.isTrue("ssl");

        om.lookupValue("hostname",host);
        om.lookupValue("port",portStr);

        if (portStr.getCString())
        {
            sscanf (portStr.getCString (), "%u", &portNumber);
        }
        
        //
        // Setting default ports.
        //
        if (!portNumber)
        {
            if (useSSL)
            {
                portNumber = 5989;
            }
            else
            {
                portNumber = 5988;
            }
        }

        //
        // default host is localhost.
        //
        if (host == String::EMPTY)
        {
            host = String("localhost");
        }

        //
        // Signal Handling - SIGINT
        //
        signal(SIGINT, endTest);

        om.lookupValue("clientid", clientId);

        om.lookupValue("pidfile", pidFile);

        clientPid = getpid();

        om.lookupValue("clientlog", clientLog);

        om.lookupValue("help", help);

    }// end of option Try block
    catch (Exception& e)
    {
        PEGASUS_STD(cerr) << argv[0] << ": " << e.getMessage()
                          << PEGASUS_STD(endl);
        exit(1);
    }
    catch (...)
    {
        PEGASUS_STD(cerr) << argv[0] << ": Unknown Error gathering options "
                          << PEGASUS_STD(endl);
        exit(1);
    }

    /* Checking whether the user asked for HELP Info...
    if (om.valueEquals("help", "true"))
    {
        String header = "Usage ";
        header.append(argv[0]);
        header.append(" -parameters -n [namespace] -c [classname] ");
        header.append(" -clientId [clientId] -pidFile [pidFile] ");
        header.append(" -clientLog [clientLog]");
        String trailer = "Assumes localhost:5988 if host not specified";
        trailer.append("\nHost may be of the form name or name:port");
        trailer.append("\nPort 5988 assumed if port number missing.");
        om.printOptionsHelpTxt(header, trailer);
        exit(0);
    } */

    try
    {
        //
        // Timer Start.
        //
        tmsc.startTime();

        tmsc.logInfo(clientId, clientPid, globalStatus, pidFile);

        //
        // connect the client.
        //
        CIMClient* client = new CIMClient();

        sprintf(pid_str, "%d", clientPid);

        stopClient = String::EMPTY;
        stopClient.append(FileSystem::extractFilePath(pidFile));
        stopClient.append("STOP_");
        stopClient.append(pid_str);

        //
        // This loop executes till the client gets stop signal from
        // controller.
        //
        while (!quit)
        {
            if (FileSystem::exists(stopClient))
            {
                if (verboseTest)
                {
                    String mes("Ending client. ");
                    tmsc.errorLog(clientPid, clientLog, mes); 
                }
                break;
            }

                if (!verboseTest)
                {
#ifdef PEGASUS_OS_TYPE_WINDOWS
                    freopen("nul","w",stdout);
#else 
                    freopen("/dev/null","w",stdout);
#endif
                }

            if (!connectedToHost)
            {
                try
                {
                    tmsc.connectClient( client, host, portNumber, userName,
                        password, useSSL, timeout, verboseTest);
                    connectedToHost = true;
                    
                   //
                   // Client has successfully connected to server.
                   // update status if previously not Success.
                   //
                   if (globalStatus != CLIENT_PASS)
                   {
                       globalStatus = CLIENT_PASS;
                       tmsc.logInfo(clientId, clientPid, globalStatus, pidFile);
                   }
                }
                catch (CannotConnectException)
                {
                    globalStatus = CLIENT_UNKNOWN;
                    tmsc.logInfo(clientId, clientPid, globalStatus, pidFile);
                    connectedToHost = false;
                }
                catch (CIMException &e)
                {
                    globalStatus = CLIENT_UNKNOWN;
                    tmsc.logInfo(clientId, clientPid, globalStatus, pidFile);
                    connectedToHost = false;
                    String mes(e.getMessage());
                    tmsc.errorLog(clientPid, clientLog, mes);
                }
                catch (Exception &e)
                {
                    globalStatus = CLIENT_UNKNOWN;
                    tmsc.logInfo(clientId, clientPid, globalStatus, pidFile);
                    connectedToHost = false;
                    String mes(e.getMessage());
                    tmsc.errorLog(clientPid, clientLog, mes);
                }
                catch (...)
                {
                    globalStatus = CLIENT_UNKNOWN;
                    tmsc.logInfo(clientId, clientPid, globalStatus, pidFile);
                    connectedToHost = false;
                    String mes("Error connencting to server in ModleWalk");
                    mes.append(" client ");
                    tmsc.errorLog(clientPid, clientLog,mes);
                }
            }/* end of if (!connectedToHost). */
            iteration++;
            totalCount++;
            if (connectedToHost)
            {
                try
                {
                    Array<CIMNamespaceName> nameSpacesArray;

                    //
                    // Enumerate all the namespaces here.
                    //
                    nameSpacesArray = getNameSpaces(
                                          tmsc,
                                          client,
                                          om,
                                          clientPid,
                                          clientLog,
                                          clientId,
                                          globalStatus,
                                          pidFile);

                    //
                    // Enumerate all qualifiers in the namespaces.
                    //
                    enumerateAllQualifiers(
                        tmsc,
                        client, 
                        nameSpacesArray,
                        clientPid,
                        clientLog,
                        clientId,
                        globalStatus,
                        pidFile);

                    //
                    // Enumerate all the class related info here.
                    //
                    enumerateClassRelatedInfo(
                        tmsc,
                        client,
                        om,
                        nameSpacesArray,
                        clientPid,
                        clientLog,
                        clientId,
                        globalStatus,
                        pidFile);

                    successCount++;
                }
                //
                // This specail catch block in needed so that we will know if
                // the connenction was lost. We then connect on the next time
                // through the loop.
                //
                
                catch (CannotConnectException)
                {
                    globalStatus = CLIENT_UNKNOWN;
                    tmsc.logInfo(clientId, clientPid, globalStatus, pidFile);
                    connectedToHost = false;
                }
                catch (CIMException &cimE)
                {
                    globalStatus = CLIENT_FAIL;
                    tmsc.logInfo(clientId, clientPid, globalStatus, pidFile);
                    String mes(cimE.getMessage());
                    tmsc.errorLog(clientPid, clientLog, mes);
                }
                catch (Exception &exp)
                {
                    globalStatus = CLIENT_FAIL;
                    tmsc.logInfo(clientId, clientPid, globalStatus, pidFile);
                    String mes(exp.getMessage());
                    tmsc.errorLog(clientPid, clientLog, mes);
                }
                catch (...)
                {
                    globalStatus = CLIENT_FAIL;
                    tmsc.logInfo(clientId, clientPid, globalStatus, pidFile);
                    String mes("Unknown Error during ModelWalk Execution");
                    tmsc.errorLog(clientPid, clientLog, mes);
                }

                nextCheck = tmsc.checkTime();
                if (nextCheck)
                {
                    tmsc.logInfo(clientId, clientPid, globalStatus, pidFile);
                    nextCheck = false;
                }

                //
                // If verbose is set, log success percentage for every 100
                // iterations. If verbose is not set, log success percentage
                // for every 10000 iterations.
                //
                if (verboseTest)
                {
                    if (iteration == 100)
                    {
                        tmsc.logErrorPercentage(
                            successCount, 
                            totalCount, 
                            clientPid,
                            clientLog,
                            clientName);
                        iteration = 0;
                    }
                }
                else
                {
                    if (iteration == 1000)
                    {
                        tmsc.logErrorPercentage(
                            successCount,
                            totalCount,
                            clientPid,
                            clientLog,
                            clientName);
                        iteration = 0;
                    }
                }
            }
        } // end of while (!quit).
    }
    catch (Exception &exp)
    {
        String expStr("Exception in ModelWalk client, causing it to exit: ");
        expStr.append(exp.getMessage());
        tmsc.errorLog(clientPid, clientLog, expStr);

        if (verboseTest)
        {
            PEGASUS_STD(cerr) << expStr.getCString() << PEGASUS_STD(endl);
        }
    }
    catch (...)
    {
        String expStr("General Exception in ModelWalk, causing it to exit");
        tmsc.errorLog(clientPid, clientLog, expStr);

        if (verboseTest)
        {
            PEGASUS_STD(cerr) << expStr.getCString() << PEGASUS_STD(endl);
        }
    }

//
// One second delay before shutdown.
//
#ifndef PEGASUS_OS_TYPE_WINDOWS
    sleep(1);
#else
    Sleep(1000);
#endif
    if (FileSystem::exists(stopClient))
    {
        //
        // Remove STOP file here. 
        //
        FileSystem::removeFile(stopClient);
    }
   
    if (verboseTest)
    {
        errorInfo.clear();
        errorInfo.append("+++++ TestModelWalkStressClient Terminated ");
        errorInfo.append("Normally +++++");
        tmsc.errorLog(clientPid, clientLog, errorInfo);
        errorInfo.clear();
    }
    return 0;
}