Exemplo n.º 1
0
void CPage::LoadCache(ConnectionPtr connection)
{
	Trace("LoadCache", "",0);

    ResultPtr result;
	ncc::safe_array<char> strQuery(10240);

	try
	{
		// load from the database
		snprintf(strQuery, strQuery.size(), SQL_WTT_LOAD_CACHE, m_runID);

		Trace("Query for LoadCache", strQuery, 0);
		result = connection->Query(strQuery);

		while(result->Next())
		{
			RowPtr row = result->GetCurrentRow();
			// add it
			std::string urlStr;
			urlStr = row->GetFieldString(1);
			{ // scope for lock
				Mutex::Lock lock(sm_urlListLock);
				m_totalUrlList.push_back(urlStr);
			}
		}
	}
	catch (const std::exception& e)
	{
		LogError2("LoadCache() - Caught Exception", e.what(), 0, SC_ERR_GROUP_DB, SC_ERR_CODE_LOAD_CACHE);
	}
}
Exemplo n.º 2
0
void CPage::loadContentError(ConnectionPtr connection)
{
	Trace("loadContentError()", "", 0);

	std::string strQuery;

	try
	{
		Mutex::Lock lock(m_contentErrorListLock);

		// get all of the pages for this customer to test
		strQuery = Format(SQL_WTT_CONTENT_ERRORS);
		if (ResultPtr result = connection->Query(strQuery))
		{
			while (result->Next())
			{
				RowPtr row = result->GetCurrentRow();

				StringIDList::value_type contentError(row->GetFieldString(1), row->GetFieldLong(2));
				contentError.first = " " + contentError.first + " ";

				Trace("loadContentError() - loading string", contentError.first.c_str(), contentError.second);
				m_contentError.push_back(contentError);
			}
		}
	}
	catch (const std::exception& e)
	{
		LogError("Exception Caught:", e.what(), 0);
	}
}
Exemplo n.º 3
0
void CPage::loadPhraseCheck(ConnectionPtr connection)
{
	Trace("loadPhraseCheck()", "", 0);

	std::string strQuery;

	try
	{
		Mutex::Lock lock(m_phraseListLock);

		strQuery = Format(SQL_WTT_PHRASE_WORDS, getPageID());
		if (ResultPtr result = connection->Query(strQuery))
		{
			Trace("RowPtr s:","",result->GetRowCount());

			while (result->Next())
			{
				RowPtr row = result->GetCurrentRow();

				StringIDList::value_type phrase(row->GetFieldString(1), row->GetFieldLong(2));

				Trace("loadPhraseCheck_LT(): pre", phrase.first.c_str(), phrase.second);
				std::transform(phrase.first.begin(), phrase.first.end(), phrase.first.begin(), tolower);
				Trace("loadPhraseCheck_LT(): post", phrase.first.c_str(), phrase.second);
				m_phrase.push_back(phrase);
			}
		}
	}
	catch (const std::exception& e)
	{
		LogError("Exception Caught:", e.what(), 0);
	}
}
Exemplo n.º 4
0
const Value
EngineBase::select1(const Expression &what,
        const Expression &from, const Expression &where)
{
    RowPtr row = select_row(what, from, where);
    if (row->size() != 1)
        throw BadSQLOperation(_T("Unable to fetch exactly one column!"));
    return row->begin()->second;
}
Exemplo n.º 5
0
bool
SqlResultSet::fetch(Row &row)
{
    RowPtr p = cursor_.fetch_row();
    if (!p.get())
        return false;
    row.swap(*p);
    return true;
}
Exemplo n.º 6
0
int TextConsole::move_string(RowPtr dest, RowPtr src, int len) {
    int dlen = len; // const int arg

    //	func("move string");

    // insert the string in destination
    dlen = dest->insert_string( &src->text[ src->pos ], dlen );

    // delete the string from the source
    dlen = src->delete_string( dlen );

    return dlen;
}
Exemplo n.º 7
0
RowPtr
SqlCursor::fetch_row()
{
    try {
        RowPtr row = backend_->fetch_row();
        if (row.get()) {
            Row::iterator j = row->begin(), jend = row->end();
            for (; j != jend; ++j) {
                String uname = str_to_upper(j->first);
                using namespace std;
                swap(j->first, uname);
            }
        }
        if (echo_) {
            if (row.get()) {
                std::ostringstream out;
                out << "fetch: ";
                Row::const_iterator j = row->begin(),
                                    jend = row->end();
                for (; j != jend; ++j)
                    out << NARROW(j->first) << "="
                        << NARROW(j->second.sql_str()) << " ";
                debug(WIDEN(out.str()));
            }
            else
                debug(_T("fetch: no more rows"));
        }
        return row;
    }
    catch (const std::exception &e) {
        connection_.mark_bad(e);
        throw;
    }
}
Exemplo n.º 8
0
void CPage::checkErrorsAlertable(ConnectionPtr connection)
{
	Trace("checkErrorsAlertable", getURL().c_str(), getOverallResultCode());

    std::string strQuery;
	std::string colour;

	// if colour = red/black then go to downtime testing and send alert to alertable people
	try
	{
		// select Colour from Severity where ErrorCode = %d
		strQuery = Format(SQL_WTT_ERROR_COLOUR, (long int)getOverallResultCode());
		if (ResultPtr result = connection->Query(strQuery))
		{
			Trace("RowPtr s:","",result->GetRowCount());

			if (result->Next())
			{
				RowPtr row = result->GetCurrentRow();

				colour += row->GetFieldString(1);
			}
		}
	}
	catch (const std::exception& e)
	{
		LogError("Exception Caught:", e.what(), 0);
	}

	Trace("checkErrorsAlertable", colour.c_str(), getOverallResultCode());

	// only report an error if page failed and not a benchmark and IS a normal page
	if ((colour == SC_SEVERITY_BLACK) || (colour == SC_SEVERITY_RED))
	{
		sm_errorAlertable = true;
	}
}
Exemplo n.º 9
0
void SamplingUpdateHeader(ConnectionPtr connection,
                          long pageID, int testServerID,
                          const std::string& startTimeStr,
                          int overallResultCode,
                          long DNSTimeOffsetMs, long firstByteTimeOffsetMs,
                          long totalLastByteTimeOffsetMs,
                          long totalBytes, resultid_t* resultID,
                          long firstDataTimeOffsetMs,
                          const std::string& sampleDbName,
                          const std::string& request,
                          const std::string& header,
                          const std::string& html,
                          const std::string& extraInfo,
                          SC_TEST_TYPE testType,
						  long customerNo,
						  SC_WEBPAGE_TYPE pageType,
                          const std::string& stepName,
						  long pageNumber,
                          long currentColourCode,
						  int localOffset,
                          long searchID,
						  const std::string& searchName,
						  long runID)
{
    // updates the ResultHeaderYYMMDD table with the results of this file / page for reloaded steps
    // it just adds the details to the current result.

    // passed in dailyDbName

    try
    {
        // Get the resultID.
        ResultPtr queryResult;
        char resultIdQuery[(EXTRA_SQL_MAX_LEN * 4) + 100];

#ifdef SHARD_AWARE
        sprintf(resultIdQuery, SQL_WTT_HEADER_SAMPLE_MAX_RESULTID_C3, sampleDbName.c_str(), pageID,
                getBoundedDateTimeString(KEY_TIME_COLUMN_RESULTHEADER).c_str());
#else
        sprintf(resultIdQuery, SQL_WTT_HEADER_SAMPLE_MAX_RESULTID, sampleDbName.c_str(), pageID);
#endif

        Trace("Issuing query", resultIdQuery, 0);
        queryResult = connection->Query(resultIdQuery);

        queryResult->Next();
        RowPtr row = queryResult->GetCurrentRow();

        *resultID = strtoull(row->GetField(1), 0, 10);
    }
    catch (const std::exception &e)
    {
        LogError("SamplingUpdateHeader() - Caught Exception querying max sampling resultID", e.what(), pageID);
    }

    ResultPtr result;
    char strQuery[(EXTRA_SQL_MAX_LEN * 4) + 200];

    try
    {
        // update the current result in the daily table
        // leave start, dns, connect and data start the same
        // change complete time
        // update result code,
#ifdef SHARD_AWARE
        sprintf(strQuery, SQL_WTT_HEADER_SAMPLE_UPD_C3,
                sampleDbName.c_str(),
                overallResultCode,
                totalLastByteTimeOffsetMs,
                totalBytes,
                *resultID,
                getBoundedDateTimeString(KEY_TIME_COLUMN_RESULTHEADER).c_str()
               );
#else
        sprintf(strQuery, SQL_WTT_HEADER_SAMPLE_UPD,
                sampleDbName.c_str(),
                overallResultCode,
                totalLastByteTimeOffsetMs,
                totalBytes,
                *resultID
               );
#endif

        Trace("SamplingUpdateHeader(): Query for ResultHeaderYYMMDD", strQuery, 0);
        result = connection->Query(strQuery);
    }
    catch (const std::exception &e)
    {
        LogError("SamplingUpdateHeader() - table ResultHeaderYYMMDD Caught Exception", e.what(), pageID);
    }

    // DO NOT store the extra info for the first component against the header - ALREADY STORED
}
Exemplo n.º 10
0
void SamplingStoreHeader(ConnectionPtr connection,
                         long pageID,
						 int testServerID,
						 const std::string& startTimeStr,
                         int overallResultCode,
                         long DNSTimeOffsetMs,
						 long firstByteTimeOffsetMs,
                         long totalLastByteTimeOffsetMs,
                         long totalBytes,
						 resultid_t* resultID,
                         long firstDataTimeOffsetMs,
                         const std::string& sampleDbName,
                         const std::string& request,
                         const std::string& header,
                         const std::string& html,
                         const std::string& extraInfo,
                         SC_TEST_TYPE testType,
						 long customerNo,
                         SC_WEBPAGE_TYPE pageType,
						 const std::string& stepName,
                         long pageNumber,
						 long currentColourCode,
                         int localOffset,
                         long searchID,
						 const std::string& searchName,
						 long runID,
                         long sslConnectTime,
						 long requestSentTime,
                         long requestHeaderSize,
						 long requestContentSize,
                         long responseHeaderSize)
{
    // stores the results of this file / page into the result detail table
    ResultPtr result;
    char strQuery[(EXTRA_SQL_MAX_LEN * 4) + 200];

    try
    {
        // remove the current one stored
        Trace("remove the current one stored", "", pageID);

#ifdef SHARD_AWARE
        sprintf(strQuery, SQL_WTT_HEADER_SAMPLE_LIST_DEL_C3, sampleDbName.c_str(), pageID, getBoundedDateTimeString(KEY_TIME_COLUMN_RESULTHEADER).c_str());
#else
        sprintf(strQuery, SQL_WTT_HEADER_SAMPLE_LIST_DEL, sampleDbName.c_str(), pageID);
#endif

        Trace("Issuing query", strQuery, 0);
        result = connection->Query(strQuery);

        bool addComma = false;
        std::string refList;

        while (result->Next())
        {
            RowPtr row = result->GetCurrentRow();
            if (addComma)
            {
                refList += ",";
            }
            refList += row->GetField(1);

            addComma = true;
        }

        if (addComma)
        {
            // there was at least one in the list to delete
#ifdef SHARD_AWARE
            sprintf(strQuery, SQL_WTT_HEADER_SAMPLE_DEL_C3, sampleDbName.c_str(), refList.c_str(), getBoundedDateTimeString(KEY_TIME_COLUMN_RESULTHEADER).c_str());
#else
            sprintf(strQuery, SQL_WTT_HEADER_SAMPLE_DEL, sampleDbName.c_str(), refList.c_str());
#endif
            Trace("Issuing query", strQuery, 0);
            result = connection->Query(strQuery);

#ifdef SHARD_AWARE
            sprintf(strQuery, SQL_WTT_HEADER_EXTRA_INFO_SAMPLE_DEL_C3, sampleDbName.c_str(), refList.c_str(), getBoundedDateTimeString(KEY_TIME_COLUMN_RESULTHEADEREXTRAINFO).c_str());
#else
            sprintf(strQuery, SQL_WTT_HEADER_EXTRA_INFO_SAMPLE_DEL, sampleDbName.c_str(), refList.c_str());
#endif
            Trace("Issuing query", strQuery, 0);
            result = connection->Query(strQuery);

#ifdef SHARD_AWARE
            sprintf(strQuery, SQL_WTT_HEADER_DETAIL_SAMPLE_DEL_C3, sampleDbName.c_str(), refList.c_str(), getBoundedDateTimeString(KEY_TIME_COLUMN_RESULTDETAIL).c_str());
#else
            sprintf(strQuery, SQL_WTT_HEADER_DETAIL_SAMPLE_DEL, sampleDbName.c_str(), refList.c_str());
#endif
            Trace("Issuing query", strQuery, 0);
            result = connection->Query(strQuery);

#ifdef SHARD_AWARE
            sprintf(strQuery, SQL_WTT_HEADER_REQUEST_SAMPLE_DEL_C3, sampleDbName.c_str(), refList.c_str(), getBoundedDateTimeString(KEY_TIME_COLUMN_REQUEST).c_str());
#else
            sprintf(strQuery, SQL_WTT_HEADER_REQUEST_SAMPLE_DEL, sampleDbName.c_str(), refList.c_str());
#endif
            Trace("Issuing query", strQuery, 0);
            result = connection->Query(strQuery);

#ifdef SHARD_AWARE
            sprintf(strQuery, SQL_WTT_HEADER_HEADER_SAMPLE_DEL_C3, sampleDbName.c_str(), refList.c_str(), getBoundedDateTimeString(KEY_TIME_COLUMN_HEADER).c_str());
#else
            sprintf(strQuery, SQL_WTT_HEADER_HEADER_SAMPLE_DEL, sampleDbName.c_str(), refList.c_str());
#endif
            Trace("Issuing query", strQuery, 0);
            result = connection->Query(strQuery);

#ifdef SHARD_AWARE
            sprintf(strQuery, SQL_WTT_HEADER_HTML_SAMPLE_DEL_C3, sampleDbName.c_str(), refList.c_str(), getBoundedDateTimeString(KEY_TIME_COLUMN_HTML).c_str());
#else
            sprintf(strQuery, SQL_WTT_HEADER_HTML_SAMPLE_DEL, sampleDbName.c_str(), refList.c_str());
#endif
            Trace("Issuing query", strQuery, 0);
            result = connection->Query(strQuery);

#ifdef SHARD_AWARE
            sprintf(strQuery, SQL_WTT_HEADER_EXTRA_SAMPLE_DEL_C3, sampleDbName.c_str(), refList.c_str(), getBoundedDateTimeString(KEY_TIME_COLUMN_EXTRAINFO).c_str());
#else
            sprintf(strQuery, SQL_WTT_HEADER_EXTRA_SAMPLE_DEL, sampleDbName.c_str(), refList.c_str());
#endif
            Trace("Issuing query", strQuery, 0);
            result = connection->Query(strQuery);
        }

        sprintf(strQuery, SQL_WTT_HEADER_SAMPLE_INS,
                sampleDbName.c_str(),
                pageID,
                testServerID,
                startTimeStr.c_str(),
                overallResultCode,
                DNSTimeOffsetMs,
                firstByteTimeOffsetMs,
                totalLastByteTimeOffsetMs,
                totalBytes,
                runID,
                firstDataTimeOffsetMs,
                int(testType),
                startTimeStr.c_str(),
                localOffset,
                customerNo,
                pageType,
                stepName.c_str(),
                pageNumber,
                currentColourCode,
                searchID,
                searchName.c_str(),
                sslConnectTime,
                requestSentTime,
                requestHeaderSize,
                requestContentSize,
                responseHeaderSize);

        Trace("Query for SampleCCYYMM.ResultHeader", strQuery, 0);
        result = connection->Query(strQuery);

        snprintf(strQuery, HTML_SQL_MAX_LEN + 100, SQL_WTT_HEADER_SAMPLE_INS_ID,
                 sampleDbName.c_str());

        Trace("Query for SampleCCYYMM.ResultHeader", strQuery, 0);
        result = connection->Query(strQuery);
        result->Next();
        RowPtr row = result->GetCurrentRow();
        Trace("LastInsertID for SampleCCYYMM.ResultHeader", row->GetField(1), 0);
        *resultID = strtoull(row->GetField(1), 0, 10);
    }
    catch (const std::exception &e)
    {
        // set the error code to 0 so that we don't connect to the wrong header
        *resultID = 0;
        LogError("SamplingStoreHeader() - table ResultHeader Caught Exception", e.what(), pageID);
    }

    // store the extra info for the first component against the header
    try
    {
        if ((!request.empty()) ||
			(!header.empty()) ||
			(!html.empty()) ||
			(!extraInfo.empty()))
        {
            int len = 0;
            char sqlStr[EXTRA_SQL_MAX_LEN + 1];

            // convert the string for inserting
            len = mysql_escape_string(sqlStr,
                                      request.c_str(),
                                      (request.size() < (EXTRA_SQL_MAX_LEN / 2)) ? (request.size()) : (EXTRA_SQL_MAX_LEN / 2));

            // terminate the string
            if ( len > (int)EXTRA_SQL_MAX_LEN)
            {
                sqlStr[EXTRA_SQL_MAX_LEN] = '\0';
            }
            else
            {
                sqlStr[len] = '\0';
            }
            // make sure last char not a \ - it would escape the final quote
            if ( sqlStr[EXTRA_SQL_MAX_LEN] == '\\')
                sqlStr[EXTRA_SQL_MAX_LEN] = ' ';

            char sqlStr2[EXTRA_SQL_MAX_LEN + 1];

            // convert the string for inserting
            len = mysql_escape_string(sqlStr2,
                                      header.c_str(),
                                      (header.size() < (EXTRA_SQL_MAX_LEN / 2)) ? (header.size()) : (EXTRA_SQL_MAX_LEN / 2));

            // terminate the string
            if ( len > (int)EXTRA_SQL_MAX_LEN)
            {
                sqlStr2[EXTRA_SQL_MAX_LEN] = '\0';
            }
            else
            {
                sqlStr2[len] = '\0';
            }
            // make sure last char not a \ - it would escape the final quote
            if ( sqlStr2[EXTRA_SQL_MAX_LEN] == '\\')
                sqlStr2[EXTRA_SQL_MAX_LEN] = ' ';

            char sqlStr3[EXTRA_SQL_MAX_LEN + 1];

            // convert the string for inserting
            len = mysql_escape_string(sqlStr3,
                                      html.c_str(),
                                      (html.size() < (EXTRA_SQL_MAX_LEN / 2)) ? (html.size()) : (EXTRA_SQL_MAX_LEN / 2));

            // terminate the string
            if ( len > (int)EXTRA_SQL_MAX_LEN)
            {
                sqlStr3[EXTRA_SQL_MAX_LEN] = '\0';
            }
            else
            {
                sqlStr3[len] = '\0';
            }
            // make sure last char not a \ - it would escape the final quote
            if ( sqlStr3[EXTRA_SQL_MAX_LEN] == '\\')
                sqlStr3[EXTRA_SQL_MAX_LEN] = ' ';

            char sqlStr4[EXTRA_SQL_MAX_LEN + 1];

            // convert the string for inserting
            len = mysql_escape_string(sqlStr4,
                                      extraInfo.c_str(),
                                      (extraInfo.size() < (EXTRA_SQL_MAX_LEN / 2)) ? (extraInfo.size()) : (EXTRA_SQL_MAX_LEN / 2));

            // terminate the string
            if ( len > (int)EXTRA_SQL_MAX_LEN)
            {
                sqlStr4[EXTRA_SQL_MAX_LEN] = '\0';
            }
            else
            {
                sqlStr4[len] = '\0';
            }
            // make sure last char not a \ - it would escape the final quote
            if ( sqlStr4[EXTRA_SQL_MAX_LEN] == '\\')
                sqlStr4[EXTRA_SQL_MAX_LEN] = ' ';

#ifdef SHARD_AWARE
            snprintf(strQuery, 204800 - 1, SQL_WTT_HEADER_EXTRA_INFO_SAMPLE_INS_C3,
                     sampleDbName.c_str(),
                     *resultID,
                     sqlStr,
                     sqlStr2,
                     sqlStr3,
                     sqlStr4,
                     startTimeStr.c_str()
                    );
#else
            snprintf(strQuery, 204800 - 1, SQL_WTT_HEADER_EXTRA_INFO_SAMPLE_INS,
                     sampleDbName.c_str(),
                     *resultID,
                     sqlStr,
                     sqlStr2,
                     sqlStr3,
                     sqlStr4
                    );
#endif

            Trace("Query for ResultHeaderExtraInfo", strQuery, 0);
            result = connection->Query(strQuery);
        }
    }
    catch (const std::exception &e)
    {
        LogError("SamplingStoreHeader() - table ResultHeaderExtraInfo Caught Exception", e.what(), pageID);
    }
}
Exemplo n.º 11
0
resultid_t runWTTI(const char* url, std::string& dbName, unsigned int wttiRunID, unsigned int shardNumber, int webkitTimeout)
{
	std::string strQuery;
	sem_init( &(CWebPage::Instance()->sm_hostSemaphore), 0, 0 );
	sem_init( &(CWebPage::Instance()->sm_threadControlSemaphore), 0, 0 );

#ifdef SHARD_AWARE
	ConnectionPtr connection(
		new Connection(
			CConfig::getShardServer(shardNumber),
			CConfig::getShardDatabase(shardNumber),
			CConfig::getShardUser(shardNumber),
			CConfig::getShardPass(shardNumber),
			CConfig::getDBRetryTime()));
#else
	ConnectionPtr connection(
		new Connection(
			CConfig::getDBServer(),
			CConfig::getDBDatabase(),
			CConfig::getDBUser(),
			CConfig::getDBPass(),
			CConfig::getDBRetryTime()));
#endif

	std::string urlToTest;

	// Get URL from the WttiTests table.
	ResultPtr resultUrl;
	strQuery = Format(SQL_WTTI_SELECT_URL, wttiRunID);
	resultUrl = connection->Query(strQuery.c_str());
	if ( resultUrl->Next() )
	{
		RowPtr row = resultUrl->GetCurrentRow();
		urlToTest = row->GetFieldString(1);

		Trace("Rows:", "", resultUrl->GetRowCount());
	}

	resultid_t dailyResultID = 0;
	PackDiagStorage store;

	int pageID = 0;
	CResultSet resultSet;
	CTcp* pTcpConnection = new CTcp("", 0);

	// Turn CSS Parsing on permanently.
	CWebPage::Instance()->sm_doCssParsing = 1;
    
    //Turn SSL validation on permanently
    CWebPage::Instance()->sm_doSslValidation = 1;
    CTcp::setDoSslValidation(1);

	// create the new web page object
	CWebPage* pPage = new CWebPage(
		resultSet,
		pTcpConnection,
		0,
		SC_SERVICE_BENCHMARK,
		urlToTest, // url
		pageID, // page id
		SC_WEBPAGE_TYPE_IMED, // page type
		true, // reset cookies
		std::string(), // auth user
		std::string(), // auth pass
		DOWNLOAD_THRESHOLD_SECS, // download threshold
		true, // reset cache
		CConfig::getServerID(),
		SC_WEBPAGE_STATUS_OK, // bodge to be status live page
		HTTP_METHOD_GET, // bodge to get GET by default
		std::string(),
		std::vector<std::string>(),
		std::vector<char>(), // emptyVector,    // bodge - no form variables
		std::string() // fileStr  // bodge - no file variables
		);

	// reset the counters
	pPage->resetPage();

	// record when we started the test
	//pPage->setStartTime();

	// make sure we don't report a hacking / login failure
	// pPage->phraseWasFound();
	// Set that a connection exists to this host.
	//pPage->isHostConnectionsExceeded(static_cast<std::string>(url));
	// do the load of this page
	//pPage->load();

	// add the page just loaded to the list of those done so it doesn't get done again
	// via a redirect
	//CWebPage::AddURLDone(url);

	// reset the first byte count for the page
	//pPage->resetSpeedCounters();

	MultiThreadSetup();

	// ----------------------
	// --- THREAD MANAGER ---
	// ----------------------
	std::string protoAndHost;
	protoAndHost = urlToTest;

	// Calculate protocol type.
	if (strncasecmp (protoAndHost.c_str(), "http://", 7) == 0)
	{
		std::string::size_type hostLength = protoAndHost.find("/", 7);
		if (hostLength != std::string::npos)
			protoAndHost = protoAndHost.substr(0, hostLength);
	}
	else if (strncasecmp (protoAndHost.c_str(), "https://", 8) == 0)
	{
		std::string::size_type hostLength = protoAndHost.find("/", 8);
		if (hostLength != std::string::npos)
			protoAndHost = protoAndHost.substr(0, hostLength);
	}
	else
	{
		std::string::size_type hostLength = protoAndHost.find("/");
		if (hostLength != std::string::npos)
			protoAndHost = "http://" + protoAndHost.substr(0, hostLength);
		else
			protoAndHost = protoAndHost.insert(0, "http://");
	}


	// Add the host that we're about to connect to so it does not get added again.
	{
		Mutex::Lock lock(CWebPage::Instance()->sm_connectedHostsLock);
		CWebPage::Instance()->sm_connectedHosts.push_back(std::make_pair(true, protoAndHost));
	}


	std::vector<ThreadArgs*> threadArgsVector;

	// JYLS WEBKIT START

#ifndef SC_NON_WEBKIT
#ifdef SC_WEBKIT_V89

	curlwrapper_init(pPage, startThread);

	//Use the default value from the opt file for these
	ww_setMaxNumberOfThreadsPerHost(CConfig::getDefaultMaxThreadsPerHost());
	ww_setMaxTotalNumberOfThreads(CConfig::getDefaultMaxTotalThreads());
	ww_setScreenXDimension(CConfig::getScreenXDimension());
	ww_setScreenYDimension(CConfig::getScreenYDimension());
	ww_setJavascriptTimeout(CConfig::getJsExecutionTimeout());

	ww_loadPageByWebkit(pPage->getURL().c_str(), true, DOWNLOAD_THRESHOLD_SECS, webkitTimeout);

#else
	bool isJavascriptEnabled = true;

	soupwrapper_init(pPage, startThread);

	ww_setPageId(pageID);
	ww_setWebkitTimeout(webkitTimeout);
	ww_setWebkitTimeoutHard(CConfig::getWebkitHardTimeout());

	load_page_by_webkit(pPage->getURL(), isJavascriptEnabled);
#endif	// SC_WEBKIT_V89
#else
	std::vector<pthread_t> threadIds;
	pthread_t threadId;
	int res = 0;
	unsigned int maxThreadsPerHost = 0;

	if (maxThreadsPerHost == 0)
		maxThreadsPerHost = CConfig::getDefaultMaxThreadsPerHost();

	//Limit the value of maxThreadsPerHost
	if (maxThreadsPerHost > CConfig::getMaxThreadsPerHostLimit())
		maxThreadsPerHost = CConfig::getMaxThreadsPerHostLimit();

	unsigned int numThreads = maxThreadsPerHost;

	for (unsigned int a = 0; a < numThreads; ++a)
	{
		ThreadArgs* args1 = new ThreadArgs;
		args1->isFirstPage = (a == 0);
		args1->pPage       = pPage;
		args1->host        = protoAndHost;
		res = pthread_create(&(threadId), NULL, startThread, (void*)args1);
		if ( res != 0 )
		{
			LogError("Failed to start thread for", protoAndHost.c_str(), 0);
		}
		else
		{
			threadIds.push_back(threadId);
			threadArgsVector.push_back(args1);

			// Wait for the second thread to signal it has started.
			sem_wait(&(CWebPage::Instance()->sm_threadControlSemaphore));
		}
	}

	std::vector<CWebPage::HostPair>::const_iterator hostIt;

	Trace("before starting threads", "", 0);

	while ((CWebPage::getUrlListSize()) > 0 || (CWebPage::Instance()->sm_numWorkingThreads > 0))
	{
		{
			Mutex::Lock lock(CWebPage::Instance()->sm_connectedHostsLock);
			for ( hostIt = CWebPage::Instance()->sm_connectedHosts.begin(); hostIt != CWebPage::Instance()->sm_connectedHosts.end(); ++hostIt )
			{
				if ( !(hostIt->first) )
				{
					unsigned int numThreads = maxThreadsPerHost;

					for (unsigned int a = 0; a < numThreads; ++a)
					{
						bool errorOccured = false;
						std::string hostToConnect;
						hostToConnect.assign(hostIt->second);

						// Start threads per new host encountered.
						Trace("Starting threads for", hostIt->second.c_str(), 0);

						ThreadArgs* args2  = new ThreadArgs;
						args2->isFirstPage = false;
						args2->pPage       = pPage;
						args2->host        = hostToConnect;

						res = pthread_create(&(threadId), NULL, startThread, (void*)args2);
						if ( res != 0 )
						{
							LogError("Failed to start thread for", hostIt->second.c_str(), 2);
							errorOccured = true;
						}
						else
						{
							threadIds.push_back(threadId);
							threadArgsVector.push_back(args2);
							// Wait for thread to start.
							sem_wait(&(CWebPage::Instance()->sm_threadControlSemaphore));
						}

						// Connected ok.
						hostIt->first = true;
					}
				}
			}
		}

		// Wait for more hosts to be encountered.
		sem_wait(&(CWebPage::Instance()->sm_hostSemaphore));
	}

#endif	// SC_NON_WEBKIT
	// JYLS WEBKIT END

	Trace("Deleting semaphore", "", 0);
	sem_destroy(&(CWebPage::Instance()->sm_hostSemaphore));
	sem_destroy(&(CWebPage::Instance()->sm_threadControlSemaphore));
#ifdef SC_NON_WEBKIT
	// Write debug information if components remain.
	if (CWebPage::getUrlListSize() > 0)
	{
		CWebPage::writeThreadsDebug();

		int count = 0;
		std::vector<ThreadArgs*>::const_iterator threadsIt;
		for (threadsIt = threadArgsVector.begin(); threadsIt != threadArgsVector.end(); ++threadsIt)
		{
			LogInfo("Threads started=", (*threadsIt)->host.c_str(), count);
			++count;
		}
	}
#endif	// SC_NON_WEBKIT

#ifndef SC_NON_WEBKIT
	// Sleep to allow for extra components from eg javascript
	usleep(1000000);

	//Actually check how many threads are running, go ahead if 0
	while (CWebPage::Instance()->sm_numWorkingThreads)
	{
		usleep(10000);
	}
#else
	void* threadResult;

	// Join started threads.
	for (unsigned int i = 0; i < threadIds.size(); ++i)
	{
		res = pthread_join(threadIds[i], &threadResult);
		if (res != 0)
		{
			LogError("Unable to join thread", "", res);
		}
		else
		{
			Trace("Joined thread", "", res);
		}
	}
#endif	// SC_NON_WEBKIT

	// Delete all ThreadArgs used.
	while ( !threadArgsVector.empty() )
	{
		delete threadArgsVector.back();
		threadArgsVector.pop_back();
	}

	setResultCode(pPage, CWebPage::Instance()->sm_firstPageResultCode);

	try
	{
		Trace("Connecting to db", "", 0);

		std::string requestStr;
		std::string headerStr;
		std::string htmlStr;
		std::string extraInfoStr;

		// extra info for ResultHeader
		if (!resultSet.getResultDetails().empty())
		{
			const CResultDetail* pResultDetail = resultSet.getFirstResultDetail();

			requestStr = pResultDetail->getRequest();
			headerStr = pResultDetail->getHeaders();
			htmlStr = pResultDetail->getPageText();
			extraInfoStr = pResultDetail->getExtraInfo();
		}

		const bool pageInError = pPage->getOverallResultCode() != SC_HEADER_RESULT_SUCCESS && pPage->getOverallResultCode() != SC_HEADER_RESULT_DUMMY_TRANSACTION;

		// 3.0.0 Daily ResultHeader tables
		StoreHeaderDaily(
			connection,
			resultSet,
			pPage->getPageID(),
			CConfig::getServerID(),
			pPage->getStartTimeStr(),
			pPage->getOverallResultCode(),
			pPage->getDNSTimeOffsetMs(),
			pPage->getConnectTimeOffsetMs(),
			pPage->getTotalLastByteTimeOffsetMs(),
			pPage->getTotalBytes(),
			&dailyResultID,
			pPage->getFirstDataTimeOffsetMs(),
			pPage->getDailyTableName(),
			PackDiagOverride(),
			store,
			pageInError,
			false,
			SC_TEST_TYPE_WTTI,
			0,
			0,
			0,
			CSCGlobal::gAgentVersion,
			pPage->getTotalGzipBytes(),
			false, //diagstored
			pPage->getSslConnectTimeOffsetMs(),
			pPage->getRequestSentTimeOffsetMs(),
			pPage->getRequestHeaderSize(),
			pPage->getRequestContentSize(),
			pPage->getResponseHeaderSize());

		// now store all the detail - always do this for wtti
		int ComponentNo = 1;
		for (CResultSet::ResultList::const_iterator p = resultSet.getResultDetails().begin(); p != resultSet.getResultDetails().end(); ++p, ++ComponentNo)
		{
			// now go and get the rest of the files - with threads
			if (const CResultDetail* pResultDetail = p->pResultDetail)
			{
				pResultDetail->setComponentNo(ComponentNo);

				// 3.0.0 Daily ResultDetail tables
				StoreResultDaily(
					connection,
					resultSet,
					ComponentNo,
					pResultDetail,
					dailyResultID,
					pPage->getDailyTableName());
			}
		}
	}
	catch (const std::exception &e)
	{
		Trace("Exception caught", e.what(), 0);
		LogError2("Exception Caught:", e.what(), 0, SC_ERR_GROUP_AGENT, SC_ERR_CODE_WTTI);
		return 0;
	}
	
#ifdef SHARD_AWARE
	// Write the result ID back to the transient table so the website can pick it up.
	strQuery = Format(SQL_WTTI_INSERT_RESULTID, dailyResultID, wttiRunID);
	Trace("Issuing query", strQuery.c_str(), dailyResultID);
	connection->Query(strQuery);
#endif

	//
	dbName = pPage->getDailyTableName();

	// complete it
	delete pPage;
	pPage = NULL;

	MultiThreadCleanup();

	// Remove /tmp/session_id.<pid>p.host files
	long int uniqueId = getpid();
	std::string identifier = WTTP_SESSION_ID_IDENTIFIER;
	strQuery = Format("rm -f /tmp/session_id.%ld%s.*", uniqueId, identifier.c_str());
	int ret = system(strQuery.c_str());
	Trace("System call returned", strQuery.c_str(), ret);

	return dailyResultID;
}
Exemplo n.º 12
0
bool checkConnectivity(ConnectionPtr connection)
{
	// checks to see if this server has connectivity
	// it does so by finding two servers (from the TestServer table) in other groups, and hence on other carriers
	// if both of these pings fail then we can assume that it is our connectivity
	// we can then re-schedule the job

	// now also used to see if we are down before the end of the check

	std::string	testHost;
	std::string	externalTestHost;
	std::string	externalTestHost1;
	std::string	externalTestHost2;
	int			ourGroup = 0;
	int			testGroup = 0;
	ResultPtr 	result;
	ncc::safe_array<char> selectQuery(1024);
	bool		retVal = true;
	std::string site;
	std::string clusterGroup;

	Trace("checkConnectivity", "CConfig::getServerID()", CConfig::getServerID());

	try
	{
#ifdef SHARD_AWARE
		ConnectionPtr passportShardConnection(
			new Connection(
					CConfig::getPassportDbServer(),
					CConfig::getPassportDbDatabase(),
					CConfig::getPassportDbUser(),
					CConfig::getPassportDbPass(),
					CConfig::getDBRetryTime()));

		// get the group of this test server
		snprintf(selectQuery, selectQuery.size(), SQL_SCS_TEST_GROUP_CG3, CConfig::getServerID());
		Trace("Issuing query",selectQuery,0);
		result = passportShardConnection->Query(selectQuery);
		Trace("Rows:","",result->GetRowCount());

		if (result->GetRowCount() > 0)
		{
			result->Next();
			RowPtr row = result->GetCurrentRow();

			ourGroup = row->GetFieldInteger(1);
		}

		snprintf(selectQuery, selectQuery.size(), SQL_SCS_RETEST_SERVERS_GROUP_RAND_CG3,
							SC_SERVER_STATUS_LIVE, ourGroup, 0);

		Trace("Issuing query",selectQuery,0);
		result = passportShardConnection->Query(selectQuery);
		Trace("Rows:","",result->GetRowCount());

#else
		// get the group of this test server
		snprintf(selectQuery, selectQuery.size(), SQL_SCS_TEST_GROUP, CConfig::getServerID());
		Trace("Issuing query",selectQuery,0);
		result = connection->Query(selectQuery);
		Trace("Rows:","",result->GetRowCount());

		if (result->GetRowCount() > 0)
		{
			result->Next();
			RowPtr row = result->GetCurrentRow();

			ourGroup = row->GetFieldInteger(1);
			site = row->GetFieldString(2);
			clusterGroup = row->GetFieldString(3);
		}

		snprintf(selectQuery, selectQuery.size(), SQL_SCS_RETEST_SERVERS_GROUP_RAND, SC_SERVER_STATUS_LIVE, ourGroup, site.c_str(), clusterGroup.c_str());
		Trace("Issuing query",selectQuery,0);
		result = connection->Query(selectQuery);
		Trace("Rows:","",result->GetRowCount());
#endif
		int testGroup1 = 0;
		int testGroup2 = 0;
		std::string externalTestHost1;
		std::string externalTestHost2;

		if (result->GetRowCount() > 0)
		{
			while(result->Next())
			{
				RowPtr row = result->GetCurrentRow();

				testHost = row->GetFieldString(2);
				testGroup = row->GetFieldInteger(3);
				externalTestHost = row->GetFieldString(4);

				Trace("externalHost",externalTestHost.c_str(),testGroup);
				if (testGroup1 == 0)
				{
					// set this to be the first group to test
					testGroup1 = testGroup;
					externalTestHost1 = externalTestHost;
				}
				else if (testGroup1 != testGroup)
				{
					// different group - use for the second test
					testGroup2 = testGroup;
					externalTestHost2 = externalTestHost;

					// all done now
					break;
				}
				// else must be same group as testGroup1 - ignore it
			}

			bool ping1OK = true;
			bool ping2OK = true;

			if ((testGroup1 != 0) && (externalTestHost1 != ""))
			{
				Trace("Tesing against group 1",externalTestHost1.c_str(),testGroup1);
				ping1OK = pingSiteAvailable(externalTestHost1);
			}
			else
			{
				// nothing to test - assume it worked ?
				Trace("Tesing against group 1","n/a",testGroup1);
			}

			if ((testGroup2 != 0) && (externalTestHost2 != ""))
			{
				Trace("Tesing against group 2",externalTestHost2.c_str(),testGroup2);
				ping2OK = pingSiteAvailable(externalTestHost2);
			}
			else if ((testGroup1 != 0) && (externalTestHost1 != ""))
			{
				// we tested a 'real' #1 - test the one from sc.opt
				Trace("Tesing against group 2 - sc.opt",CConfig::getPing1Name().c_str(),testGroup2);
				ping2OK = pingSiteAvailable(CConfig::getPing1Name());
			}
			else
			{
				// else nothing to test - assume it worked ?
				Trace("Tesing against group 2","n/a",testGroup2);
			}

			if (!ping1OK && !ping2OK)
			{
				// first two have failed
				// check yahoo
				if (!pingSiteAvailable(CConfig::getPing1Name()))
				{
					retVal = false;
				}
			}
		}
	}
	catch (const std::exception& e)
	{
		Trace("Exception caught", e.what(), 0);
		printf("Error: %s\n", e.what());
	}

	return retVal;
}
Exemplo n.º 13
0
void schedSecondTest(ConnectionPtr connection, long idNo, int portNo, SC_TEST_TYPE testType, unsigned int shardNumber)
{
	Trace("schedSecondTest", "", idNo);

	std::string	strQuery;

	// find a server to send the test to
	try
	{
#ifdef SHARD_AWARE
		ConnectionPtr passportShardConnection(
			new Connection(
					CConfig::getPassportDbServer(),
					CConfig::getPassportDbDatabase(),
					CConfig::getPassportDbUser(),
					CConfig::getPassportDbPass(),
					CConfig::getDBRetryTime()));

		// get the group of this test server
		strQuery = Format(SQL_SCS_TEST_GROUP_CG3, CConfig::getServerID());
		TraceSQL("Issuing query", "", 0);

		int	testGroup = 0;
		if (ResultPtr result = passportShardConnection->Query(strQuery))
		{
			TraceSQL("Rows:","",result->GetRowCount());
			if (result->GetRowCount() > 0)
			{
				result->Next();
				RowPtr row = result->GetCurrentRow();

				testGroup = row->GetFieldInteger(1);
			}
		}

		// get the carrier to exclude
		if (portNo == TCP_PORT_WTTGD)
		{
			strQuery = Format(SQL_SCS_TEST_GROUP_EXCLUDE_SCRIPT, idNo);
		}
		else if (portNo == TCP_PORT_ETTD)
		{
			strQuery = Format(SQL_SCS_TEST_GROUP_EXCLUDE_ISP, idNo);
		}
		else
		{
			strQuery = Format(SQL_SCS_TEST_GROUP_EXCLUDE_WEBPAGE, idNo);
		}
		TraceSQL("Issuing query", "", 0);

		unsigned int carrierToExclude = 0;
		if (ResultPtr result = connection->Query(strQuery))
		{
			TraceSQL("Rows:","",result->GetRowCount());
			if (result->GetRowCount() > 0)
			{
				result->Next();
				RowPtr row = result->GetCurrentRow();

				// make it the customer one
				carrierToExclude = row->GetFieldInteger(1);

				// override with Script or WebPage one
				if (row->GetFieldInteger(2) != 0)
					carrierToExclude = row->GetFieldInteger(2);
			}
		}

#ifdef SC_NON_WEBKIT
		strQuery = Format(SQL_SCS_RETEST_SERVERS_GROUP_RAND_CG3, SC_SERVER_STATUS_LIVE, testGroup, carrierToExclude);
#else
#ifdef SC_WEBKIT_V89
		strQuery = Format(SQL_SCS_RETEST_SERVERS_GROUP_RAND_CG3, SC_SERVER_JACK_STATUS_LIVE_89, testGroup, carrierToExclude);
#else
		strQuery = Format(SQL_SCS_RETEST_SERVERS_GROUP_RAND_CG3, SC_SERVER_JACK_STATUS_LIVE, testGroup, carrierToExclude);
#endif	// SC_WEBKIT_V89
#endif	// SC_NON_WEBKIT
		TraceSQL("Issuing query", "", 0);

		if (ResultPtr result = passportShardConnection->Query(strQuery))
		{
			TraceSQL("Rows:","",result->GetRowCount());

#else
		// get the group of this test server
		strQuery = Format(SQL_SCS_TEST_GROUP, CConfig::getServerID());
		TraceSQL("Issuing query", "", 0);

		if (ResultPtr result = connection->Query(strQuery))
		{
			std::string site;
			std::string clusterGroup;

			TraceSQL("Rows:","",result->GetRowCount());
			if (result->GetRowCount() > 0)
			{
				result->Next();
				RowPtr row = result->GetCurrentRow();

				testGroup = row->GetFieldInteger(1);
				site = row->GetFieldString(2);
				clusterGroup = row->GetFieldString(3);
			}
		
			// first work out the minute
			//time_t timeNow = time(NULL);
			//struct tm* timeStruct;
			//timeStruct = localtime( &timeNow );
			strQuery = Format(SQL_SCS_RETEST_SERVERS_GROUP_RAND, SC_SERVER_STATUS_LIVE, testGroup, site.c_str(), clusterGroup.c_str());
		}
		TraceSQL("Issuing query", "", 0);

		if (ResultPtr result = connection->Query(strQuery))
		{
			TraceSQL("Rows:","",result->GetRowCount());
#endif
			if (result->GetRowCount() > 0)
			{
				std::string	testHost;

				while (result->Next())
				{
					RowPtr row = result->GetCurrentRow();

					testHost = row->GetFieldString(2);

					// try to send the job

					// send the test request
					CTcp tcpConnection(testHost, portNo);
					int fd = tcpConnection.tcp_connect(WTT_WTTD_TIMEOUT_SECS);
					if (fd > 0)
					{
#ifdef SHARD_AWARE
						// send the info to the testing tool
						if (testType == SC_TEST_TYPE_UNKNOWN)
						{
							strQuery = Format("%ld %d", idNo, shardNumber);
						}
						else
						{
							strQuery = Format("%ld %d %d", idNo, shardNumber, int(testType));
						}
#else
						// send the info to the testing tool
						if (testType == SC_TEST_TYPE_UNKNOWN)
						{
							strQuery = Format("%ld", idNo);
						}
						else
						{
							strQuery = Format("%ld %d", idNo, int(testType));
						}
#endif
						TraceLogic("tcpConnection.writen", strQuery.c_str(), strQuery.size())
						if (tcpConnection.writen(strQuery.c_str(), strQuery.size()) > 0)
						{
							Trace("Job sent to test server", strQuery.c_str(), fd);
							return;
						}
						else
						{
							LogError("Error sending job to test server", strQuery.c_str(), idNo);
						}
					}
					else
					{
						LogError("Timed out sending job to test server", "", idNo);
					}
				}
			}
Exemplo n.º 14
0
// per page tests
void runWTTT(scriptid_t scriptID, unsigned int shardNumber, const char* runNo, long batchID, bool wait, bool debug)
{
	// only do a sleep if the first run - not for manual or retests
	if ((atoi(runNo) == 1) && wait)
	{
		TraceNoise("Sleeping for a bit", runNo, scriptID);

		// sleep for an offset of seconds
		sleep(scriptID % 60);
	}

	ConnectionPtr connection;
	std::string strQuery;
	bool download = true;

	try
	{
#ifdef SHARD_AWARE
		TraceLogic("Connecting to db", CConfig::getShardDatabase(shardNumber).c_str(), shardNumber);
		connection.reset(
			new Connection(
					CConfig::getShardServer(shardNumber),
					CConfig::getShardDatabase(shardNumber),
					CConfig::getShardUser(shardNumber),
					CConfig::getShardPass(shardNumber),
					CConfig::getDBRetryTime()));
#else
		TraceLogic("Connecting to db", CConfig::getDBDatabase(), 0);
		connection.reset(
			new Connection(
					CConfig::getDBServer(),
					CConfig::getDBDatabase(),
					CConfig::getDBUser(),
					CConfig::getDBPass(),
					CConfig::getDBRetryTime()));
#endif
	}
	catch (const std::exception &e)
	{
		LogError("Exception Caught:", e.what(), scriptID);
		return;
	}

	// if the ScriptID == 0 - get it from the batch table
	if (scriptID == 0)
	{
		strQuery = Format(SQL_WTTT_BATCH_SEL, batchID);
		TraceSQL("Issuing", "", 0);
		if (ResultPtr result = connection->Query(strQuery))
		{
			if (result->Next())
			{
				RowPtr row = result->GetCurrentRow();

				scriptID = row->GetFieldLong(1);

				TraceLogic("batchID", "", batchID);
				TraceLogic("scriptID", "", scriptID);
			}
		}
	}

	// script and archive names
	const std::string filename = Format("/home/sc/bin/monitor_scripts/%ld_%u", scriptID, shardNumber);
	const std::string gzfilename = filename + ".gz";

	// Get script details
	long customerNo = 0;
	SC_SCRIPT_LANGUAGE scriptlanguage = SC_SCRIPT_LANGUAGE_UNKNOWN;
	std::string scriptversion;
	strQuery = Format(SQL_WTTT_SCRIPTINFO_SEL, scriptID);
	TraceSQL("Issuing", "", 0);
	if (ResultPtr result = connection->Query(strQuery))
	{
		if (result->Next())
		{
			RowPtr row = result->GetCurrentRow();

			customerNo = row->GetFieldLong(1);
			scriptlanguage = static_cast<SC_SCRIPT_LANGUAGE>(row->GetFieldLong(2));

			// Field can be NULL, so check before dereference
			if (const char* str = row->GetField(3))
			{
				scriptversion = str;
			}

			TraceLogic("customerNo", "", customerNo);
			TraceLogic("scriptlanguage", to_string(scriptlanguage).c_str(), int(scriptlanguage));
			TraceLogic("scriptversion", scriptversion.c_str(), scriptversion.size());
		}
	}

	switch (scriptlanguage)
	{
	case SC_SCRIPT_LANGUAGE_UNKNOWN:
		//
		// No recognised script or script language returned
		//
		LogError(
			"Script not found",
			Format("ScriptID=%d Shard=%d DB.name=%s DB.host=%s",
				scriptID,
				shardNumber,
				CConfig::getShardDatabase(shardNumber).c_str(),
				CConfig::getShardServer(shardNumber).c_str()).c_str(),
			0);
			exit(1);
		break;

	case SC_SCRIPT_LANGUAGE_PHP:
		//
		// Handle PHP script
		//
		strQuery = Format("select Code, length(Code) from Script where ScriptID=%ld", scriptID);
		TraceSQL("Issuing", "", 0);
		if (ResultPtr result = connection->Query(strQuery))
		{
			if (result->Next())
			{
				RowPtr row = result->GetCurrentRow();

				int scriptLength = row->GetFieldInteger(2);
				std::string scriptString(row->GetField(1), scriptLength);

				TraceLogic("length(Code)", "", scriptLength);

				umask(SC_WTTT_UMASK);
				int filePtr = open(filename.c_str(), O_CREAT | O_TRUNC | O_WRONLY, S_IRWXU);
				if (filePtr != -1)
				{
					// output the body to the file
					int nbytes = write(filePtr, scriptString.c_str(), scriptLength);
					if (nbytes != scriptLength)
					{
						LogError("Incomplete write of PHP executeable to disc", to_string(nbytes).c_str(), scriptLength);
					}
					close(filePtr);
				}
				else
				{
					LogError("Unable to write PHP executable to disc", "", scriptID);
				}
			}
		}
		break;

	case SC_SCRIPT_LANGUAGE_CXX:
		//
		// Handle compiled C++ script
		//
		if (Exist(filename))
		{
			// Is the file out of date?
			strQuery = Format(SQL_WTTT_SCRIPTAGE_SEL, scriptID);
			TraceSQL("Issuing", "", 0);
			if (ResultPtr result = connection->Query(strQuery))
			{
				if (result->Next())
				{
					RowPtr row = result->GetCurrentRow();

					long dbage = row->GetFieldLong(1);

					TraceLogic("dbage", "", dbage);

					struct stat statbuf;
					bzero(&statbuf, 0);
					int ret = stat(filename.c_str(), &statbuf);
					TraceLogic("stat", "download", ret);

					download = ret == -1  ||  statbuf.st_mtime < dbage;
					TraceLogic("Overwrite the existing %s?", "download", int(download));

					TraceLogic("statbuf.st_mtime", "", statbuf.st_mtime);
				}
			}
		}

		//
		// Extract script from database
		//
		if (download)
		{
			strQuery = Format(SQL_WTTT_SCRIPT_SEL, scriptID);
			TraceSQL("Issuing", "", 0);
			if (ResultPtr result = connection->Query(strQuery))
			{
				if (result->Next())
				{
					RowPtr row = result->GetCurrentRow();

					int scriptLength = row->GetFieldInteger(2);
					std::string scriptString(row->GetField(1), scriptLength);

					// Field can be NULL, so check before dereference
					int agentVersion = 0;
					if (const char* str = row->GetField(3))
					{
						agentVersion = atoi(str);
					}

					TraceLogic("scriptLength", "", scriptLength);
					TraceLogic("scriptString", scriptString.c_str(), scriptString.size());
					TraceLogic("agentVersion", "", agentVersion);

					umask(SC_WTTT_UMASK);
					int filePtr = open(gzfilename.c_str(), O_CREAT | O_TRUNC | O_WRONLY, S_IRWXU);
					if (filePtr != -1)
					{
						// output the body to the file
						int nbytes = write(filePtr, scriptString.c_str(), scriptLength);
						close(filePtr);

						if (nbytes == scriptLength)
						{
							// unzip, set permissions and prelink
							doUnzip(gzfilename);
							chmod(filename.c_str(), 0775);

							bool prelink = false;
							if (!CConfig::getIMMode())
							{
								TraceLogic("Connecting to passport db", CConfig::getPassportDbDatabase().c_str(), 0);
								ConnectionPtr connectionPassport(
									new Connection(
											CConfig::getPassportDbServer(),
											CConfig::getPassportDbDatabase(),
											CConfig::getPassportDbUser(),
											CConfig::getPassportDbPass(),
											CConfig::getDBRetryTime()));

								strQuery = Format("select Prelink from LibraryVersions where Version=%d", agentVersion);
								TraceSQL("Issuing", "", 0);
								if (ResultPtr result = connectionPassport->Query(strQuery))
								{
									if (result->Next())
									{
										RowPtr row = result->GetCurrentRow();

										prelink = row->GetFieldInteger(1) != 0;

										TraceLogic("prelink", to_string(prelink).c_str(), int(prelink));
									}
								}

								TraceLogic("prelink", "", int(prelink));
							}
							else
							{
								TraceLogic("prelink", "Don't prelink on IM node", 0);
							}

							if (prelink)
							{
								int ret = doPrelink(filename, SC_AGENT_VER_NUM);
								if (ret != 0)
								{
									LogError("Prelink failed", filename.c_str(), ret);
								}
							}
						}
						else
						{
							LogError("Incomplete write of C++ executeable to disc", to_string(nbytes).c_str(), scriptLength);
							return;
						}
					}
					else
					{
						LogError("Unable to write C++ executable to disc", "", scriptID);
						return;
					}
				}
				else
				{
					LogError("C++ Executable not in database", gzfilename.c_str(), result->GetRowCount());
					return;
				}
			}
			else
			{
				LogError("Query for C++ executable failed", strQuery.c_str(), result->GetRowCount());
				return;
			}
		}
		break;

	default:
		LogError("Fatal: Unknown language", "", int(scriptlanguage));
		return;
	}

	// close the db connection
	Clear(connection);

	//
	// Build command line
	//
	std::string systemCmd;

#ifdef USE_SHARED
	// For version 7
	if (scriptlanguage == SC_SCRIPT_LANGUAGE_CXX &&
		Exist("/home/sc/lib/libsiteconfidence.so." + scriptversion) &&
		Exist("/home/sc/lib/libscparser.so." + scriptversion) &&
		Exist("/home/sc/lib/libsiteconfidenceinterface.so." + scriptversion))
	{
		std::ostringstream os;
		os << "export LD_PRELOAD=";
		os << "/home/sc/lib/libsiteconfidence.so." << scriptversion << ":";
		os << "/home/sc/lib/libscparser.so." << scriptversion << ":";
		os << "/home/sc/lib/libsiteconfidenceinterface.so." << scriptversion << "; ";

		systemCmd += os.str();
	}
#endif

#ifdef SHARD_AWARE
	// set environment variables for debug and shardNumber
	{
		std::ostringstream os;
		os	<< "export SHARD_NUMBER=" << shardNumber << "; "
			<< "export SC_DEBUG_MANUAL_MODE=" << (debug != 0) << "; ";

		systemCmd += os.str();
	}
#endif

	// program command line
	{
		std::ostringstream os;
		os << filename << " " << runNo << " " << batchID << " " << scriptID << " " << customerNo;

		systemCmd += os.str();
	}

	//Print(Format("starting script:%s\n", systemCmd.c_str()));

	// execute
	TraceLogic("Starting script", systemCmd.c_str(), scriptID);
	int sysVal = system(systemCmd.c_str());
	int exitVal = WEXITSTATUS(sysVal);
	TraceLogic("System call returned", systemCmd.c_str(), exitVal);

	//Print(Format("script returned:%d\n", exitVal));
}
Exemplo n.º 15
0
void CPage::loadJavaScriptCheck(ConnectionPtr connection)
{
	Trace("loadJavaScriptCheck()", "",0);

	std::string strQuery;

	// need to change all to lower case
	std::string	lowerCaseBody;

	try
	{
		Mutex::Lock lock(m_javascriptListLock);

		strQuery = Format(SQL_WTT_JAVASCRIPT1);
		if (ResultPtr result = connection->Query(strQuery))
		{
			Trace("RowPtr s:","",result->GetRowCount());

			while(result->Next())
			{
				RowPtr row = result->GetCurrentRow();

				// add to the list
				Trace("loadJavaScriptCheck() - search", row->GetField(1),0);
				Trace("loadJavaScriptCheck() - replace", row->GetField(2),0);
				m_javascriptSearchList.push_back(row->GetFieldString(1));
				m_javascriptReplaceList.push_back(row->GetFieldString(2));
			}
		}

		strQuery = Format(SQL_WTT_JAVASCRIPT2, getPageID());
		if (ResultPtr result = connection->Query(strQuery))
		{
			Trace("RowPtr s:","",result->GetRowCount());

			while(result->Next())
			{
				RowPtr row = result->GetCurrentRow();

				// add to the list
				Trace("loadJavaScriptCheck() - search", row->GetField(1),0);
				Trace("loadJavaScriptCheck() - replace", row->GetField(2),0);
				m_javascriptSearchList.push_back(row->GetFieldString(1));
				m_javascriptReplaceList.push_back(row->GetFieldString(2));
			}
		}
	}
	catch (const std::exception& e)
	{
		LogError("Exception Caught:", e.what(), 0);
	}

	try
	{
		Mutex::Lock lock(m_javascriptExtractListLock);

		strQuery = Format(SQL_WTT_JAVASCRIPT3, getPageID());
		if (ResultPtr result = connection->Query(strQuery))
		{
			Trace("RowPtr s:","",result->GetRowCount());

			while(result->Next())
			{
				RowPtr row = result->GetCurrentRow();

				// add to the list
				Trace("loadJavaScriptCheck() - start", row->GetField(1),0);
				Trace("loadJavaScriptCheck() - end", row->GetField(2),0);
				m_javascriptStartList.push_back(row->GetFieldString(1));
				m_javascriptEndList.push_back(row->GetFieldString(2));
			}
		}
	}
	catch (const std::exception& e)
	{
		LogError("Exception Caught:", e.what(), 0);
	}
}