Ejemplo n.º 1
0
void App::RenderOwner()
{
    static char s_message[1024];
    static bool s_dealtWith = false;

    if( !s_dealtWith )
    {
        // First try to read data/world-encode.dat
        // If it is present open it, encode it into world.dat, then delete it
        TextFileReader *encode = new TextFileReader( "data/world-encode.txt" );
        if( encode->IsOpen() )
        {
            encode->ReadLine();
            char *encodeMe = encode->GetRestOfLine();
            TextFileWriter encodeTo( "data/world.dat", true );
            encodeTo.printf( "%s\n", encodeMe );
        }
        delete encode;
        DeleteThisFile( "data/world-encode.txt" );


        // Now open the encoded world.dat file
        // Decrypt the contents for printing onto the screen
        TextFileReader reader( "data/world.dat" );
        AppAssert( reader.IsOpen() );
        reader.ReadLine();
        char *line = reader.GetRestOfLine();
        sprintf( s_message, "%s", line );
        char *nextLine = strchr( s_message, '\n' );
        if( nextLine ) *nextLine = '\x0';
        //strupr( s_message );

        s_dealtWith = true;
    }

    g_renderer->Text( 10, g_windowManager->WindowH() - 20, Colour(255,50,50,255), 20, s_message );
}
Ejemplo n.º 2
0
//
// case 0: depart-level
//	dep-id => [pid,...] => [{pid,*,*}, ...]
//	host=[auto]
// case 1: pid-level
//	pid, *, *
//	host=[auto]
// case 2: mid-level
//	pid, mid, *
//	host=[auto]
// case 3: host-level
//	pid, mid, *
//	host=ip
// case 4: expand-to-individual-hosts
//	pid,mid,iid as in [0-2] (no case #3)
//	host=[auto]
//
static void handleRequest(QueryParameters& parameters)
{
	// step 1: context
	const char *strContext = parameters.getString("context", "resource");
	int context;
	if (!strcmp(strContext, "business")) {
		context = CT_BUSINESS;
	}
	else if (!strcmp(strContext, "resource")) {
		context = CT_RESOURCE;
	}
	else {
		outputError(501, "Invalid context parameter");
		return;
	}

	// step 2: group - how to combine stats together
	int totalView = 0;
	const char *strGroup = parameters.getString("group", "total");
	if (!strcmp(strGroup, "total")) {
		totalView = 1;
	}
	else if (!strcmp(strGroup, "list")) {
		totalView = 0;
	}
	else {
		outputError(501, "invalid group parameter, which should be total|list.");
		return;
	}
	
	// step 3: time period, span, align
	int64_t startDtime, endDtime;
	int spanUnit, spanCount;
	if (parseDtimeSpan(parameters, startDtime, endDtime, spanUnit, spanCount) < 0)
		return;

	// move ahead one span for some calculation need its previous stats
	startDtime -= spanLength(spanUnit, spanCount);
	int mergeCount = (endDtime - startDtime) / spanLength(spanUnit, spanCount);

//	char buf1[128], buf2[128];
//	APPLOG_DEBUG("parsed start=%s, end=%s, mergeCount=%d", 
//		formatDtime(buf1, sizeof buf1, startDtime),
//		formatDtime(buf2, sizeof buf2, endDtime), mergeCount);

	StatMerger merger(NULL, NULL, NULL, NULL, spanUnit, spanCount, mergeCount);
	merger.periodStartTime = startDtime;
	
	// step 4: ids
// TODO: group by department...
//	uint16_t did = parameters.getInt("did", 0);
	uint16_t pid = parameters.getInt("pid", 0);
	uint16_t mid = parameters.getInt("mid", 0);

	if (pid == 0) {
		outputError(501, "pid can not be 0(ANY) now");
		return;
	}

	int cpuTotal = 0, cpuCores = 0; std::tr1::unordered_set<int> cpuIds;
	int memory = 0;
	int loadAvg = 0;
	int netAll = 0; std::tr1::unordered_set<int> netIds;
	int diskAll = 0; std::tr1::unordered_set<int> diskIds;

	// step 4.1: parse iids
	const char *strIid = parameters.getString("iid", "all");
	if (strcmp(strIid, "all") == 0) {
		cpuTotal = 1;	// no cpu-cores
		memory = 1;
		loadAvg = 1;
		netAll = 1;
		diskAll = 1;
	}
	else {
		char ss[1024]; 
		strncpy(ss, strIid, sizeof ss); ss[sizeof(ss) - 1] = 0;

		char *endptr, *nptr = strtok_r(ss, MULTIVAL_SEPARATORS, &endptr);
		while (nptr != NULL) {
			if (!strcmp(nptr, "cpu-total")) cpuTotal = 1;
			else if (!strcmp(nptr, "cpu-cores")) cpuCores = 1;
			else if (!strncmp(nptr, "cpu-", 4)) cpuIds.insert(strtol(nptr + 4, NULL, 0));
			else if (!strcmp(nptr, "mem")) memory = 1;
			else if (!strcmp(nptr, "load-avg")) loadAvg = 1;
			else if (!strcmp(nptr, "net-all")) netAll = 1;
			// TODO: mapping net-name to its id
			else if (!strncmp(nptr, "net-", 4)) netIds.insert(strtol(nptr + 4, NULL, 0));
			else if (!strcmp(nptr, "disk-all")) diskAll = 1;
			// TODO: mapping disk-name to its id
			else if (!strncmp(nptr, "disk-", 5)) diskIds.insert(strtol(nptr + 5, NULL, 0));
			else {
				outputError(501, "invalid iid parameter");
				return;
			}

			nptr = strtok_r(NULL, MULTIVAL_SEPARATORS, &endptr);
		}
	}

	// step 4.2: get all possible iids first
	local_key_set_t ids;
	host_set_t hosts;

	// step 4.3: get hosts and mapping iids with hosts
	const char *strHost = parameters.getString("host", "auto");
	if (strcmp(strHost, "auto")) {
		// individual host(s)
		char ss[1024];
		strncpy(ss, strHost, sizeof ss); ss[sizeof(ss) - 1] = 0;

		char *endptr, *nptr = strtok_r(ss, MULTIVAL_SEPARATORS, &endptr);
		while (nptr != NULL) {
			stat_ip_t hip;
			if (inet_pton(AF_INET, nptr, &hip.ip.ip4) == 1) {
				hip.ver = 4; 
			}
			else if (inet_pton(AF_INET6, nptr, &hip.ip.ip6[0]) == 1) {
				hip.ver = 6;
			}
			else {
				outputError(501, "invalid host parameter");
				return;
			}

			hosts.insert(hip);
			nptr = strtok_r(NULL, MULTIVAL_SEPARATORS, &endptr);
		}
	}

	unsigned char buf[8192], rspBuf[8192];
	Memorybuffer msg(buf, sizeof buf, false);
	MemoryBuffer rsp(rspBuf, sizeof rspBuf, false);

	struct proto_h16_head *h = (struct proto_h16_head *)msg.data();
	memset(h, sizeof(*h), 0);
	msg.setWptr(sizeof(*h));

	h->cmd = CMD_STAT_GET_SYSTEM_STATS_REQ;
	h->syn = nextSyn++;
	h->ack = 0;
	h->ver = 1;
	
	msg.writeUint8(context);
	msg.writeUint8(totalView);
	msg.writeInt64(startDtime);
	msg.writeInt64(endDtime);
	msg.writeUint8(spanUnit);
	msg.writeUint8(spanCount);
	msg.writeUint16(pid);
	msg.writeUint16(mid);

	msg.writeUint16(hosts.size());
	for (hosts::iterator iter = hosts.begin(); iter != hosts.end(); ++iter) {
		if (encodeTo(msg, *iter) < 0)
			break;
	}
	
	beyondy::TimedoutCountdown timer(10*1000);
	ClientConnection client(storageAddress, 10*1000, 3);
	if (client.request(&msg, &rsp) < 0) {
		APPLOG_ERROR("request to %s failed: %m", storageAddress);
		return -1;
	}

	struct proto_h16_res *h2 = (struct proto_h16_res *)rsp.data();
	rsp.setRptr(sizeof(*h2));

	if (combiner.parseFrom(&rsp) < 0) {
		APPLOG_ERROR("parse combiner from rsp-msg failed");
		return -1;
	}

	// further merge
	StatCombiner combiner(spanUnit, spanCount, startDtime, mergeCount);
	int gtype = combiner.groupType();

	// output
	printf("Status: 200 OK\r\n");
	printf("Content-Type: application/json\r\n");
	printf("\r\n");

	int64_t spanInterval = spanLength(spanUnit, spanCount);
	int64_t ts = startDtime + spanInterval;
	char buf[128];
	printf("{\"start\":\"%s\"", formatDtime(buf, sizeof buf, ts));
	printf(",\"end\":\"%s\"", formatDtime(buf, sizeof buf, endDtime));
	printf(",\"span\":\"%s\"", formatSpan(buf, sizeof buf, spanUnit, spanCount));
	printf(",\"stats\":[");
	for (int i = 1; i < mergeCount; ++i) {
		
		printf("%s{\"dtime\":\"%s\"", i == 1 ? "" : ",", formatDtime(buf, sizeof buf, ts));
		printf(",\"data\":[");

		bool first = true;
		if (!cpuIds.empty()) {
			outputCpuCombinedGauges(gtype, combiner.mergedGauges[i-1], combiner.mergedGauges[i], first, cpuIds);
		}

		if (memory) {
			outputMemCombinedGauges(gtype, combiner.mergedGauges[i-1], combiner.mergedGauges[i], first);
		}

		if (loadAvg) {
			outputLoadavgCombinedGauges(gtype, combiner.mergedGauges[i-1], combiner.mergedGauges[i], first);
		}

		if (!netIds.empty()) {
			outputNetCombinedGauges(gtype, combiner.mergedGauges[i-1], combiner.mergedGauges[i], first, netIds);
		}

		if (!diskIds.empty()) {
			outputDiskCombinedGauges(gtype, combiner.mergedGauges[i-1], combiner.mergedGauges[i], first, diskIds);
		}

		printf("]}");

		ts += spanInterval;
	}

	printf("]}");
	return;
}